Compare commits

...

5 Commits

7 changed files with 431 additions and 18 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

80
Dialogs/newitemdialog.cpp Normal file
View File

@ -0,0 +1,80 @@
#include "newitemdialog.h"
#include <QGridLayout>
#include <QJsonArray>
#include <QJsonObject>
#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 QDoubleSpinBox();
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);
}
void NewItemDialog::accept() {
QJsonObject itemObject;
itemObject.insert("Name", m_nameEdit->text());
itemObject.insert("Description", m_descriptionEdit->text());
itemObject.insert("Info", m_infoEdit->text());
itemObject.insert("Amount", m_amountBox->value());
itemObject.insert("Factor", m_factorBox->value());
QJsonDocument jsonDoc;
QJsonArray itemArray;
itemArray.append(itemObject);
jsonDoc.setArray(itemArray);
emit addItems(jsonDoc.toJson(QJsonDocument::Compact));
// resetContent();
AbstractDialog::accept();
}

42
Dialogs/newitemdialog.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef NEWITEMDIALOG_H
#define NEWITEMDIALOG_H
#include "abstractdialog.h"
class QDoubleSpinBox;
class QLineEdit;
class QSpinBox;
class QLabel;
class NewItemDialog : public AbstractDialog {
Q_OBJECT
public:
NewItemDialog(QWidget* parent = nullptr);
void createContent() override;
signals:
void addItems(const QByteArray& jsonDoc);
public slots:
void accept() override;
// void reject() 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;
QDoubleSpinBox* m_factorBox = nullptr;
};
#endif // NEWITEMDIALOG_H

View File

@ -5,8 +5,10 @@
#include <QMessageBox>
#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?";
@ -30,21 +32,27 @@ 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());
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);
m_tableModel = m_core->getModel();
ui->tableView->setModel(m_tableModel.get());
connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this,
&MainWindow::onSelectionChanged);
onSelectionChanged(QItemSelection(), QItemSelection());
}
MainWindow::~MainWindow() { delete ui; }
@ -87,6 +95,26 @@ void MainWindow::closeEvent(QCloseEvent* event) {
}
}
void MainWindow::showStatusMessage(const QString text) {
qInfo() << text;
ui->statusbar->showMessage(text);
}
void MainWindow::onSelectionChanged(const QItemSelection& selected,
const QItemSelection& deselected) {
Q_UNUSED(selected);
Q_UNUSED(deselected);
QItemSelection localSelection = ui->tableView->selectionModel()->selection();
if (localSelection.empty()) {
// qDebug() << "Nothing selected. Disabling delete action";
m_deleteItemAct->setEnabled(false);
} else {
// qDebug() << "Something selected. Enabling delete action";
m_deleteItemAct->setEnabled(true);
}
}
void MainWindow::onAboutClicked() {
const QString applicationName = APPLICATION_NAME;
const QString titlePrefix = tr("About ");
@ -101,11 +129,6 @@ void MainWindow::onAboutClicked() {
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();
@ -125,6 +148,149 @@ void MainWindow::on_pushButton_clicked() {
ui->label->setText(prefix + m_core->toString());
}
void MainWindow::openNewItemDialog() {
showStatusMessage(tr("Invoked 'Edit|New Item'"));
m_newItemDialog->show();
}
void MainWindow::deleteSelectedtItems() {
showStatusMessage(tr("Invoked 'Edit|Delete Item'"));
QItemSelection localSelection = ui->tableView->selectionModel()->selection();
if (localSelection.empty()) {
qDebug() << "No items selected. Nothing to remove.";
} else {
for (QList<QItemSelectionRange>::reverse_iterator iter = localSelection.rbegin(),
rend = localSelection.rend();
iter != rend; ++iter) {
// qInfo() << "iter:" << *iter;
// const QModelIndex parentIndex = iter->parent();
const int topRow = iter->top();
const int bottomRow = iter->bottom();
const int nRows = bottomRow - topRow + 1;
m_tableModel->removeRows(topRow, nRows);
}
}
}
void MainWindow::createActions() {
// TODO add generic menu actions (file/new, edit/cut, ...)
createFileActions();
createEditActions();
}
void MainWindow::createFileActions() {
m_newFileAct = make_unique<QAction>(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<QAction>(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<QAction>(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<QAction>(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<QAction>(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<QAction>(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<QAction>(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<QAction>(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<QAction>(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<QAction>(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<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.get(), &QAction::triggered, this, &MainWindow::openNewItemDialog);
ui->menu_Edit->addAction(m_openNewItemDialogAct.get());
m_openEditItemDialogAct = make_unique<QAction>(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<QAction>(tr("&Delete item(s)"), this);
m_deleteItemAct->setShortcuts(QKeySequence::Delete);
m_deleteItemAct->setStatusTip(tr("Delete currently selected item(s)"));
connect(m_deleteItemAct.get(), &QAction::triggered, this, &MainWindow::deleteSelectedtItems);
ui->menu_Edit->addAction(m_deleteItemAct.get());
ui->menu_Edit->addSeparator();
m_findItemAct = make_unique<QAction>(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();
@ -135,6 +301,9 @@ void MainWindow::createHelpMenu() {
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
}
void MainWindow::createActions() {
// TODO add generic menu actions (file/new, edit/cut, ...)
void MainWindow::createGuiDialogs() {
m_newItemDialog = make_unique<NewItemDialog>(this);
m_newItemDialog->createContent();
connect(m_newItemDialog.get(), &NewItemDialog::addItems, m_tableModel.get(),
&TableModel::appendItems);
}

View File

@ -1,18 +1,23 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QItemSelection>
#include <QMainWindow>
class NewItemDialog;
class TableModel;
QT_BEGIN_NAMESPACE
class GenericCore;
class QAbstractItemModel;
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class GenericCore;
using namespace std;
class MainWindow : public QMainWindow {
Q_OBJECT
@ -28,21 +33,52 @@ class MainWindow : public QMainWindow {
void closeEvent(QCloseEvent* event) override;
private slots:
void onAboutClicked();
void showStatusMessage(const QString text);
void onSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
void onAboutClicked();
void on_actionCheck_for_update_triggered();
void on_pushButton_clicked();
/// slots for menu actions
void openNewItemDialog();
void deleteSelectedtItems();
private:
Ui::MainWindow* ui;
// GenericCore* m_core;
std::unique_ptr<GenericCore> m_core;
std::shared_ptr<QAbstractItemModel> m_tableModel;
unique_ptr<GenericCore> m_core;
shared_ptr<TableModel> m_tableModel;
/// File actions
unique_ptr<QAction> m_newFileAct;
unique_ptr<QAction> m_openAct;
unique_ptr<QAction> m_saveAct;
unique_ptr<QAction> m_importAct;
unique_ptr<QAction> m_exportAct;
unique_ptr<QAction> m_printAct;
unique_ptr<QAction> m_exitAct;
/// Edit actions
unique_ptr<QAction> m_undoAct;
unique_ptr<QAction> m_redoAct;
unique_ptr<QAction> m_cutAct;
unique_ptr<QAction> m_copyAct;
unique_ptr<QAction> m_pasteAct;
unique_ptr<QAction> m_openNewItemDialogAct;
unique_ptr<QAction> m_openEditItemDialogAct;
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