keepassxc

Форк
0
/
DialogyWidget.cpp 
84 строки · 2.4 Кб
1
/*
2
 *  Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
3
 *
4
 *  This program is free software: you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation, either version 2 or (at your option)
7
 *  version 3 of the License.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "DialogyWidget.h"
19

20
#include <QKeyEvent>
21
#include <QPushButton>
22

23
DialogyWidget::DialogyWidget(QWidget* parent)
24
    : QWidget(parent)
25
{
26
    setAutoFillBackground(true);
27
}
28

29
void DialogyWidget::keyPressEvent(QKeyEvent* e)
30
{
31
#ifdef Q_OS_MACOS
32
    if (e->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Period) {
33
        if (!clickButton(QDialogButtonBox::Cancel)) {
34
            e->ignore();
35
        }
36
    } else
37
#endif
38
        if (!e->modifiers() || e->modifiers() == Qt::ControlModifier
39
            || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {
40
        switch (e->key()) {
41
        case Qt::Key_Enter:
42
        case Qt::Key_Return:
43
            if (!clickButton(QDialogButtonBox::Ok)) {
44
                e->ignore();
45
            }
46
            break;
47
        case Qt::Key_Escape:
48
            if (!clickButton(QDialogButtonBox::Cancel)) {
49
                if (!clickButton(QDialogButtonBox::Close)) {
50
                    e->ignore();
51
                }
52
            }
53
            break;
54
        default:
55
            e->ignore();
56
        }
57
    } else {
58
        e->ignore();
59
    }
60
}
61

62
bool DialogyWidget::clickButton(QDialogButtonBox::StandardButton standardButton)
63
{
64
    QPushButton* pb;
65

66
    if (standardButton == QDialogButtonBox::Ok) {
67
        pb = qobject_cast<QPushButton*>(focusWidget());
68
        if (pb && pb->isVisible() && pb->isEnabled() && pb->hasFocus()) {
69
            pb->click();
70
            return true;
71
        }
72
    }
73

74
    QList<QDialogButtonBox*> buttonBoxes = findChildren<QDialogButtonBox*>();
75
    for (auto buttonBox : buttonBoxes) {
76
        pb = buttonBox->button(standardButton);
77
        if (pb && pb->isVisible() && pb->isEnabled()) {
78
            pb->click();
79
            return true;
80
        }
81
    }
82

83
    return false;
84
}
85

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.