diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-02-27 19:46:48 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-02-28 08:12:26 -0800 |
commit | cb80ff746b687a3ad29b53d9f633cbdc6b142c71 (patch) | |
tree | 50a22d892fffd1175dbba94b49d1b712a604039b /qt-models | |
parent | 9214bdb3c576c0e26745939a18ea75634aef8ce4 (diff) | |
download | subsurface-cb80ff746b687a3ad29b53d9f633cbdc6b142c71.tar.gz |
crash fix: Don't cast to CylindersModel or CylindersModelFiltered
The tank-info-delegate cast its model to CylindersModelFiltered,
since this is what the equipment-tab uses since implementing the
filtering of unused cylinders. However, the planner users the same
delegate and still uses the unfiltered CylindersModel. This means
that the (dynamic) cast returns a null pointer and crashes.
One possibility would be to derive CylindersModelFiltered and
CylindersModel from the same class that defines virtual functions
and cast to that class.
This is a different attempt: don't cast (i.e. stay with a
QAbstractItemModel and play it via Qt's model-view system. Firstly,
replace the passInData function by a role to setData(). Secondly,
read the working-pressure and size via new columns using data().
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models')
-rw-r--r-- | qt-models/cylindermodel.cpp | 55 | ||||
-rw-r--r-- | qt-models/cylindermodel.h | 7 |
2 files changed, 33 insertions, 29 deletions
diff --git a/qt-models/cylindermodel.cpp b/qt-models/cylindermodel.cpp index 4f5a4ab77..4c56d9914 100644 --- a/qt-models/cylindermodel.cpp +++ b/qt-models/cylindermodel.cpp @@ -236,6 +236,10 @@ QVariant CylindersModel::data(const QModelIndex &index, int role) const break; case USE: return gettextFromC::tr(cylinderuse_text[cyl->cylinder_use]); + case WORKINGPRESS_INT: + return static_cast<int>(cyl->type.workingpressure.mbar); + case SIZE_INT: + return static_cast<int>(cyl->type.size.mliter); } break; case Qt::DecorationRole: @@ -283,33 +287,35 @@ cylinder_t *CylindersModel::cylinderAt(const QModelIndex &index) return get_cylinder(&displayed_dive, index.row()); } -// this is our magic 'pass data in' function that allows the delegate to get -// the data here without silly unit conversions; -// so we only implement the two columns we care about -void CylindersModel::passInData(const QModelIndex &index, const QVariant &value) -{ - cylinder_t *cyl = cylinderAt(index); - switch (index.column()) { - case SIZE: - if (cyl->type.size.mliter != value.toInt()) { - cyl->type.size.mliter = value.toInt(); - dataChanged(index, index); - } - break; - case WORKINGPRESS: - if (cyl->type.workingpressure.mbar != value.toInt()) { - cyl->type.workingpressure.mbar = value.toInt(); - dataChanged(index, index); - } - break; - } -} - bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, int role) { QString vString; cylinder_t *cyl = cylinderAt(index); + if (!cyl) + return false; + + if (role == PASS_IN_ROLE) { + // this is our magic 'pass data in' function that allows the delegate to get + // the data here without silly unit conversions; + // so we only implement the two columns we care about + switch (index.column()) { + case SIZE: + if (cyl->type.size.mliter != value.toInt()) { + cyl->type.size.mliter = value.toInt(); + dataChanged(index, index); + } + return true; + case WORKINGPRESS: + if (cyl->type.workingpressure.mbar != value.toInt()) { + cyl->type.workingpressure.mbar = value.toInt(); + dataChanged(index, index); + } + return true; + } + return false; + } + switch (index.column()) { case TYPE: if (!value.isNull()) { @@ -641,11 +647,6 @@ 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)); diff --git a/qt-models/cylindermodel.h b/qt-models/cylindermodel.h index 9534af8ad..41de5fae5 100644 --- a/qt-models/cylindermodel.h +++ b/qt-models/cylindermodel.h @@ -25,16 +25,20 @@ public: MOD, MND, USE, + WORKINGPRESS_INT, + SIZE_INT, COLUMNS }; + enum Roles { + PASS_IN_ROLE = Qt::UserRole + 1 // For setting data: don't do any conversions + }; explicit CylindersModel(QObject *parent = 0); QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; Qt::ItemFlags flags(const QModelIndex &index) const override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - void passInData(const QModelIndex &index, const QVariant &value); void add(); void clear(); void updateDive(); @@ -67,7 +71,6 @@ public: void add(); void updateDive(); cylinder_t *cylinderAt(const QModelIndex &index); - void passInData(const QModelIndex &index, const QVariant &value); public slots: void remove(QModelIndex index); |