add events

This commit is contained in:
2026-03-22 20:19:12 +01:00
parent 8b9d388b1d
commit 298b2c9078
6 changed files with 87 additions and 4 deletions

View File

@@ -153,7 +153,13 @@ QString NoteService::update(QUuid listUuid, QUuid noteUuid, QString newValue)
throw std::runtime_error("Note not found"); throw std::runtime_error("Note not found");
} }
Note n = Note(listUuid, noteUuid, newValue, false); std::optional<Note> note = getByUUID(noteUuid);
if (!note.has_value()) {
throw std::runtime_error("database integrity corrupted");
}
Note n = Note(listUuid, noteUuid, newValue, note.value().isFinished());
emit onNoteUpdated(n); emit onNoteUpdated(n);
return newValue; return newValue;
} }

View File

@@ -10,6 +10,16 @@ InputDialog::InputDialog(QWidget *parent, QString title, QString headline, QStri
ui->message->setText(message); ui->message->setText(message);
} }
InputDialog::InputDialog(QWidget *parent, QString title, QString headline, QString message, QString content) : QDialog(parent), ui(new Ui::InputDialog)
{
ui->setupUi(this);
this->setWindowTitle(title);
ui->errorLabel->setVisible(false);
ui->headline->setText(headline);
ui->message->setText(message);
ui->lineEdit->setText(content);
}
InputDialog::~InputDialog() InputDialog::~InputDialog()
{ {
delete ui; delete ui;

View File

@@ -14,6 +14,7 @@ class InputDialog : public QDialog
public: public:
InputDialog(QWidget *parent, QString title, QString headline, QString message); InputDialog(QWidget *parent, QString title, QString headline, QString message);
InputDialog(QWidget *parent, QString title, QString headline, QString message, QString content);
~InputDialog(); ~InputDialog();
QString getInput(); QString getInput();

View File

@@ -6,6 +6,7 @@
#include "src/core/noteservice.h" #include "src/core/noteservice.h"
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QMenu>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
@@ -20,12 +21,14 @@ MainWindow::MainWindow(QWidget *parent)
// ui // ui
connect(ui->addListButton, &QPushButton::clicked, this, &MainWindow::openCreateListDialog); connect(ui->addListButton, &QPushButton::clicked, this, &MainWindow::openCreateListDialog);
connect(ui->lists, &QListWidget::currentRowChanged, this, &MainWindow::onListSelected); connect(ui->lists, &QListWidget::currentRowChanged, this, &MainWindow::onListSelected);
connect(ui->lists, &QWidget::customContextMenuRequested, this, &MainWindow::onListRightClick);
connect(ui->saveNoteButton, &QPushButton::clicked, this, &MainWindow::onSaveNoteButtonClicked); connect(ui->saveNoteButton, &QPushButton::clicked, this, &MainWindow::onSaveNoteButtonClicked);
connect(ui->notes, &QListWidget::itemChanged, this, &MainWindow::onNoteChanged); connect(ui->notes, &QListWidget::itemChanged, this, &MainWindow::onNoteChanged);
// services // services
connect(ListService::getInstance(), &ListService::onListCreated, this, &MainWindow::onListCreated); connect(ListService::getInstance(), &ListService::onListCreated, this, &MainWindow::onListCreated);
connect(ListService::getInstance(), &ListService::onListUpdated, this, &MainWindow::onListCreated); connect(ListService::getInstance(), &ListService::onListUpdated, this, &MainWindow::onListUpdate);
connect(ListService::getInstance(), &ListService::onListDeleted, this, &MainWindow::onListDeleted);
connect(NoteService::getInstance(), &NoteService::onNoteCreated, this, &MainWindow::onNoteCreated); connect(NoteService::getInstance(), &NoteService::onNoteCreated, this, &MainWindow::onNoteCreated);
preload(); preload();
@@ -63,7 +66,7 @@ void MainWindow::onListCreated(List value)
void MainWindow::onListUpdate(List value) void MainWindow::onListUpdate(List value)
{ {
QListWidgetItem *item = nullptr; QListWidgetItem *item = nullptr;
for (int i = 0; i > ui->lists->count(); i++) { for (int i = 0; i < ui->lists->count(); i++) {
item = ui->lists->item(i); item = ui->lists->item(i);
if (item->data(Qt::UserRole).toUuid() == value.getUUID()) { if (item->data(Qt::UserRole).toUuid() == value.getUUID()) {
@@ -73,6 +76,20 @@ void MainWindow::onListUpdate(List value)
} }
} }
void MainWindow::onListDeleted(QUuid uuid)
{
QListWidgetItem *item = nullptr;
for (int i = 0; i < ui->lists->count(); i++) {
item = ui->lists->item(i);
if (item->data(Qt::UserRole).toUuid() == uuid) {
ui->lists->removeItemWidget(item);
delete item;
return;
}
}
}
void MainWindow::onNoteCreated(Note value) void MainWindow::onNoteCreated(Note value)
{ {
QListWidgetItem* item = new QListWidgetItem(); QListWidgetItem* item = new QListWidgetItem();
@@ -97,7 +114,7 @@ void MainWindow::onNoteUpdated(Note value)
return; return;
} }
for (int i = 0; i > ui->notes->count(); i++) { for (int i = 0; i < ui->notes->count(); i++) {
item = ui->notes->item(i); item = ui->notes->item(i);
if (item->data(Qt::UserRole).toUuid() == value.getUUID()) { if (item->data(Qt::UserRole).toUuid() == value.getUUID()) {
@@ -169,6 +186,48 @@ void MainWindow::onNoteChanged(QListWidgetItem *item)
} }
} }
void MainWindow::onListRightClick(const QPoint &pos)
{
if (ui->lists->selectedItems().length() == 1) {
QMenu menu(this);
QAction *renameAction = new QAction(tr("Rename"), this);
connect(renameAction, &QAction::triggered, this, &MainWindow::onListContextMenuRename);
menu.addAction(renameAction);
menu.addSeparator();
QAction *deleteAction = new QAction(tr("Delete"), this);
connect(deleteAction, &QAction::triggered, this, &MainWindow::onListContextMenuDelete);
menu.addAction(deleteAction);
menu.exec(ui->lists->mapToGlobal(pos));
}
}
void MainWindow::onListContextMenuDelete(bool _)
{
}
void MainWindow::onListContextMenuRename(bool)
{
if (ui->lists->selectedItems().length() == 1) {
QListWidgetItem *item = ui->lists->selectedItems()[0];
InputDialog d = InputDialog(this, "Edit", "Edit", "Edit the list's name", item->text());
auto res = d.exec();
// execute, ignore if not saved
if (res != QDialog::Accepted) {
return;
}
QString newListName = d.getInput();
ListService::getInstance()->update(item->data(Qt::UserRole).toUuid(), newListName);
}
}
void MainWindow::preload() void MainWindow::preload()
{ {
QList<List> lists = ListService::getInstance()->getAll(); QList<List> lists = ListService::getInstance()->getAll();

View File

@@ -25,11 +25,15 @@ private slots:
void openCreateListDialog(bool); void openCreateListDialog(bool);
void onListCreated(List value); void onListCreated(List value);
void onListUpdate(List value); void onListUpdate(List value);
void onListDeleted(QUuid uuid);
void onNoteCreated(Note value); void onNoteCreated(Note value);
void onNoteUpdated(Note value); void onNoteUpdated(Note value);
void onListSelected(int i); void onListSelected(int i);
void onSaveNoteButtonClicked(bool); void onSaveNoteButtonClicked(bool);
void onNoteChanged(QListWidgetItem*); void onNoteChanged(QListWidgetItem*);
void onListRightClick(const QPoint &pos);
void onListContextMenuDelete(bool);
void onListContextMenuRename(bool);
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;

View File

@@ -109,6 +109,9 @@ border-bottom: 2px solid rgb(242, 242, 242)</string>
<fontweight>Light</fontweight> <fontweight>Light</fontweight>
</font> </font>
</property> </property>
<property name="contextMenuPolicy">
<enum>Qt::ContextMenuPolicy::CustomContextMenu</enum>
</property>
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">QListView { <string notr="true">QListView {
show-decoration-selected: 1; /* make the selection span the entire width of the view */ show-decoration-selected: 1; /* make the selection span the entire width of the view */