Skip to content

Commit

Permalink
Add a checkbox which can be toggled by pressing space bar
Browse files Browse the repository at this point in the history
  • Loading branch information
gzotti committed Oct 23, 2021
1 parent 3d3282b commit 5648306
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ IF(STELLARIUM_GUI_MODE STREQUAL "Standard")
gui/ObsListDialog.cpp
gui/ObsListCreateEditDialog.hpp
gui/ObsListCreateEditDialog.cpp
gui/SpaceCheckBox.hpp
gui/SpaceCheckBox.cpp
gui/StelDialog.hpp
gui/StelDialog_p.hpp
gui/StelDialog.cpp
Expand Down
37 changes: 37 additions & 0 deletions src/gui/SpaceCheckBox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/***************************************************************************
* Copyright (C) 2021 Georg Zotti *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. *
***************************************************************************/

#include "SpaceCheckBox.hpp"
#include <QKeyEvent>

SpaceCheckBox::SpaceCheckBox(QWidget* parent)
: QCheckBox(parent)
{
}

void SpaceCheckBox::keyReleaseEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Space:
toggle();
break;
default:
QCheckBox::keyReleaseEvent(e);
}
}
42 changes: 42 additions & 0 deletions src/gui/SpaceCheckBox.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Stellarium
* Copyright (C) 2021 Georg Zotti
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/

#ifndef SPACECHECKBOX_HPP
#define SPACECHECKBOX_HPP

#include <QCheckBox>

//! @class SpaceCheckBox
//! A QCheckBox which can be toggled by pressing (actually releasing) Space when it has focus.
//! To use this class in the QtCreator UI designer, add a regular QCheckBox to the UI,
//! then right-click on it and change its type to SpaceCheckBox.
//! Then it makes sense to put this checkbox into a useful GUI tab order.
class SpaceCheckBox : public QCheckBox
{
Q_OBJECT
public:
SpaceCheckBox(QWidget* parent=Q_NULLPTR);
~SpaceCheckBox() Q_DECL_OVERRIDE {}

protected:
//! This toggles the checkbox on releasing the Space bar.
virtual void keyReleaseEvent(QKeyEvent *e) Q_DECL_OVERRIDE;
};

#endif // SPACECHECKBOX_HPP

0 comments on commit 5648306

Please sign in to comment.