Add object editor into main

Wed, 22 Jun 2022 20:10:57 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 22 Jun 2022 20:10:57 +0300
changeset 251
94b0a30a1886
parent 250
2837b549e616
child 252
da4876bfd822

Add object editor into main

src/main.cpp file | annotate | diff | comparison | revisions
src/model.cpp file | annotate | diff | comparison | revisions
src/model.h file | annotate | diff | comparison | revisions
--- a/src/main.cpp	Wed Jun 22 16:53:35 2022 +0300
+++ b/src/main.cpp	Wed Jun 22 20:10:57 2022 +0300
@@ -288,10 +288,14 @@
 	};
 }
 
-void initializeTools(Ui_MainWindow* ui, DocumentManager* documents, QWidget* parent)
+struct ToolWidgets
 {
-	CircleToolOptionsWidget* circleToolOptions = new CircleToolOptionsWidget{parent};
-	ObjectEditor* objectEditor = new ObjectEditor{parent};
+	CircleToolOptionsWidget* circleToolOptions;
+	ObjectEditor* objectEditor;
+};
+
+void initializeTools(Ui_MainWindow* ui, ToolWidgets* toolWidgets, QWidget* parent)
+{
 	const struct
 	{
 		QString name, tooltip;
@@ -302,7 +306,7 @@
 			.name = QObject::tr("Select"),
 			.tooltip = QObject::tr("Select elements from the model."),
 			.icon = {":/icons/navigate-outline.png"},
-			.widget = objectEditor,
+			.widget = toolWidgets->objectEditor,
 		},
 		{
 			.name = QObject::tr("Draw"),
@@ -314,7 +318,7 @@
 			.name = QObject::tr("Circle"),
 			.tooltip = QObject::tr("Draw circular primitives."),
 			.icon = {":/icons/linetype-circularprimitive.png"},
-			.widget = circleToolOptions,
+			.widget = toolWidgets->circleToolOptions,
 		},
 	};
 	for (int i = 0; i < countof(editingModesInfo); ++i) {
@@ -335,14 +339,6 @@
 			ui->toolWidgetStack->setCurrentIndex(i);
 		});
 	}
-	QObject::connect(
-		circleToolOptions,
-		&CircleToolOptionsWidget::optionsChanged,
-		[ui, documents](const CircleToolOptions& options) {
-			if (ModelData* data = currentModelData(ui, documents)) {
-				data->tools->setCircleToolOptions(options);
-			}
-		});
 }
 
 constexpr bool sortModelIndexesByRow(const QModelIndex& a, const QModelIndex& b)
@@ -377,6 +373,10 @@
 	MessageLog messageLog;
 	Signal settingsChanged;
 	ui.setupUi(&mainWindow);
+	ToolWidgets toolWidgets{
+		.circleToolOptions = new CircleToolOptionsWidget{&mainWindow},
+		.objectEditor = new ObjectEditor{&mainWindow},
+	};
 	const uiutilities::KeySequenceMap defaultKeyboardShortcuts =
 		uiutilities::makeKeySequenceMap(uiutilities::collectActions(&mainWindow));
 	const auto saveSettings = [&]{
@@ -463,7 +463,7 @@
 			QObject::connect(
 				data->itemSelectionModel.get(),
 				&QItemSelectionModel::selectionChanged,
-				[modelId, &documents]{
+				[modelId, &documents, &toolWidgets]{
 				ModelData* data = findModelData(&documents, modelId);
 				if (data != nullptr) {
 					auto resolveIndex = [&data](const QModelIndex& index){
@@ -472,6 +472,17 @@
 					const auto selection = data->itemSelectionModel->selection();
 					const auto indices = fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex);
 					data->canvas->setSelection(indices);
+					/*
+					if (indices.size() == 1) {
+						opt<std::size_t> index = data->model->find(*indices.begin());
+						if (index.has_value()) {
+							toolWidgets.objectEditor->setObject((*data->model)[*index]);
+						}
+					}
+					else {
+						toolWidgets.objectEditor->reset();
+					}
+					*/
 				}
 			});
 			data->canvas->setRenderPreferences(renderPreferences);
@@ -633,7 +644,7 @@
 			action->setChecked(action->data().value<EditingMode>() == mode);
 		}
 	};
-	initializeTools(&ui, &documents, &mainWindow);
+	initializeTools(&ui, &toolWidgets, &mainWindow);
 	for (QAction* action : ui.editingModesToolBar->actions()) {
 		QObject::connect(action, &QAction::triggered, [&, action]{
 			if (ModelData* data = currentModelData(&ui, &documents)) {
@@ -669,6 +680,14 @@
 			ui.messageLog->scrollToBottom();
 		}
 	});
+	QObject::connect(
+		toolWidgets.circleToolOptions,
+		&CircleToolOptionsWidget::optionsChanged,
+		[&ui, &documents](const CircleToolOptions& options) {
+			if (ModelData* data = currentModelData(&ui, &documents)) {
+				data->tools->setCircleToolOptions(options);
+			}
+		});
 	mainWindow.setWindowTitle(title());
 	mainWindow.tabifyDockWidget(ui.messageLogDock, ui.toolOptionsDock);
 	mainWindow.restoreGeometry(setting<Setting::MainWindowGeometry>());
--- a/src/model.cpp	Wed Jun 22 16:53:35 2022 +0300
+++ b/src/model.cpp	Wed Jun 22 20:10:57 2022 +0300
@@ -203,7 +203,7 @@
 	Q_EMIT this->dataChanged(index, index);
 }
 
-std::optional<int> Model::find(ModelId id) const
+std::optional<std::size_t> Model::find(ModelId id) const
 {
 	return pointerToOptional(findInMap(this->positions, id));
 }
--- a/src/model.h	Wed Jun 22 16:53:35 2022 +0300
+++ b/src/model.h	Wed Jun 22 20:10:57 2022 +0300
@@ -220,7 +220,7 @@
 	const ModelElement& at(std::size_t position) const;
 	ModelId idAt(std::size_t position) const;
 	void assignAt(std::size_t position, const ModelElement& element);
-	std::optional<int> find(ModelId id) const;
+	std::optional<std::size_t> find(ModelId id) const;
 	void remove(std::size_t index);
 	int rowCount(const QModelIndex&) const override;
 	QVariant data(const QModelIndex& index, int role) const override;

mercurial