src/mainwindow.cpp

Wed, 22 Jan 2020 01:17:11 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 22 Jan 2020 01:17:11 +0200
changeset 27
c57fb7a5ffa3
parent 26
3a9e761e4faa
child 36
bbb901b97404
permissions
-rw-r--r--

commit work done on plugging vao to the gl renderer, renders nonsense for now

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 23
diff changeset
18
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
19 #include <QLabel>
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
20 #include <QVBoxLayout>
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
21 #include <QCloseEvent>
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
22 #include <QFileDialog>
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
23 #include <QMessageBox>
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 #include "mainwindow.h"
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 #include "ui_mainwindow.h"
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
26 #include "settingseditor/settingseditor.h"
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
27 #include "version.h"
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
28 #include "document.h"
16
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
29 #include "uiutilities.h"
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 MainWindow::MainWindow(QWidget *parent) :
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
32 QMainWindow{parent},
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
33 ui{std::make_unique<Ui_MainWindow>()},
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
34 documents{this},
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
35 settings{},
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
36 libraries{this}
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 {
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
38 this->ui->setupUi(this);
16
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
39 defaultKeyboardShortcuts = uiutilities::makeKeySequenceMap(uiutilities::collectActions(this));
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
40 connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel);
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
41 connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
42 connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close);
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
43 connect(ui->actionSettingsEditor, &QAction::triggered, this, &MainWindow::runSettingsEditor);
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
44 this->updateTitle();
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
45 this->restoreSettings();
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
46 this->newModel();
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
47 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
48
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
49 MainWindow::~MainWindow()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
50 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
51 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
52
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
53 void MainWindow::newModel()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
54 {
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
55 this->openModelForEditing(documents.newModel());
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
56 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
57
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
58 void MainWindow::openModel()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
59 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
60 const QString path = QFileDialog::getOpenFileName(
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
61 this,
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
62 tr("Open model"),
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
63 "",
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
64 tr("LDraw models (*.ldr *.dat)"));
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
65 if (not path.isEmpty())
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
66 {
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
67 this->openModelFromPath(path);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
68 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
69 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
70
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
71 void MainWindow::openModelFromPath(const QString& path)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
72 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
73 QString errorString;
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
74 QTextStream errorStream{&errorString};
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
75 QString modelName = this->documents.openModel(path, errorStream);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
76 if (not modelName.isEmpty())
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
77 {
23
3387a84ddaba fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
78 this->documents.loadDependenciesForModel(modelName, path, this->libraries, errorStream);
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
79 if (not errorString.isEmpty())
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
80 {
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
81 QMessageBox::warning(
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
82 this,
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
83 tr("Problem loading references"),
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
84 errorString);
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
85 }
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
86 this->openModelForEditing(modelName);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
87 this->addRecentlyOpenedFile(path);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
88 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
89 else
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
90 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
91 QMessageBox::critical(
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
92 this,
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
93 tr("Problem opening file"),
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
94 utility::format(
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
95 tr("Could not open %1: %2"),
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
96 path,
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
97 errorString));
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
98 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
99 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
100
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
101 /**
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
102 * @brief Changes the application language to the specified language
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
103 * @param localeCode Code of the locale to translate to
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
104 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
105 void MainWindow::changeLanguage(QString localeCode)
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
106 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
107 if (not localeCode.isEmpty() and localeCode != this->currentLanguage)
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
108 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
109 this->currentLanguage = localeCode;
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
110 if (localeCode == "system")
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
111 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
112 localeCode = QLocale::system().name();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
113 }
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
114 QLocale::setDefault({localeCode});
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
115 qApp->removeTranslator(&this->translator);
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
116 const bool loadSuccessful = this->translator.load(pathToTranslation(localeCode));
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
117 if (loadSuccessful)
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
118 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
119 qApp->installTranslator(&this->translator);
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
120 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
121 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
122 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
123
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
124 void MainWindow::addRecentlyOpenedFile(const QString& path)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
125 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
126 this->recentlyOpenedFiles.removeAll(path);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
127 this->recentlyOpenedFiles.insert(0, path);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
128 while (this->recentlyOpenedFiles.size() > maxRecentlyOpenedFiles)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
129 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
130 this->recentlyOpenedFiles.removeLast();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
131 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
132 this->saveSettings();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
133 this->updateRecentlyOpenedDocumentsMenu();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
134 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
135
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
136 void MainWindow::openModelForEditing(const QString& modelName)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
137 {
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
138 Document* document = new Document{this->documents.findModelByName(modelName), &this->documents, this->colorTable};
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
139 this->ui->tabs->addTab(document, modelName);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
140 this->ui->tabs->setCurrentWidget(document);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
141 document->restoreSplitterState(this->documentSplitterState);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
142 connect(document, &Document::splitterChanged, this, &MainWindow::handleDocumentSplitterChange);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
143 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
144
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
145 void MainWindow::runSettingsEditor()
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
146 {
16
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
147 SettingsEditor settingsEditor{&this->settings, this->defaultKeyboardShortcuts, this};
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
148 const int result = settingsEditor.exec();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
149 if (result == QDialog::Accepted)
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
150 {
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
151 this->restoreSettings();
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
152 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
153 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
154
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
155 void MainWindow::handleDocumentSplitterChange()
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
156 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
157 Document* currentDocument = qobject_cast<Document*>(this->ui->tabs->currentWidget());
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
158 if (currentDocument != nullptr)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
159 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
160 this->documentSplitterState = currentDocument->saveSplitterState();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
161 for (int i = 0; i < this->ui->tabs->count(); i += 1)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
162 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
163 Document* document = qobject_cast<Document*>(this->ui->tabs->widget(i));
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
164 if (document != nullptr and document != currentDocument)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
165 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
166 document->restoreSplitterState(this->documentSplitterState);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
167 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
168 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
169 this->settings.setValue("MainWindow/DocumentSplitterState", this->documentSplitterState);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
170 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
171 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
172
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
173 void MainWindow::updateRecentlyOpenedDocumentsMenu()
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
174 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
175 this->ui->menuRecentFiles->clear();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
176 for (const QString& path : this->recentlyOpenedFiles)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
177 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
178 QAction* action = new QAction{path, this};
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
179 action->setData(path);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
180 this->ui->menuRecentFiles->addAction(action);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
181 connect(action, &QAction::triggered, this, &MainWindow::openRecentFile);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
182 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
183 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
184
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
185 void MainWindow::openRecentFile()
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
186 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
187 QAction* action = qobject_cast<QAction*>(this->sender());
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
188 if (action != nullptr)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
189 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
190 const QString path = action->data().toString();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
191 this->openModelFromPath(path);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
192 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
193 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
194
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
195 void MainWindow::changeEvent(QEvent* event)
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
196 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
197 if (event != nullptr)
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
198 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
199 switch (event->type())
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
200 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
201 case QEvent::LanguageChange:
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
202 this->ui->retranslateUi(this);
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
203 break;
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
204 default:
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
205 break;
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
206 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
207 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
208 QMainWindow::changeEvent(event);
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
209 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
210
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
211 /**
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
212 * @brief Handles closing the main window
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
213 * @param event Event information
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
214 */
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
215 void MainWindow::closeEvent(QCloseEvent* event)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
216 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
217 saveSettings();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
218 event->accept();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
219 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
220
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
221 /**
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
222 * @brief Updates the title of the main window so to contain the app's name
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
223 * and version as well as the open document name.
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
224 */
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
225 void MainWindow::updateTitle()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
226 {
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
227 QString title = ::appName;
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
228 title += " ";
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
229 title += fullVersionString();
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
230 setWindowTitle(title);
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
231 }
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
232
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
233 /**
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
234 * @brief Stores the settings of the main window, storing geometry, etc
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
235 */
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
236 void MainWindow::saveSettings()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
237 {
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
238 this->settings.setValue("MainWindow/Geometry", this->saveGeometry());
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
239 this->settings.setValue("MainWindow/RecentlyOpened", this->recentlyOpenedFiles);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
240 this->settings.setValue("MainWindow/DocumentSplitterState", this->documentSplitterState);
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
241 this->libraries.storeToSettings(&this->settings);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
242 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
243
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
244 /**
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
245 * @brief Restores saved settings relating to the main window
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
246 */
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
247 void MainWindow::restoreSettings()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
248 {
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
249 this->restoreGeometry(this->settings.value("MainWindow/Geometry").toByteArray());
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
250 this->recentlyOpenedFiles = this->settings.value("MainWindow/RecentlyOpened").toStringList();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
251 this->documentSplitterState = this->settings.value("MainWindow/DocumentSplitterState").toByteArray();
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
252 const QString systemLocale = QLocale::system().name();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
253 const QVariant defaultLocale = this->settings.value("locale", systemLocale);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
254 changeLanguage(defaultLocale.toString());
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
255 this->libraries.restoreFromSettings(&this->settings);
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
256 this->updateRecentlyOpenedDocumentsMenu();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
257 this->loadColors();
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
258 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
259
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
260 QString MainWindow::pathToTranslation(const QString& localeCode)
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
261 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
262 QDir dir {":/locale"};
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
263 return dir.filePath(localeCode + ".qm");
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
264 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
265
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
266 void MainWindow::loadColors()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
267 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
268 QTextStream errors;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
269 this->colorTable = this->libraries.loadColorTable(errors);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
270 }

mercurial