Add dialogy widget which implements default button behaviour.

This commit is contained in:
Florian Geyer 2012-04-24 11:09:40 +02:00 committed by Felix Geyer
parent d3fbb291bd
commit 279793365a
3 changed files with 131 additions and 0 deletions

View File

@ -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

90
src/gui/DialogyWidget.cpp Normal file
View File

@ -0,0 +1,90 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "DialogyWidget.h"
#include <QtGui/QDialogButtonBox>
#include <QtGui/QPushButton>
#include <QtGui/QKeyEvent>
#include <QtCore/QDebug>
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<QDialogButtonBox*> buttonBoxes = findChildren<QDialogButtonBox*>();
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;
}

39
src/gui/DialogyWidget.h Normal file
View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_DIALOGYWIDGET_H
#define KEEPASSX_DIALOGYWIDGET_H
#include <QtGui/QWidget>
#include <QtGui/QDialogButtonBox>
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