--- a/src/mainwindow.cpp Thu Oct 03 23:44:28 2019 +0300 +++ b/src/mainwindow.cpp Sat Oct 05 23:47:03 2019 +0300 @@ -1,21 +1,27 @@ +#include <QLabel> +#include <QVBoxLayout> +#include <QCloseEvent> +#include <QFileDialog> +#include <QMessageBox> #include "mainwindow.h" #include "ui_mainwindow.h" +#include "settingseditor/settingseditor.h" #include "version.h" -#include <QLabel> -#include <QVBoxLayout> MainWindow::MainWindow(QWidget *parent) : QMainWindow{parent}, ui{std::make_unique<Ui_MainWindow>()}, - documents{this} + documents{this}, + settings{}, + libraries{this} { this->ui->setupUi(this); connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel); connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel); connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close); + connect(ui->actionSettingsEditor, &QAction::triggered, this, &MainWindow::runSettingsEditor); this->updateTitle(); - this->loadLocales(); - changeLanguage(QLocale::system().name()); + this->restoreSettings(); } MainWindow::~MainWindow() @@ -28,26 +34,28 @@ this->updateTabs(); } -#include <QFileDialog> -#include <QMessageBox> void MainWindow::openModel() { const QString path = QFileDialog::getOpenFileName( - this, tr("Open model"), "", tr("LDraw models (*.ldr *.dat)")); + this, + tr("Open model"), + "", + tr("LDraw models (*.ldr *.dat)")); if (not path.isEmpty()) { QFile file{path}; const bool open_result = file.open(QIODevice::ReadOnly); if (open_result) { - + QMessageBox::critical(this, "Not implemented", "This functionality is not done yet"); } else { - QMessageBox::critical(this, tr("Problem opening file"), - tr("Could not open %1: %2") - .arg(path) - .arg(file.errorString())); + const QString errorString = format( + tr("Could not open %1: %2"), + path, + file.errorString()); + QMessageBox::critical(this, tr("Problem opening file"), errorString); } } } @@ -56,11 +64,15 @@ * @brief Changes the application language to the specified language * @param localeCode Code of the locale to translate to */ -void MainWindow::changeLanguage(const QString& localeCode) +void MainWindow::changeLanguage(QString localeCode) { if (not localeCode.isEmpty() and localeCode != this->currentLanguage) { this->currentLanguage = localeCode; + if (localeCode == "system") + { + localeCode = QLocale::system().name(); + } QLocale::setDefault({localeCode}); qApp->removeTranslator(&this->translator); const bool loadSuccessful = this->translator.load(pathToTranslation(localeCode)); @@ -71,13 +83,13 @@ } } -void MainWindow::languageChangeRequested() +void MainWindow::runSettingsEditor() { - QAction* const senderAction = qobject_cast<QAction*>(sender()); - if (senderAction != nullptr) + SettingsEditor settingsEditor{&this->settings, this}; + const int result = settingsEditor.exec(); + if (result == QDialog::Accepted) { - const QString localeCode = senderAction->data().toString(); - this->changeLanguage(localeCode); + this->restoreSettings(); } } @@ -98,6 +110,16 @@ } /** + * @brief Handles closing the main window + * @param event Event information + */ +void MainWindow::closeEvent(QCloseEvent* event) +{ + saveSettings(); + event->accept(); +} + +/** * @brief Creates a new tab widget for the specified model. * @param model Model to get a new tab widget for * @return widget @@ -154,30 +176,29 @@ setWindowTitle(title); } +/** + * @brief Stores the settings of the main window, storing geometry, etc + */ +void MainWindow::saveSettings() +{ + this->settings.setValue("mainwindow/geometry", this->saveGeometry()); + this->libraries.storeToSettings(&this->settings); +} + +/** + * @brief Restores saved settings relating to the main window + */ +void MainWindow::restoreSettings() +{ + this->restoreGeometry(this->settings.value("mainwindow/geometry").toByteArray()); + const QString systemLocale = QLocale::system().name(); + const QVariant defaultLocale = this->settings.value("locale", systemLocale); + changeLanguage(defaultLocale.toString()); + this->libraries.restoreFromSettings(&this->settings); +} + QString MainWindow::pathToTranslation(const QString& localeCode) { QDir dir {":/locale"}; return dir.filePath(localeCode + ".qm"); } - -void MainWindow::loadLocales() -{ - QDir dir {":/locale"}; - this->ui->menuLanguage->clear(); - QVector<QString> localeCodes = {"en"}; - for (const QFileInfo& file : dir.entryInfoList(QDir::Files)) - { - localeCodes.append(file.baseName()); - } - for (const QString& localeCode : localeCodes) - { - const QLocale locale{localeCode}; - const QString languageName = QLocale::languageToString(locale.language()); - const QIcon flag{":/flags/" + localeCode + ".png"}; - QAction* action = new QAction{languageName, this}; - action->setData(localeCode); - action->setIcon(flag); - this->ui->menuLanguage->addAction(action); - connect(action, &QAction::triggered, this, &MainWindow::languageChangeRequested); - } -}