src/document.cpp

changeset 203
1909a0123c72
parent 201
5d201ee4a9c3
child 204
52e10e8d88cc
--- a/src/document.cpp	Tue Jun 07 20:44:19 2022 +0300
+++ b/src/document.cpp	Tue Jun 07 21:35:29 2022 +0300
@@ -18,8 +18,8 @@
 
 #include <QMouseEvent>
 #include <QMessageBox>
+#include <QVBoxLayout>
 #include "document.h"
-#include "ui_document.h"
 #include "model.h"
 #include "ui/objecteditor.h"
 
@@ -33,56 +33,24 @@
 	canvas{new Canvas{model, this, documents, colorTable, this}},
 	model{model},
 	documents{documents},
-	vertexMap{model},
-	ui{*new Ui_Document},
-	toolsBar{new QToolBar{this}}
+	vertexMap{model}
 {
-	this->ui.setupUi(this);
-	const int listWidth = static_cast<int>(this->width() / 3);
-	this->ui.viewportListSplitter->setSizes({listWidth * 2, listWidth});
-	this->ui.toolsBarContainer->setLayout(new QVBoxLayout{this->ui.toolsBarContainer});
-	this->ui.toolsBarContainer->layout()->addWidget(this->toolsBar);
-	this->ui.listView->setModel(model);
-	this->ui.viewportFrame->setLayout(new QVBoxLayout{this->ui.listView});
-	this->ui.viewportFrame->layout()->addWidget(this->canvas);
-	this->toolsBar->setOrientation(Qt::Vertical);
 	this->setMouseTracking(true);
-	connect(this->ui.viewportListSplitter, &QSplitter::splitterMoved, this, &EditorTabWidget::splitterChanged);
 	connect(this->canvas, &Canvas::mouseClick, this, &EditorTabWidget::canvasMouseClick);
 	connect(this->canvas, &Canvas::mouseMove, this, &EditorTabWidget::canvasMouseMove);
 	connect(this->canvas, &Canvas::newStatusText, this, &EditorTabWidget::newStatusText);
-	connect(this->ui.listView->selectionModel(), &QItemSelectionModel::selectionChanged,
-		[&](const QItemSelection& selected, const QItemSelection& deselected)
-	{
-		auto resolveIndex = [this](const QModelIndex& index){ return this->model->idAt(index.row()); };
-		auto resolve = [resolveIndex](const QItemSelection& selection)
-		{
-			return fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex);
-		};
-		this->canvas->handleSelectionChange(resolve(selected), resolve(deselected));
-	});
 	connect(this->model, &Model::dataChanged, this->canvas, qOverload<>(&Canvas::update));
 	connect(&this->vertexMap, &VertexMap::verticesChanged, [&]()
 	{
 		this->canvas->rebuildVertices(this);
 	});
 	this->canvas->drawState = &this->drawState;
-	this->initializeTools();
+	QVBoxLayout* layout = new QVBoxLayout{this};
+	layout->addWidget(this->canvas);
 }
 
 EditorTabWidget::~EditorTabWidget()
 {
-	delete &this->ui;
-}
-
-QByteArray EditorTabWidget::saveSplitterState() const
-{
-	return this->ui.viewportListSplitter->saveState();
-}
-
-void EditorTabWidget::restoreSplitterState(const QByteArray& state)
-{
-	this->ui.viewportListSplitter->restoreState(state);
 }
 
 void EditorTabWidget::applyToVertices(VertexMap::ApplyFunction fn) const
@@ -90,61 +58,9 @@
 	this->vertexMap.apply(fn);
 }
 
-const char INDEX_PROPERTY[] = "_editing_mode_index";
-
-void EditorTabWidget::initializeTools()
+void EditorTabWidget::setEditMode(EditingMode mode)
 {
-	const struct
-	{
-		QString name, tooltip;
-		QPixmap icon;
-		QWidget* widget;
-	} editingModesInfo[] = {
-		{
-			.name = tr("Select"),
-			.tooltip = tr("Select elements from the model."),
-			.icon = {":/icons/navigate-outline.png"},
-			//.widget = this->objectEditor,
-		},
-		{
-			.name = tr("Draw"),
-			.tooltip = tr("Draw new elements into the model."),
-			.icon = {":/icons/pencil-outline.png"},
-			.widget = nullptr,
-		},
-	};
-	for (int i = 0; i < countof(editingModesInfo); ++i) {
-		const auto& editingModeInfo = editingModesInfo[i];
-		QAction* action = new QAction{editingModeInfo.name, this};
-		action->setCheckable(true);
-		action->setChecked(i == 0);
-		action->setToolTip(editingModeInfo.tooltip);
-		action->setIcon(QPixmap{editingModeInfo.icon});
-		action->setProperty(INDEX_PROPERTY, i);
-		this->toolsBar->addAction(action);
-		QWidget* widget = editingModeInfo.widget;
-		if (widget == nullptr) {
-			widget = new QWidget{this};
-		}
-		this->ui.toolWidgetStack->addWidget(widget);
-		this->toolActions.push_back(action);
-		connect(action, &QAction::triggered, this, &EditorTabWidget::editingModeTriggered);
-	}
-	this->ui.listView->selectAll();
-}
-
-void EditorTabWidget::editingModeTriggered()
-{
-	QAction* triggeredAction = qobject_cast<QAction*>(this->sender());
-	if (triggeredAction != nullptr)
-	{
-		const int index = triggeredAction->property(INDEX_PROPERTY).toInt();
-		this->drawState.mode = static_cast<EditingMode>(index);
-		this->ui.toolWidgetStack->setCurrentIndex(index);
-		for (QAction* action : this->toolActions) {
-			action->setChecked(action == triggeredAction);
-		}
-	}
+	this->drawState.mode = mode;
 }
 
 void updatePreviewPolygon(DrawState* drawState)
@@ -184,7 +100,7 @@
 			if (highlighted != ModelId{0}) {
 				selected.insert(highlighted);
 			}
-			this->select(selected);
+			//this->select(selected);
 			event->accept();
 		}
 		break;
@@ -232,6 +148,7 @@
 		break;
 	}
 }
+/*
 
 void EditorTabWidget::select(const QSet<ModelId> &selected)
 {
@@ -248,12 +165,17 @@
 	}
 	selectionModel->select(itemSelection, QItemSelectionModel::ClearAndSelect);
 }
-
+*/
 const QSet<ModelId> EditorTabWidget::selectedObjects() const
 {
 	return this->canvas->selectedObjects();
 }
 
+EditingMode EditorTabWidget::currentEditingMode() const
+{
+	return this->drawState.mode;
+}
+
 void EditorTabWidget::closeShape()
 {
 	if (this->drawState.polygon.size() >= 2 and this->drawState.polygon.size() <= 4)

mercurial