109 lines
3.4 KiB
C++
109 lines
3.4 KiB
C++
#include "mainwindow.h"
|
|
#include "./ui_mainwindow.h"
|
|
|
|
#include <QCloseEvent>
|
|
#include <QMessageBox>
|
|
|
|
#include "data/settingshandler.h"
|
|
#include "genericcore.h"
|
|
|
|
static QString updateTextClean = "Do you want to update the application now?";
|
|
static QString updateTextDirty = "Do you want to save the tasks & update the application now?";
|
|
|
|
MainWindow::MainWindow(QWidget* parent)
|
|
: QMainWindow(parent)
|
|
, ui(new Ui::MainWindow) {
|
|
ui->setupUi(this);
|
|
|
|
m_core = new GenericCore();
|
|
|
|
/// application icon
|
|
const QString iconString = "://feature.png";
|
|
#ifdef QT_DEBUG
|
|
QPixmap pixmap = QPixmap(iconString);
|
|
QTransform transform = QTransform();
|
|
transform.rotate(180);
|
|
QPixmap rotated = pixmap.transformed(transform);
|
|
setWindowIcon(QIcon(rotated));
|
|
#else
|
|
setWindowIcon(QIcon(iconString));
|
|
#endif
|
|
|
|
/// TODO restore window geometry and state (& save it in closeEvent)
|
|
const QVariantMap settings = SettingsHandler::getSettings("GUI");
|
|
restoreGeometry(settings.value("geometry").toByteArray());
|
|
restoreState(settings.value("windowState").toByteArray());
|
|
|
|
connect(m_core, &GenericCore::displayStatusMessage, this, &MainWindow::displayStatusMessage);
|
|
connect(this, &MainWindow::displayStatusMessage, this, &MainWindow::showStatusMessage);
|
|
connect(this, &MainWindow::checkForUpdates, this,
|
|
&MainWindow::on_actionCheck_for_update_triggered, Qt::QueuedConnection);
|
|
}
|
|
|
|
MainWindow::~MainWindow() {
|
|
delete ui;
|
|
delete m_core;
|
|
}
|
|
|
|
void MainWindow::on_pushButton_clicked() {
|
|
const QString prefix("Backend provided by: ");
|
|
ui->label->setText(prefix + m_core->toString());
|
|
}
|
|
|
|
void MainWindow::showStatusMessage(const QString text) {
|
|
qInfo() << text;
|
|
ui->statusbar->showMessage(text);
|
|
}
|
|
|
|
void MainWindow::on_actionCheck_for_update_triggered() {
|
|
showStatusMessage("Checking for update...");
|
|
const bool updateAvailable = m_core->isApplicationUpdateAvailable();
|
|
if (updateAvailable) {
|
|
const QString text = isWindowModified() ? updateTextDirty : updateTextClean;
|
|
const QMessageBox::StandardButton clickedButton = QMessageBox::question(
|
|
this, tr("Update available."), text, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
|
|
if (clickedButton == QMessageBox::Yes) {
|
|
m_core->triggerApplicationUpdate();
|
|
close();
|
|
}
|
|
}
|
|
}
|
|
|
|
void MainWindow::closeEvent(QCloseEvent* event) {
|
|
if (isWindowModified()) {
|
|
QMessageBox msgBox;
|
|
msgBox.setWindowTitle(windowTitle() + " - Save dialog");
|
|
msgBox.setText("The document has been modified.");
|
|
msgBox.setInformativeText("Do you want to save your changes?");
|
|
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
|
|
msgBox.setDefaultButton(QMessageBox::Save);
|
|
int ret = msgBox.exec();
|
|
|
|
switch (ret) {
|
|
case QMessageBox::Save:
|
|
// TODO m_core->saveItems();
|
|
event->accept();
|
|
break;
|
|
case QMessageBox::Discard:
|
|
event->accept();
|
|
break;
|
|
case QMessageBox::Cancel:
|
|
event->ignore();
|
|
break;
|
|
default:
|
|
/// should never be reached
|
|
qCritical() << "unexpected switch case in closeEvent:" << ret;
|
|
event->ignore();
|
|
break;
|
|
}
|
|
} else {
|
|
event->accept();
|
|
}
|
|
|
|
if (event->isAccepted()) {
|
|
qInfo() << "Saving GUI settings...";
|
|
SettingsHandler::saveSettings({{"geometry", saveGeometry()}, {"windowState", saveState()}},
|
|
"GUI");
|
|
}
|
|
}
|