Add delete
This commit is contained in:
@@ -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> 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> 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)
|
||||
|
||||
@@ -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<Note> getByList(QUuid listUuid);
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <QDialogButtonBox>
|
||||
#include <QMenu>
|
||||
#include <QMessageBox>
|
||||
|
||||
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<Note> toDelete = ns->getByList(uuid);
|
||||
foreach (Note n, toDelete) {
|
||||
ns->onNoteDeleted(uuid, n.getUUID());
|
||||
}
|
||||
ListService::getInstance()->remove(uuid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::onListContextMenuRename(bool)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -20,9 +20,12 @@
|
||||
<string>ToDo</string>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
<string notr="true">background-color: rgb(249, 255, 251);</string>
|
||||
<string notr="true"/>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<property name="styleSheet">
|
||||
<string notr="true">background-color: rgb(249, 255, 251);</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
@@ -105,7 +108,7 @@ border-bottom: 2px solid rgb(242, 242, 242)</string>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>15</pointsize>
|
||||
<pointsize>12</pointsize>
|
||||
<fontweight>Light</fontweight>
|
||||
</font>
|
||||
</property>
|
||||
@@ -124,6 +127,8 @@ border-bottom: 2px solid rgb(242, 242, 242)</string>
|
||||
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);
|
||||
}</string>
|
||||
}
|
||||
</string>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Shadow::Plain</enum>
|
||||
@@ -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;
|
||||
}</string>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
|
||||
Reference in New Issue
Block a user