# HG changeset patch # User Teemu Piippo # Date 1573232707 -7200 # Node ID aeb5f203b3eb48e16fa046262f93514ff3df2f10 # Parent 9e18ec63eec3d8910a9b30107ee27a2011d7c1c3 things diff -r 9e18ec63eec3 -r aeb5f203b3eb CMakeLists.txt --- a/CMakeLists.txt Sun Nov 03 18:17:08 2019 +0200 +++ b/CMakeLists.txt Fri Nov 08 19:05:07 2019 +0200 @@ -24,6 +24,7 @@ src/model.cpp src/modeleditcontext.cpp src/parser.cpp + src/uiutilities.cpp src/version.cpp src/vertex.cpp src/linetypes/comment.cpp @@ -35,6 +36,7 @@ src/linetypes/quadrilateral.cpp src/linetypes/subfilereference.cpp src/linetypes/triangle.cpp + src/settingseditor/keyboardshortcutseditor.cpp src/settingseditor/librarieseditor.cpp src/settingseditor/settingseditor.cpp ) @@ -50,6 +52,7 @@ src/model.h src/modeleditcontext.h src/parser.h + src/uiutilities.h src/utility.h src/version.h src/vertex.h @@ -62,6 +65,7 @@ src/linetypes/quadrilateral.h src/linetypes/subfilereference.h src/linetypes/triangle.h + src/settingseditor/keyboardshortcutseditor.h src/settingseditor/librarieseditor.h src/settingseditor/settingseditor.h ) diff -r 9e18ec63eec3 -r aeb5f203b3eb locale/fi.ts --- a/locale/fi.ts Sun Nov 03 18:17:08 2019 +0200 +++ b/locale/fi.ts Fri Nov 08 19:05:07 2019 +0200 @@ -10,6 +10,19 @@ + KeyboardShortcutsEditor + + + Action + + + + + Shortcut + + + + LibrariesEditor @@ -148,27 +161,27 @@ - + Open model - + LDraw models (*.ldr *.dat) - + Problem loading references - + Problem opening file - + Could not open %1: %2 @@ -192,7 +205,7 @@ - + System language @@ -206,5 +219,10 @@ LDraw parts libraries + + + Keyboard shortcuts + + diff -r 9e18ec63eec3 -r aeb5f203b3eb locale/sv.ts --- a/locale/sv.ts Sun Nov 03 18:17:08 2019 +0200 +++ b/locale/sv.ts Fri Nov 08 19:05:07 2019 +0200 @@ -16,6 +16,17 @@ + KeyboardShortcutsEditor + + Action + + + + Shortcut + + + + Libraries Official library @@ -228,5 +239,9 @@ LDraw parts libraries LDraw-bitbibliotek + + Keyboard shortcuts + + diff -r 9e18ec63eec3 -r aeb5f203b3eb src/mainwindow.cpp --- a/src/mainwindow.cpp Sun Nov 03 18:17:08 2019 +0200 +++ b/src/mainwindow.cpp Fri Nov 08 19:05:07 2019 +0200 @@ -8,6 +8,7 @@ #include "settingseditor/settingseditor.h" #include "version.h" #include "document.h" +#include "uiutilities.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow{parent}, @@ -17,6 +18,7 @@ libraries{this} { this->ui->setupUi(this); + defaultKeyboardShortcuts = uiutilities::makeKeySequenceMap(uiutilities::collectActions(this)); connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel); connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel); connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close); @@ -124,7 +126,7 @@ void MainWindow::runSettingsEditor() { - SettingsEditor settingsEditor{&this->settings, this}; + SettingsEditor settingsEditor{&this->settings, this->defaultKeyboardShortcuts, this}; const int result = settingsEditor.exec(); if (result == QDialog::Accepted) { diff -r 9e18ec63eec3 -r aeb5f203b3eb src/mainwindow.h --- a/src/mainwindow.h Sun Nov 03 18:17:08 2019 +0200 +++ b/src/mainwindow.h Fri Nov 08 19:05:07 2019 +0200 @@ -2,10 +2,10 @@ #include #include #include -#include -#include +#include #include "documentmanager.h" #include "libraries.h" +#include "uiutilities.h" class MainWindow : public QMainWindow { @@ -33,6 +33,7 @@ QSettings settings; LibraryManager libraries; QByteArray documentSplitterState; + uiutilities::KeySequenceMap defaultKeyboardShortcuts; static constexpr int maxRecentlyOpenedFiles = 10; QStringList recentlyOpenedFiles; void updateTitle(); diff -r 9e18ec63eec3 -r aeb5f203b3eb src/settingseditor/keyboardshortcutseditor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/settingseditor/keyboardshortcutseditor.cpp Fri Nov 08 19:05:07 2019 +0200 @@ -0,0 +1,58 @@ +#include +#include "keyboardshortcutseditor.h" +#include "uiutilities.h" + +KeyboardShortcutsEditor::KeyboardShortcutsEditor(QObject* subject, QObject* parent) : + QAbstractTableModel{parent}, + actions{uiutilities::collectActions(subject)} +{ +} + +int KeyboardShortcutsEditor::rowCount(const QModelIndex&) const +{ + return this->actions.size(); +} + +int KeyboardShortcutsEditor::columnCount(const QModelIndex&) const +{ + return 2; +} + +QVariant KeyboardShortcutsEditor::data( + const QModelIndex& index, + int role) const +{ + QAction* const action = this->actions[index.row()]; + const Column column = static_cast(index.column()); + switch(role) + { + case Qt::DisplayRole: + switch (column) { + case TitleColumn: + return action->text(); + case ShortcutColumn: + return action->shortcut().toString(QKeySequence::NativeText); + } + break; + } + return {}; +} + +QVariant KeyboardShortcutsEditor::headerData( + int section, + Qt::Orientation orientation, + int role) const +{ + if (orientation == Qt::Horizontal and role == Qt::DisplayRole) + { + switch (static_cast(section)) + { + case TitleColumn: + return tr("Action"); + case ShortcutColumn: + return tr("Shortcut"); + } + } + return {}; +} + diff -r 9e18ec63eec3 -r aeb5f203b3eb src/settingseditor/keyboardshortcutseditor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/settingseditor/keyboardshortcutseditor.h Fri Nov 08 19:05:07 2019 +0200 @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +class KeyboardShortcutsEditor : public QAbstractTableModel +{ + Q_OBJECT +public: + enum Column + { + TitleColumn, + ShortcutColumn, + }; + explicit KeyboardShortcutsEditor(QObject* subject, QObject* parent = nullptr); + int rowCount(const QModelIndex&) const override; + int columnCount(const QModelIndex&) const override; + QVariant data(const QModelIndex& index, int role) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; +private: + const QVector actions; +}; diff -r 9e18ec63eec3 -r aeb5f203b3eb src/settingseditor/settingseditor.cpp --- a/src/settingseditor/settingseditor.cpp Sun Nov 03 18:17:08 2019 +0200 +++ b/src/settingseditor/settingseditor.cpp Fri Nov 08 19:05:07 2019 +0200 @@ -1,13 +1,19 @@ #include +#include "keyboardshortcutseditor.h" #include "settingseditor.h" #include "ui_settingseditor.h" -SettingsEditor::SettingsEditor(QSettings* settings, QWidget* parent) : +SettingsEditor::SettingsEditor( + QSettings* settings, + const uiutilities::KeySequenceMap& defaultKeyboardShortcuts, + QWidget* parent +) : QDialog{parent}, ui{*new Ui_SettingsEditor}, settings{settings}, libraries{settings, this}, - librariesEditor{settings, this} + librariesEditor{settings, this}, + defaultKeyboardShortcuts{defaultKeyboardShortcuts} { this->ui.setupUi(this); this->loadLocales(); @@ -20,6 +26,7 @@ &SettingsEditor::accepted, this, &SettingsEditor::handleAccepted); + this->ui.keyboardShortcutsView->setModel(new KeyboardShortcutsEditor{parent, this}); } SettingsEditor::~SettingsEditor() diff -r 9e18ec63eec3 -r aeb5f203b3eb src/settingseditor/settingseditor.h --- a/src/settingseditor/settingseditor.h Sun Nov 03 18:17:08 2019 +0200 +++ b/src/settingseditor/settingseditor.h Fri Nov 08 19:05:07 2019 +0200 @@ -3,12 +3,15 @@ #include "main.h" #include "librarieseditor.h" #include "libraries.h" +#include "uiutilities.h" class SettingsEditor : public QDialog { Q_OBJECT public: - SettingsEditor(QSettings* settings, QWidget* parent = nullptr); + SettingsEditor(QSettings* settings, + const uiutilities::KeySequenceMap& defaultKeyboardShortcuts = {}, + QWidget* parent = nullptr); ~SettingsEditor(); private slots: void handleAccepted(); @@ -17,6 +20,7 @@ QSettings* const settings; LibraryManager libraries; LibrariesEditor librariesEditor; + const uiutilities::KeySequenceMap defaultKeyboardShortcuts; void loadLocales(); void setDefaults(); void setCurrentLanguage(const QString& localeCode); diff -r 9e18ec63eec3 -r aeb5f203b3eb src/settingseditor/settingseditor.ui --- a/src/settingseditor/settingseditor.ui Sun Nov 03 18:17:08 2019 +0200 +++ b/src/settingseditor/settingseditor.ui Fri Nov 08 19:05:07 2019 +0200 @@ -17,7 +17,7 @@ - 0 + 2 @@ -52,6 +52,16 @@ LDraw parts libraries + + + Keyboard shortcuts + + + + + + + diff -r 9e18ec63eec3 -r aeb5f203b3eb src/uiutilities.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/uiutilities.cpp Fri Nov 08 19:05:07 2019 +0200 @@ -0,0 +1,26 @@ +#include "uiutilities.h" + +QVector uiutilities::collectActions(QObject* subject) +{ + QVector actions; + for (QAction* action : subject->findChildren()) + { + if (not action->text().isEmpty() + and action->data().isNull() + and not action->objectName().isEmpty()) + { + actions.push_back(action); + } + } + return actions; +} + +uiutilities::KeySequenceMap uiutilities::makeKeySequenceMap(const QVector& actions) +{ + KeySequenceMap result; + for (QAction* action : actions) + { + result[action->objectName()] = action->shortcut(); + } + return result; +} diff -r 9e18ec63eec3 -r aeb5f203b3eb src/uiutilities.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/uiutilities.h Fri Nov 08 19:05:07 2019 +0200 @@ -0,0 +1,9 @@ +#pragma once +#include + +namespace uiutilities +{ + using KeySequenceMap = QMap; + QVector collectActions(QObject* subject); + KeySequenceMap makeKeySequenceMap(const QVector& actions); +}