Sun, 10 Jan 2021 17:21:32 +0200
work on tools
locale/fi.ts | file | annotate | diff | comparison | revisions | |
src/main.h | file | annotate | diff | comparison | revisions | |
src/mainwindow.cpp | file | annotate | diff | comparison | revisions | |
src/mainwindow.h | file | annotate | diff | comparison | revisions |
--- a/locale/fi.ts Sun Jan 10 15:28:44 2021 +0200 +++ b/locale/fi.ts Sun Jan 10 17:21:32 2021 +0200 @@ -232,37 +232,37 @@ <translation type="unfinished"></translation> </message> <message> - <location filename="../src/mainwindow.cpp" line="102"/> + <location filename="../src/mainwindow.cpp" line="106"/> <source>Error</source> <translation type="unfinished"></translation> </message> <message> - <location filename="../src/mainwindow.cpp" line="102"/> + <location filename="../src/mainwindow.cpp" line="106"/> <source>Unable to construct %1</source> <translation type="unfinished"></translation> </message> <message> - <location filename="../src/mainwindow.cpp" line="123"/> + <location filename="../src/mainwindow.cpp" line="129"/> <source>Open model</source> <translation>Avaa malli</translation> </message> <message> - <location filename="../src/mainwindow.cpp" line="125"/> + <location filename="../src/mainwindow.cpp" line="131"/> <source>LDraw models (*.ldr *.dat)</source> <translation>LDraw-mallit (*.ldr *.dat)</translation> </message> <message> - <location filename="../src/mainwindow.cpp" line="144"/> + <location filename="../src/mainwindow.cpp" line="150"/> <source>Problem loading references</source> <translation type="unfinished">Ongelma viitteiden lataamisessa</translation> </message> <message> - <location filename="../src/mainwindow.cpp" line="154"/> + <location filename="../src/mainwindow.cpp" line="160"/> <source>Problem opening file</source> <translation>Ongelma tiedoston avaamisessa</translation> </message> <message> - <location filename="../src/mainwindow.cpp" line="156"/> + <location filename="../src/mainwindow.cpp" line="162"/> <source>Could not open %1: %2</source> <translation>Ei voitu avata %1: %2</translation> </message>
--- a/src/main.h Sun Jan 10 15:28:44 2021 +0200 +++ b/src/main.h Sun Jan 10 17:21:32 2021 +0200 @@ -153,3 +153,62 @@ .arg(toDouble(vec.z)) .arg(toDouble(vec.w)); } + +template<typename K, typename V> +struct KeyValuePair +{ + K key; + V value; +}; + +template<typename K, typename V, typename IteratorType> +struct MapItemsIterator : IteratorType +{ + template<typename... Ts> + MapItemsIterator(Ts&&... args) : IteratorType{args...} {} + auto operator*() const + { + return KeyValuePair<K, V>{this->key(), this->value()}; + } +}; + +template<typename K, typename V, typename MapType, typename IteratorType> +struct MapItems +{ + MapType& map; + IteratorType begin() + { + return IteratorType(this->map.begin()); + } + + IteratorType end() + { + return IteratorType(this->map.end()); + } +}; + +/* + * Python's dict.items for QMap: use in a for loop to iterate a map to + * get both keys and values. Iteration yields KeyValuePairs. + */ +template<typename K, typename V> +auto items(const QMap<K, V>& map) +{ + return MapItems< + const K&, + const V&, + const QMap<K, V>, + MapItemsIterator<K, const V, typename QMap<K, V>::const_iterator> + >{map}; +} + +template<typename K, typename V> +auto items(QMap<K, V>& map) +{ + return MapItems< + const K&, + V&, + QMap<K, V>, + MapItemsIterator<K, const V, typename QMap<K, V>::iterator> + >{map}; +}
--- a/src/mainwindow.cpp Sun Jan 10 15:28:44 2021 +0200 +++ b/src/mainwindow.cpp Sun Jan 10 17:21:32 2021 +0200 @@ -93,15 +93,21 @@ BaseTool* const toolInstance = qobject_cast<BaseTool*>(objectInstance); if (toolInstance) { - this->selectedTool = coalesce(this->selectedTool, toolInstance); this->tools.append(toolInstance); - qInfo() << toolInstance->name(); + QAction* action = new QAction{toolInstance->name(), this}; + action->setCheckable(true); + this->toolActions[toolInstance] = action; + action->setToolTip(toolInstance->toolTip()); + connect(action, &QAction::triggered, this, &MainWindow::toolActionTriggered); + this->ui->toolsBar->addAction(action); } else { QMessageBox::critical(this, tr("Error"), tr("Unable to construct %1").arg(metaObject->className())); } } + + this->selectTool(this->tools[0]); } // MainWindow needs a destructor even if it is empty because otherwise the destructor of the @@ -370,3 +376,32 @@ QTextStream errors; this->colorTable = this->libraries.loadColorTable(errors); } + +void MainWindow::toolActionTriggered() +{ + QAction* action = qobject_cast<QAction*>(sender()); + if (action != nullptr) + { + BaseTool* tool = nullptr; + for (auto&& pair : items(this->toolActions)) + { + if (pair.value == action) + { + tool = pair.key; + } + } + this->selectTool(tool); + } +} + +void MainWindow::selectTool(BaseTool* tool) +{ + if (tool != nullptr && tool != this->selectedTool) + { + this->selectedTool = tool; + for (auto&& pair : items(this->toolActions)) + { + pair.value->setChecked(pair.key == tool); + } + } +}
--- a/src/mainwindow.h Sun Jan 10 15:28:44 2021 +0200 +++ b/src/mainwindow.h Sun Jan 10 17:21:32 2021 +0200 @@ -60,6 +60,7 @@ gl::RenderPreferences renderPreferences; QVector<class BaseTool*> tools; BaseTool* selectedTool = nullptr; + QMap<BaseTool*, QAction*> toolActions; void updateTitle(); void updateRenderPreferences(); void saveSettings(); @@ -70,4 +71,6 @@ void openModelForEditing(const QString& modelName); static QString pathToTranslation(const QString& localeCode); void loadColors(); + Q_SLOT void toolActionTriggered(); + void selectTool(BaseTool* tool); };