Fix crashing

Wed, 20 Jul 2022 12:59:07 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 20 Jul 2022 12:59:07 +0300
changeset 335
c5830bce1c23
parent 334
083b0d8e220a
child 336
e07425ac5834

Fix crashing
Add option to set tool button style

src/documentmanager.cpp file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
src/mainwindow.ui file | annotate | diff | comparison | revisions
src/model.cpp file | annotate | diff | comparison | revisions
src/parser.cpp file | annotate | diff | comparison | revisions
src/settings.h 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/documentmanager.cpp	Thu Jul 14 16:47:59 2022 +0300
+++ b/src/documentmanager.cpp	Wed Jul 20 12:59:07 2022 +0300
@@ -20,6 +20,7 @@
 #include <QDir>
 #include <QFileInfo>
 #include <QSaveFile>
+#include <QPlainTextDocumentLayout>
 #include <deque>
 #include "src/documentmanager.h"
 #include "src/parser.h"
@@ -29,6 +30,13 @@
 {
 }
 
+static std::unique_ptr<QTextDocument> newTextDocument()
+{
+	std::unique_ptr<QTextDocument> newModel = std::make_unique<QTextDocument>(nullptr);
+	newModel->setDocumentLayout(new QPlainTextDocumentLayout{newModel.get()});
+	return newModel;
+}
+
 /**
  * @brief Creates a new model.
  * @returns the ID of the new model
@@ -37,7 +45,7 @@
 {
 	const ModelId modelId{++this->modelIdCounter};
 	this->openModels.emplace(std::make_pair(modelId, ModelInfo{
-		.model = std::make_unique<Model>(this),
+		.model = newTextDocument(),
 		.id = modelId,
 		.opentype = OpenType::ManuallyOpened,
 	}));
@@ -111,7 +119,6 @@
  * @param openType rationale behind opening this file
  * @returns model id, or no value on error
  */
-#include <QPlainTextDocumentLayout>
 std::optional<ModelId> DocumentManager::openModel(
 	const QString& path,
 	QTextStream& errorStream,
@@ -120,8 +127,7 @@
 	QFile file{path};
 	const QString name = pathToName(QFileInfo{path});
 	file.open(QFile::ReadOnly | QFile::Text);
-	std::unique_ptr<QTextDocument> newModel = std::make_unique<QTextDocument>(nullptr);
-	newModel->setDocumentLayout(new QPlainTextDocumentLayout{newModel.get()});
+	std::unique_ptr<QTextDocument> newModel = newTextDocument();
 	newModel->setPlainText(file.readAll());
 	std::optional<ModelId> result;
 	if (file.error() == QFile::NoError)
--- a/src/main.cpp	Thu Jul 14 16:47:59 2022 +0300
+++ b/src/main.cpp	Wed Jul 20 12:59:07 2022 +0300
@@ -86,6 +86,7 @@
 	qRegisterMetaTypeStreamOperators<Libraries>("Libraries");
 	qRegisterMetaTypeStreamOperators<gl::RenderStyle>();
 	qRegisterMetaTypeStreamOperators<QMdiArea::ViewMode>();
+	qRegisterMetaTypeStreamOperators<Qt::ToolButtonStyle>();
 #endif
 }
 
@@ -494,6 +495,7 @@
 		updateRenderPreferences(&ui, &renderPreferences, &documents);
 		ui.mdiArea->setViewMode(setting<Setting::ViewMode>());
 		ui.retranslateUi(&mainWindow);
