Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PickcharsDialog.cpp « autotype « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 798f2e228b94771743b94efe1a9f9794cea5c0e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 *  Copyright (C) 2021 Team KeePassXC <team@keepassxc.org>
 *
 *  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 "PickcharsDialog.h"
#include "ui_PickcharsDialog.h"

#include "gui/Icons.h"

#include <QPushButton>
#include <QShortcut>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
#include <QScreen>
#else
#include <QDesktopWidget>
#endif

PickcharsDialog::PickcharsDialog(const QString& string, QWidget* parent)
    : QDialog(parent)
    , m_ui(new Ui::PickcharsDialog())
{
    if (string.isEmpty()) {
        reject();
    }

    // Places the window on the active (virtual) desktop instead of where the main window is.
    setAttribute(Qt::WA_X11BypassTransientForHint);
    setWindowFlags((windowFlags() | Qt::WindowStaysOnTopHint | Qt::MSWindowsFixedSizeDialogHint)
                   & ~Qt::WindowContextHelpButtonHint);
    setWindowIcon(icons()->applicationIcon());

    m_ui->setupUi(this);

    // Increase max columns with longer passwords for better display
    int width = 10;
    if (string.length() >= 100) {
        width = 20;
    } else if (string.length() >= 60) {
        width = 15;
    }

    int count = 0;
    for (const auto& ch : string) {
        auto btn = new QPushButton(QString::number(count + 1));
        btn->setProperty("char", ch);
        btn->setProperty("count", count);
        connect(btn, &QPushButton::clicked, this, &PickcharsDialog::charSelected);
        m_ui->charsGrid->addWidget(btn, count / width, count % width);
        m_lastSelected = count;
        ++count;
    }
    // Prevent stretched buttons
    if (m_ui->charsGrid->rowCount() == 1 && m_ui->charsGrid->columnCount() < 5) {
        m_ui->charsGrid->addItem(new QSpacerItem(5, 5, QSizePolicy::MinimumExpanding), count / width, count % width);
    }
    m_ui->charsGrid->itemAtPosition(0, 0)->widget()->setFocus();

    connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

    // Navigate grid layout using up/down/left/right motion
    new QShortcut(Qt::Key_Up, this, SLOT(upPressed()));
    new QShortcut(Qt::Key_Down, this, SLOT(downPressed()));
    // Remove last selected character
    auto shortcut = new QShortcut(Qt::Key_Backspace, this);
    connect(shortcut, &QShortcut::activated, this, [this] {
        auto text = m_ui->selectedChars->text();
        m_ui->selectedChars->setText(text.left(text.size() - 1));
    });
    // Submit the form
    shortcut = new QShortcut(Qt::CTRL + Qt::Key_S, this);
    connect(shortcut, &QShortcut::activated, this, [this] { accept(); });
}

void PickcharsDialog::upPressed()
{
    auto focus = focusWidget();
    if (!focus) {
        return;
    }

    auto count = focus->property("count");
    if (count.isValid()) {
        // Lower bound not checked by QGridLayout::itemAt https://bugreports.qt.io/browse/QTBUG-91261
        auto upCount = count.toInt() - m_ui->charsGrid->columnCount();
        if (upCount >= 0) {
            m_ui->charsGrid->itemAt(upCount)->widget()->setFocus();
        }
    } else if (focus == m_ui->selectedChars) {
        // Move back to the last selected button
        auto item = m_ui->charsGrid->itemAt(m_lastSelected);
        if (item) {
            item->widget()->setFocus();
        }
    } else if (focus == m_ui->pressTab) {
        m_ui->selectedChars->setFocus();
    }
}

void PickcharsDialog::downPressed()
{
    auto focus = focusWidget();
    if (!focus) {
        return;
    }

    auto count = focus->property("count");
    if (count.isValid()) {
        auto item = m_ui->charsGrid->itemAt(count.toInt() + m_ui->charsGrid->columnCount());
        if (item) {
            item->widget()->setFocus();
        } else {
            // Store the currently selected button and move to the line edit
            m_lastSelected = count.toInt();
            m_ui->selectedChars->setFocus();
        }
    } else if (focus == m_ui->selectedChars) {
        m_ui->pressTab->setFocus();
    }
}

QString PickcharsDialog::selectedChars()
{
    return m_ui->selectedChars->text();
}

bool PickcharsDialog::pressTab()
{
    return m_ui->pressTab->isChecked();
}

void PickcharsDialog::charSelected()
{
    auto btn = qobject_cast<QPushButton*>(sender());
    if (!btn) {
        return;
    }

    m_ui->selectedChars->setText(m_ui->selectedChars->text() + btn->property("char").toChar());
}

void PickcharsDialog::showEvent(QShowEvent* event)
{
    QDialog::showEvent(event);

    // Center on active screen
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
    auto screen = QApplication::screenAt(QCursor::pos());
    if (!screen) {
        // screenAt can return a nullptr, default to the primary screen
        screen = QApplication::primaryScreen();
    }
    QRect screenGeometry = screen->availableGeometry();
#else
    QRect screenGeometry = QApplication::desktop()->availableGeometry(QCursor::pos());
#endif
    move(screenGeometry.center().x() - (size().width() / 2), screenGeometry.center().y() - (size().height() / 2));
}