91 { |
91 { |
92 QObject* const objectInstance = metaObject->newInstance(Q_ARG(QObject*, this)); |
92 QObject* const objectInstance = metaObject->newInstance(Q_ARG(QObject*, this)); |
93 BaseTool* const toolInstance = qobject_cast<BaseTool*>(objectInstance); |
93 BaseTool* const toolInstance = qobject_cast<BaseTool*>(objectInstance); |
94 if (toolInstance) |
94 if (toolInstance) |
95 { |
95 { |
96 this->selectedTool = coalesce(this->selectedTool, toolInstance); |
|
97 this->tools.append(toolInstance); |
96 this->tools.append(toolInstance); |
98 qInfo() << toolInstance->name(); |
97 QAction* action = new QAction{toolInstance->name(), this}; |
|
98 action->setCheckable(true); |
|
99 this->toolActions[toolInstance] = action; |
|
100 action->setToolTip(toolInstance->toolTip()); |
|
101 connect(action, &QAction::triggered, this, &MainWindow::toolActionTriggered); |
|
102 this->ui->toolsBar->addAction(action); |
99 } |
103 } |
100 else |
104 else |
101 { |
105 { |
102 QMessageBox::critical(this, tr("Error"), tr("Unable to construct %1").arg(metaObject->className())); |
106 QMessageBox::critical(this, tr("Error"), tr("Unable to construct %1").arg(metaObject->className())); |
103 } |
107 } |
104 } |
108 } |
|
109 |
|
110 this->selectTool(this->tools[0]); |
105 } |
111 } |
106 |
112 |
107 // MainWindow needs a destructor even if it is empty because otherwise the destructor of the |
113 // MainWindow needs a destructor even if it is empty because otherwise the destructor of the |
108 // std::unique_ptr is resolved in the header file, where it will complain about Ui_MainWindow |
114 // std::unique_ptr is resolved in the header file, where it will complain about Ui_MainWindow |
109 // being incomplete. |
115 // being incomplete. |
368 void MainWindow::loadColors() |
374 void MainWindow::loadColors() |
369 { |
375 { |
370 QTextStream errors; |
376 QTextStream errors; |
371 this->colorTable = this->libraries.loadColorTable(errors); |
377 this->colorTable = this->libraries.loadColorTable(errors); |
372 } |
378 } |
|
379 |
|
380 void MainWindow::toolActionTriggered() |
|
381 { |
|
382 QAction* action = qobject_cast<QAction*>(sender()); |
|
383 if (action != nullptr) |
|
384 { |
|
385 BaseTool* tool = nullptr; |
|
386 for (auto&& pair : items(this->toolActions)) |
|
387 { |
|
388 if (pair.value == action) |
|
389 { |
|
390 tool = pair.key; |
|
391 } |
|
392 } |
|
393 this->selectTool(tool); |
|
394 } |
|
395 } |
|
396 |
|
397 void MainWindow::selectTool(BaseTool* tool) |
|
398 { |
|
399 if (tool != nullptr && tool != this->selectedTool) |
|
400 { |
|
401 this->selectedTool = tool; |
|
402 for (auto&& pair : items(this->toolActions)) |
|
403 { |
|
404 pair.value->setChecked(pair.key == tool); |
|
405 } |
|
406 } |
|
407 } |