+		mainWindow.setToolButtonStyle(setting<Setting::ToolButtonStyle>());
 		settingsChanged.emit();
 	};
 	const auto addRecentlyOpenedFile = [&](const QString& path){
--- a/src/mainwindow.ui	Thu Jul 14 16:47:59 2022 +0300
+++ b/src/mainwindow.ui	Wed Jul 20 12:59:07 2022 +0300
@@ -6,7 +6,7 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>729</width>
+    <width>959</width>
     <height>600</height>
    </rect>
   </property>
@@ -44,8 +44,8 @@
     <rect>
      <x>0</x>
      <y>0</y>
-     <width>729</width>
-     <height>35</height>
+     <width>959</width>
+     <height>29</height>
     </rect>
    </property>
    <widget class="QMenu" name="menuFile">
@@ -88,13 +88,6 @@
     <property name="title">
      <string>Edit</string>
     </property>
-    <addaction name="actionCut"/>
-    <addaction name="actionCopy"/>
-    <addaction name="actionPaste"/>
-    <addaction name="actionDelete"/>
-    <addaction name="separator"/>
-    <addaction name="actionSelectAll"/>
-    <addaction name="separator"/>
     <addaction name="actionInvert"/>
     <addaction name="separator"/>
     <addaction name="actionGridFine"/>
@@ -120,9 +113,6 @@
    <property name="windowTitle">
     <string>toolBar</string>
    </property>
-   <property name="toolButtonStyle">
-    <enum>Qt::ToolButtonTextUnderIcon</enum>
-   </property>
    <attribute name="toolBarArea">
     <enum>TopToolBarArea</enum>
    </attribute>
@@ -138,9 +128,6 @@
    <property name="windowTitle">
     <string>toolBar</string>
    </property>
-   <property name="toolButtonStyle">
-    <enum>Qt::ToolButtonTextUnderIcon</enum>
-   </property>
    <attribute name="toolBarArea">
     <enum>TopToolBarArea</enum>
    </attribute>
@@ -157,9 +144,6 @@
    <property name="windowTitle">
     <string>toolBar_2</string>
    </property>
-   <property name="toolButtonStyle">
-    <enum>Qt::ToolButtonTextUnderIcon</enum>
-   </property>
    <attribute name="toolBarArea">
     <enum>TopToolBarArea</enum>
    </attribute>
@@ -168,17 +152,11 @@
    </attribute>
    <addaction name="actionInvert"/>
    <addaction name="actionDelete"/>
-   <addaction name="actionGridFine"/>
-   <addaction name="actionGridMedium"/>
-   <addaction name="actionGridCoarse"/>
   </widget>
   <widget class="QToolBar" name="editingModesToolBar">
    <property name="windowTitle">
     <string>toolBar_2</string>
    </property>
-   <property name="toolButtonStyle">
-    <enum>Qt::ToolButtonTextUnderIcon</enum>
-   </property>
    <attribute name="toolBarArea">
     <enum>LeftToolBarArea</enum>
    </attribute>
@@ -272,6 +250,21 @@
     </layout>
    </widget>
   </widget>
+  <widget class="QToolBar" name="gridToolBar">
+   <property name="windowTitle">
+    <string>toolBar_2</string>
+   </property>
+   <attribute name="toolBarArea">
+    <enum>TopToolBarArea</enum>
+   </attribute>
+   <attribute name="toolBarBreak">
+    <bool>false</bool>
+   </attribute>
+   <addaction name="actionGridFine"/>
+   <addaction name="actionGridMedium"/>
+   <addaction name="actionGridCoarse"/>
+   <addaction name="actionAdjustGridToView"/>
+  </widget>
   <action name="actionQuit">
    <property name="icon">
     <iconset resource="../resources.qrc">
--- a/src/model.cpp	Thu Jul 14 16:47:59 2022 +0300
+++ b/src/model.cpp	Wed Jul 20 12:59:07 2022 +0300
@@ -175,11 +175,6 @@
 	}
 }
 
-void save(const Model &model, QTextStream* stream)
-{
-	*stream << model.toPlainText();
-}
-
 /**
  * @brief Sets the path to the model
  * @param path New path to use
--- a/src/parser.cpp	Thu Jul 14 16:47:59 2022 +0300
+++ b/src/parser.cpp	Wed Jul 20 12:59:07 2022 +0300
@@ -22,9 +22,9 @@
 #include "src/model.h"
 #include "src/parser.h"
 
-#define NUMBER_REGEX R"([+-]?(?:(?:\d+\.?\d*)|(?:\.\d+)))"
+#define NUMBER_REGEX R"(([+-]?(?:(?:\d+\.?\d*)|(?:\.\d+))))"
 #define SPACE_REGEX R"(\s+)"
-#define VEC3_REGEX "(" NUMBER_REGEX SPACE_REGEX NUMBER_REGEX SPACE_REGEX NUMBER_REGEX ")"
+#define VEC3_REGEX NUMBER_REGEX SPACE_REGEX NUMBER_REGEX SPACE_REGEX NUMBER_REGEX
 #define TWO_VECTORS VEC3_REGEX SPACE_REGEX VEC3_REGEX
 #define THREE_VECTORS TWO_VECTORS SPACE_REGEX VEC3_REGEX
 #define FOUR_VECTORS THREE_VECTORS SPACE_REGEX VEC3_REGEX
@@ -64,6 +64,7 @@
 {
 	const int index = attribIndex<LineType<T, Attribs...>, Attrib>;
 	const TextRange& range = parsed.positions[index];
+	const QString content = parsed.content;
 	return parsed.content.mid(range.start, range.length);
 }
 
@@ -103,6 +104,7 @@
 				.length = match.capturedLength(i + 1),
 			};
 		}
+		parsed->content = match.captured(0);
 		return parsed;
 	};
 	if (auto line1Match = tryRe(exprs().subfileRe)) {
--- a/src/settings.h	Thu Jul 14 16:47:59 2022 +0300
+++ b/src/settings.h	Wed Jul 20 12:59:07 2022 +0300
@@ -25,6 +25,7 @@
 // Setting definitions
 
 Q_DECLARE_METATYPE(QMdiArea::ViewMode)
+Q_DECLARE_METATYPE(Qt::ToolButtonStyle)
 
 // Rendering options
 SETTING(BackgroundColor, (QColor{48, 48, 48}))
@@ -41,6 +42,7 @@
 SETTING(RecentFiles, QStringList{})
 SETTING(ViewMode, QMdiArea::TabbedView)
 SETTING(LogOpenGLDebugMessages, false)
+SETTING(ToolButtonStyle, Qt::ToolButtonFollowStyle)
 
 // File management options
 SETTING(Libraries, Libraries{})
--- a/src/settingseditor/settingseditor.cpp	Thu Jul 14 16:47:59 2022 +0300
+++ b/src/settingseditor/settingseditor.cpp	Wed Jul 20 12:59:07 2022 +0300
@@ -22,6 +22,11 @@
 	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->ui.toolButtonStyle->addItem(tr("Icons only"), Qt::ToolButtonIconOnly);
+	this->ui.toolButtonStyle->addItem(tr("Text only"), Qt::ToolButtonTextOnly);
+	this->ui.toolButtonStyle->addItem(tr("Text beside icon"), Qt::ToolButtonTextBesideIcon);
+	this->ui.toolButtonStyle->addItem(tr("Text under icon"), Qt::ToolButtonTextUnderIcon);
+	this->ui.toolButtonStyle->addItem(tr("Style default"), Qt::ToolButtonFollowStyle);
 	this->loadSettings();
 	this->setWindowTitle(tr("Settings"));
 	this->setWindowIcon(QIcon{":/icons/settings-outline.png"});
@@ -55,6 +60,11 @@
 	if (viewMode != -1) {
 		setSetting<Setting::ViewMode>(static_cast<QMdiArea::ViewMode>(viewMode));
 	}
+	const QVariant toolButtonStyle = this->ui.toolButtonStyle->currentData();
+	if (toolButtonStyle.isValid()) {
+		setSetting<Setting::ToolButtonStyle>(
+			static_cast<Qt::ToolButtonStyle>(toolButtonStyle.toInt()));
+	}
 	this->librariesEditor.saveSettings();
 	Q_EMIT this->settingsChanged();
 }
@@ -72,4 +82,9 @@
 	if (viewModeButton != nullptr) {
 		viewModeButton->setChecked(true);
 	}
+	int toolButtonStyleIndex = this->ui.toolButtonStyle->findData(
+		setting<Setting::ToolButtonStyle>());
+	if (toolButtonStyleIndex != -1) {
+		this->ui.toolButtonStyle->setCurrentIndex(toolButtonStyleIndex);
+	}
 }
--- a/src/settingseditor/settingseditor.ui	Thu Jul 14 16:47:59 2022 +0300
+++ b/src/settingseditor/settingseditor.ui	Wed Jul 20 12:59:07 2022 +0300
@@ -30,8 +30,8 @@
        <rect>
         <x>0</x>
         <y>0</y>
-        <width>694</width>
-        <height>563</height>
+        <width>690</width>
+        <height>558</height>
        </rect>
       </property>
       <layout class="QVBoxLayout" name="verticalLayout">
@@ -90,6 +90,16 @@
                   </item>
                  </layout>
                 </item>
+                <item row="1" column="0">
+                 <widget class="QLabel" name="label">
+                  <property name="text">
+                   <string>Tool button style:</string>
+                  </property>
+                 </widget>
+                </item>
+                <item row="1" column="1">
+                 <widget class="QComboBox" name="toolButtonStyle"/>
+                </item>
                </layout>
               </widget>
              </item>

mercurial