src/main.cpp

Thu, 09 Jun 2022 19:11:27 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Thu, 09 Jun 2022 19:11:27 +0300
changeset 211
b27b90fb993f
parent 206
654661eab7f3
child 212
27259810da6d
permissions
-rw-r--r--

Now builds again

201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
1 #include <QApplication>
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
2 #include <QFileDialog>
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
3 #include <QMessageBox>
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
4 #include <QMdiSubWindow>
203
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
5 #include <QStackedWidget>
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
6 #include "mainwindow.h"
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
7 #include "ui_mainwindow.h"
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
8 #include "version.h"
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
9 #include "document.h"
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
10 #include "settingseditor/settingseditor.h"
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
11 #include "widgets/colorselectdialog.h"
24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 17
diff changeset
12
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
13 static const QDir LOCALE_DIR {":/locale"};
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
15 static void doQtRegistrations()
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 {
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
17 QCoreApplication::setApplicationName(::appName);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
18 QCoreApplication::setOrganizationName("hecknology.net");
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
19 QCoreApplication::setOrganizationDomain("hecknology.net");
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
20 qRegisterMetaTypeStreamOperators<Library>("Library");
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
21 qRegisterMetaTypeStreamOperators<Libraries>("Libraries");
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
22 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
23
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
24 template<typename BaseType, typename MemberType, typename DataType>
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
25 struct MemberData
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
26 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
27 std::size_t member;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
28 DataType payload;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
29 constexpr MemberType memberInstance(BaseType* instance) const
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
30 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
31 return *reinterpret_cast<MemberType*>(reinterpret_cast<char*>(instance) + this->member);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
32 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
33 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
34
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
35 static constexpr MemberData<Ui_MainWindow, QAction*, gl::RenderStyle> renderStyleButtons[] = {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
36 { offsetof(Ui_MainWindow, actionRenderStyleNormal), gl::RenderStyle::Normal },
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
37 { offsetof(Ui_MainWindow, actionRenderStyleBfc), gl::RenderStyle::BfcRedGreen },
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
38 { offsetof(Ui_MainWindow, actionRenderStyleRandom), gl::RenderStyle::RandomColors },
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
39 { offsetof(Ui_MainWindow, actionRenderStylePickScene), gl::RenderStyle::PickScene },
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
40 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
41
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
42 static std::optional<ModelId> openModelFromPath(
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
43 const QString& path,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
44 const LibraryManager* libraries,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
45 DocumentManager* documents,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
46 QWidget* parent)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
47 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
48 QString errorString;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
49 QTextStream errorStream{&errorString};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
50 const std::optional<ModelId> modelIdOpt = documents->openModel(
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
51 path,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
52 errorStream,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
53 DocumentManager::OpenType::ManuallyOpened);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
54 if (modelIdOpt.has_value()) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
55 documents->loadDependenciesForModel(modelIdOpt.value(), path, *libraries, errorStream);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
56 if (not errorString.isEmpty()) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
57 QMessageBox::warning(
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
58 parent,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
59 QObject::tr("Problem loading references"),
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
60 errorString);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
61 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
62 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
63 else {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
64 QMessageBox::critical(
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
65 parent,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
66 QObject::tr("Problem opening file"),
206
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 205
diff changeset
67 QObject::tr("Could not open %1: %2").arg(quoted(path), errorString)
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 205
diff changeset
68 );
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
69 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
70 return modelIdOpt;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
71 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
72
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
73 static QString getOpenModelPath(QWidget* parent)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
74 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
75 return QFileDialog::getOpenFileName(
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
76 parent,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
77 QObject::tr("Open model"),
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
78 "",
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
79 QObject::tr("LDraw models (*.ldr *.dat)"));
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
80 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
81
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
82 static const QString localeCode(const QString& locale)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
83 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
84 if (locale == "system") {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
85 return QLocale::system().name();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
86 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
87 else {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
88 return locale;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
89 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
90 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
91
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
92 /**
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
93 * @brief Changes the application language to the specified language
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
94 */
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
95 static void changeLanguage(const QString& locale, QTranslator* translator)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
96 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
97 if (not locale.isEmpty()) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
98 const QString localeCode = ::localeCode(locale);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
99 QLocale::setDefault({localeCode});
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
100 qApp->removeTranslator(translator);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
101 const QString path = LOCALE_DIR.filePath(localeCode + ".qm");
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
102 const bool loadSuccessful = translator->load(path);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
103 if (loadSuccessful)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
104 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
105 qApp->installTranslator(translator);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
106 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
107 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
108 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
109
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
110 /*
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
111 void MainWindow::handleDocumentSplitterChange()
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
112 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
113 EditorTabWidget* currentDocument = this->currentDocument();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
114 if (currentDocument != nullptr)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
115 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
116 this->documentSplitterState = currentDocument->saveSplitterState();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
117 for (int i = 0; i < this->ui->tabs->count(); i += 1)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
118 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
119 EditorTabWidget* document = qobject_cast<EditorTabWidget*>(this->ui->tabs->widget(i));
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
120 if (document != nullptr and document != currentDocument)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
121 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
122 document->restoreSplitterState(this->documentSplitterState);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
123 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
124 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
125 this->settings.setMainSplitterState(this->documentSplitterState);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
126 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
127 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
128 */
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
129
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
130 static EditorTabWidget* currentTabWidget(Ui_MainWindow* ui)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
131 {
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
132 QMdiSubWindow* activeSubWindow = ui->mdiArea->activeSubWindow();
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
133 if (activeSubWindow == nullptr) {
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
134 return nullptr;
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
135 }
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
136 else {
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
137 return qobject_cast<EditorTabWidget*>(activeSubWindow->widget());
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
138 }
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
139 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
140
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
141
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
142 static void closeDocument(DocumentManager* documents, EditorTabWidget *document)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
143 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
144 std::optional<ModelId> modelId = documents->findIdForModel(document->model);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
145 if (modelId.has_value()) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
146 documents->closeDocument(modelId.value());
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
147 delete document;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
148 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
149 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
150
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
151 static std::optional<ModelId> findCurrentModelId(Ui_MainWindow* ui, DocumentManager* documents)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
152 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
153 const EditorTabWidget* tab = currentTabWidget(ui);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
154 if (tab != nullptr) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
155 return documents->findIdForModel(tab->model);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
156 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
157 else {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
158 return {};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
159 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
160 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
161
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
162 /**
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
163 * @brief Updates the title of the main window so to contain the app's name
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
164 * and version as well as the open document name.
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
165 */
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
166 static QString title()
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
167 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
168 QString title = ::appName;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
169 title += " ";
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
170 title += fullVersionString();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
171 return title;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
172 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
173
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 204
diff changeset
174 static ColorTable loadColors(const LibraryManager* libraries)
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
175 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
176 QTextStream errors;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
177 return libraries->loadColorTable(errors);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
178 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
179
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
180 static QString tabName(const QFileInfo& fileInfo)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
181 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
182 QString result = fileInfo.baseName();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
183 if (result.isEmpty()) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
184 result = QObject::tr("<unnamed>");
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
185 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
186 return result;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
187 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
188
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
189 void rebuildRecentFilesMenu(QMenu* menu, const QStringList& strings, QWidget* parent)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
190 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
191 menu->clear();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
192 for (const QString& path : strings) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
193 QAction* action = new QAction{path, parent};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
194 action->setData(path);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
195 menu->addAction(action);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
196 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
197 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
198
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
199 static void updateRenderPreferences(
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
200 Ui_MainWindow* ui,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
201 const gl::RenderPreferences* renderPreferences)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
202 {
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
203 for (QMdiSubWindow* subWindow : ui->mdiArea->subWindowList()) {
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
204 EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(subWindow->widget());
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
205 if (tab != nullptr) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
206 tab->canvas->setRenderPreferences(*renderPreferences);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
207 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
208 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
209 for (auto data : ::renderStyleButtons) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
210 QAction* action = data.memberInstance(ui);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
211 action->setChecked(renderPreferences->style == data.payload);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
212 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
213 ui->actionDrawAxes->setChecked(renderPreferences->drawAxes);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
214 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
215
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
216 static gl::RenderPreferences loadRenderPreferences(Configuration* settings)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
217 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
218 return gl::RenderPreferences{
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
219 .style = static_cast<gl::RenderStyle>(settings->renderStyle()),
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
220 .mainColor = settings->mainColor(),
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
221 .backgroundColor = settings->backgroundColor(),
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
222 .selectedColor = settings->selectedColor(),
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
223 .lineThickness = settings->lineThickness(),
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
224 .lineAntiAliasing = settings->lineAntiAliasing(),
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
225 .drawAxes = settings->drawAxes(),
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
226 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
227 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
228
203
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
229 void initializeTools(Ui_MainWindow* ui, QWidget* parent)
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
230 {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
231 const struct
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
232 {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
233 QString name, tooltip;
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
234 QPixmap icon;
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
235 QWidget* widget;
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
236 } editingModesInfo[] = {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
237 {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
238 .name = QObject::tr("Select"),
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
239 .tooltip = QObject::tr("Select elements from the model."),
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
240 .icon = {":/icons/navigate-outline.png"},
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
241 //.widget = this->objectEditor,
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
242 },
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
243 {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
244 .name = QObject::tr("Draw"),
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
245 .tooltip = QObject::tr("Draw new elements into the model."),
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
246 .icon = {":/icons/pencil-outline.png"},
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
247 .widget = nullptr,
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
248 },
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
249 };
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
250 for (int i = 0; i < countof(editingModesInfo); ++i) {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
251 const auto& editingModeInfo = editingModesInfo[i];
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
252 QAction* action = new QAction{editingModeInfo.name, parent};
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
253 action->setCheckable(true);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
254 action->setChecked(i == 0);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
255 action->setData(static_cast<EditingMode>(i));
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
256 action->setToolTip(editingModeInfo.tooltip);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
257 action->setIcon(QPixmap{editingModeInfo.icon});
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
258 ui->editingModesToolBar->addAction(action);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
259 QWidget* widget = editingModeInfo.widget;
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
260 if (widget == nullptr) {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
261 widget = new QWidget{parent};
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
262 }
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
263 ui->toolWidgetStack->addWidget(widget);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
264 }
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
265 }
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
266
204
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
267 constexpr bool sortModelIndexesByRow(const QModelIndex& a, const QModelIndex& b)
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
268 {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
269 return a.row() < b.row();
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
270 }
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
271
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
272 std::vector<int> rows(const QModelIndexList& indexList)
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
273 {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
274 std::vector<int> result;
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
275 result.reserve(indexList.size());
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
276 for (const QModelIndex& index : indexList)
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
277 {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
278 result.push_back(index.row());
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
279 }
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
280 return result;
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
281 }
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
282
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
283 int main(int argc, char *argv[])
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
284 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
285 doQtRegistrations();
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents: 2
diff changeset
286 QApplication app{argc, argv};
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
287 QMainWindow mainWindow;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
288 Ui_MainWindow ui;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
289 DocumentManager documents{&mainWindow};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
290 QString currentLanguage = "en";
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
291 QTranslator translator{&mainWindow};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
292 Configuration settings;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
293 LibraryManager libraries{&mainWindow};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
294 QByteArray documentSplitterState;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
295 QStringList recentlyOpenedFiles;
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 204
diff changeset
296 ColorTable colorTable;
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
297 gl::RenderPreferences renderPreferences;
203
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
298 QMap<Model*, QItemSelectionModel*> itemSelectionModels;
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
299 ui.setupUi(&mainWindow);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
300 const uiutilities::KeySequenceMap defaultKeyboardShortcuts =
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
301 uiutilities::makeKeySequenceMap(uiutilities::collectActions(&mainWindow));
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
302 const auto saveSettings = [&]{
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
303 settings.setMainWindowGeometry(mainWindow.saveGeometry());
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
304 settings.setRecentFiles(recentlyOpenedFiles);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
305 settings.setMainSplitterState(documentSplitterState);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
306 settings.setRenderStyle(static_cast<int>(renderPreferences.style));
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
307 settings.setDrawAxes(renderPreferences.drawAxes);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
308 libraries.storeToSettings(&settings);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
309 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
310 const auto updateRecentlyOpenedDocumentsMenu = [&]{
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
311 rebuildRecentFilesMenu(ui.menuRecentFiles, recentlyOpenedFiles, &mainWindow);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
312 for (QAction* action : ui.menuRecentFiles->findChildren<QAction*>()) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
313 QString path = action->data().toString();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
314 QObject::connect(
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
315 action,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
316 &QAction::triggered,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
317 [path, &libraries, &documents, &mainWindow]() {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
318 openModelFromPath(path, &libraries, &documents, &mainWindow);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
319 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
320 );
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
321 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
322 };
204
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
323 static constexpr auto executeAction = [&](
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
324 Model* model, const ModelAction& action
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
325 ) {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
326 std::visit(overloaded{
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
327 [model](const AppendToModel& action){
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
328 model->append(action.newElement);
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
329 },
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
330 [model](const DeleteFromModel& action){
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
331 model->remove(action.position);
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
332 },
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
333 }, action);
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
334 };
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
335 const auto restoreSettings = [&]{
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
336 recentlyOpenedFiles = settings.recentFiles();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
337 documentSplitterState = settings.mainSplitterState();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
338 renderPreferences = loadRenderPreferences(&settings);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
339 changeLanguage(settings.locale(), &translator);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
340 libraries.restoreFromSettings(&settings);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
341 updateRecentlyOpenedDocumentsMenu();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
342 colorTable = loadColors(&libraries);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
343 updateRenderPreferences(&ui, &renderPreferences);
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
344 ui.mdiArea->setViewMode(static_cast<QMdiArea::ViewMode>(settings.viewMode()));
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
345 ui.retranslateUi(&mainWindow);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
346 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
347 const auto addRecentlyOpenedFile = [&](const QString& path){
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
348 constexpr int maxRecentlyOpenedFiles = 10;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
349 recentlyOpenedFiles.removeAll(path);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
350 recentlyOpenedFiles.insert(0, path);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
351 while (recentlyOpenedFiles.size() > maxRecentlyOpenedFiles)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
352 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
353 recentlyOpenedFiles.removeLast();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
354 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
355 saveSettings();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
356 updateRecentlyOpenedDocumentsMenu();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
357 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
358 const auto openModelForEditing = [&](const ModelId modelId){
203
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
359 Model* model = documents.getModelById(modelId);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
360 EditorTabWidget* document = new EditorTabWidget{model, &documents, colorTable};
204
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
361 QObject::connect(
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
362 document,
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
363 &EditorTabWidget::modelAction,
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
364 std::bind(executeAction, model, std::placeholders::_1));
203
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
365 QItemSelectionModel* selectionModel = new QItemSelectionModel{model};
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
366 itemSelectionModels[model] = selectionModel;
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
367 QObject::connect(selectionModel, &QItemSelectionModel::selectionChanged,
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
368 [model, document](const QItemSelection& selected, const QItemSelection& deselected)
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
369 {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
370 auto resolveIndex = [&model](const QModelIndex& index){
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
371 return model->idAt(index.row());
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
372 };
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
373 auto resolve = [&resolveIndex](const QItemSelection& selection)
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
374 {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
375 return fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
376 };
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
377 document->canvas->handleSelectionChange(resolve(selected), resolve(deselected));
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
378 });
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
379 document->canvas->setRenderPreferences(renderPreferences);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
380 QObject::connect(
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
381 document,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
382 &EditorTabWidget::newStatusText,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
383 [&](const QString& newStatusText) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
384 mainWindow.statusBar()->showMessage(newStatusText);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
385 });
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
386 const QFileInfo fileInfo{*documents.modelPath(modelId)};
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
387 QMdiSubWindow* subWindow = ui.mdiArea->addSubWindow(document);
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
388 subWindow->setWindowTitle(tabName(fileInfo));
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
389 subWindow->show();
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
390 };
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
391 QObject::connect(ui.actionNew, &QAction::triggered, [&]{
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
392 openModelForEditing(documents.newModel());
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
393 });
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
394 QObject::connect(ui.actionOpen, &QAction::triggered, [&]{
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
395 const QString path = getOpenModelPath(&mainWindow);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
396 if (not path.isEmpty())
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
397 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
398 const std::optional<ModelId> id = openModelFromPath(path, &libraries, &documents, &mainWindow);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
399 if (id.has_value()) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
400 openModelForEditing(id.value());
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
401 addRecentlyOpenedFile(path);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
402 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
403 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
404 });
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
405 QObject::connect(ui.actionSettingsEditor, &QAction::triggered, [&]{
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
406 SettingsEditor settingsEditor{&settings, defaultKeyboardShortcuts, &mainWindow};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
407 const int result = settingsEditor.exec();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
408 if (result == QDialog::Accepted)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
409 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
410 restoreSettings();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
411 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
412 });
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
413 QObject::connect(ui.actionQuit, &QAction::triggered, &mainWindow, &QMainWindow::close);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
414 QObject::connect(ui.actionAdjustGridToView, &QAction::triggered, [&ui]{
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
415 EditorTabWidget* tab = currentTabWidget(&ui);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
416 if (tab != nullptr)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
417 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
418 adjustGridToView(tab->canvas);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
419 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
420 });
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
421 QObject::connect(ui.actionClose, &QAction::triggered, [&ui, &documents]{
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
422 EditorTabWidget* tab = currentTabWidget(&ui);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
423 if (tab != nullptr)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
424 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
425 closeDocument(&documents, tab);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
426 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
427 });
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
428 const auto save = [&](ModelId modelId){
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
429 QString error;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
430 QTextStream errorStream{&error};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
431 const bool succeeded = documents.saveModel(modelId, errorStream);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
432 if (not succeeded)
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
433 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
434 QMessageBox::critical(&mainWindow, QObject::tr("Save error"), error);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
435 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
436 else
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
437 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
438 const QString* pathPtr = documents.modelPath(modelId);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
439 if (pathPtr != nullptr) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
440 addRecentlyOpenedFile(*pathPtr);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
441 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
442 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
443 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
444 const auto actionSaveAs = [&]{
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
445 const std::optional<ModelId> modelId = findCurrentModelId(&ui, &documents);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
446 if (modelId.has_value())
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
447 {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
448 const QString* pathPtr = documents.modelPath(*modelId);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
449 QString defaultPath = (pathPtr != nullptr) ? *pathPtr : "";
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
450 const QString newPath = QFileDialog::getSaveFileName(
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
451 &mainWindow,
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
452 QObject::tr("Save as…"),
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
453 QFileInfo{defaultPath}.absoluteDir().path(),
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
454 QObject::tr("LDraw files (*.ldr *dat);;All files (*)")
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
455 );
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
456 if (not newPath.isEmpty()) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
457 QString error;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
458 QTextStream errorStream{&error};
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
459 documents.setModelPath(*modelId, newPath, libraries, errorStream);
202
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
460 QMdiSubWindow* const subWindow = ui.mdiArea->currentSubWindow();
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
461 if (subWindow != nullptr) {
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
462 subWindow->setWindowTitle(tabName(QFileInfo{newPath}));
b05af0bab735 Replaced the tab widget with an MDI area
Teemu Piippo <teemu@hecknology.net>
parents: 201
diff changeset
463 }
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
464 save(*modelId);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
465 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
466 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
467 };
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
468 QObject::connect(ui.actionSaveAs, &QAction::triggered, actionSaveAs);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
469 QObject::connect(ui.actionSave, &QAction::triggered, [&]{
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
470 if (currentTabWidget(&ui) != nullptr) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
471 const std::optional<ModelId> modelId = findCurrentModelId(&ui, &documents);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
472 if (modelId.has_value()) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
473 const QString* path = documents.modelPath(*modelId);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
474 if (path == nullptr or path->isEmpty()) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
475 actionSaveAs();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
476 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
477 else {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
478 save(*modelId);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
479 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
480 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
481 }
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
482 });
204
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
483 QObject::connect(ui.actionDelete, &QAction::triggered, [&]{
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
484 EditorTabWidget* tab = currentTabWidget(&ui);
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
485 if (tab != nullptr) {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
486 std::vector<int> selectedRows = rows(ui.modelListView->selectionModel()->selectedRows());
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
487 std::sort(selectedRows.begin(), selectedRows.end(), std::greater<int>{});
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
488 for (int row : selectedRows) {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
489 executeAction(tab->model, DeleteFromModel{.position = row});
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
490 }
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
491 }
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
492 });
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
493 QObject::connect(ui.actionDrawAxes, &QAction::triggered, [&](bool drawAxes){
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
494 renderPreferences.drawAxes = drawAxes;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
495 saveSettings();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
496 updateRenderPreferences(&ui, &renderPreferences);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
497 });
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
498 for (auto data : ::renderStyleButtons) {
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
499 QAction* action = data.memberInstance(&ui);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
500 QObject::connect(action, &QAction::triggered, [&, data]{
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
501 renderPreferences.style = data.payload;
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
502 saveSettings();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
503 updateRenderPreferences(&ui, &renderPreferences);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
504 });
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
505 }
203
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
506 const auto checkEditingModeAction = [&ui](EditingMode mode) {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
507 for (QAction* action : ui.editingModesToolBar->actions()) {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
508 action->setChecked(action->data().value<EditingMode>() == mode);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
509 }
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
510 };
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
511 initializeTools(&ui, &mainWindow);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
512 for (QAction* action : ui.editingModesToolBar->actions()) {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
513 QObject::connect(action, &QAction::triggered, [&, action]{
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
514 EditorTabWidget* tab = currentTabWidget(&ui);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
515 if (tab != nullptr) {
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
516 const EditingMode mode = action->data().value<EditingMode>();
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
517 tab->setEditMode(mode);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
518 checkEditingModeAction(mode);
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
519 }
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
520 });
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
521 }
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
522 QObject::connect(ui.mdiArea, &QMdiArea::subWindowActivated,
204
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
523 [&](QMdiSubWindow* subWindow) {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
524 if (subWindow != nullptr) {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
525 EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(subWindow->widget());
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
526 if (tab != nullptr) {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
527 checkEditingModeAction(tab->currentEditingMode());
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
528 QItemSelectionModel* selectionModel = itemSelectionModels.value(tab->model);
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
529 if (selectionModel != nullptr) {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
530 ui.modelListView->setModel(tab->model);
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
531 ui.modelListView->setSelectionModel(selectionModel);
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
532 }
203
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
533 }
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
534 }
1909a0123c72 Move editing modes tool bar, tool options widget stack and model list view into the main window
Teemu Piippo <teemu@hecknology.net>
parents: 202
diff changeset
535 });
201
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
536 mainWindow.setWindowTitle(title());
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
537 mainWindow.restoreGeometry(settings.mainWindowGeometry());
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
538 restoreSettings();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
539 updateRenderPreferences(&ui, &renderPreferences);
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
540 mainWindow.show();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
541 const int result = app.exec();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
542 saveSettings();
5d201ee4a9c3 Continue giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
543 return result;
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
544 }

mercurial