Make editing tools disabled until a document is selected

Mon, 27 Jun 2022 23:58:04 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 27 Jun 2022 23:58:04 +0300
changeset 278
304386ff96a4
parent 277
3406191af523
child 279
cd70c845563a

Make editing tools disabled until a document is selected

src/main.cpp file | annotate | diff | comparison | revisions
--- a/src/main.cpp	Mon Jun 27 23:57:47 2022 +0300
+++ b/src/main.cpp	Mon Jun 27 23:58:04 2022 +0300
@@ -172,7 +172,7 @@
 	}
 }
 
-ModelData* findModelData(DocumentManager* documents, ModelId modelId)
+static ModelData* findModelData(const DocumentManager* documents, ModelId modelId)
 {
 	return documents->findPayload<ModelData>(modelId);
 }
@@ -183,7 +183,7 @@
 	return qobject_cast<ModelSubWindow*>(w);
 }
 
-static ModelData* currentModelData(Ui_MainWindow* ui, DocumentManager* documents)
+static ModelData* currentModelData(Ui_MainWindow* ui, const DocumentManager* documents)
 {
 	if (auto* const activeSubWindow = currentModelSubWindow(ui)) {
 		return findModelData(documents, activeSubWindow->modelId);
@@ -333,7 +333,7 @@
 		const auto& editingModeInfo = editingModesInfo[i];
 		QAction* action = new QAction{editingModeInfo.name, parent};
 		action->setCheckable(true);
-		action->setChecked(i == 0);
+		action->setEnabled(false);
 		action->setData(static_cast<EditingMode>(i));
 		action->setToolTip(editingModeInfo.tooltip);
 		action->setIcon(QPixmap{editingModeInfo.icon});
@@ -670,9 +670,11 @@
 			updateRenderPreferences(&ui, &renderPreferences, &documents);
 		});
 	}
-	const auto checkEditingModeAction = [&ui](EditingMode mode) {
+	const auto checkEditingModeAction = [&ui, &documents](EditingMode mode) {
+		const bool hasDocument = currentModelData(&ui, &documents) != nullptr;
 		for (QAction* action : ui.editingModesToolBar->actions()) {
-			action->setChecked(action->data().value<EditingMode>() == mode);
+			action->setEnabled(hasDocument);
+			action->setChecked(hasDocument and action->data().value<EditingMode>() == mode);
 		}
 	};
 	initializeTools(&ui, &toolWidgets, &mainWindow);

mercurial