Commit 34484c93 authored by David Redondo's avatar David Redondo 🏎
Browse files

Make all buttons in the main window activatable with enter

Summary:
In a QDialog QPushbuttons will have autoDefault==true and the Dialog will call
setDefault(true) on them. This allows the user to activate the Buttons with the
enter key. However we also use QToolButtons in the main window with no visible
difference to the user. This caused unexpected activations of the help button
(the first default button) when a tool button was focused. In a custom event
handler we can check if the current focused widget is tool or push button when
the enter key is pressed and activate them accordingly.

BUG: 412184
FIXED-IN: 19.08.3

Reviewers: #spectacle, ngraham

Reviewed By: #spectacle, ngraham

Subscribers: ngraham, aprcela, #spectacle

Tags: #spectacle

Differential Revision: https://phabricator.kde.org/D24508
parent 93bfdb2e
......@@ -30,6 +30,7 @@
#include <QDesktopServices>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QKeyEvent>
#include <QPrintDialog>
#include <QPushButton>
#include <QTimer>
......@@ -134,6 +135,7 @@ void KSMainWindow::init()
// the Button Bar
mDialogButtonBox->setStandardButtons(QDialogButtonBox::Help);
mDialogButtonBox->button(QDialogButtonBox::Help)->setAutoDefault(false);
mConfigureButton->setDefaultAction(KStandardAction::preferences(this, SLOT(showPreferencesDialog()), this));
mConfigureButton->setText(i18n("Configure..."));
......@@ -144,11 +146,13 @@ void KSMainWindow::init()
KGuiItem::assign(mToolsButton, KGuiItem(i18n("Tools")));
mToolsButton->setIcon(QIcon::fromTheme(QStringLiteral("tools"),
QIcon::fromTheme(QStringLiteral("application-menu"))));
mToolsButton->setAutoDefault(false);
mDialogButtonBox->addButton(mToolsButton, QDialogButtonBox::ActionRole);
mToolsButton->setMenu(mToolsMenu);
KGuiItem::assign(mSendToButton, KGuiItem(i18n("Export")));
mSendToButton->setIcon(QIcon::fromTheme(QStringLiteral("document-share")));
mSendToButton->setAutoDefault(false);
mDialogButtonBox->addButton(mSendToButton, QDialogButtonBox::ActionRole);
mClipboardButton->setDefaultAction(KStandardAction::copy(this, SLOT(sendToClipboard()), this));
......@@ -513,3 +517,23 @@ void KSMainWindow::restoreWindowTitle()
setWindowTitle(SpectacleConfig::instance()->lastSaveFile().fileName());
}
}
/* This event handler enables all Buttons to be activated with Enter. Normally only QPushButton can
* be activated with Enter but we also use QToolButtons so we handle the event ourselves */
void KSMainWindow::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Return) {
QWidget *fw = focusWidget();
auto pb = qobject_cast<QPushButton*>(fw);
if (pb) {
pb->animateClick();
return;
}
auto tb = qobject_cast<QToolButton*>(fw);
if (tb) {
tb->animateClick();
return;
}
}
QDialog::keyPressEvent(event);
}
......@@ -95,6 +95,8 @@ class KSMainWindow: public QDialog
private:
void keyPressEvent(QKeyEvent *event) override;
KSWidget *mKSWidget;
QFrame *mDivider;
QDialogButtonBox *mDialogButtonBox;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment