diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 891261cd3..ef1acd911 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -54,6 +54,7 @@ set(keepassx_SOURCES gui/DatabaseSettingsWidget.cpp gui/DatabaseTabWidget.cpp gui/DatabaseWidget.cpp + gui/DialogyWidget.cpp gui/EditEntryWidget.cpp gui/EditGroupWidget.cpp gui/EntryAttachmentsModel.cpp @@ -86,6 +87,7 @@ set(keepassx_MOC gui/DatabaseSettingsWidget.h gui/DatabaseTabWidget.h gui/DatabaseWidget.h + gui/DialogyWidget.h gui/EditEntryWidget.h gui/EditGroupWidget.h gui/EntryAttachmentsModel.h diff --git a/src/gui/DialogyWidget.cpp b/src/gui/DialogyWidget.cpp new file mode 100644 index 000000000..33172d971 --- /dev/null +++ b/src/gui/DialogyWidget.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2012 Felix Geyer + * + * 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 or (at your option) + * version 3 of the License. + * + * 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, see . + */ + +#include "DialogyWidget.h" + +#include +#include +#include +#include + +DialogyWidget::DialogyWidget(QWidget* parent) + : QWidget(parent) +{ +} + +void DialogyWidget::keyPressEvent(QKeyEvent *e) +{ +#ifdef Q_WS_MAC + if(e->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Period) { + if (!clickButton(QDialogButtonBox::Cancel)) { + e->ignore(); + } + } + else +#endif + if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) { + switch (e->key()) { + case Qt::Key_Enter: + case Qt::Key_Return: + if (!clickButton(QDialogButtonBox::Ok)) { + e->ignore(); + } + break; + case Qt::Key_Escape: + if (!clickButton(QDialogButtonBox::Cancel)) { + e->ignore(); + } + break; + default: + e->ignore(); + } + } + else { + e->ignore(); + } +} + +bool DialogyWidget::clickButton(QDialogButtonBox::StandardButton button) +{ + Q_ASSERT(button == QDialogButtonBox::Ok || button == QDialogButtonBox::Cancel); + + QList buttonBoxes = findChildren(); + for (int i = 0; i < buttonBoxes.size(); ++i) { + QDialogButtonBox* buttonBox = buttonBoxes.at(i); + QPushButton* pb; + QPushButton* pbCancel = buttonBox->button(QDialogButtonBox::Cancel); + if (button == QDialogButtonBox::Ok) { + if (pbCancel && pbCancel->isVisible() && pbCancel->isEnabled() && pbCancel->hasFocus()) { + pbCancel->click(); + return true; + } + pb = buttonBox->button(button); + } + else { + pb = pbCancel; + } + + if (pb) { + if (pb->isVisible() && pb->isEnabled()) { + pb->click(); + return true; + } + } + } + return false; +} diff --git a/src/gui/DialogyWidget.h b/src/gui/DialogyWidget.h new file mode 100644 index 000000000..f5f8d2d13 --- /dev/null +++ b/src/gui/DialogyWidget.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2012 Felix Geyer + * + * 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 or (at your option) + * version 3 of the License. + * + * 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, see . + */ + +#ifndef KEEPASSX_DIALOGYWIDGET_H +#define KEEPASSX_DIALOGYWIDGET_H + +#include +#include + +class DialogyWidget : public QWidget +{ + Q_OBJECT + +public: + explicit DialogyWidget(QWidget* parent = 0); + +protected: + virtual void keyPressEvent(QKeyEvent *e); + +private: + bool clickButton(QDialogButtonBox::StandardButton button); + +}; + +#endif // KEEPASSX_DIALOGYWIDGET_H