Skip to content

Commit

Permalink
Add a space pressable pushbutton with focus highlight indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
gzotti committed Oct 23, 2021
1 parent 7bc94c6 commit f0156a2
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
6 changes: 4 additions & 2 deletions data/gui/normalStyle.css
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,10 @@ QPushButton {
font-size: 100%;
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(183, 184, 185), stop: 1 rgb(111, 113, 114));
}

QPushButton:focus {
/*
* Only the space-pressable pushbutton should show a focus highlight!
*/
SpacePushButton:focus {
border: 2px solid rgb(161, 161, 161);
}

Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ IF(STELLARIUM_GUI_MODE STREQUAL "Standard")
gui/ObsListCreateEditDialog.cpp
gui/SpaceCheckBox.hpp
gui/SpaceCheckBox.cpp
gui/SpacePushButton.hpp
gui/SpacePushButton.cpp
gui/StelDialog.hpp
gui/StelDialog_p.hpp
gui/StelDialog.cpp
Expand Down
37 changes: 37 additions & 0 deletions src/gui/SpacePushButton.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 "SpacePushButton.hpp"
#include <QKeyEvent>

SpacePushButton::SpacePushButton(QWidget* parent)
: QPushButton(parent)
{
}

void SpacePushButton::keyPressEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Space:
animateClick();
break;
default:
QPushButton::keyPressEvent(e);
}
}
42 changes: 42 additions & 0 deletions src/gui/SpacePushButton.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 SPACEPUSHBUTTON_HPP
#define SPACEPUSHBUTTON_HPP

#include <QPushButton>

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

protected:
//! This triggers the button on pressing the Space bar.
virtual void keyPressEvent(QKeyEvent *e) Q_DECL_OVERRIDE;
};

#endif // SPACEPUSHBUTTON_HPP

0 comments on commit f0156a2

Please sign in to comment.