Skip to content

Commit

Permalink
Sort unused cylinders to the end of cylinder list
Browse files Browse the repository at this point in the history
Sort the unused cylinders to the end of the cylinder list when
option to display unused cylinders is set to off.

Signed-off-by: Stefan Fuchs <sfuchs@gmx.de>
  • Loading branch information
sfuchs79 committed Nov 30, 2017
1 parent 1208bc8 commit aaf1677
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
63 changes: 58 additions & 5 deletions qt-models/cylindermodel.cpp
Expand Up @@ -486,15 +486,28 @@ static bool show_cylinder(struct dive *dive, int i)

void CylindersModel::updateDive()
{
clear();
rows = 0;
int i, j;
int invisible_rows = 0;
int last_row = 0;
#ifdef DEBUG_CYL
dump_cylinders(&displayed_dive, true);
#endif
for (int i = 0; i < MAX_CYLINDERS; i++) {
if (show_cylinder(&displayed_dive, i))
rows = i + 1;
// Find last row
for (i = 0; i < MAX_CYLINDERS; i++) {
if (!cylinder_none(&displayed_dive.cylinder[i]))
last_row = i;
}
// Move invisible cylinders to the end of the list
for (i = j = 0; j <= last_row; i++, j++) {
if (!show_cylinder(&displayed_dive, i)) {
moveAtPos(i, last_row);
invisible_rows++;
if (i != last_row)
i--;
}
}
clear();
rows = last_row + 1 - invisible_rows;
if (rows > 0) {
beginInsertRows(QModelIndex(), 0, rows - 1);
endInsertRows();
Expand Down Expand Up @@ -602,6 +615,46 @@ void CylindersModel::moveAtFirst(int cylid)
endMoveRows();
}

void CylindersModel::moveAtPos(int cylid, int pos)
{
int mapping[MAX_CYLINDERS];
cylinder_t temp_cyl;

if (cylid == pos)
return;

beginMoveRows(QModelIndex(), cylid, cylid, QModelIndex(), pos + 1);
memmove(&temp_cyl, &displayed_dive.cylinder[cylid], sizeof(temp_cyl));
if (pos > cylid) {
for (int i = cylid + 1; i <= pos; i++) {
memmove(&displayed_dive.cylinder[i - 1], &displayed_dive.cylinder[i], sizeof(temp_cyl));
mapping[i] = i - 1;
}
memmove(&displayed_dive.cylinder[pos], &temp_cyl, sizeof(temp_cyl));
mapping[cylid] = pos;
for (int i = 0; i < cylid; i++)
mapping[i] = i;
for (int i = pos + 1; i < MAX_CYLINDERS; i++)
mapping[i] = i;
} else {
for (int i = cylid - 1; i >= pos; i--) {
memmove(&displayed_dive.cylinder[i + 1], &displayed_dive.cylinder[i], sizeof(temp_cyl));
mapping[i] = i + 1;
}
memmove(&displayed_dive.cylinder[pos], &temp_cyl, sizeof(temp_cyl));
mapping[cylid] = pos;
for (int i = 0; i < pos; i++)
mapping[i] = i;
for (int i = cylid + 1; i < MAX_CYLINDERS; i++)
mapping[i] = i;
}
cylinder_renumber(&displayed_dive, mapping);
if (in_planner())
DivePlannerPointsModel::instance()->cylinderRenumber(mapping);
changed = true;
endMoveRows();
}

void CylindersModel::updateDecoDepths(pressure_t olddecopo2)
{
pressure_t decopo2;
Expand Down
1 change: 1 addition & 0 deletions qt-models/cylindermodel.h
Expand Up @@ -41,6 +41,7 @@ class CylindersModel : public CleanerTableModel {
void updateDecoDepths(pressure_t olddecopo2);
void updateTrashIcon();
void moveAtFirst(int cylid);
void moveAtPos(int cylid, int pos);
cylinder_t *cylinderAt(const QModelIndex &index);
bool changed;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
Expand Down

0 comments on commit aaf1677

Please sign in to comment.