things

Fri, 08 Nov 2019 19:05:07 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 08 Nov 2019 19:05:07 +0200
changeset 16
aeb5f203b3eb
parent 15
9e18ec63eec3
child 17
a5111f4e6412

things

CMakeLists.txt file | annotate | diff | comparison | revisions
locale/fi.ts file | annotate | diff | comparison | revisions
locale/sv.ts file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.h file | annotate | diff | comparison | revisions
src/settingseditor/keyboardshortcutseditor.cpp file | annotate | diff | comparison | revisions
src/settingseditor/keyboardshortcutseditor.h file | annotate | diff | comparison | revisions
src/settingseditor/settingseditor.cpp file | annotate | diff | comparison | revisions
src/settingseditor/settingseditor.h file | annotate | diff | comparison | revisions
src/settingseditor/settingseditor.ui file | annotate | diff | comparison | revisions
src/uiutilities.cpp file | annotate | diff | comparison | revisions
src/uiutilities.h file | annotate | diff | comparison | revisions
--- 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
 )
--- 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 @@
     </message>
 </context>
 <context>
+    <name>KeyboardShortcutsEditor</name>
+    <message>
+        <location filename="../src/settingseditor/keyboardshortcutseditor.cpp" line="51"/>
+        <source>Action</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <location filename="../src/settingseditor/keyboardshortcutseditor.cpp" line="53"/>
+        <source>Shortcut</source>
+        <translation type="unfinished"></translation>
+    </message>
+</context>
+<context>
     <name>LibrariesEditor</name>
     <message>
         <location filename="../src/settingseditor/librarieseditor.ui" line="14"/>
@@ -148,27 +161,27 @@
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/mainwindow.cpp" line="42"/>
+        <location filename="../src/mainwindow.cpp" line="44"/>
         <source>Open model</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/mainwindow.cpp" line="44"/>
+        <location filename="../src/mainwindow.cpp" line="46"/>
         <source>LDraw models (*.ldr *.dat)</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/mainwindow.cpp" line="63"/>
+        <location filename="../src/mainwindow.cpp" line="65"/>
         <source>Problem loading references</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/mainwindow.cpp" line="73"/>
+        <location filename="../src/mainwindow.cpp" line="75"/>
         <source>Problem opening file</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/mainwindow.cpp" line="75"/>
+        <location filename="../src/mainwindow.cpp" line="77"/>
         <source>Could not open %1: %2</source>
         <translation type="unfinished"></translation>
     </message>
@@ -192,7 +205,7 @@
     </message>
     <message>
         <location filename="../src/settingseditor/settingseditor.ui" line="38"/>
-        <location filename="../src/settingseditor/settingseditor.cpp" line="47"/>
+        <location filename="../src/settingseditor/settingseditor.cpp" line="49"/>
         <source>System language</source>
         <translation type="unfinished"></translation>
     </message>
@@ -206,5 +219,10 @@
         <source>LDraw parts libraries</source>
         <translation type="unfinished"></translation>
     </message>
+    <message>
+        <location filename="../src/settingseditor/settingseditor.ui" line="57"/>
+        <source>Keyboard shortcuts</source>
+        <translation type="unfinished"></translation>
+    </message>
 </context>
 </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 @@
     </message>
 </context>
 <context>
+    <name>KeyboardShortcutsEditor</name>
+    <message>
+        <source>Action</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <source>Shortcut</source>
+        <translation type="unfinished"></translation>
+    </message>
+</context>
+<context>
     <name>Libraries</name>
     <message>
         <source>Official library</source>
@@ -228,5 +239,9 @@
         <source>LDraw parts libraries</source>
         <translation>LDraw-bitbibliotek</translation>
     </message>
+    <message>
+        <source>Keyboard shortcuts</source>
+        <translation type="unfinished"></translation>
+    </message>
 </context>
 </TS>
--- 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)
 	{
--- 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 <QMainWindow>
 #include <QTranslator>
 #include <QSettings>
-#include <memory>
-#include <vector>
+#include <QKeySequence>
 #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();
--- /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 <QAction>
+#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<Column>(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<Column>(section))
+		{
+		case TitleColumn:
+			return tr("Action");
+		case ShortcutColumn:
+			return tr("Shortcut");
+		}
+	}
+	return {};
+}
+
--- /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 <QWidget>
+#include <QAbstractTableModel>
+
+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<QAction*> actions;
+};
--- 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 <QSettings>
+#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()
--- 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);
--- 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 @@
    <item>
     <widget class="QTabWidget" name="tabWidget">
      <property name="currentIndex">
-      <number>0</number>
+      <number>2</number>
      </property>
      <widget class="QWidget" name="tabGeneral">
       <attribute name="title">
@@ -52,6 +52,16 @@
        <string>LDraw parts libraries</string>
       </attribute>
      </widget>
+     <widget class="QWidget" name="tab">
+      <attribute name="title">
+       <string>Keyboard shortcuts</string>
+      </attribute>
+      <layout class="QVBoxLayout" name="verticalLayout_2">
+       <item>
+        <widget class="QTableView" name="keyboardShortcutsView"/>
+       </item>
+      </layout>
+     </widget>
     </widget>
    </item>
    <item>
--- /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<QAction*> uiutilities::collectActions(QObject* subject)
+{
+	QVector<QAction*> actions;
+	for (QAction* action : subject->findChildren<QAction*>())
+	{
+		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<QAction*>& actions)
+{
+	KeySequenceMap result;
+	for (QAction* action : actions)
+	{
+		result[action->objectName()] = action->shortcut();
+	}
+	return result;
+}
--- /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 <QAction>
+
+namespace uiutilities
+{
+	using KeySequenceMap = QMap<QString, QKeySequence>;
+	QVector<QAction*> collectActions(QObject* subject);
+	KeySequenceMap makeKeySequenceMap(const QVector<QAction*>& actions);
+}

mercurial