Add document closing

Sat, 05 Mar 2022 13:18:28 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:18:28 +0200
changeset 160
536efae7137e
parent 159
1a04364d20b5
child 161
6fe485a84d06

Add document closing

icons/close-circle-outline.png file | annotate | diff | comparison | revisions
icons_svg/close-circle-outline.svg file | annotate | diff | comparison | revisions
ldforge.qrc file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.h file | annotate | diff | comparison | revisions
src/mainwindow.ui file | annotate | diff | comparison | revisions
Binary file icons/close-circle-outline.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/icons_svg/close-circle-outline.svg	Sat Mar 05 13:18:28 2022 +0200
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<svg
+   xmlns:dc="http://purl.org/dc/elements/1.1/"
+   xmlns:cc="http://creativecommons.org/ns#"
+   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+   xmlns:svg="http://www.w3.org/2000/svg"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+   width="512"
+   height="512"
+   viewBox="0 0 512 512"
+   version="1.1"
+   id="svg10"
+   sodipodi:docname="close-circle-outline.svg"
+   inkscape:version="1.0.2 (e86c870879, 2021-01-15)">
+  <metadata
+     id="metadata16">
+    <rdf:RDF>
+      <cc:Work
+         rdf:about="">
+        <dc:format>image/svg+xml</dc:format>
+        <dc:type
+           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
+      </cc:Work>
+    </rdf:RDF>
+  </metadata>
+  <defs
+     id="defs14" />
+  <sodipodi:namedview
+     pagecolor="#ffffff"
+     bordercolor="#666666"
+     borderopacity="1"
+     objecttolerance="10"
+     gridtolerance="10"
+     guidetolerance="10"
+     inkscape:pageopacity="0"
+     inkscape:pageshadow="2"
+     inkscape:window-width="1920"
+     inkscape:window-height="970"
+     id="namedview12"
+     showgrid="false"
+     inkscape:zoom="1.546875"
+     inkscape:cx="256"
+     inkscape:cy="256"
+     inkscape:window-x="0"
+     inkscape:window-y="29"
+     inkscape:window-maximized="1"
+     inkscape:current-layer="svg10" />
+  <title
+     id="title2">ionicons-v5-m</title>
+  <path
+     d="M448,256c0-106-86-192-192-192S64,150,64,256s86,192,192,192S448,362,448,256Z"
+     style="fill:#a0a0a0;stroke:#000;stroke-miterlimit:10;stroke-width:32px;fill-opacity:1"
+     id="path4" />
+  <line
+     x1="320"
+     y1="320"
+     x2="192"
+     y2="192"
+     style="fill:none;stroke:#000;stroke-linecap:round;stroke-linejoin:round;stroke-width:32px"
+     id="line6" />
+  <line
+     x1="192"
+     y1="320"
+     x2="320"
+     y2="192"
+     style="fill:none;stroke:#000;stroke-linecap:round;stroke-linejoin:round;stroke-width:32px"
+     id="line8" />
+</svg>
--- a/ldforge.qrc	Sat Mar 05 12:47:10 2022 +0200
+++ b/ldforge.qrc	Sat Mar 05 13:18:28 2022 +0200
@@ -25,5 +25,6 @@
         <file>icons/pencil-outline.png</file>
         <file>icons/warning-outline.png</file>
         <file>icons/linetype-conditionaledge.png</file>
+        <file>icons/close-circle-outline.png</file>
     </qresource>
 </RCC>
--- a/src/mainwindow.cpp	Sat Mar 05 12:47:10 2022 +0200
+++ b/src/mainwindow.cpp	Sat Mar 05 13:18:28 2022 +0200
@@ -76,6 +76,8 @@
 		this, &MainWindow::actionSave);
 	connect(this->ui->actionSaveAs, &QAction::triggered,
 		this, &MainWindow::actionSaveAs);
+	connect(this->ui->actionClose, &QAction::triggered, this, &MainWindow::actionClose);
+	connect(this->ui->tabs, &QTabWidget::tabCloseRequested, this, &MainWindow::handleTabCloseButton);
 	for (auto data : ::renderStyleButtons)
 	{
 		QAction* action = data.memberInstance(this->ui.get());
@@ -214,28 +216,6 @@
 	return qobject_cast<Document*>(this->ui->tabs->currentWidget());
 }
 
