Sun, 03 Nov 2019 12:17:41 +0200
major update with many things
#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 "document.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow{parent}, ui{std::make_unique<Ui_MainWindow>()}, 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->restoreSettings(); } MainWindow::~MainWindow() { } void MainWindow::newModel() { documents.newModel(); this->updateTabs(); } void MainWindow::openModel() { const QString path = QFileDialog::getOpenFileName( this, tr("Open model"), "", tr("LDraw models (*.ldr *.dat)")); if (not path.isEmpty()) { QString errorString; QTextStream errorStream{&errorString}; QString modelName = this->documents.openModel(path, errorStream); if (not modelName.isEmpty()) { Document* document = new Document{this->documents.findModelByName(modelName)}; this->ui->tabs->addTab(document, modelName); } else { const QString errorMessage = utility::format( tr("Could not open %1: %2"), path, errorString); QMessageBox::critical(this, tr("Problem opening file"), errorMessage); } } } /** * @brief Changes the application language to the specified language * @param localeCode Code of the locale to translate to */ 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)); if (loadSuccessful) { qApp->installTranslator(&this->translator); } } } void MainWindow::runSettingsEditor() { SettingsEditor settingsEditor{&this->settings, this}; const int result = settingsEditor.exec(); if (result == QDialog::Accepted) { this->restoreSettings(); } } void MainWindow::changeEvent(QEvent* event) { if (event != nullptr) { switch (event->type()) { case QEvent::LanguageChange: this->ui->retranslateUi(this); break; default: break; } } QMainWindow::changeEvent(event); } /** * @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 */ QWidget* MainWindow::createWidgetForModel(Model* model) { Q_UNUSED(model); QWidget* widget = new QWidget(ui->tabs); QLabel* label = new QLabel("asdf", widget); QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(label); widget->setLayout(layout); return widget; } /** * @brief Gets a tab widget for the specified model. If it does not exist, * it will be created. * @param model Model to get a tab widget for * @return widget */ QWidget* MainWindow::getWidgetForModel(Model* model) { QWidget* widget = this->modelWidgets.value(model); if (widget == nullptr) { QWidget* const new_widget = createWidgetForModel(model); this->modelWidgets[model] = new_widget; return new_widget; } else { return widget; } } /** * @brief Updates the tab widget */ void MainWindow::updateTabs() { } /** * @brief Updates the title of the main window so to contain the app's name * and version as well as the open document name. */ void MainWindow::updateTitle() { QString title = ::appName; title += " "; title += fullVersionString(); 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"); }