Replaced the tab widget with an MDI area

Tue, 07 Jun 2022 20:44:19 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 07 Jun 2022 20:44:19 +0300
changeset 202
b05af0bab735
parent 201
5d201ee4a9c3
child 203
1909a0123c72

Replaced the tab widget with an MDI area

src/configurationoptions.txt file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
src/mainwindow.ui file | annotate | diff | comparison | revisions
src/settingseditor/settingseditor.cpp file | annotate | diff | comparison | revisions
src/settingseditor/settingseditor.ui file | annotate | diff | comparison | revisions
--- a/src/configurationoptions.txt	Tue Jun 07 01:37:26 2022 +0300
+++ b/src/configurationoptions.txt	Tue Jun 07 20:44:19 2022 +0300
@@ -20,6 +20,7 @@
 option MainWindowGeometry = QByteArray{}
 option MainSplitterState = QByteArray{}
 option RecentFiles = QStringList{}
+option ViewMode = 1
 
 # File management options
 option Libraries = QVector<Library>{}
--- a/src/main.cpp	Tue Jun 07 01:37:26 2022 +0300
+++ b/src/main.cpp	Tue Jun 07 20:44:19 2022 +0300
@@ -1,6 +1,7 @@
 #include <QApplication>
 #include <QFileDialog>
 #include <QMessageBox>
+#include <QMdiSubWindow>
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
 #include "version.h"
@@ -126,7 +127,13 @@
 
 static EditorTabWidget* currentTabWidget(Ui_MainWindow* ui)
 {
-	return qobject_cast<EditorTabWidget*>(ui->tabs->currentWidget());
+	QMdiSubWindow* activeSubWindow = ui->mdiArea->activeSubWindow();
+	if (activeSubWindow == nullptr) {
+		return nullptr;
+	}
+	else {
+		return qobject_cast<EditorTabWidget*>(activeSubWindow->widget());
+	}
 };
 
 
@@ -141,12 +148,14 @@
 
 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)
@@ -201,8 +210,8 @@
 	Ui_MainWindow* ui,
 	const gl::RenderPreferences* renderPreferences)
 {
-	for (int i = 0; i < ui->tabs->count(); i += 1) {
-		EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(ui->tabs->widget(i));
+	for (QMdiSubWindow* subWindow : ui->mdiArea->subWindowList()) {
+		EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(subWindow->widget());
 		if (tab != nullptr) {
 			tab->canvas->setRenderPreferences(*renderPreferences);
 		}
@@ -275,6 +284,7 @@
 		updateRecentlyOpenedDocumentsMenu();
 		colorTable = loadColors(&libraries);
 		updateRenderPreferences(&ui, &renderPreferences);
+		ui.mdiArea->setViewMode(static_cast<QMdiArea::ViewMode>(settings.viewMode()));
 		ui.retranslateUi(&mainWindow);
 	};
 	const auto addRecentlyOpenedFile = [&](const QString& path){
@@ -302,15 +312,13 @@
 				mainWindow.statusBar()->showMessage(newStatusText);
 			});
 		const QFileInfo fileInfo{*documents.modelPath(modelId)};
-		ui.tabs->addTab(document, tabName(fileInfo));
-		ui.tabs->setCurrentWidget(document);
+		QMdiSubWindow* subWindow = ui.mdiArea->addSubWindow(document);
+		subWindow->setWindowTitle(tabName(fileInfo));
+		subWindow->show();
 		document->restoreSplitterState(documentSplitterState);
 	};
