Cylinders: Add CylindersModelFiltered

When the show_unused_cylinders flag is not set, the cylinder tables
in the equipment tab and the planner should not show unused cylinders.
However, the code in CylindersModel is fundamentally broken if the
unused cylinders are not at the end of the list: The correct number
of cylinders is shown, but not the correct cylinders.

Therefore, add a higher-level CylindersModelFiltered model on top
of CylindersModel that does the actual filtering. Some calls are
routed through to the base model (notably those that take indexes,
as these have to be mapped), for some calls the caller has to get
access to the source model first. We might want to adjust this.

For filtering, reuse the already existing show_cylinder function
and export it via CylindersModel.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-01-30 20:12:11 +01:00 committed by Dirk Hohndel
parent b37c261c95
commit 6622f42aab
9 changed files with 140 additions and 78 deletions

View file

@ -14,9 +14,9 @@ CylindersModel::CylindersModel(QObject *parent) :
changed(false),
rows(0)
{
// enum {REMOVE, TYPE, SIZE, WORKINGPRESS, START, END, O2, HE, DEPTH, MOD, MND, USE};
// enum {REMOVE, TYPE, SIZE, WORKINGPRESS, START, END, O2, HE, DEPTH, MOD, MND, USE, IS_USED};
setHeaderDataStrings(QStringList() << "" << tr("Type") << tr("Size") << tr("Work press.") << tr("Start press.") << tr("End press.") << tr("O₂%") << tr("He%")
<< tr("Deco switch at") <<tr("Bot. MOD") <<tr("MND") << tr("Use"));
<< tr("Deco switch at") <<tr("Bot. MOD") <<tr("MND") << tr("Use") << "Is used");
connect(&diveListNotifier, &DiveListNotifier::cylindersReset, this, &CylindersModel::cylindersReset);
}
@ -29,12 +29,6 @@ QVariant CylindersModel::headerData(int section, Qt::Orientation orientation, in
return CleanerTableModel::headerData(section, orientation, role);
}
CylindersModel *CylindersModel::instance()
{
static CylindersModel self;
return &self;
}
static QString get_cylinder_string(const cylinder_t *cyl)
{
QString unit;
@ -129,6 +123,28 @@ static QVariant percent_string(fraction_t fraction)
return QString("%L1%").arg(permille / 10.0, 0, 'f', 1);
}
bool CylindersModel::cylinderUsed(int i) const
{
const struct dive *dive = &displayed_dive;
if (i < 0 || i >= dive->cylinders.nr)
return false;
if (is_cylinder_used(dive, i))
return true;
cylinder_t *cyl = get_cylinder(dive, i);
if (cyl->start.mbar || cyl->sample_start.mbar ||
cyl->end.mbar || cyl->sample_end.mbar)
return true;
if (cyl->manually_added)
return true;
/*
* The cylinder has some data, but none of it is very interesting,
* it has no pressures and no gas switches. Do we want to show it?
*/
return false;
}
QVariant CylindersModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= rows)
@ -447,40 +463,13 @@ void CylindersModel::clear()
}
}
static bool show_cylinder(struct dive *dive, int i)
{
if (i < 0 || i >= dive->cylinders.nr)
return false;
if (is_cylinder_used(dive, i))
return true;
cylinder_t *cyl = get_cylinder(dive, i);
if (cyl->start.mbar || cyl->sample_start.mbar ||
cyl->end.mbar || cyl->sample_end.mbar)
return true;
if (cyl->manually_added)
return true;
/*
* The cylinder has some data, but none of it is very interesting,
* it has no pressures and no gas switches. Do we want to show it?
*/
return prefs.display_unused_tanks;
}
void CylindersModel::updateDive()
{
#ifdef DEBUG_CYL
dump_cylinders(&displayed_dive, true);
#endif
beginResetModel();
// TODO: this is fundamentally broken - it assumes that unused cylinders are at
// the end. Fix by using a QSortFilterProxyModel.
rows = 0;
for (int i = 0; i < displayed_dive.cylinders.nr; ++i) {
if (show_cylinder(&displayed_dive, i))
++rows;
}
rows = displayed_dive.cylinders.nr;
endResetModel();
}
@ -493,7 +482,6 @@ Qt::ItemFlags CylindersModel::flags(const QModelIndex &index) const
void CylindersModel::remove(QModelIndex index)
{
if (index.column() == USE) {
cylinder_t *cyl = cylinderAt(index);
if (cyl->cylinder_use == OC_GAS)
@ -622,3 +610,54 @@ void CylindersModel::cylindersReset(const QVector<dive *> &dives)
// And update the model..
updateDive();
}
CylindersModelFiltered *CylindersModelFiltered::instance()
{
static CylindersModelFiltered self;
return &self;
}
CylindersModelFiltered::CylindersModelFiltered(QObject *parent) : QSortFilterProxyModel(parent)
{
setSourceModel(&source);
}
void CylindersModelFiltered::updateDive()
{
source.updateDive();
}
void CylindersModelFiltered::clear()
{
source.clear();
}
void CylindersModelFiltered::add()
{
source.add();
}
CylindersModel *CylindersModelFiltered::model()
{
return &source;
}
void CylindersModelFiltered::remove(QModelIndex index)
{
source.remove(mapToSource(index));
}
void CylindersModelFiltered::passInData(const QModelIndex &index, const QVariant &value)
{
source.passInData(mapToSource(index), value);
}
cylinder_t *CylindersModelFiltered::cylinderAt(const QModelIndex &index)
{
return source.cylinderAt(mapToSource(index));
}
bool CylindersModelFiltered::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
return prefs.display_unused_tanks || source.cylinderUsed(source_row);
}