src/main.cpp

changeset 203
1909a0123c72
parent 202
b05af0bab735
child 204
52e10e8d88cc
--- a/src/main.cpp	Tue Jun 07 20:44:19 2022 +0300
+++ b/src/main.cpp	Tue Jun 07 21:35:29 2022 +0300
@@ -2,6 +2,7 @@
 #include <QFileDialog>
 #include <QMessageBox>
 #include <QMdiSubWindow>
+#include <QStackedWidget>
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
 #include "version.h"
@@ -146,18 +147,6 @@
 	}
 }
 
-static void handleTabCloseButton(Ui_MainWindow* ui, DocumentManager* documents, int tabIndex)
-{
-	/*
-	if (tabIndex >= 0 and tabIndex < ui->tabs->count()) {
-		EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(ui->tabs->widget(tabIndex));
-		if (tab != nullptr) {
-			closeDocument(documents, tab);
-		}
-	}
-	*/
-}
-
 static std::optional<ModelId> findCurrentModelId(Ui_MainWindow* ui, DocumentManager* documents)
 {
 	const EditorTabWidget* tab = currentTabWidget(ui);
@@ -236,6 +225,44 @@
 	};
 }
 
+void initializeTools(Ui_MainWindow* ui, QWidget* parent)
+{
+	const struct
+	{
+		QString name, tooltip;
+		QPixmap icon;
+		QWidget* widget;
+	} editingModesInfo[] = {
+		{
+			.name = QObject::tr("Select"),
+			.tooltip = QObject::tr("Select elements from the model."),
+			.icon = {":/icons/navigate-outline.png"},
+			//.widget = this->objectEditor,
+		},
+		{
+			.name = QObject::tr("Draw"),
+			.tooltip = QObject::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, parent};
+		action->setCheckable(true);
+		action->setChecked(i == 0);
+		action->setData(static_cast<EditingMode>(i));
+		action->setToolTip(editingModeInfo.tooltip);
+		action->setIcon(QPixmap{editingModeInfo.icon});
+		ui->editingModesToolBar->addAction(action);
+		QWidget* widget = editingModeInfo.widget;
+		if (widget == nullptr) {
+			widget = new QWidget{parent};
+		}
+		ui->toolWidgetStack->addWidget(widget);
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	doQtRegistrations();
@@ -251,6 +278,7 @@
 	QStringList recentlyOpenedFiles;
 	ldraw::ColorTable colorTable;
 	gl::RenderPreferences renderPreferences;
+	QMap<Model*, QItemSelectionModel*> itemSelectionModels;
 	ui.setupUi(&mainWindow);
 	const uiutilities::KeySequenceMap defaultKeyboardShortcuts =
 		uiutilities::makeKeySequenceMap(uiutilities::collectActions(&mainWindow));
@@ -299,11 +327,22 @@
 		updateRecentlyOpenedDocumentsMenu();
 	};
 	const auto openModelForEditing = [&](const ModelId modelId){
-		EditorTabWidget* document = new EditorTabWidget{
-			documents.getModelById(modelId),
-			&documents,
-			colorTable,
-		};
+		Model* model = documents.getModelById(modelId);
+		EditorTabWidget* document = new EditorTabWidget{model, &documents, colorTable};
+		QItemSelectionModel* selectionModel = new QItemSelectionModel{model};
+		itemSelectionModels[model] = selectionModel;
+		QObject::connect(selectionModel, &QItemSelectionModel::selectionChanged,
+			[model, document](const QItemSelection& selected, const QItemSelection& deselected)
+		{
+			auto resolveIndex = [&model](const QModelIndex& index){
+				return model->idAt(index.row());
+			};
+			auto resolve = [&resolveIndex](const QItemSelection& selection)
+			{
+				return fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex);
+			};
+			document->canvas->handleSelectionChange(resolve(selected), resolve(deselected));
+		});
 		document->canvas->setRenderPreferences(renderPreferences);
 		QObject::connect(
 			document,
@@ -315,7 +354,6 @@
 		QMdiSubWindow* subWindow = ui.mdiArea->addSubWindow(document);
 		subWindow->setWindowTitle(tabName(fileInfo));
 		subWindow->show();
-		document->restoreSplitterState(documentSplitterState);
 	};
 	QObject::connect(ui.actionNew, &QAction::triggered, [&]{
 		openModelForEditing(documents.newModel());
@@ -409,11 +447,6 @@
 			}
 		}
 	});
-	/*
-	QObject::connect(ui.mdiArea, &QTabWidget::tabCloseRequested, [&](int index){
-		handleTabCloseButton(&ui, &documents, index);
-	});
-	*/
 	QObject::connect(ui.actionDrawAxes, &QAction::triggered, [&](bool drawAxes){
 		renderPreferences.drawAxes = drawAxes;
 		saveSettings();
@@ -427,6 +460,34 @@
 			updateRenderPreferences(&ui, &renderPreferences);
 		});
 	}
+	const auto checkEditingModeAction = [&ui](EditingMode mode) {
+		for (QAction* action : ui.editingModesToolBar->actions()) {
+			action->setChecked(action->data().value<EditingMode>() == mode);
+		}
+	};
+	initializeTools(&ui, &mainWindow);
+	for (QAction* action : ui.editingModesToolBar->actions()) {
+		QObject::connect(action, &QAction::triggered, [&, action]{
+			EditorTabWidget* tab = currentTabWidget(&ui);
+			if (tab != nullptr) {
+				const EditingMode mode = action->data().value<EditingMode>();
+				tab->setEditMode(mode);
+				checkEditingModeAction(mode);
+			}
+		});
+	}
+	QObject::connect(ui.mdiArea, &QMdiArea::subWindowActivated,
+	[&](QMdiSubWindow* subWindow){
+		EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(subWindow->widget());
+		if (tab != nullptr) {
+			checkEditingModeAction(tab->currentEditingMode());
+			QItemSelectionModel* selectionModel = itemSelectionModels.value(tab->model);
+			if (selectionModel != nullptr) {
+				ui.modelListView->setModel(tab->model);
+				ui.modelListView->setSelectionModel(selectionModel);
+			}
+		}
+	});
 	mainWindow.setWindowTitle(title());
 	mainWindow.restoreGeometry(settings.mainWindowGeometry());
 	restoreSettings();

mercurial