# HG changeset patch # User Teemu Piippo # Date 1610292092 -7200 # Node ID d9a3b153f679278a66669722ad34b08153263ce0 # Parent 165777a20dc78b5394d187a528fef52de349e7d7 work on tools diff -r 165777a20dc7 -r d9a3b153f679 locale/fi.ts --- 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 @@ - + Error - + Unable to construct %1 - + Open model Avaa malli - + LDraw models (*.ldr *.dat) LDraw-mallit (*.ldr *.dat) - + Problem loading references Ongelma viitteiden lataamisessa - + Problem opening file Ongelma tiedoston avaamisessa - + Could not open %1: %2 Ei voitu avata %1: %2 diff -r 165777a20dc7 -r d9a3b153f679 src/main.h --- 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 +struct KeyValuePair +{ + K key; + V value; +}; + +template +struct MapItemsIterator : IteratorType +{ + template + MapItemsIterator(Ts&&... args) : IteratorType{args...} {} + auto operator*() const + { + return KeyValuePair{this->key(), this->value()}; + } +}; + +template +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 +auto items(const QMap& map) +{ + return MapItems< + const K&, + const V&, + const QMap, + MapItemsIterator::const_iterator> + >{map}; +} + +template +auto items(QMap& map) +{ + return MapItems< + const K&, + V&, + QMap, + MapItemsIterator::iterator> + >{map}; +} diff -r 165777a20dc7 -r d9a3b153f679 src/mainwindow.cpp --- 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(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(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); + } + } +} diff -r 165777a20dc7 -r d9a3b153f679 src/mainwindow.h --- 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 tools; BaseTool* selectedTool = nullptr; + QMap 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); };