src/mainwindow.cpp

Sun, 03 Nov 2019 18:09:47 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 03 Nov 2019 18:09:47 +0200
changeset 13
6e838748867b
parent 12
fe67489523b5
child 16
aeb5f203b3eb
permissions
-rw-r--r--

renamings

7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
1 #include <QLabel>
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
2 #include <QVBoxLayout>
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
3 #include <QCloseEvent>
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
4 #include <QFileDialog>
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
5 #include <QMessageBox>
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 #include "mainwindow.h"
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 #include "ui_mainwindow.h"
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
8 #include "settingseditor/settingseditor.h"
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
9 #include "version.h"
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
10 #include "document.h"
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 MainWindow::MainWindow(QWidget *parent) :
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
13 QMainWindow{parent},
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
14 ui{std::make_unique<Ui_MainWindow>()},
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
15 documents{this},
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
16 settings{},
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
17 libraries{this}
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 {
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
19 this->ui->setupUi(this);
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
20 connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel);
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
21 connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
22 connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close);
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
23 connect(ui->actionSettingsEditor, &QAction::triggered, this, &MainWindow::runSettingsEditor);
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
24 this->updateTitle();
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
25 this->restoreSettings();
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
26 this->newModel();
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
27 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
28
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
29 MainWindow::~MainWindow()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
30 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
31 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
32
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
33 void MainWindow::newModel()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
34 {
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
35 this->openModelForEditing(documents.newModel());
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
36 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
37
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
38 void MainWindow::openModel()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
39 {
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
40 const QString path = QFileDialog::getOpenFileName(
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
41 this,
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
42 tr("Open model"),
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
43 "",
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
44 tr("LDraw models (*.ldr *.dat)"));
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
45 if (not path.isEmpty())
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
46 {
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
47 this->openModelFromPath(path);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
48 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
49 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
50
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
51 void MainWindow::openModelFromPath(const QString& path)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
52 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
53 QString errorString;
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
54 QTextStream errorStream{&errorString};
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
55 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
56 if (not modelName.isEmpty())
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
57 {
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
58 this->documents.loadDependenciesForModel(modelName, this->libraries, errorStream);
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
59 if (not errorString.isEmpty())
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
60 {
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
61 QMessageBox::warning(
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
62 this,
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
63 tr("Problem loading references"),
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
64 errorString);
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 9
diff changeset
65 }
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
66 this->openModelForEditing(modelName);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
67 this->addRecentlyOpenedFile(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 else
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 QMessageBox::critical(
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
72 this,
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
73 tr("Problem opening file"),
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
74 utility::format(
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
75 tr("Could not open %1: %2"),
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
76 path,
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
77 errorString));
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
78 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
79 }
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
80
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
81 /**
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
82 * @brief Changes the application language to the specified language
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
83 * @param localeCode Code of the locale to translate to
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
84 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
85 void MainWindow::changeLanguage(QString localeCode)
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
86 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
87 if (not localeCode.isEmpty() and localeCode != this->currentLanguage)
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
88 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
89 this->currentLanguage = localeCode;
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
90 if (localeCode == "system")
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
91 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
92 localeCode = QLocale::system().name();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
93 }
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
94 QLocale::setDefault({localeCode});
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
95 qApp->removeTranslator(&this->translator);
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
96 const bool loadSuccessful = this->translator.load(pathToTranslation(localeCode));
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
97 if (loadSuccessful)
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
98 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
99 qApp->installTranslator(&this->translator);
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
100 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
101 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
102 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
103
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
104 void MainWindow::addRecentlyOpenedFile(const QString& path)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
105 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
106 this->recentlyOpenedFiles.removeAll(path);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
107 this->recentlyOpenedFiles.insert(0, path);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
108 while (this->recentlyOpenedFiles.size() > maxRecentlyOpenedFiles)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
109 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
110 this->recentlyOpenedFiles.removeLast();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
111 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
112 this->saveSettings();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
113 this->updateRecentlyOpenedDocumentsMenu();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
114 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
115
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
116 void MainWindow::openModelForEditing(const QString& modelName)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
117 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
118 Document* document = new Document{this->documents.findModelByName(modelName)};
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
119 this->ui->tabs->addTab(document, modelName);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
120 this->ui->tabs->setCurrentWidget(document);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
121 document->restoreSplitterState(this->documentSplitterState);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
122 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
123 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
124
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
125 void MainWindow::runSettingsEditor()
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
126 {
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
127 SettingsEditor settingsEditor{&this->settings, this};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
128 const int result = settingsEditor.exec();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
129 if (result == QDialog::Accepted)
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
130 {
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
131 this->restoreSettings();
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
132 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
133 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
134
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
135 void MainWindow::handleDocumentSplitterChange()
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
136 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
137 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
138 if (currentDocument != nullptr)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
139 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
140 this->documentSplitterState = currentDocument->saveSplitterState();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
141 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
142 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
143 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
144 if (document != nullptr and document != currentDocument)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
145 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
146 document->restoreSplitterState(this->documentSplitterState);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
147 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
148 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
149 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
150 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
151 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
152
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
153 void MainWindow::updateRecentlyOpenedDocumentsMenu()
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
154 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
155 this->ui->menuRecentFiles->clear();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
156 for (const QString& path : this->recentlyOpenedFiles)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
157 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
158 QAction* action = new QAction{path, this};
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
159 action->setData(path);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
160 this->ui->menuRecentFiles->addAction(action);
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
161 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
162 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
163 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
164
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
165 void MainWindow::openRecentFile()
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
166 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
167 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
168 if (action != nullptr)
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
169 {
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
170 const QString path = action->data().toString();
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
171 this->openModelFromPath(path);
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 }
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
174
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
175 void MainWindow::changeEvent(QEvent* event)
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
176 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
177 if (event != nullptr)
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
178 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
179 switch (event->type())
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
180 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
181 case QEvent::LanguageChange:
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
182 this->ui->retranslateUi(this);
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
183 break;
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
184 default:
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
185 break;
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
186 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
187 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
188 QMainWindow::changeEvent(event);
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
189 }
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
190
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
191 /**
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
192 * @brief Handles closing the main window
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
193 * @param event Event information
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
194 */
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
195 void MainWindow::closeEvent(QCloseEvent* event)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
196 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
197 saveSettings();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
198 event->accept();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
199 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
200
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
201 /**
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
202 * @brief Updates the title of the main window so to contain the app's name
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
203 * and version as well as the open document name.
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
204 */
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
205 void MainWindow::updateTitle()
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
206 {
1
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
207 QString title = ::appName;
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
208 title += " ";
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
209 title += fullVersionString();
51d14b0c68c0 add version
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
210 setWindowTitle(title);
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
211 }
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
212
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
213 /**
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
214 * @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
215 */
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
216 void MainWindow::saveSettings()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
217 {
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
218 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
219 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
220 this->settings.setValue("MainWindow/DocumentSplitterState", this->documentSplitterState);
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
221 this->libraries.storeToSettings(&this->settings);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
222 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
223
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
224 /**
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
225 * @brief Restores saved settings relating to the main window
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
226 */
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
227 void MainWindow::restoreSettings()
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
228 {
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
229 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
230 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
231 this->documentSplitterState = this->settings.value("MainWindow/DocumentSplitterState").toByteArray();
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
232 const QString systemLocale = QLocale::system().name();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
233 const QVariant defaultLocale = this->settings.value("locale", systemLocale);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
234 changeLanguage(defaultLocale.toString());
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
235 this->libraries.restoreFromSettings(&this->settings);
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
236 this->updateRecentlyOpenedDocumentsMenu();
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
237 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
238
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
239 QString MainWindow::pathToTranslation(const QString& localeCode)
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
240 {
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
241 QDir dir {":/locale"};
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
242 return dir.filePath(localeCode + ".qm");
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
243 }

mercurial