#include "mainwindow.h" #include "./ui_mainwindow.h" #include #include #include "../../ApplicationConfig.h" #include "Dialogs/newitemdialog.h" #include "data/settingshandler.h" #include "genericcore.h" #include "model/tablemodel.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 = std::make_unique(); /// 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 const QVariantMap settings = SettingsHandler::getSettings("GUI"); restoreGeometry(settings.value("geometry").toByteArray()); restoreState(settings.value("windowState").toByteArray()); m_tableModel = m_core->getModel(); ui->tableView->setModel(m_tableModel.get()); createActions(); createHelpMenu(); createGuiDialogs(); connect(m_core.get(), &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; } 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"); } } void MainWindow::onAboutClicked() { const QString applicationName = APPLICATION_NAME; const QString titlePrefix = tr("About "); const QString aboutText = tr(QString("%1 v%2 is a template for Qt applications." "

Working-Copy_Collective website" "

Mail to support" "

It uses the Qt Framework.") .arg(applicationName) .arg(APPLICATION_VERSION) .toLatin1()); QMessageBox::about(this, titlePrefix + applicationName, aboutText); } 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::on_pushButton_clicked() { const QString prefix("Backend provided by: "); ui->label->setText(prefix + m_core->toString()); } void MainWindow::openNewItemDialog() { showStatusMessage(tr("Invoked 'Edit|New Item'")); m_newItemDialog->show(); } void MainWindow::createActions() { // TODO add generic menu actions (file/new, edit/cut, ...) createFileActions(); createEditActions(); } void MainWindow::createFileActions() { m_newFileAct = make_unique(tr("&New File"), this); m_newFileAct->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_N)); m_newFileAct->setStatusTip(tr("Create a new file")); // connect(m_newAct, &QAction::triggered, this, &MainWindow::newFile); m_newFileAct->setEnabled(false); ui->menu_File->addAction(m_newFileAct.get()); m_openAct = make_unique(tr("&Open..."), this); m_openAct->setShortcuts(QKeySequence::Open); m_openAct->setStatusTip(tr("Open an existing file")); // connect(m_openAct, &QAction::triggered, this, &MainWindow::open); m_openAct->setEnabled(false); ui->menu_File->addAction(m_openAct.get()); m_saveAct = make_unique(tr("&Save"), this); m_saveAct->setShortcuts(QKeySequence::Save); m_saveAct->setStatusTip(tr("Save the document to disk")); // connect(m_saveAct, &QAction::triggered, this, &MainWindow::save); m_saveAct->setEnabled(false); ui->menu_File->addAction(m_saveAct.get()); ui->menu_File->addSeparator(); m_importAct = make_unique(tr("&Import CSV..."), this); m_importAct->setStatusTip(tr("Import an existing CSV file")); // connect(m_importAct, &QAction::triggered, this, &MainWindow::importCSV); m_importAct->setEnabled(false); ui->menu_File->addAction(m_importAct.get()); m_exportAct = make_unique(tr("&Export CSV..."), this); m_exportAct->setStatusTip(tr("Export content to a CSV document to disk")); // connect(m_exportAct, &QAction::triggered, this, &MainWindow::exportCSV); m_exportAct->setEnabled(false); ui->menu_File->addAction(m_exportAct.get()); ui->menu_File->addSeparator(); m_printAct = make_unique(tr("&Print..."), this); m_printAct->setShortcuts(QKeySequence::Print); m_printAct->setStatusTip(tr("Print the document")); // connect(m_printAct, &QAction::triggered, this, &MainWindow::print); m_printAct->setEnabled(false); ui->menu_File->addAction(m_printAct.get()); ui->menu_File->addSeparator(); m_exitAct = make_unique(tr("E&xit"), this); m_exitAct->setShortcuts(QKeySequence::Quit); m_exitAct->setStatusTip(tr("Exit the application")); connect(m_exitAct.get(), &QAction::triggered, this, &QWidget::close); ui->menu_File->addAction(m_exitAct.get()); } void MainWindow::createEditActions() { m_cutAct = make_unique(tr("Cu&t"), this); m_cutAct->setShortcuts(QKeySequence::Cut); m_cutAct->setStatusTip( tr("Cut the current selection's contents to the " "clipboard")); // connect(m_cutAct, &QAction::triggered, this, &MainWindow::cut); m_cutAct->setEnabled(false); ui->menu_Edit->addAction(m_cutAct.get()); m_copyAct = make_unique(tr("&Copy"), this); m_copyAct->setShortcuts(QKeySequence::Copy); m_copyAct->setStatusTip( tr("Copy the current selection's contents to the " "clipboard")); // connect(m_copyAct, &QAction::triggered, this, &MainWindow::copy); m_copyAct->setEnabled(false); ui->menu_Edit->addAction(m_copyAct.get()); m_pasteAct = make_unique(tr("&Paste"), this); m_pasteAct->setShortcuts(QKeySequence::Paste); m_pasteAct->setStatusTip( tr("Paste the clipboard's contents into the current " "selection")); // connect(m_pasteAct, &QAction::triggered, this, &MainWindow::paste); m_pasteAct->setEnabled(false); ui->menu_Edit->addAction(m_pasteAct.get()); ui->menu_Edit->addSeparator(); m_openNewItemDialogAct = make_unique(tr("&New item"), this); m_openNewItemDialogAct->setShortcut(QKeySequence::New); m_openNewItemDialogAct->setStatusTip(tr("Opens a dialog to add a new item")); connect(m_openNewItemDialogAct.get(), &QAction::triggered, this, &MainWindow::openNewItemDialog); // m_openNewItemDialogAct->setEnabled(false); ui->menu_Edit->addAction(m_openNewItemDialogAct.get()); m_openEditItemDialogAct = make_unique(tr("&Edit item"), this); m_openEditItemDialogAct->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_E)); m_openEditItemDialogAct->setStatusTip(tr("Opens a edit dialog for the current item")); // connect(m_openEditItemDialogAct, &QAction::triggered, this, &MainWindow::openEditDialog); m_openEditItemDialogAct->setEnabled(false); ui->menu_Edit->addAction(m_openEditItemDialogAct.get()); m_deleteItemAct = make_unique(tr("&Delete item"), this); m_deleteItemAct->setShortcuts(QKeySequence::Delete); m_deleteItemAct->setStatusTip(tr("Delete currently selected items")); // connect(m_deleteAct, &QAction::triggered, this, &MainWindow::deleteItem); m_deleteItemAct->setEnabled(false); ui->menu_Edit->addAction(m_deleteItemAct.get()); ui->menu_Edit->addSeparator(); m_findItemAct = make_unique(tr("&Find item(s)"), this); m_findItemAct->setShortcuts(QKeySequence::Find); m_findItemAct->setStatusTip(tr("Opens a dialog to find item(s) by containing text")); // connect(m_findItemAct, &QAction::triggered, this, &MainWindow::findItems); m_findItemAct->setEnabled(false); ui->menu_Edit->addAction(m_findItemAct.get()); } void MainWindow::createHelpMenu() { QMenu* helpMenu = ui->menu_Help; helpMenu->addSeparator(); QAction* aboutAct = helpMenu->addAction(tr("&About"), this, &MainWindow::onAboutClicked); aboutAct->setStatusTip(tr("Show the application's About box")); QAction* aboutQtAct = helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt); aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); } void MainWindow::createGuiDialogs() { m_newItemDialog = make_unique(this); m_newItemDialog->createContent(); connect(m_newItemDialog.get(), &NewItemDialog::addItems, m_tableModel.get(), &TableModel::appendItems); }