Added an AbstractDialog class and using it as base for a NewItemDialog class (no new item functionality yet).

This commit is contained in:
2025-12-07 10:56:30 +01:00
parent c7012ceff5
commit 928b795f4e
7 changed files with 214 additions and 14 deletions

View File

@ -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

View File

@ -0,0 +1,51 @@
#include "abstractdialog.h"
#include <QDialogButtonBox>
#include <QGuiApplication>
#include <QLabel>
#include <QScreen>
#include <QVBoxLayout>
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); }

33
Dialogs/abstractdialog.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef ABSTRACTDIALOG_H
#define ABSTRACTDIALOG_H
#include <QDialog>
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

60
Dialogs/newitemdialog.cpp Normal file
View File

@ -0,0 +1,60 @@
#include "newitemdialog.h"
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QSpinBox>
#include <model/tablemodel.h>
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);
}

32
Dialogs/newitemdialog.h Normal file
View File

@ -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

View File

@ -5,6 +5,7 @@
#include <QMessageBox>
#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<QAction>(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<QAction>(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<NewItemDialog>(this);
m_newItemDialog->createContent();
}

View File

@ -3,10 +3,11 @@
#include <QMainWindow>
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<QAction> m_deleteItemAct;
unique_ptr<QAction> m_findItemAct;
/// Dialogs
unique_ptr<NewItemDialog> m_newItemDialog;
/// Setup functions
void createActions();
void createFileActions();
void createEditActions();
void createHelpMenu();
void createGuiDialogs();
};
#endif // MAINWINDOW_H