order when checked

This commit is contained in:
2026-03-25 21:29:54 +01:00
parent d75d484075
commit 4331512587
6 changed files with 82 additions and 14 deletions

View File

@@ -157,7 +157,7 @@ QList<List> ListService::getAll()
QSqlDatabase db = getOpenDatabase(); QSqlDatabase db = getOpenDatabase();
QSqlQuery query(db); 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()); throw makeSqlException("Failed to read lists", query.lastError().text());
} }

View File

@@ -175,7 +175,7 @@ void NoteService::remove(QUuid listUuid, QUuid noteUuid)
emit onNoteDeleted(listUuid, noteUuid); emit onNoteDeleted(listUuid, noteUuid);
} }
QList<Note> NoteService::getByList(QUuid listUuid) std::list<Note> NoteService::getByList(QUuid listUuid)
{ {
QSqlDatabase db = getOpenDatabase(); QSqlDatabase db = getOpenDatabase();
@@ -187,12 +187,12 @@ QList<Note> NoteService::getByList(QUuid listUuid)
throw makeSqlException("Failed to read notes by list", query.lastError().text()); throw makeSqlException("Failed to read notes by list", query.lastError().text());
} }
QList<Note> notes; std::list<Note> notes;
while (query.next()) { while (query.next()) {
const QUuid noteId(query.value(0).toString()); const QUuid noteId(query.value(0).toString());
const QString content = query.value(1).toString(); const QString content = query.value(1).toString();
const bool finished = query.value(2).toBool(); const bool finished = query.value(2).toBool();
notes.append(Note(listUuid, noteId, content, finished)); notes.push_back(Note(listUuid, noteId, content, finished));
} }
return notes; return notes;

View File

@@ -6,6 +6,7 @@
#include <QString> #include <QString>
#include <QUuid> #include <QUuid>
#include <optional> #include <optional>
#include <list>
#include "src/obj/note.h" #include "src/obj/note.h"
@@ -24,7 +25,7 @@ public:
QString update(QUuid listUuid, QUuid noteUuid, QString newValue, bool isFinished); QString update(QUuid listUuid, QUuid noteUuid, QString newValue, bool isFinished);
void remove(QUuid listUuid, QUuid noteUuid); void remove(QUuid listUuid, QUuid noteUuid);
QList<Note> getByList(QUuid listUuid); std::list<Note> getByList(QUuid listUuid);
std::optional<Note> getByUUID(QUuid noteUuid); std::optional<Note> getByUUID(QUuid noteUuid);
signals: signals:

View File

@@ -8,6 +8,7 @@
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QMenu> #include <QMenu>
#include <QMessageBox> #include <QMessageBox>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
@@ -102,7 +103,13 @@ void MainWindow::onNoteCreated(Note value)
item->setText(value.getContent()); item->setText(value.getContent());
item->setData(Qt::UserRole, QVariant(value.getUUID())); item->setData(Qt::UserRole, QVariant(value.getUUID()));
if (noteSeparation == -1) {
this->ui->notes->addItem(item); this->ui->notes->addItem(item);
} else {
this->ui->notes->insertItem(noteSeparation, item);
}
updateNoteSeparation();
} }
void MainWindow::onNoteUpdated(Note value) void MainWindow::onNoteUpdated(Note value)
@@ -120,23 +127,40 @@ void MainWindow::onNoteUpdated(Note value)
item = ui->notes->item(i); item = ui->notes->item(i);
if (item->data(Qt::UserRole).toUuid() == value.getUUID()) { 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()); 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; return;
} }
} }
updateNoteSeparation();
} }
void MainWindow::onNoteDeleted(QUuid listUuid, QUuid noteUuid) void MainWindow::onNoteDeleted(QUuid listUuid, QUuid noteUuid)
{ {
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() != listUuid) { if (item->data(Qt::UserRole).toUuid() != listUuid) {
qDebug() << "item uuid not matching";
return; return;
} }
@@ -146,9 +170,11 @@ void MainWindow::onNoteDeleted(QUuid listUuid, QUuid noteUuid)
if (item->data(Qt::UserRole).toUuid() == noteUuid) { if (item->data(Qt::UserRole).toUuid() == noteUuid) {
ui->notes->removeItemWidget(item); ui->notes->removeItemWidget(item);
delete item; delete item;
updateNoteSeparation();
return; return;
} }
} }
updateNoteSeparation();
} }
void MainWindow::onListSelected(int i) void MainWindow::onListSelected(int i)
@@ -158,18 +184,23 @@ void MainWindow::onListSelected(int i)
ui->newNoteEdit->setDisabled(true); ui->newNoteEdit->setDisabled(true);
ui->saveNoteButton->setDisabled(true); ui->saveNoteButton->setDisabled(true);
ui->notes->setDisabled(true); ui->notes->setDisabled(true);
noteSeparation = -1;
return; return;
} }
QListWidgetItem *item = ui->lists->item(i); QListWidgetItem *item = ui->lists->item(i);
QVariant uuid = item->data(Qt::UserRole); QVariant uuid = item->data(Qt::UserRole);
QList<Note> notes = NoteService::getInstance()->getByList(uuid.toUuid()); std::list<Note> notes = NoteService::getInstance()->getByList(uuid.toUuid());
notes.sort([](Note &a, Note &b){
return a.isFinished() < b.isFinished();
});
ui->notes->clear(); ui->notes->clear();
foreach (Note n, notes) { for (Note &n : notes) {
onNoteCreated(n); onNoteCreated(n);
} }
updateNoteSeparation();
ui->newNoteEdit->setEnabled(true); ui->newNoteEdit->setEnabled(true);
ui->saveNoteButton->setEnabled(true); ui->saveNoteButton->setEnabled(true);
ui->notes->setEnabled(true); ui->notes->setEnabled(true);
@@ -249,8 +280,8 @@ void MainWindow::onListContextMenuDelete(bool)
if (box.exec() == QMessageBox::Yes) { if (box.exec() == QMessageBox::Yes) {
NoteService *ns = NoteService::getInstance(); NoteService *ns = NoteService::getInstance();
QList<Note> toDelete = ns->getByList(uuid); std::list<Note> toDelete = ns->getByList(uuid);
foreach (Note n, toDelete) { for (Note n : toDelete) {
ns->onNoteDeleted(uuid, n.getUUID()); ns->onNoteDeleted(uuid, n.getUUID());
} }
ListService::getInstance()->remove(uuid); ListService::getInstance()->remove(uuid);
@@ -276,7 +307,7 @@ void MainWindow::onListContextMenuRename(bool)
} }
} }
void MainWindow::preload() inline void MainWindow::preload()
{ {
QList<List> lists = ListService::getInstance()->getAll(); QList<List> lists = ListService::getInstance()->getAll();
@@ -288,3 +319,26 @@ void MainWindow::preload()
ui->lists->setCurrentItem(ui->lists->item(0)); 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;
});
}

View File

@@ -40,7 +40,10 @@ private slots:
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
int noteSeparation = -1;
void preload(); void preload();
void updateNoteSeparation();
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@@ -204,6 +204,16 @@ QListView::item:hover {
<property name="bottomMargin"> <property name="bottomMargin">
<number>0</number> <number>0</number>
</property> </property>
<item>
<widget class="QLabel" name="version">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>0.0.1</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="horizontalSpacer"> <spacer name="horizontalSpacer">
<property name="orientation"> <property name="orientation">