src/mainwindow.cpp

changeset 125
f127982d3412
parent 124
f9f308c8e0c5
child 128
7c834fe36b25
--- a/src/mainwindow.cpp	Sun Aug 29 20:05:24 2021 +0300
+++ b/src/mainwindow.cpp	Sun Aug 29 20:39:55 2021 +0300
@@ -28,14 +28,6 @@
 #include "document.h"
 #include "uiutilities.h"
 #include "widgets/colorselectdialog.h"
-#include "tools/basetool.h"
-#include "tools/drawtool.h"
-#include "tools/selecttool.h"
-
-static const QMetaObject* const toolMetaObjects[] = {
-	&SelectTool::staticMetaObject,
-	&DrawTool::staticMetaObject,
-};
 
 template<typename BaseType, typename MemberType, typename DataType>
 struct MemberData
@@ -86,28 +78,6 @@
 	this->restoreSettings();
 	this->updateRenderPreferences();
 	this->newModel();
-
-	for (const QMetaObject* const metaObject : ::toolMetaObjects)
-	{
-		QObject* const objectInstance = metaObject->newInstance(Q_ARG(QObject*, this));
-		BaseTool* const toolInstance = qobject_cast<BaseTool*>(objectInstance);
-		if (toolInstance)
-		{
-			this->tools.append(toolInstance);
-			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
@@ -211,17 +181,6 @@
 	this->ui->tabs->addTab(document, modelName);
 	this->ui->tabs->setCurrentWidget(document);
 	document->restoreSplitterState(this->documentSplitterState);
-	connect(document, &Document::splitterChanged, this, &MainWindow::handleDocumentSplitterChange);
-	connect(document, &Document::mouseClick, this, &MainWindow::canvasMouseReleased);
-	connect(document, &Document::mouseMove, this, &MainWindow::canvasMouseMoved);
-	connect(document, &Document::keyReleased, this, &MainWindow::canvasKeyReleased);
-	document->setCanvasOverpaintCallback([&](Canvas* canvas, QPainter* painter)
-	{
-		if (this->selectedTool != nullptr)
-		{
-			this->selectedTool->overpaint(canvas, painter);
-		}
-	});
 }
 
 void MainWindow::runSettingsEditor()
@@ -234,9 +193,14 @@
 	}
 }
 
+Document* MainWindow::currentDocument()
+{
+	return qobject_cast<Document*>(this->ui->tabs->currentWidget());
+}
+
 void MainWindow::handleDocumentSplitterChange()
 {
-	Document* currentDocument = qobject_cast<Document*>(this->ui->tabs->currentWidget());
+	Document* currentDocument = this->currentDocument();
 	if (currentDocument != nullptr)
 	{
 		this->documentSplitterState = currentDocument->saveSplitterState();
@@ -387,79 +351,11 @@
 	this->colorTable = this->libraries.loadColorTable(errors);
 }
 
-void MainWindow::toolActionTriggered()
+void MainWindow::keyReleaseEvent(QKeyEvent* event)
 {
-	QAction* action = qobject_cast<QAction*>(sender());
-	if (action != nullptr)
+	Document* document = this->currentDocument();
+	if (document != 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)
-	{
-		if (this->selectedTool != nullptr)
-		{
-			this->selectedTool->reset();
-		}
-		this->selectedTool = tool;
-		for (auto&& pair : items(this->toolActions))
-		{
-			pair.value->setChecked(pair.key == tool);
-		}
+		document->handleKeyPress(event);
 	}
 }
-
-void MainWindow::canvasMousePressed(QMouseEvent *event)
-{
-	Q_UNUSED(event)
-}
-
-void MainWindow::canvasMouseReleased(Document* document, Canvas* canvas, QMouseEvent* event)
-{
-	if (this->selectedTool != nullptr)
-	{
-		this->selectedTool->mouseClick(document, canvas, event);
-	}
-}
-
-void MainWindow::canvasMouseDoubleClicked(QMouseEvent* event)
-{
-	Q_UNUSED(event)
-}
-
-void MainWindow::canvasMouseMoved(Document* document, Canvas* canvas, QMouseEvent* event)
-{
-	if (this->selectedTool != nullptr)
-	{
-		this->selectedTool->mouseMove(document, canvas, event);
-	}
-}
-
-void MainWindow::keyReleaseEvent(QKeyEvent *event)
-{
-	if (this->selectedTool != nullptr)
-	{
-		this->selectedTool->keyReleased(event);
-	}
-}
-
-void MainWindow::canvasKeyReleased(Document *document, Canvas *canvas, QKeyEvent *event)
-{
-#if 0
-	if (this->selectedTool != nullptr)
-	{
-		this->selectedTool->keyReleased(document, canvas, event);
-	}
-#endif
-}

mercurial