src/mainwindow.cpp

changeset 7
68443f5be176
parent 6
73e448b2943d
child 8
44679e468ba9
equal deleted inserted replaced
6:73e448b2943d 7:68443f5be176
1 #include <QLabel>
2 #include <QVBoxLayout>
3 #include <QCloseEvent>
4 #include <QFileDialog>
5 #include <QMessageBox>
1 #include "mainwindow.h" 6 #include "mainwindow.h"
2 #include "ui_mainwindow.h" 7 #include "ui_mainwindow.h"
8 #include "settingseditor/settingseditor.h"
3 #include "version.h" 9 #include "version.h"
4 #include <QLabel>
5 #include <QVBoxLayout>
6 10
7 MainWindow::MainWindow(QWidget *parent) : 11 MainWindow::MainWindow(QWidget *parent) :
8 QMainWindow{parent}, 12 QMainWindow{parent},
9 ui{std::make_unique<Ui_MainWindow>()}, 13 ui{std::make_unique<Ui_MainWindow>()},
10 documents{this} 14 documents{this},
15 settings{},
16 libraries{this}
11 { 17 {
12 this->ui->setupUi(this); 18 this->ui->setupUi(this);
13 connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel); 19 connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel);
14 connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel); 20 connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel);
15 connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close); 21 connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close);
22 connect(ui->actionSettingsEditor, &QAction::triggered, this, &MainWindow::runSettingsEditor);
16 this->updateTitle(); 23 this->updateTitle();
17 this->loadLocales(); 24 this->restoreSettings();
18 changeLanguage(QLocale::system().name());
19 } 25 }
20 26
21 MainWindow::~MainWindow() 27 MainWindow::~MainWindow()
22 { 28 {
23 } 29 }
26 { 32 {
27 documents.newModel(); 33 documents.newModel();
28 this->updateTabs(); 34 this->updateTabs();
29 } 35 }
30 36
31 #include <QFileDialog>
32 #include <QMessageBox>
33 void MainWindow::openModel() 37 void MainWindow::openModel()
34 { 38 {
35 const QString path = QFileDialog::getOpenFileName( 39 const QString path = QFileDialog::getOpenFileName(
36 this, tr("Open model"), "", tr("LDraw models (*.ldr *.dat)")); 40 this,
41 tr("Open model"),
42 "",
43 tr("LDraw models (*.ldr *.dat)"));
37 if (not path.isEmpty()) 44 if (not path.isEmpty())
38 { 45 {
39 QFile file{path}; 46 QFile file{path};
40 const bool open_result = file.open(QIODevice::ReadOnly); 47 const bool open_result = file.open(QIODevice::ReadOnly);
41 if (open_result) 48 if (open_result)
42 { 49 {
43 50 QMessageBox::critical(this, "Not implemented", "This functionality is not done yet");
44 } 51 }
45 else 52 else
46 { 53 {
47 QMessageBox::critical(this, tr("Problem opening file"), 54 const QString errorString = format(
48 tr("Could not open %1: %2") 55 tr("Could not open %1: %2"),
49 .arg(path) 56 path,
50 .arg(file.errorString())); 57 file.errorString());
58 QMessageBox::critical(this, tr("Problem opening file"), errorString);
51 } 59 }
52 } 60 }
53 } 61 }
54 62
55 /** 63 /**
56 * @brief Changes the application language to the specified language 64 * @brief Changes the application language to the specified language
57 * @param localeCode Code of the locale to translate to 65 * @param localeCode Code of the locale to translate to
58 */ 66 */
59 void MainWindow::changeLanguage(const QString& localeCode) 67 void MainWindow::changeLanguage(QString localeCode)
60 { 68 {
61 if (not localeCode.isEmpty() and localeCode != this->currentLanguage) 69 if (not localeCode.isEmpty() and localeCode != this->currentLanguage)
62 { 70 {
63 this->currentLanguage = localeCode; 71 this->currentLanguage = localeCode;
72 if (localeCode == "system")
73 {
74 localeCode = QLocale::system().name();
75 }
64 QLocale::setDefault({localeCode}); 76 QLocale::setDefault({localeCode});
65 qApp->removeTranslator(&this->translator); 77 qApp->removeTranslator(&this->translator);
66 const bool loadSuccessful = this->translator.load(pathToTranslation(localeCode)); 78 const bool loadSuccessful = this->translator.load(pathToTranslation(localeCode));
67 if (loadSuccessful) 79 if (loadSuccessful)
68 { 80 {
69 qApp->installTranslator(&this->translator); 81 qApp->installTranslator(&this->translator);
70 } 82 }
71 } 83 }
72 } 84 }
73 85
74 void MainWindow::languageChangeRequested() 86 void MainWindow::runSettingsEditor()
75 { 87 {
76 QAction* const senderAction = qobject_cast<QAction*>(sender()); 88 SettingsEditor settingsEditor{&this->settings, this};
77 if (senderAction != nullptr) 89 const int result = settingsEditor.exec();
78 { 90 if (result == QDialog::Accepted)
79 const QString localeCode = senderAction->data().toString(); 91 {
80 this->changeLanguage(localeCode); 92 this->restoreSettings();
81 } 93 }
82 } 94 }
83 95
84 void MainWindow::changeEvent(QEvent* event) 96 void MainWindow::changeEvent(QEvent* event)
85 { 97 {
93 default: 105 default:
94 break; 106 break;
95 } 107 }
96 } 108 }
97 QMainWindow::changeEvent(event); 109 QMainWindow::changeEvent(event);
110 }
111
112 /**
113 * @brief Handles closing the main window
114 * @param event Event information
115 */
116 void MainWindow::closeEvent(QCloseEvent* event)
117 {
118 saveSettings();
119 event->accept();
98 } 120 }
99 121
100 /** 122 /**
101 * @brief Creates a new tab widget for the specified model. 123 * @brief Creates a new tab widget for the specified model.
102 * @param model Model to get a new tab widget for 124 * @param model Model to get a new tab widget for
152 title += " "; 174 title += " ";
153 title += fullVersionString(); 175 title += fullVersionString();
154 setWindowTitle(title); 176 setWindowTitle(title);
155 } 177 }
156 178
179 /**
180 * @brief Stores the settings of the main window, storing geometry, etc
181 */
182 void MainWindow::saveSettings()
183 {
184 this->settings.setValue("mainwindow/geometry", this->saveGeometry());
185 this->libraries.storeToSettings(&this->settings);
186 }
187
188 /**
189 * @brief Restores saved settings relating to the main window
190 */
191 void MainWindow::restoreSettings()
192 {
193 this->restoreGeometry(this->settings.value("mainwindow/geometry").toByteArray());
194 const QString systemLocale = QLocale::system().name();
195 const QVariant defaultLocale = this->settings.value("locale", systemLocale);
196 changeLanguage(defaultLocale.toString());
197 this->libraries.restoreFromSettings(&this->settings);
198 }
199
157 QString MainWindow::pathToTranslation(const QString& localeCode) 200 QString MainWindow::pathToTranslation(const QString& localeCode)
158 { 201 {
159 QDir dir {":/locale"}; 202 QDir dir {":/locale"};
160 return dir.filePath(localeCode + ".qm"); 203 return dir.filePath(localeCode + ".qm");
161 } 204 }
162
163 void MainWindow::loadLocales()
164 {
165 QDir dir {":/locale"};
166 this->ui->menuLanguage->clear();
167 QVector<QString> localeCodes = {"en"};
168 for (const QFileInfo& file : dir.entryInfoList(QDir::Files))
169 {
170 localeCodes.append(file.baseName());
171 }
172 for (const QString& localeCode : localeCodes)
173 {
174 const QLocale locale{localeCode};
175 const QString languageName = QLocale::languageToString(locale.language());
176 const QIcon flag{":/flags/" + localeCode + ".png"};
177 QAction* action = new QAction{languageName, this};
178 action->setData(localeCode);
179 action->setIcon(flag);
180 this->ui->menuLanguage->addAction(action);
181 connect(action, &QAction::triggered, this, &MainWindow::languageChangeRequested);
182 }
183 }

mercurial