-/*
-void MainWindow::closeDocument(Document *document)
-{
-	const int tabIndex = this->ui->tabs->indexOf(document);
-	if (tabIndex != -1)
-	{
-		this->ui->tabs->removeTab(tabIndex);
-		const QString closedDocumentName = pathToName(document->modelPath());
-		delete document;
-	}
-	QSet<QString> openedDocumentNames;
-	for (int i = 0; i < this->ui->tabs->count(); i += 1)
-	{
-		Document* document = qobject_cast<Document*>(this->ui->tabs->widget(i));
-		if (document != nullptr)
-		{
-			openedDocumentNames.insert(pathToName(document->modelPath()));
-		}
-	}
-}
-*/
-
 void MainWindow::handleDocumentSplitterChange()
 {
 	Document* currentDocument = this->currentDocument();
@@ -340,6 +320,47 @@
 	}
 }
 
+/**
+ * @brief Handles the "Close" (Ctrl+W) action
+ */
+void MainWindow::actionClose()
+{
+	if (this->currentDocument() != nullptr)
+	{
+		this->closeDocument(this->currentDocument());
+	}
+}
+
+/**
+ * @brief Removes the document at the specified tab index
+ * @param index
+ */
+void MainWindow::handleTabCloseButton(int tabIndex)
+{
+	if (tabIndex >= 0 and tabIndex < this->ui->tabs->count())
+	{
+		Document* document = qobject_cast<Document*>(this->ui->tabs->widget(tabIndex));
+		if (document != nullptr)
+		{
+			this->closeDocument(document);
+		}
+	}
+}
+
+/**
+ * @brief Closes the specified document
+ * @param document
+ */
+void MainWindow::closeDocument(Document *document)
+{
+	std::optional<ModelId> modelId = this->documents.findIdForModel(&document->getModel());
+	if (modelId.has_value())
+	{
+		this->documents.closeDocument(modelId.value());
+		delete document;
+	}
+}
+
 void MainWindow::changeEvent(QEvent* event)
 {
 	if (event != nullptr)
--- a/src/mainwindow.h	Sat Mar 05 12:47:10 2022 +0200
+++ b/src/mainwindow.h	Sat Mar 05 13:18:28 2022 +0200
@@ -46,6 +46,8 @@
 	void setRenderStyle(gl::RenderStyle renderStyle);
 	void actionSave();
 	void actionSaveAs();
+	void actionClose();
+	void handleTabCloseButton(int tabIndex);
 protected:
 	void changeEvent(QEvent* event) override;
 	void closeEvent(QCloseEvent* event) override;
@@ -53,7 +55,6 @@
 private:
 	std::unique_ptr<class Ui_MainWindow> ui;
 	DocumentManager documents;
-	QMap<Model*, QWidget*> modelWidgets;
 	QString currentLanguage = "en";
 	QTranslator translator;
 	Configuration settings;
@@ -75,4 +76,5 @@
 	static QString pathToTranslation(const QString& localeCode);
 	void loadColors();
 	Document *currentDocument();
+	void closeDocument(Document* document);
 };
--- a/src/mainwindow.ui	Sat Mar 05 12:47:10 2022 +0200
+++ b/src/mainwindow.ui	Sat Mar 05 13:18:28 2022 +0200
@@ -16,7 +16,11 @@
   <widget class="QWidget" name="centralwidget">
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
-     <widget class="QTabWidget" name="tabs"/>
+     <widget class="QTabWidget" name="tabs">
+      <property name="tabsClosable">
+       <bool>true</bool>
+      </property>
+     </widget>
     </item>
    </layout>
   </widget>
@@ -40,9 +44,10 @@
     </widget>
     <addaction name="actionNew"/>
     <addaction name="actionOpen"/>
+    <addaction name="menuRecentFiles"/>
     <addaction name="actionSave"/>
     <addaction name="actionSaveAs"/>
-    <addaction name="menuRecentFiles"/>
+    <addaction name="actionClose"/>
     <addaction name="separator"/>
     <addaction name="actionSettingsEditor"/>
     <addaction name="separator"/>
@@ -210,6 +215,18 @@
     <string>Ctrl+Shift+S</string>
    </property>
   </action>
+  <action name="actionClose">
+   <property name="icon">
+    <iconset resource="../ldforge.qrc">
+     <normaloff>:/icons/close-circle-outline.png</normaloff>:/icons/close-circle-outline.png</iconset>
+   </property>
+   <property name="text">
+    <string>Close</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+W</string>
+   </property>
+  </action>
  </widget>
  <resources>
   <include location="../ldforge.qrc"/>

mercurial