src/mainwindow.cpp

Sat, 05 Oct 2019 23:47:03 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Oct 2019 23:47:03 +0300
changeset 7
68443f5be176
parent 6
73e448b2943d
child 8
44679e468ba9
permissions
-rw-r--r--

added the settings editor

#include <QLabel>
#include <QVBoxLayout>
#include <QCloseEvent>
#include <QFileDialog>
#include <QMessageBox>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "settingseditor/settingseditor.h"
#include "version.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())
	{
		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
		{
			const QString errorString = format(
				tr("Could not open %1: %2"),
				path,
				file.errorString());
			QMessageBox::critical(this, tr("Problem opening file"), errorString);
		}
	}
}

/**
 * @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");
}

mercurial