sorting, cleanup

This commit is contained in:
leca 2025-05-23 23:45:11 +03:00
parent ac355b7d9e
commit 20b08f493d
5 changed files with 60 additions and 23 deletions

View File

@ -211,14 +211,10 @@ void MainWindow::on_parse_button_clicked() {
}
void MainWindow::on_add_new_check_button_clicked() {
Check *new_check = new Check();/* parse_new_check();
Check *new_check = parse_new_check();
if (new_check == nullptr) {
return;
}*/
new_check->set_date("123");
new_check->set_total(rand() * 1800);
// checks.push_back(*new_check);
}
unsigned int newRowIndex = checks.size();
model->insertRows(newRowIndex, 1);
@ -303,15 +299,10 @@ void MainWindow::on_deleteSelectedButton_clicked() {
for (auto &row : select->selectedIndexes()) {
if (row.column() != 0) continue;
to_delete_positions.push_back(row.row());
// model->removeRows(row.row(), 1);
// checks.erase(std::next(checks.begin() + row.row()));
// std::cout << row.data().toString().toStdString() << std::endl;
}
std::sort(to_delete_positions.begin(), to_delete_positions.end(), std::greater<unsigned int>());
for (unsigned int position : to_delete_positions) {
model->removeRows(position, 1);
// checks.erase(checks.begin() + position);
// emit model->dataChanged(model->index(position, 0), model->index(position, 1));
}
emit model->dataChanged(model->index(checks.size(), 0), model->index(checks.size() + to_delete_positions.size(), 1));
ui->checkQueueTable->clearSelection();

View File

@ -298,32 +298,32 @@
<translation>No checks to parse</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="271"/>
<location filename="../mainwindow.cpp" line="267"/>
<source>Captcha was not solved correctly!</source>
<translation>Captcha was not solved correctly!</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="273"/>
<location filename="../mainwindow.cpp" line="269"/>
<source>Captcha is incorrect</source>
<translation>Captcha is incorrect</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="278"/>
<location filename="../mainwindow.cpp" line="274"/>
<source>Internal server error. Please, try again later.</source>
<translation>Internal server error. Please, try again later.</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="280"/>
<location filename="../mainwindow.cpp" line="276"/>
<source>Internal server error</source>
<translation>Internal server error</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="285"/>
<location filename="../mainwindow.cpp" line="281"/>
<source>Check not found. Please, ensure correctness of entered data.</source>
<translation>Check not found. Please, ensure correctness of entered data.</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="287"/>
<location filename="../mainwindow.cpp" line="283"/>
<source>Check was not found</source>
<translation>Check was not found</translation>
</message>

View File

@ -286,32 +286,32 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="271"/>
<location filename="../mainwindow.cpp" line="267"/>
<source>Captcha was not solved correctly!</source>
<translation>Капча была решена неверно!</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="273"/>
<location filename="../mainwindow.cpp" line="269"/>
<source>Captcha is incorrect</source>
<translation>Капча введена неверно</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="278"/>
<location filename="../mainwindow.cpp" line="274"/>
<source>Internal server error. Please, try again later.</source>
<translation>Внутренняя ошибка сервера. Пожалуйста, попробуйте снова позже.</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="280"/>
<location filename="../mainwindow.cpp" line="276"/>
<source>Internal server error</source>
<translation>Внутренняя ошибка сервера</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="285"/>
<location filename="../mainwindow.cpp" line="281"/>
<source>Check not found. Please, ensure correctness of entered data.</source>
<translation>Чек не найден. Пожалуйста, убедитесь в правильности введённых данных.</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="287"/>
<location filename="../mainwindow.cpp" line="283"/>
<source>Check was not found</source>
<translation>Чек не найден</translation>
</message>

View File

@ -2,6 +2,7 @@
#include <QMimeData>
#include <QIODevice>
#include <QDateTime>
CheckQueueTableModel::CheckQueueTableModel(std::vector<Check> *checks, QObject *parent)
: checks(checks), QAbstractTableModel{parent}
@ -160,3 +161,45 @@ bool CheckQueueTableModel::dropMimeData(const QMimeData *data, Qt::DropAction ac
return true;
}
void CheckQueueTableModel::sort(int column, Qt::SortOrder order) {
beginResetModel();
switch (column) {
case 0:
std::sort(checks->begin(), checks->end(),
[&](const Check& a, const Check& b) {
if (order == Qt::AscendingOrder) {
return compare(a, b, column);
} else {
return !compare(a, b, column);
}
});
break;
case 1:
std::sort(checks->begin(), checks->end(),
[&](const Check& a, const Check& b) {
if (order == Qt::AscendingOrder) {
return compare(a, b, column);
} else {
return !compare(a, b, column);
}
});
}
endResetModel();
}
bool CheckQueueTableModel::compare(const Check &check_a, const Check &check_b, int column) {
switch (column) {
case 0:
return
QDateTime::fromString(QString::fromStdString(((Check &)check_a).get_date()), "yyyyMMddThhmm")
>
QDateTime::fromString(QString::fromStdString(((Check &)check_b).get_date()), "yyyyMMddThhmm");
break;
case 1:
return ((Check &)check_a).get_total() > ((Check &)check_b).get_total();
break;
default: return false;
}
}

View File

@ -29,8 +29,11 @@ public:
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &);
QMimeData* mimeData(const QModelIndexList &indexes) const override;
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
void sort(int column, Qt::SortOrder order) override;
private:
std::vector<Check> *checks;
bool compare(const Check& check_a, const Check& check_b, int column);
signals:
void editCompleted(const QString &);
};