diff --git a/src/core/listservice.cpp b/src/core/listservice.cpp index bf7f78a..367da52 100644 --- a/src/core/listservice.cpp +++ b/src/core/listservice.cpp @@ -157,7 +157,7 @@ QList ListService::getAll() QSqlDatabase db = getOpenDatabase(); QSqlQuery query(db); - if (!query.exec("SELECT id, name FROM lists ORDER BY name COLLATE NOCASE ASC;")) { + if (!query.exec("SELECT id, name FROM lists ORDER BY id;")) { throw makeSqlException("Failed to read lists", query.lastError().text()); } diff --git a/src/core/noteservice.cpp b/src/core/noteservice.cpp index 1e785d6..d92be30 100644 --- a/src/core/noteservice.cpp +++ b/src/core/noteservice.cpp @@ -175,7 +175,7 @@ void NoteService::remove(QUuid listUuid, QUuid noteUuid) emit onNoteDeleted(listUuid, noteUuid); } -QList NoteService::getByList(QUuid listUuid) +std::list NoteService::getByList(QUuid listUuid) { QSqlDatabase db = getOpenDatabase(); @@ -187,12 +187,12 @@ QList NoteService::getByList(QUuid listUuid) throw makeSqlException("Failed to read notes by list", query.lastError().text()); } - QList notes; + std::list notes; while (query.next()) { const QUuid noteId(query.value(0).toString()); const QString content = query.value(1).toString(); const bool finished = query.value(2).toBool(); - notes.append(Note(listUuid, noteId, content, finished)); + notes.push_back(Note(listUuid, noteId, content, finished)); } return notes; diff --git a/src/core/noteservice.h b/src/core/noteservice.h index 62856cb..1e45deb 100644 --- a/src/core/noteservice.h +++ b/src/core/noteservice.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "src/obj/note.h" @@ -24,7 +25,7 @@ public: QString update(QUuid listUuid, QUuid noteUuid, QString newValue, bool isFinished); void remove(QUuid listUuid, QUuid noteUuid); - QList getByList(QUuid listUuid); + std::list getByList(QUuid listUuid); std::optional getByUUID(QUuid noteUuid); signals: diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 126e39f..a3f77b4 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -8,6 +8,7 @@ #include #include #include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -102,7 +103,13 @@ void MainWindow::onNoteCreated(Note value) item->setText(value.getContent()); item->setData(Qt::UserRole, QVariant(value.getUUID())); - this->ui->notes->addItem(item); + if (noteSeparation == -1) { + this->ui->notes->addItem(item); + } else { + this->ui->notes->insertItem(noteSeparation, item); + } + + updateNoteSeparation(); } void MainWindow::onNoteUpdated(Note value) @@ -120,23 +127,40 @@ void MainWindow::onNoteUpdated(Note value) item = ui->notes->item(i); if (item->data(Qt::UserRole).toUuid() == value.getUUID()) { - item->setCheckState((value.isFinished() ? Qt::Checked : Qt::Unchecked)); + item->setCheckState(value.isFinished() ? Qt::Checked : Qt::Unchecked); item->setText(value.getContent()); + + if (ui->notes->count() == 1) { + return; + } + + if (noteSeparation == -1) { + if (item->checkState() == Qt::Checked) { + QListWidgetItem* tmp = ui->notes->takeItem(i); + ui->notes->addItem(tmp); + } + } + + if ((item->checkState() == Qt::Checked && i < noteSeparation) || (item->checkState() == Qt::Unchecked && i > noteSeparation)) { + QListWidgetItem* tmp = ui->notes->takeItem(i); + ui->notes->insertItem(noteSeparation-1, tmp); + } + + updateNoteSeparation(); return; } } + updateNoteSeparation(); } 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; } @@ -146,9 +170,11 @@ void MainWindow::onNoteDeleted(QUuid listUuid, QUuid noteUuid) if (item->data(Qt::UserRole).toUuid() == noteUuid) { ui->notes->removeItemWidget(item); delete item; + updateNoteSeparation(); return; } } + updateNoteSeparation(); } void MainWindow::onListSelected(int i) @@ -158,18 +184,23 @@ void MainWindow::onListSelected(int i) ui->newNoteEdit->setDisabled(true); ui->saveNoteButton->setDisabled(true); ui->notes->setDisabled(true); + noteSeparation = -1; return; } QListWidgetItem *item = ui->lists->item(i); QVariant uuid = item->data(Qt::UserRole); - QList notes = NoteService::getInstance()->getByList(uuid.toUuid()); + std::list notes = NoteService::getInstance()->getByList(uuid.toUuid()); + notes.sort([](Note &a, Note &b){ + return a.isFinished() < b.isFinished(); + }); ui->notes->clear(); - foreach (Note n, notes) { + for (Note &n : notes) { onNoteCreated(n); } + updateNoteSeparation(); ui->newNoteEdit->setEnabled(true); ui->saveNoteButton->setEnabled(true); ui->notes->setEnabled(true); @@ -249,8 +280,8 @@ void MainWindow::onListContextMenuDelete(bool) if (box.exec() == QMessageBox::Yes) { NoteService *ns = NoteService::getInstance(); - QList toDelete = ns->getByList(uuid); - foreach (Note n, toDelete) { + std::list toDelete = ns->getByList(uuid); + for (Note n : toDelete) { ns->onNoteDeleted(uuid, n.getUUID()); } ListService::getInstance()->remove(uuid); @@ -276,7 +307,7 @@ void MainWindow::onListContextMenuRename(bool) } } -void MainWindow::preload() +inline void MainWindow::preload() { QList lists = ListService::getInstance()->getAll(); @@ -288,3 +319,26 @@ void MainWindow::preload() ui->lists->setCurrentItem(ui->lists->item(0)); } } + +inline void MainWindow::updateNoteSeparation() +{ + QTimer::singleShot(0, [this](){ + if (ui->notes->count() <= 1) { + noteSeparation = -1; + } + + if (ui->notes->count() == 2) { + noteSeparation = 1; + } + + int insertHere = -1; + for (int i = 0; i < ui->notes->count(); i++) { + if (ui->notes->item(i)->checkState() == Qt::Checked) { + insertHere = i; + break; + } + } + + this->noteSeparation = insertHere; + }); +} diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index b9df1e1..64655ab 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -40,7 +40,10 @@ private slots: private: Ui::MainWindow *ui; + int noteSeparation = -1; + void preload(); + void updateNoteSeparation(); }; #endif // MAINWINDOW_H diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index eb2288e..e6a0568 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -204,6 +204,16 @@ QListView::item:hover { 0 + + + + false + + + 0.0.1 + + +