From 928b795f4ed95764ba37c0b7e83a26fa217c7e83 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Sun, 7 Dec 2025 10:56:30 +0100 Subject: [PATCH] Added an AbstractDialog class and using it as base for a NewItemDialog class (no new item functionality yet). --- CMakeLists.txt | 2 ++ Dialogs/abstractdialog.cpp | 51 ++++++++++++++++++++++++++++++++ Dialogs/abstractdialog.h | 33 +++++++++++++++++++++ Dialogs/newitemdialog.cpp | 60 ++++++++++++++++++++++++++++++++++++++ Dialogs/newitemdialog.h | 32 ++++++++++++++++++++ mainwindow.cpp | 38 ++++++++++++++++-------- mainwindow.h | 12 ++++++-- 7 files changed, 214 insertions(+), 14 deletions(-) create mode 100644 Dialogs/abstractdialog.cpp create mode 100644 Dialogs/abstractdialog.h create mode 100644 Dialogs/newitemdialog.cpp create mode 100644 Dialogs/newitemdialog.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cd661e0..fd42f13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,8 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) ${PROJECT_SOURCES} utils/messagehandler.h assets/icons.qrc + Dialogs/abstractdialog.h Dialogs/abstractdialog.cpp + Dialogs/newitemdialog.h Dialogs/newitemdialog.cpp ) # Define target properties for Android with Qt 6 as: # set_property(TARGET ${TARGET_APP} APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR diff --git a/Dialogs/abstractdialog.cpp b/Dialogs/abstractdialog.cpp new file mode 100644 index 0000000..31582bc --- /dev/null +++ b/Dialogs/abstractdialog.cpp @@ -0,0 +1,51 @@ +#include "abstractdialog.h" + +#include +#include +#include +#include +#include + +AbstractDialog::AbstractDialog(QWidget* parent) + : QDialog(parent) { + setWindowTitle(tr("Dialog does what?...")); + setModal(true); + + m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + connect(m_buttonBox, &QDialogButtonBox::accepted, this, &AbstractDialog::accept); + connect(m_buttonBox, &QDialogButtonBox::rejected, this, &AbstractDialog::reject); + + m_contentContainer = new QLabel("content", this); + + m_outerLayout = new QVBoxLayout; + m_outerLayout->addWidget(m_contentContainer); + m_outerLayout->addWidget(m_buttonBox); + + setLayout(m_outerLayout); +} + +void AbstractDialog::show() { + centerInParent(); + QWidget::show(); +} + +void AbstractDialog::accept() { QDialog::accept(); } + +void AbstractDialog::reject() { QDialog::reject(); } + +void AbstractDialog::centerInParent() { + // BUG the centering in the parent doesn't work the first time (and the later ones seem off too) + QWidget* parent = parentWidget(); + + if (parent) { + auto parentRect = parent->geometry(); + move(parentRect.center() - rect().center()); + } else { + QRect screenGeometry = QGuiApplication::screens().at(0)->geometry(); + int x = (screenGeometry.width() - width()) / 2; + int y = (screenGeometry.height() - height()) / 2; + move(x, y); + } +} + +void AbstractDialog::closeEvent(QCloseEvent* event) { QWidget::closeEvent(event); } diff --git a/Dialogs/abstractdialog.h b/Dialogs/abstractdialog.h new file mode 100644 index 0000000..66f2240 --- /dev/null +++ b/Dialogs/abstractdialog.h @@ -0,0 +1,33 @@ +#ifndef ABSTRACTDIALOG_H +#define ABSTRACTDIALOG_H + +#include + +class QGridLayout; +class QDialogButtonBox; +class QVBoxLayout; + +class AbstractDialog : public QDialog { + Q_OBJECT + public: + AbstractDialog(QWidget* parent = nullptr); + virtual void createContent() = 0; + + /// QDialog interface + public slots: + void show(); + void accept() override; + void reject() override; + + protected: + void centerInParent(); + + protected: + QWidget* m_contentContainer; + QVBoxLayout* m_outerLayout; + QDialogButtonBox* m_buttonBox = nullptr; + + void closeEvent(QCloseEvent* event) override; +}; + +#endif // ABSTRACTDIALOG_H diff --git a/Dialogs/newitemdialog.cpp b/Dialogs/newitemdialog.cpp new file mode 100644 index 0000000..f0159dd --- /dev/null +++ b/Dialogs/newitemdialog.cpp @@ -0,0 +1,60 @@ +#include "newitemdialog.h" + +#include +#include +#include +#include + +#include + +NewItemDialog::NewItemDialog(QWidget* parent) + : AbstractDialog(parent) {} + +void NewItemDialog::createContent() { + if (m_contentContainer) { + delete m_contentContainer; + } + + setWindowTitle(tr("New item...")); + + m_contentContainer = new QWidget(this); + + // REFACTOR deduce label names and types from meta data & use a factory + m_nameLabel = new QLabel("&Name"); + m_nameEdit = new QLineEdit(); + m_nameLabel->setBuddy(m_nameEdit); + + m_descriptionLabel = new QLabel("&Description"); + m_descriptionEdit = new QLineEdit(); + m_descriptionLabel->setBuddy(m_descriptionEdit); + + m_infoLabel = new QLabel("&Info"); + m_infoEdit = new QLineEdit(); + m_infoLabel->setBuddy(m_infoEdit); + + m_amountLabel = new QLabel("&Amount"); + m_amountBox = new QSpinBox(); + m_amountBox->setMaximum(1000); + m_amountLabel->setBuddy(m_amountBox); + + m_factorLabel = new QLabel("&Factor"); + m_factorBox = new QSpinBox(); + m_factorBox->setMaximum(1000); + m_factorLabel->setBuddy(m_factorBox); + + QGridLayout* layout = new QGridLayout(); + layout->addWidget(m_nameLabel, 0, 0, 1, 1); + layout->addWidget(m_nameEdit, 0, 1, 1, 1); + layout->addWidget(m_descriptionLabel, 1, 0, 1, 1); + layout->addWidget(m_descriptionEdit, 1, 1, 1, 1); + layout->addWidget(m_infoLabel, 2, 0, 1, 1); + layout->addWidget(m_infoEdit, 2, 1, 1, 1); + layout->addWidget(m_amountLabel, 3, 0, 1, 1); + layout->addWidget(m_amountBox, 3, 1, 1, 1); + layout->addWidget(m_factorLabel, 4, 0, 1, 1); + layout->addWidget(m_factorBox, 4, 1, 1, 1); + + m_contentContainer->setLayout(layout); + + m_outerLayout->insertWidget(0, m_contentContainer); +} diff --git a/Dialogs/newitemdialog.h b/Dialogs/newitemdialog.h new file mode 100644 index 0000000..c1f3655 --- /dev/null +++ b/Dialogs/newitemdialog.h @@ -0,0 +1,32 @@ +#ifndef NEWITEMDIALOG_H +#define NEWITEMDIALOG_H + +#include "abstractdialog.h" + +class QLineEdit; +class QSpinBox; +class QLabel; +class NewItemDialog : public AbstractDialog { + public: + NewItemDialog(QWidget* parent = nullptr); + + void createContent() override; + + private: + QLabel* m_nameLabel = nullptr; + QLineEdit* m_nameEdit = nullptr; + + QLabel* m_descriptionLabel = nullptr; + QLineEdit* m_descriptionEdit = nullptr; + + QLabel* m_infoLabel = nullptr; + QLineEdit* m_infoEdit = nullptr; + + QLabel* m_amountLabel = nullptr; + QSpinBox* m_amountBox = nullptr; + + QLabel* m_factorLabel = nullptr; + QSpinBox* m_factorBox = nullptr; +}; + +#endif // NEWITEMDIALOG_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 9dc7987..63215c4 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -5,6 +5,7 @@ #include #include "../../ApplicationConfig.h" +#include "Dialogs/newitemdialog.h" #include "data/settingshandler.h" #include "genericcore.h" @@ -30,13 +31,14 @@ MainWindow::MainWindow(QWidget* parent) setWindowIcon(QIcon(iconString)); #endif - createActions(); - createHelpMenu(); - const QVariantMap settings = SettingsHandler::getSettings("GUI"); restoreGeometry(settings.value("geometry").toByteArray()); restoreState(settings.value("windowState").toByteArray()); + createActions(); + createHelpMenu(); + createGuiDialogs(); + connect(m_core.get(), &GenericCore::displayStatusMessage, this, &MainWindow::displayStatusMessage); connect(this, &MainWindow::displayStatusMessage, this, &MainWindow::showStatusMessage); @@ -125,14 +127,11 @@ void MainWindow::on_pushButton_clicked() { ui->label->setText(prefix + m_core->toString()); } -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")); +void MainWindow::openNewItemDialog() { + showStatusMessage(tr("Invoked 'Edit|New Item'")); + qInfo() << "'open new item dialog' action triggered..."; - QAction* aboutQtAct = helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt); - aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); + m_newItemDialog->show(); } void MainWindow::createActions() { @@ -228,8 +227,8 @@ void MainWindow::createEditActions() { 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, &QAction::triggered, this, &MainWindow::openNewItemDialog); - m_openNewItemDialogAct->setEnabled(false); + 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); @@ -255,3 +254,18 @@ void MainWindow::createEditActions() { 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(); +} diff --git a/mainwindow.h b/mainwindow.h index 40ea7ec..c57565c 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -3,10 +3,11 @@ #include -QT_BEGIN_NAMESPACE - +class NewItemDialog; class QAbstractItemModel; +QT_BEGIN_NAMESPACE + namespace Ui { class MainWindow; } @@ -37,6 +38,9 @@ class MainWindow : public QMainWindow { void on_pushButton_clicked(); + /// slots for menu actions + void openNewItemDialog(); + private: Ui::MainWindow* ui; @@ -63,10 +67,14 @@ class MainWindow : public QMainWindow { unique_ptr m_deleteItemAct; unique_ptr m_findItemAct; + /// Dialogs + unique_ptr m_newItemDialog; + /// Setup functions void createActions(); void createFileActions(); void createEditActions(); void createHelpMenu(); + void createGuiDialogs(); }; #endif // MAINWINDOW_H