From 298b2c907849b46dc781ff152c667c4c0cb62042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lie=20DELHAIE?= Date: Sun, 22 Mar 2026 20:19:12 +0100 Subject: [PATCH] add events --- src/core/noteservice.cpp | 8 +++- src/gui/dialog/input/inputdialog.cpp | 10 +++++ src/gui/dialog/input/inputdialog.h | 1 + src/gui/mainwindow.cpp | 65 ++++++++++++++++++++++++++-- src/gui/mainwindow.h | 4 ++ src/gui/mainwindow.ui | 3 ++ 6 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/core/noteservice.cpp b/src/core/noteservice.cpp index 207b6ad..e0bbd94 100644 --- a/src/core/noteservice.cpp +++ b/src/core/noteservice.cpp @@ -153,7 +153,13 @@ QString NoteService::update(QUuid listUuid, QUuid noteUuid, QString newValue) throw std::runtime_error("Note not found"); } - Note n = Note(listUuid, noteUuid, newValue, false); + std::optional 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); return newValue; } diff --git a/src/gui/dialog/input/inputdialog.cpp b/src/gui/dialog/input/inputdialog.cpp index 899bedd..0209f29 100644 --- a/src/gui/dialog/input/inputdialog.cpp +++ b/src/gui/dialog/input/inputdialog.cpp @@ -10,6 +10,16 @@ InputDialog::InputDialog(QWidget *parent, QString title, QString headline, QStri 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() { delete ui; diff --git a/src/gui/dialog/input/inputdialog.h b/src/gui/dialog/input/inputdialog.h index bda2d3f..e829d97 100644 --- a/src/gui/dialog/input/inputdialog.h +++ b/src/gui/dialog/input/inputdialog.h @@ -14,6 +14,7 @@ class InputDialog : public QDialog public: InputDialog(QWidget *parent, QString title, QString headline, QString message); + InputDialog(QWidget *parent, QString title, QString headline, QString message, QString content); ~InputDialog(); QString getInput(); diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index dbe1288..8c3a831 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -6,6 +6,7 @@ #include "src/core/noteservice.h" #include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -20,12 +21,14 @@ MainWindow::MainWindow(QWidget *parent) // ui connect(ui->addListButton, &QPushButton::clicked, this, &MainWindow::openCreateListDialog); 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->notes, &QListWidget::itemChanged, this, &MainWindow::onNoteChanged); // services 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); preload(); @@ -63,7 +66,7 @@ void MainWindow::onListCreated(List value) void MainWindow::onListUpdate(List value) { 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); 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) { QListWidgetItem* item = new QListWidgetItem(); @@ -97,7 +114,7 @@ void MainWindow::onNoteUpdated(Note value) return; } - for (int i = 0; i > ui->notes->count(); i++) { + for (int i = 0; i < ui->notes->count(); i++) { item = ui->notes->item(i); 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() { QList lists = ListService::getInstance()->getAll(); diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index a01533f..106d873 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -25,11 +25,15 @@ private slots: void openCreateListDialog(bool); void onListCreated(List value); void onListUpdate(List value); + void onListDeleted(QUuid uuid); void onNoteCreated(Note value); void onNoteUpdated(Note value); void onListSelected(int i); void onSaveNoteButtonClicked(bool); void onNoteChanged(QListWidgetItem*); + void onListRightClick(const QPoint &pos); + void onListContextMenuDelete(bool); + void onListContextMenuRename(bool); private: Ui::MainWindow *ui; diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index 6aa97d6..b7b8037 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -109,6 +109,9 @@ border-bottom: 2px solid rgb(242, 242, 242) Light + + Qt::ContextMenuPolicy::CustomContextMenu + QListView { show-decoration-selected: 1; /* make the selection span the entire width of the view */