-	const auto newModel = [&openModelForEditing](DocumentManager* documents){
-		openModelForEditing(documents->newModel());
-	};
-	QObject::connect(ui.actionNew, &QAction::triggered, [&newModel, &documents]{
-		newModel(&documents);
+	QObject::connect(ui.actionNew, &QAction::triggered, [&]{
+		openModelForEditing(documents.newModel());
 	});
 	QObject::connect(ui.actionOpen, &QAction::triggered, [&]{
 		const QString path = getOpenModelPath(&mainWindow);
@@ -378,7 +386,10 @@
 				QString error;
 				QTextStream errorStream{&error};
 				documents.setModelPath(*modelId, newPath, libraries, errorStream);
-				ui.tabs->setTabText(ui.tabs->currentIndex(), QFileInfo{newPath}.fileName());
+				QMdiSubWindow* const subWindow = ui.mdiArea->currentSubWindow();
+				if (subWindow != nullptr) {
+					subWindow->setWindowTitle(tabName(QFileInfo{newPath}));
+				}
 				save(*modelId);
 			}
 		}
@@ -398,9 +409,11 @@
 			}
 		}
 	});
-	QObject::connect(ui.tabs, &QTabWidget::tabCloseRequested, [&](int index){
+	/*
+	QObject::connect(ui.mdiArea, &QTabWidget::tabCloseRequested, [&](int index){
 		handleTabCloseButton(&ui, &documents, index);
 	});
+	*/
 	QObject::connect(ui.actionDrawAxes, &QAction::triggered, [&](bool drawAxes){
 		renderPreferences.drawAxes = drawAxes;
 		saveSettings();
@@ -418,7 +431,6 @@
 	mainWindow.restoreGeometry(settings.mainWindowGeometry());
 	restoreSettings();
 	updateRenderPreferences(&ui, &renderPreferences);
-	newModel(&documents);
 	mainWindow.show();
 	const int result = app.exec();
 	saveSettings();
--- a/src/mainwindow.ui	Tue Jun 07 01:37:26 2022 +0300
+++ b/src/mainwindow.ui	Tue Jun 07 20:44:19 2022 +0300
@@ -16,10 +16,19 @@
   <widget class="QWidget" name="centralwidget">
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
-     <widget class="QTabWidget" name="tabs">
+     <widget class="QMdiArea" name="mdiArea">
+      <property name="viewMode">
+       <enum>QMdiArea::TabbedView</enum>
+      </property>
+      <property name="documentMode">
+       <bool>true</bool>
+      </property>
       <property name="tabsClosable">
        <bool>true</bool>
       </property>
+      <property name="tabsMovable">
+       <bool>true</bool>
+      </property>
      </widget>
     </item>
    </layout>
@@ -30,7 +39,7 @@
      <x>0</x>
      <y>0</y>
      <width>800</width>
-     <height>27</height>
+     <height>22</height>
     </rect>
    </property>
    <widget class="QMenu" name="menuFile">
--- a/src/settingseditor/settingseditor.cpp	Tue Jun 07 01:37:26 2022 +0300
+++ b/src/settingseditor/settingseditor.cpp	Tue Jun 07 20:44:19 2022 +0300
@@ -1,4 +1,5 @@
 #include <QSettings>
+#include <QMdiArea>
 #include "gl/common.h"
 #include "keyboardshortcutseditor.h"
 #include "settingseditor.h"
@@ -17,6 +18,9 @@
 	defaultKeyboardShortcuts{defaultKeyboardShortcuts}
 {
 	this->ui.setupUi(this);
+	this->ui.keyboardShortcutsView->setModel(new KeyboardShortcutsEditor{parent, this});
+	this->ui.viewModeButtonGroup->setId(this->ui.viewModeTabs, int{QMdiArea::TabbedView});
+	this->ui.viewModeButtonGroup->setId(this->ui.viewModeSubWindows, int{QMdiArea::SubWindowView});
 	this->loadLocales();
 	this->setDefaults();
 	QVBoxLayout* layout = new QVBoxLayout{this};
@@ -27,7 +31,6 @@
 		&SettingsEditor::accepted,
 		this,
 		&SettingsEditor::handleAccepted);
-	this->ui.keyboardShortcutsView->setModel(new KeyboardShortcutsEditor{parent, this});
 }
 
 SettingsEditor::~SettingsEditor()
@@ -43,6 +46,10 @@
 	this->settings->setSelectedColor(this->ui.selectedColorButton->selectedColor());
 	this->settings->setLineThickness(static_cast<GLfloat>(this->ui.lineThickness->value()));
 	this->settings->setLineAntiAliasing(this->ui.lineAntiAliasing->isChecked());
+	const int viewMode = this->ui.viewModeButtonGroup->checkedId();
+	if (viewMode != -1) {
+		this->settings->setViewMode(viewMode);
+	}
 	this->librariesEditor.saveSettings(this->settings);
 }
 
@@ -76,6 +83,10 @@
 	this->ui.selectedColorButton->setSelectedColor(this->settings->selectedColor());
 	this->ui.lineThickness->setValue(static_cast<double>(this->settings->lineThickness()));
 	this->ui.lineAntiAliasing->setChecked(this->settings->lineAntiAliasing());
+	auto* const viewModeButton = this->ui.viewModeButtonGroup->button(this->settings->viewMode());
+	if (viewModeButton != nullptr) {
+		viewModeButton->setChecked(true);
+	}
 }
 
 void SettingsEditor::setCurrentLanguage(const QString& localeCode)
--- a/src/settingseditor/settingseditor.ui	Tue Jun 07 01:37:26 2022 +0300
+++ b/src/settingseditor/settingseditor.ui	Tue Jun 07 20:44:19 2022 +0300
@@ -68,6 +68,56 @@
             </item>
            </layout>
           </item>
+          <item row="1" column="0">
+           <widget class="QLabel" name="label_6">
+            <property name="text">
+             <string>View mode:</string>
+            </property>
+           </widget>
+          </item>
+          <item row="1" column="1">
+           <layout class="QHBoxLayout" name="horizontalLayout_3">
+            <item>
+             <widget class="QRadioButton" name="viewModeTabs">
+              <property name="toolTip">
+               <string>Arrange opened documents using tabs</string>
+              </property>
+              <property name="text">
+               <string>Tabs</string>
+              </property>
+              <attribute name="buttonGroup">
+               <string notr="true">viewModeButtonGroup</string>
+              </attribute>
+             </widget>
+            </item>
+            <item>
+             <widget class="QRadioButton" name="viewModeSubWindows">
+              <property name="toolTip">
+               <string>Arrange opened documents using resizable and movable subwindows</string>
+              </property>
+              <property name="text">
+               <string>Subwindows</string>
+              </property>
+              <attribute name="buttonGroup">
+               <string notr="true">viewModeButtonGroup</string>
+              </attribute>
+             </widget>
+            </item>
+            <item>
+             <spacer name="horizontalSpacer">
+              <property name="orientation">
+               <enum>Qt::Horizontal</enum>
+              </property>
+              <property name="sizeHint" stdset="0">
+               <size>
+                <width>40</width>
+                <height>20</height>
+               </size>
+              </property>
+             </spacer>
+            </item>
+           </layout>
+          </item>
          </layout>
         </widget>
        </item>
@@ -207,6 +257,18 @@
    <header>widgets/colorbutton.h</header>
   </customwidget>
  </customwidgets>
+ <tabstops>
+  <tabstop>language</tabstop>
+  <tabstop>viewModeTabs</tabstop>
+  <tabstop>viewModeSubWindows</tabstop>
+  <tabstop>mainColorButton</tabstop>
+  <tabstop>selectedColorButton</tabstop>
+  <tabstop>backgroundColorButton</tabstop>
+  <tabstop>lineThickness</tabstop>
+  <tabstop>lineAntiAliasing</tabstop>
+  <tabstop>tabWidget</tabstop>
+  <tabstop>keyboardShortcutsView</tabstop>
+ </tabstops>
  <resources/>
  <connections>
   <connection>
@@ -242,4 +304,7 @@
    </hints>
   </connection>
  </connections>
+ <buttongroups>
+  <buttongroup name="viewModeButtonGroup"/>
+ </buttongroups>
 </ui>

mercurial