sorting, cleanup
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 &);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user