summaryrefslogtreecommitdiffstats
path: root/tests/manual/keyevents/main.cpp
blob: 888b7f9d3fe31374e1356d0ace910fdff4f7ab02 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtWidgets>
#include <vector>
#include <memory>
#include <type_traits>

#include <private/qkeymapper_p.h>
#include <private/qguiapplication_p.h>

static const QKeySequence keySequences[] = {
    QKeySequence("Ctrl+C"),
    QKeySequence("Ctrl++"),
};

class KeyEventModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    explicit KeyEventModel(QObject *parent = nullptr)
        : QAbstractTableModel(parent)
    {
    }

    enum Columns {
        Language,
        Direction,
        Type,
        ScanCode,
        VirtualKey,
        Modifiers,
        Key,
        Text,
        PortableText,
        NativeText,
        PossibleKeys,
        FirstKeySequence,
        LastKeySequence = FirstKeySequence + std::size(keySequences) - 1,
        KeySequenceEdit,
        ColumnCount
    };

    int rowCount(const QModelIndex & = QModelIndex()) const override
    {
        return int(m_events.size());
    }

    int columnCount(const QModelIndex & = QModelIndex()) const override
    {
        return ColumnCount;
    }

    QVariant headerData(int column, Qt::Orientation orientation, int role) const override
    {
        if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
            switch (column) {
            case Language: return QString("language");
            case Direction: return QString("direction");
            case Type: return QString("type");
            case ScanCode: return QString("nativeScanCode");
            case VirtualKey: return QString("nativeVirtualKey");
            case Modifiers: return QString("modifiers");
            case Key: return QString("key");
            case Text: return QString("text");
            case PortableText: return QString("PortableText");
            case NativeText: return QString("NativeText");
            case PossibleKeys: return QString("keyCombinations");
            case KeySequenceEdit: return m_customKeySequence.toString();
            default: {
                auto keySequence = keySequences[column - FirstKeySequence];
                return keySequence.toString();
            }
            }
        }
        return QVariant();
    }

    template <typename T>
    static QString toString(T &&object, int verbosity = QDebug::DefaultVerbosity)
    {
        QString buffer;
        QDebug stream(&buffer);
        stream.setVerbosity(verbosity);
        stream.nospace() << std::forward<T>(object);
        return buffer;
    }

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
    {
        if (role == Qt::DisplayRole) {
            auto &event = m_events.at(index.row());
            auto *keyEvent = event.keyEvent.get();
            switch (int column = index.column()) {
            case Language: return event.language;
            case Direction: return toString(event.layoutDirection, QDebug::MinimumVerbosity);
            case Type: return toString(keyEvent->type(), QDebug::MinimumVerbosity);
            case ScanCode: return keyEvent->nativeScanCode();
            case VirtualKey: return keyEvent->nativeVirtualKey();
            case Modifiers: return toString(keyEvent->modifiers(), QDebug::MinimumVerbosity);
            case Key: return toString(Qt::Key(keyEvent->key()), QDebug::MinimumVerbosity);
            case Text: return keyEvent->text();
            case PortableText: return event.keySequence.toString(QKeySequence::PortableText);
            case NativeText: return event.keySequence.toString(QKeySequence::NativeText);
            case PossibleKeys: {
                QStringList keyCombinations;
                for (auto combination : event.possibleKeyCombinations)
                    keyCombinations << QKeySequence(combination).toString(QKeySequence::NativeText);
                static constexpr auto leftToRightOverride = QChar(0x202d);
                return leftToRightOverride + keyCombinations.join("    ");
            }
            default: {
                QStringList matches;
                if (event.keySequenceEquals[column - FirstKeySequence])
                    matches << "K";
                if (event.shortcutMatches[column - FirstKeySequence])
                    matches << "S";
                return matches.join(" ");
            }
            }
        } else if (role == Qt::TextAlignmentRole) {
            return Qt::AlignCenter;
        }

        return QVariant();
    }

    struct Event {
        std::unique_ptr<QKeyEvent> keyEvent;
        QString language;
        Qt::LayoutDirection layoutDirection;
        QKeySequence keySequence;
        using PossibleKeysList = decltype(std::declval<QKeyMapper>().possibleKeys(nullptr));
        PossibleKeysList possibleKeyCombinations;
        // Hard-coded key sequences, plus room for KeySequenceEdit
        bool keySequenceEquals[std::size(keySequences) + 1] = {};
        bool shortcutMatches[std::size(keySequences) + 1] = {};
    };

    bool eventFilter(QObject *object, QEvent *event) override
    {
        if (!m_enabled)
            return false;

        switch (auto type = event->type()) {
        case QEvent::KeyPress:
        case QEvent::KeyRelease: {
            auto *keyEvent = static_cast<QKeyEvent*>(event);
            auto *inputMethod = qGuiApp->inputMethod();
            auto row = int(m_events.size());
            beginInsertRows(QModelIndex(), row, row);
            m_events.push_back({
                std::unique_ptr<QKeyEvent>(keyEvent->clone()),
                QLocale::languageToString(inputMethod->locale().language()),
                inputMethod->inputDirection(),
                QKeySequence(keyEvent->keyCombination()),
                QKeyMapper::instance()->possibleKeys(keyEvent)
            });

            Event &event = m_events.back();

            if (type == QEvent::KeyPress) {
                for (size_t i = 0; i < std::size(event.keySequenceEquals); ++i) {
                    QKeySequence keySequence = i == std::size(keySequences) ?
                        m_customKeySequence : keySequences[i];

                    event.keySequenceEquals[i] = event.keySequence == keySequence;

                    QShortcut shortcut(keySequence, object, [&] {
                         event.shortcutMatches[i] = true;
                    }, Qt::ApplicationShortcut);
                    QShortcutMap &shortcutMap = QGuiApplicationPrivate::instance()->shortcutMap;
                    shortcutMap.tryShortcut(keyEvent);
                }
            }

            endInsertRows();
            return false;
        }
        case QEvent::ShortcutOverride: {
            auto *keyEvent = static_cast<QKeyEvent*>(event);
            if (!keyEvent->matches(QKeySequence::Quit)) {
                event->accept();
                return true;
            }
            return false;
        }
        default:
            return false;
        }
    }

    void reset()
    {
        beginResetModel();
        m_events.clear();
        endResetModel();
    }

    Q_SLOT void setCustomKeySequence(const QKeySequence &keySequence)
    {
        m_customKeySequence = keySequence;
        emit headerDataChanged(Qt::Horizontal, KeySequenceEdit, ColumnCount);
    }

    bool m_enabled = true;
    std::vector<Event> m_events;
    QKeySequence m_customKeySequence;
};

