Add delete

This commit is contained in:
2026-03-22 21:43:22 +01:00
parent 298b2c9078
commit 9feee144e3
5 changed files with 88 additions and 47 deletions

View File

@@ -136,42 +136,15 @@ QUuid NoteService::create(QUuid listUuid, QString value)
return newNoteUuid; 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(); QSqlDatabase db = getOpenDatabase();
QSqlQuery query(db); 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(":id", toDbUuid(noteUuid));
query.bindValue(":list_id", toDbUuid(listUuid)); query.bindValue(":list_id", toDbUuid(listUuid));
query.bindValue(":content", newValue); 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> 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); query.bindValue(":finished", isFinished);
if (!query.exec()) { if (!query.exec()) {
@@ -181,13 +154,9 @@ void NoteService::setFinishedValue(QUuid listUuid, QUuid noteUuid, bool isFinish
throw std::runtime_error("Note not found"); throw std::runtime_error("Note not found");
} }
std::optional<Note> note = getByUUID(noteUuid); Note n = Note(listUuid, noteUuid, newValue, isFinished);
if (!note.has_value()) {
throw std::runtime_error("database integrity corrupted");
}
Note n = Note(listUuid, noteUuid, note.value().getContent(), isFinished);
emit onNoteUpdated(n); emit onNoteUpdated(n);
return newValue;
} }
void NoteService::remove(QUuid listUuid, QUuid noteUuid) void NoteService::remove(QUuid listUuid, QUuid noteUuid)

View File

@@ -21,8 +21,7 @@ public:
static NoteService* getInstance(); static NoteService* getInstance();
QUuid create(QUuid listUuid, QString value); QUuid create(QUuid listUuid, QString value);
QString update(QUuid listUuid, QUuid noteUuid, QString newValue); QString update(QUuid listUuid, QUuid noteUuid, QString newValue, bool isFinished);
void setFinishedValue(QUuid listUuid, QUuid noteUuid, bool isFinished);
void remove(QUuid listUuid, QUuid noteUuid); void remove(QUuid listUuid, QUuid noteUuid);
QList<Note> getByList(QUuid listUuid); QList<Note> getByList(QUuid listUuid);

View File

