diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-10-10 12:40:27 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-10-11 08:35:20 -0700 |
commit | 215e5a45447f53047bfcc167da46714bf1496e8f (patch) | |
tree | aec5877f482b5f53c9fb92372ff69228402c51dc /qt-models/divecomputermodel.cpp | |
parent | 5931be4c880bcefb406510b714f83fec30e9bc4d (diff) | |
download | subsurface-215e5a45447f53047bfcc167da46714bf1496e8f.tar.gz |
desktop: make divecomputer table sortable
Add a small proxy-model on top of DiveComputerModel so that clicking
on table headers makes the table sortable.
The UI feature here is not as important as the fact that the UI does
its own sorting and we can keep the device-table in the core sorted
differently.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models/divecomputermodel.cpp')
-rw-r--r-- | qt-models/divecomputermodel.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/qt-models/divecomputermodel.cpp b/qt-models/divecomputermodel.cpp index 7c562c35c..dcac0a0dc 100644 --- a/qt-models/divecomputermodel.cpp +++ b/qt-models/divecomputermodel.cpp @@ -79,3 +79,37 @@ void DiveComputerModel::keepWorkingList() mark_divelist_changed(true); device_table.devices = dcs; } + +// Convenience function to access alternative columns +static QString getData(const QModelIndex &idx, int col) +{ + const QAbstractItemModel *model = idx.model(); + QModelIndex idx2 = model->index(idx.row(), col, idx.parent()); + return model->data(idx2).toString(); +} + +// Helper function: sort data pointed to by the given indexes. +// For equal data, sort by two alternative rows. +// All sorting is by case-insensitive string comparison. +static bool sortHelper(const QModelIndex &i1, const QModelIndex &i2, int altRow1, int altRow2) +{ + if(int cmp = i1.data().toString().compare(i2.data().toString())) + return cmp < 0; + if(int cmp = getData(i1, altRow1).compare(getData(i2, altRow1))) + return cmp < 0; + return getData(i1, altRow2).compare(getData(i2, altRow2)) < 0; +} + +bool DiveComputerSortedModel::lessThan(const QModelIndex &i1, const QModelIndex &i2) const +{ + // We assume that i1.column() == i2.column() + switch (i1.column()) { + case DiveComputerModel::ID: + return sortHelper(i1, i2, DiveComputerModel::MODEL, DiveComputerModel::NICKNAME); + case DiveComputerModel::MODEL: + default: + return sortHelper(i1, i2, DiveComputerModel::ID, DiveComputerModel::NICKNAME); + case DiveComputerModel::NICKNAME: + return sortHelper(i1, i2, DiveComputerModel::MODEL, DiveComputerModel::ID); + } +} |