Sun, 03 Nov 2019 12:17:41 +0200
major update with many things
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 | 6 | #include "mainwindow.h" |
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 | 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 | 11 | |
12 | MainWindow::MainWindow(QWidget *parent) : | |
3 | 13 | QMainWindow{parent}, |
5 | 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 | 18 | { |
6 | 19 | this->ui->setupUi(this); |
5 | 20 | connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel); |
6 | 21 | connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel); |
3 | 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 | 24 | this->updateTitle(); |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
25 | this->restoreSettings(); |
5 | 26 | } |
27 | ||
28 | MainWindow::~MainWindow() | |
29 | { | |
30 | } | |
31 | ||
32 | void MainWindow::newModel() | |
33 | { | |
34 | documents.newModel(); | |
35 | this->updateTabs(); | |
36 | } | |
37 | ||
38 | void MainWindow::openModel() | |
39 | { | |
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 | 45 | if (not path.isEmpty()) |
46 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
47 | QString errorString; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
48 | QTextStream errorStream{&errorString}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
49 | QString modelName = this->documents.openModel(path, errorStream); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
50 | if (not modelName.isEmpty()) |
5 | 51 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
52 | Document* document = new Document{this->documents.findModelByName(modelName)}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
53 | this->ui->tabs->addTab(document, modelName); |
5 | 54 | } |
55 | else | |
56 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
57 | const QString errorMessage = utility::format( |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
58 | tr("Could not open %1: %2"), |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
59 | path, |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
60 | errorString); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
61 | QMessageBox::critical(this, tr("Problem opening file"), errorMessage); |
5 | 62 | } |
63 | } | |
64 | } | |
65 | ||
66 | /** | |
6 | 67 | * @brief Changes the application language to the specified language |
68 | * @param localeCode Code of the locale to translate to | |
69 | */ | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
70 | void MainWindow::changeLanguage(QString localeCode) |
6 | 71 | { |
72 | if (not localeCode.isEmpty() and localeCode != this->currentLanguage) | |
73 | { | |
74 | this->currentLanguage = localeCode; | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
75 | if (localeCode == "system") |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
76 | { |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
77 | localeCode = QLocale::system().name(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
78 | } |
6 | 79 | QLocale::setDefault({localeCode}); |
80 | qApp->removeTranslator(&this->translator); | |
81 | const bool loadSuccessful = this->translator.load(pathToTranslation(localeCode)); | |
82 | if (loadSuccessful) | |
83 | { | |
84 | qApp->installTranslator(&this->translator); | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
89 | void MainWindow::runSettingsEditor() |
6 | 90 | { |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
91 | SettingsEditor settingsEditor{&this->settings, this}; |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
92 | const int result = settingsEditor.exec(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
93 | if (result == QDialog::Accepted) |
6 | 94 | { |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
95 | this->restoreSettings(); |
6 | 96 | } |
97 | } | |
98 | ||
99 | void MainWindow::changeEvent(QEvent* event) | |
100 | { | |
101 | if (event != nullptr) | |
102 | { | |
103 | switch (event->type()) | |
104 | { | |
105 | case QEvent::LanguageChange: | |
106 | this->ui->retranslateUi(this); | |
107 | break; | |
108 | default: | |
109 | break; | |
110 | } | |
111 | } | |
112 | QMainWindow::changeEvent(event); | |
113 | } | |
114 | ||
115 | /** | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
116 | * @brief Handles closing the main window |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
117 | * @param event Event information |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
118 | */ |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
119 | void MainWindow::closeEvent(QCloseEvent* event) |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
120 | { |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
121 | saveSettings(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
122 | event->accept(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
123 | } |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
124 | |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
125 | /** |
5 | 126 | * @brief Creates a new tab widget for the specified model. |
127 | * @param model Model to get a new tab widget for | |
128 | * @return widget | |
129 | */ | |
130 | QWidget* MainWindow::createWidgetForModel(Model* model) | |
131 | { | |
132 | Q_UNUSED(model); | |
133 | QWidget* widget = new QWidget(ui->tabs); | |
134 | QLabel* label = new QLabel("asdf", widget); | |
135 | QVBoxLayout* layout = new QVBoxLayout; | |
136 | layout->addWidget(label); | |
137 | widget->setLayout(layout); | |
138 | return widget; | |
139 | } | |
140 | ||
141 | /** | |
142 | * @brief Gets a tab widget for the specified model. If it does not exist, | |
143 | * it will be created. | |
144 | * @param model Model to get a tab widget for | |
145 | * @return widget | |
146 | */ | |
147 | QWidget* MainWindow::getWidgetForModel(Model* model) | |
148 | { | |
149 | QWidget* widget = this->modelWidgets.value(model); | |
150 | if (widget == nullptr) | |
151 | { | |
152 | QWidget* const new_widget = createWidgetForModel(model); | |
153 | this->modelWidgets[model] = new_widget; | |
154 | return new_widget; | |
155 | } | |
156 | else | |
157 | { | |
158 | return widget; | |
159 | } | |
160 | } | |
161 | ||
162 | /** | |
163 | * @brief Updates the tab widget | |
164 | */ | |
165 | void MainWindow::updateTabs() | |
166 | { | |
167 | ||
168 | } | |
169 | ||
170 | /** | |
171 | * @brief Updates the title of the main window so to contain the app's name | |
172 | * and version as well as the open document name. | |
173 | */ | |
174 | void MainWindow::updateTitle() | |
175 | { | |
1 | 176 | QString title = ::appName; |
177 | title += " "; | |
178 | title += fullVersionString(); | |
179 | setWindowTitle(title); | |
0 | 180 | } |
6 | 181 | |
7
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
182 | /** |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
183 | * @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
|
184 | */ |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
185 | void MainWindow::saveSettings() |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
186 | { |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
187 | this->settings.setValue("mainwindow/geometry", this->saveGeometry()); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
188 | this->libraries.storeToSettings(&this->settings); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
189 | } |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
190 | |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
191 | /** |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
192 | * @brief Restores saved settings relating to the main window |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
193 | */ |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
194 | void MainWindow::restoreSettings() |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
195 | { |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
196 | this->restoreGeometry(this->settings.value("mainwindow/geometry").toByteArray()); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
197 | const QString systemLocale = QLocale::system().name(); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
198 | const QVariant defaultLocale = this->settings.value("locale", systemLocale); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
199 | changeLanguage(defaultLocale.toString()); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
200 | this->libraries.restoreFromSettings(&this->settings); |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
201 | } |
68443f5be176
added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
202 | |
6 | 203 | QString MainWindow::pathToTranslation(const QString& localeCode) |
204 | { | |
205 | QDir dir {":/locale"}; | |
206 | return dir.filePath(localeCode + ".qm"); | |
207 | } |