@@ -7,6 +7,7 @@
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QMenu> #include <QMenu>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
@@ -22,6 +23,7 @@ MainWindow::MainWindow(QWidget *parent)
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->lists, &QWidget::customContextMenuRequested, this, &MainWindow::onListRightClick);
connect(ui->newNoteEdit, &QLineEdit::returnPressed, this, &MainWindow::onNewNoteEditReturnPressed);
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);
@@ -30,6 +32,8 @@ MainWindow::MainWindow(QWidget *parent)
connect(ListService::getInstance(), &ListService::onListUpdated, this, &MainWindow::onListUpdate); connect(ListService::getInstance(), &ListService::onListUpdated, this, &MainWindow::onListUpdate);
connect(ListService::getInstance(), &ListService::onListDeleted, this, &MainWindow::onListDeleted); connect(ListService::getInstance(), &ListService::onListDeleted, this, &MainWindow::onListDeleted);
connect(NoteService::getInstance(), &NoteService::onNoteCreated, this, &MainWindow::onNoteCreated); 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(); preload();
} }
@@ -39,7 +43,7 @@ MainWindow::~MainWindow()
delete ui; delete ui;
} }
void MainWindow::openCreateListDialog(bool _) void MainWindow::openCreateListDialog(bool)
{ {
// create the input dialog // create the input dialog
InputDialog d = InputDialog(this, "Create a list", "New List", "Give a name to this list"); 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(); QListWidgetItem *item = ui->lists->currentItem();
if (item == nullptr) { if (item == nullptr) {
qDebug() << "item null";
return; return;
} }
if (item->data(Qt::UserRole).toUuid() != value.getParentUUID()) { if (item->data(Qt::UserRole).toUuid() != value.getParentUUID()) {
qDebug() << "item uuid not matching";
return; 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) void MainWindow::onListSelected(int i)
{ {
if (i == -1) { if (i == -1) {
@@ -149,7 +175,7 @@ void MainWindow::onListSelected(int i)
ui->notes->setEnabled(true); ui->notes->setEnabled(true);
} }
void MainWindow::onSaveNoteButtonClicked(bool _) void MainWindow::onSaveNoteButtonClicked(bool)
{ {
QString content = ui->newNoteEdit->text(); QString content = ui->newNoteEdit->text();
if (content.isEmpty()) { if (content.isEmpty()) {
@@ -167,6 +193,11 @@ void MainWindow::onSaveNoteButtonClicked(bool _)
ui->newNoteEdit->clear(); ui->newNoteEdit->clear();
} }
void MainWindow::onNewNoteEditReturnPressed()
{
onSaveNoteButtonClicked(false);
}
void MainWindow::onNoteChanged(QListWidgetItem *item) void MainWindow::onNoteChanged(QListWidgetItem *item)
{ {
NoteService *service = NoteService::getInstance(); NoteService *service = NoteService::getInstance();
@@ -180,8 +211,7 @@ void MainWindow::onNoteChanged(QListWidgetItem *item)
if (content.isEmpty()) { if (content.isEmpty()) {
service->remove(note.getParentUUID(), note.getUUID()); service->remove(note.getParentUUID(), note.getUUID());
} else { } else {
service->update(note.getParentUUID(), note.getUUID(), content); service->update(note.getParentUUID(), note.getUUID(), content, item->checkState() == Qt::Checked ? true : false);
service->setFinishedValue(note.getParentUUID(), note.getUUID(), 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<Note> toDelete = ns->getByList(uuid);
foreach (Note n, toDelete) {
ns->onNoteDeleted(uuid, n.getUUID());
}
ListService::getInstance()->remove(uuid);
}
}
} }
void MainWindow::onListContextMenuRename(bool) void MainWindow::onListContextMenuRename(bool)

View File

@@ -28,8 +28,10 @@ private slots:
void onListDeleted(QUuid uuid); void onListDeleted(QUuid uuid);
void onNoteCreated(Note value); void onNoteCreated(Note value);
void onNoteUpdated(Note value); void onNoteUpdated(Note value);
void onNoteDeleted(QUuid listUuid, QUuid noteUuid);
void onListSelected(int i); void onListSelected(int i);
void onSaveNoteButtonClicked(bool); void onSaveNoteButtonClicked(bool);
void onNewNoteEditReturnPressed();
void onNoteChanged(QListWidgetItem*); void onNoteChanged(QListWidgetItem*);
void onListRightClick(const QPoint &pos); void onListRightClick(const QPoint &pos);
void onListContextMenuDelete(bool); void onListContextMenuDelete(bool);

View File

@@ -20,9 +20,12 @@
<string>ToDo</string> <string>ToDo</string>
</property> </property>
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">background-color: rgb(249, 255, 251);</string> <string notr="true"/>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<property name="styleSheet">
<string notr="true">background-color: rgb(249, 255, 251);</string>
</property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<property name="leftMargin"> <property name="leftMargin">
<number>0</number> <number>0</number>
@@ -105,7 +108,7 @@ border-bottom: 2px solid rgb(242, 242, 242)</string>
</property> </property>
<property name="font"> <property name="font">
<font> <font>
<pointsize>15</pointsize> <pointsize>12</pointsize>
<fontweight>Light</fontweight> <fontweight>Light</fontweight>
</font> </font>
</property> </property>
@@ -124,6 +127,8 @@ border-bottom: 2px solid rgb(242, 242, 242)</string>
QListView::item { QListView::item {
padding: 8px; padding: 8px;
border: none; border: none;
color: rgb(252, 252, 252);
font-size: 10pt;
} }
QListView::item:selected { QListView::item:selected {
@@ -140,7 +145,8 @@ QListView::item:selected:active {
QListView::item:hover { QListView::item:hover {
background-color: rgb(152, 193, 116); background-color: rgb(152, 193, 116);
}</string> }
</string>
</property> </property>
<property name="frameShadow"> <property name="frameShadow">
<enum>QFrame::Shadow::Plain</enum> <enum>QFrame::Shadow::Plain</enum>
@@ -276,6 +282,23 @@ QListView::item:selected:!active {
QListView::item:hover { QListView::item:hover {
background-color: rgb(204, 204, 204); 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;
}</string> }</string>
</property> </property>
<property name="frameShadow"> <property name="frameShadow">