From 9feee144e34ff9b32de57bed0c503cf9294f3765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lie=20DELHAIE?= Date: Sun, 22 Mar 2026 21:43:22 +0100 Subject: [PATCH] Add delete --- src/core/noteservice.cpp | 39 +++---------------------- src/core/noteservice.h | 3 +- src/gui/mainwindow.cpp | 62 +++++++++++++++++++++++++++++++++++----- src/gui/mainwindow.h | 2 ++ src/gui/mainwindow.ui | 29 +++++++++++++++++-- 5 files changed, 88 insertions(+), 47 deletions(-) diff --git a/src/core/noteservice.cpp b/src/core/noteservice.cpp index e0bbd94..1e785d6 100644 --- a/src/core/noteservice.cpp +++ b/src/core/noteservice.cpp @@ -136,42 +136,15 @@ QUuid NoteService::create(QUuid listUuid, QString value) return newNoteUuid; } -QString NoteService::update(QUuid listUuid, QUuid noteUuid, QString newValue) +QString NoteService::update(QUuid listUuid, QUuid noteUuid, QString newValue, bool isFinished) { QSqlDatabase db = getOpenDatabase(); QSqlQuery query(db); - query.prepare("UPDATE notes SET content = :content WHERE id = :id AND list_id = :list_id;"); + query.prepare("UPDATE notes SET content = :content, finished = :finished WHERE id = :id AND list_id = :list_id;"); query.bindValue(":id", toDbUuid(noteUuid)); query.bindValue(":list_id", toDbUuid(listUuid)); query.bindValue(":content", newValue); - - if (!query.exec()) { - throw makeSqlException("Failed to update note", query.lastError().text()); - } - if (query.numRowsAffected() <= 0) { - throw std::runtime_error("Note not found"); - } - - 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; -} - -void NoteService::setFinishedValue(QUuid listUuid, QUuid noteUuid, bool isFinished) -{ - QSqlDatabase db = getOpenDatabase(); - - QSqlQuery query(db); - query.prepare("UPDATE notes SET finished = :finished WHERE id = :id AND list_id = :list_id;"); - query.bindValue(":id", toDbUuid(noteUuid)); - query.bindValue(":list_id", toDbUuid(listUuid)); query.bindValue(":finished", isFinished); if (!query.exec()) { @@ -181,13 +154,9 @@ void NoteService::setFinishedValue(QUuid listUuid, QUuid noteUuid, bool isFinish throw std::runtime_error("Note not found"); } - std::optional note = getByUUID(noteUuid); - - if (!note.has_value()) { - throw std::runtime_error("database integrity corrupted"); - } - Note n = Note(listUuid, noteUuid, note.value().getContent(), isFinished); + Note n = Note(listUuid, noteUuid, newValue, isFinished); emit onNoteUpdated(n); + return newValue; } void NoteService::remove(QUuid listUuid, QUuid noteUuid) diff --git a/src/core/noteservice.h b/src/core/noteservice.h index 907220f..62856cb 100644 --- a/src/core/noteservice.h +++ b/src/core/noteservice.h @@ -21,8 +21,7 @@ public: static NoteService* getInstance(); QUuid create(QUuid listUuid, QString value); - QString update(QUuid listUuid, QUuid noteUuid, QString newValue); - void setFinishedValue(QUuid listUuid, QUuid noteUuid, bool isFinished); + QString update(QUuid listUuid, QUuid noteUuid, QString newValue, bool isFinished); void remove(QUuid listUuid, QUuid noteUuid); QList getByList(QUuid listUuid); diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 8c3a831..126e39f 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -7,6 +7,7 @@ #include #include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -22,6 +23,7 @@ MainWindow::MainWindow(QWidget *parent) 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->newNoteEdit, &QLineEdit::returnPressed, this, &MainWindow::onNewNoteEditReturnPressed); connect(ui->saveNoteButton, &QPushButton::clicked, this, &MainWindow::onSaveNoteButtonClicked); connect(ui->notes, &QListWidget::itemChanged, this, &MainWindow::onNoteChanged); @@ -30,6 +32,8 @@ MainWindow::MainWindow(QWidget *parent) 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::onNoteUpdated, this, &MainWindow::onNoteUpdated); + connect(NoteService::getInstance(), &NoteService::onNoteDeleted, this, &MainWindow::onNoteDeleted); preload(); } @@ -39,7 +43,7 @@ MainWindow::~MainWindow() delete ui; } -void MainWindow::openCreateListDialog(bool _) +void MainWindow::openCreateListDialog(bool) { // create the input dialog InputDialog d = InputDialog(this, "Create a list", "New List", "Give a name to this list"); @@ -105,12 +109,10 @@ void MainWindow::onNoteUpdated(Note value) { QListWidgetItem *item = ui->lists->currentItem(); if (item == nullptr) { - qDebug() << "item null"; return; } if (item->data(Qt::UserRole).toUuid() != value.getParentUUID()) { - qDebug() << "item uuid not matching"; return; } @@ -125,6 +127,30 @@ void MainWindow::onNoteUpdated(Note value) } } +void MainWindow::onNoteDeleted(QUuid listUuid, QUuid noteUuid) +{ + QListWidgetItem *item = ui->lists->currentItem(); + if (item == nullptr) { + qDebug() << "item null"; + return; + } + + if (item->data(Qt::UserRole).toUuid() != listUuid) { + qDebug() << "item uuid not matching"; + return; + } + + for (int i = 0; i < ui->notes->count(); i++) { + item = ui->notes->item(i); + + if (item->data(Qt::UserRole).toUuid() == noteUuid) { + ui->notes->removeItemWidget(item); + delete item; + return; + } + } +} + void MainWindow::onListSelected(int i) { if (i == -1) { @@ -149,7 +175,7 @@ void MainWindow::onListSelected(int i) ui->notes->setEnabled(true); } -void MainWindow::onSaveNoteButtonClicked(bool _) +void MainWindow::onSaveNoteButtonClicked(bool) { QString content = ui->newNoteEdit->text(); if (content.isEmpty()) { @@ -167,6 +193,11 @@ void MainWindow::onSaveNoteButtonClicked(bool _) ui->newNoteEdit->clear(); } +void MainWindow::onNewNoteEditReturnPressed() +{ + onSaveNoteButtonClicked(false); +} + void MainWindow::onNoteChanged(QListWidgetItem *item) { NoteService *service = NoteService::getInstance(); @@ -180,8 +211,7 @@ void MainWindow::onNoteChanged(QListWidgetItem *item) if (content.isEmpty()) { service->remove(note.getParentUUID(), note.getUUID()); } else { - service->update(note.getParentUUID(), note.getUUID(), content); - service->setFinishedValue(note.getParentUUID(), note.getUUID(), item->checkState() == Qt::Checked ? true : false); + service->update(note.getParentUUID(), note.getUUID(), content, item->checkState() == Qt::Checked ? true : false); } } } @@ -205,9 +235,27 @@ void MainWindow::onListRightClick(const QPoint &pos) } } -void MainWindow::onListContextMenuDelete(bool _) +void MainWindow::onListContextMenuDelete(bool) { + if (ui->lists->selectedItems().length() == 1) { + QListWidgetItem *item = ui->lists->selectedItems()[0]; + QUuid uuid = item->data(Qt::UserRole).toUuid(); + QMessageBox box(this); + box.setWindowTitle("Delete"); + box.setText(QString("Do you want to delete '%1'?").arg(item->text())); + box.addButton(QMessageBox::Yes); + box.addButton(QMessageBox::Cancel); + + if (box.exec() == QMessageBox::Yes) { + NoteService *ns = NoteService::getInstance(); + QList toDelete = ns->getByList(uuid); + foreach (Note n, toDelete) { + ns->onNoteDeleted(uuid, n.getUUID()); + } + ListService::getInstance()->remove(uuid); + } + } } void MainWindow::onListContextMenuRename(bool) diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index 106d873..b9df1e1 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -28,8 +28,10 @@ private slots: void onListDeleted(QUuid uuid); void onNoteCreated(Note value); void onNoteUpdated(Note value); + void onNoteDeleted(QUuid listUuid, QUuid noteUuid); void onListSelected(int i); void onSaveNoteButtonClicked(bool); + void onNewNoteEditReturnPressed(); void onNoteChanged(QListWidgetItem*); void onListRightClick(const QPoint &pos); void onListContextMenuDelete(bool); diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index b7b8037..eb2288e 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -20,9 +20,12 @@ ToDo - background-color: rgb(249, 255, 251); + + + background-color: rgb(249, 255, 251); + 0 @@ -105,7 +108,7 @@ border-bottom: 2px solid rgb(242, 242, 242) - 15 + 12 Light @@ -124,6 +127,8 @@ border-bottom: 2px solid rgb(242, 242, 242) QListView::item { padding: 8px; border: none; + color: rgb(252, 252, 252); + font-size: 10pt; } QListView::item:selected { @@ -140,7 +145,8 @@ QListView::item:selected:active { QListView::item:hover { background-color: rgb(152, 193, 116); -} +} + QFrame::Shadow::Plain @@ -276,6 +282,23 @@ QListView::item:selected:!active { QListView::item:hover { background-color: rgb(204, 204, 204); +} + +QListWidget::indicator { + width: 18px; + height: 18px; + border: 1px solid rgb(119, 167, 92); + border-radius: 10px; + background: rgb(226, 226, 226); +} + +QListWidget::indicator:checked { + background: rgb(119, 167, 92); + border: 2px solid rgb(119, 167, 92); +} + +QListWidget::indicator:unchecked:hover { + border: 2px solid #aaa; }