src/mainwindow.cpp

changeset 97
d9a3b153f679
parent 96
165777a20dc7
child 102
9f435f66bd0c
child 103
94719518e310
--- 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);
+		}
+	}
+}

mercurial