class KeyEventWindow : public QMainWindow
{
public:
    KeyEventWindow()
    {
        setWindowTitle(QString("Qt %1 on %2").arg(QT_VERSION_STR).arg(QSysInfo::prettyProductName()));
        auto *tableView = new QTableView(this);
        m_keyEventModel = new KeyEventModel(this);
        tableView->setModel(m_keyEventModel);
        tableView->installEventFilter(m_keyEventModel);

        tableView->setFocusPolicy(Qt::ClickFocus);
        tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
        tableView->setSelectionMode(QAbstractItemView::NoSelection);
        tableView->setWordWrap(false);

        QObject::connect(tableView->model(), &QAbstractItemModel::rowsInserted,
                         tableView, &QTableView::scrollToBottom);

        QMenu *menu = menuBar()->addMenu("File");
        menu->addAction("Save...", this, &KeyEventWindow::save);
        menu->addAction("Clear", this, [this]{
            m_keyEventModel->reset();
        });
        auto *enableAction = menu->addAction("Enabled", this, [this]{
            auto *action = static_cast<QAction*>(sender());
            m_keyEventModel->m_enabled = action->isChecked();
        });
        enableAction->setCheckable(true);
        enableAction->setChecked(true);

        auto *toolBar = addToolBar("Tools");
        toolBar->setMovable(false);
        toolBar->addWidget(new QLabel("Key sequence editor:"));
        auto *keySequenceEdit = new QKeySequenceEdit;
        keySequenceEdit->setMaximumSequenceLength(1);
        connect(keySequenceEdit, &QKeySequenceEdit::keySequenceChanged,
                m_keyEventModel, &KeyEventModel::setCustomKeySequence);
        keySequenceEdit->installEventFilter(m_keyEventModel);
        toolBar->addWidget(keySequenceEdit);
        toolBar->addWidget(new QLabel("Free form text input:"));
        toolBar->addWidget(new QLineEdit);

        setCentralWidget(tableView);
        centralWidget()->setFocus();
    }

    void save()
    {
        auto homeDirectory = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
        QString fileName = QFileDialog::getSaveFileName(this, "Save events",
                            QString("%1/events.csv").arg(homeDirectory));

        QFile file(fileName);
        if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
            QMessageBox::critical(this, "Could not open file", file.errorString());
            return;
        }
        QTextStream output(&file);
        const auto columns = m_keyEventModel->columnCount();
        for (int c = 0; c < columns; ++c) {
            output << m_keyEventModel->headerData(c, Qt::Horizontal, Qt::DisplayRole).toString()
                   << ((c < columns - 1) ? ";" : "");
        }
        output << "\n";
        for (int r = 0; r < m_keyEventModel->rowCount(); ++r) {
            for (int c = 0; c < m_keyEventModel->columnCount(); ++c) {
                auto index = m_keyEventModel->index(r, c);
                output << m_keyEventModel->data(index).toString()
                       << ((c < columns - 1) ? ";" : "");
            }
            output << "\n";
        }
    }

    void keyPressEvent(QKeyEvent *keyEvent) override
    {
        if (keyEvent->matches(QKeySequence::Quit))
            qGuiApp->quit();
    }

    KeyEventModel *m_keyEventModel = nullptr;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    KeyEventWindow keyEventWindow;
    keyEventWindow.showMaximized();

    return a.exec();
}

#include "main.moc"