Skip to content

Commit f4f4ca2

Browse files
committed
Pass model reference to Item/Row
Give Item/NameItem/Row access to their parent ScalarSourceModel by adding a const ScalarSourceModel& parameter and storing it in Item::_scalarSourceModel. Update construction sites (appendRow/Row) to pass *this. Use the model reference in Item::data() to respect getShowFullPathName() and use getGuiName() for display. Add getScalarSourceModel() accessor. Also change getDatasets() to return by value and remove some dead/commented code. These changes allow items to query model state reliably and simplify row construction.
1 parent e4e5170 commit f4f4ca2

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

src/ScalarSourceModel.cpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ ScalarSourceModel::ScalarSourceModel(QObject* parent /*= nullptr*/) :
1111
QStandardItemModel(parent),
1212
_showFullPathName(true)
1313
{
14-
appendRow(Row({})); // Constant source
15-
appendRow(Row({})); // Selection source
14+
appendRow(Row(*this, {})); // Constant source
15+
appendRow(Row(*this, {})); // Selection source
1616
}
1717

18-
ScalarSourceModel::Item::Item(const mv::Dataset<>& scalarDataset) :
18+
ScalarSourceModel::Item::Item(const ScalarSourceModel& scalarSourceModel, const mv::Dataset<>& scalarDataset) :
19+
_scalarSourceModel(scalarSourceModel),
1920
_scalarDataset(scalarDataset)
2021
{
2122
}
@@ -48,8 +49,8 @@ QVariant ScalarSourceModel::Item::data(int role) const
4849
{
4950
if (rowIndex == 2)
5051
return _scalarDataset->text();
51-
//else
52-
// return _showFullPathName ? scalarDataset->getLocation() : scalarDataset->text();
52+
else
53+
return getScalarSourceModel().getShowFullPathName() ? getScalarDataset()->getLocation() : getScalarDataset()->getGuiName();
5354
}
5455
else {
5556
if (rowIndex == DefaultRow::Constant)
@@ -67,13 +68,18 @@ QVariant ScalarSourceModel::Item::data(int role) const
6768
return {};
6869
}
6970

71+
const ScalarSourceModel& ScalarSourceModel::Item::getScalarSourceModel() const
72+
{
73+
return _scalarSourceModel;
74+
}
75+
7076
const mv::Dataset<>& ScalarSourceModel::Item::getScalarDataset() const
7177
{
7278
return _scalarDataset;
7379
}
7480

75-
ScalarSourceModel::NameItem::NameItem(const mv::Dataset<>& scalarDataset) :
76-
Item(scalarDataset)
81+
ScalarSourceModel::NameItem::NameItem(const ScalarSourceModel& scalarSourceModel, const mv::Dataset<>& scalarDataset) :
82+
Item(scalarSourceModel, scalarDataset)
7783
{
7884
connect(&const_cast<Dataset<>&>(getScalarDataset()), &Dataset<>::guiNameChanged, this, [this]() {
7985
emitDataChanged();
@@ -158,27 +164,12 @@ QVariant ScalarSourceModel::IdItem::data(int role) const
158164
return Item::data(role);
159165
}
160166

161-
/*
162-
int ScalarSourceModel::rowIndex(const Dataset<DatasetImpl>& dataset) const
163-
{
164-
// Only proceed if we have a valid dataset
165-
if (!dataset.isValid())
166-
return -1;
167-
168-
// Return the index of the dataset and add one for the constant point size option
169-
return _datasets.indexOf(dataset) + DefaultRow::DatasetStart;
170-
}
171-
*/
172-
173-
174-
175167
void ScalarSourceModel::addDataset(const Dataset<DatasetImpl>& dataset)
176168
{
177-
// Avoid duplicates
178169
if (hasDataset(dataset))
179170
return;
180171

181-
appendRow(Row(dataset));
172+
appendRow(Row(*this, dataset));
182173
}
183174

184175
bool ScalarSourceModel::hasDataset(const Dataset<DatasetImpl>& dataset) const
@@ -205,7 +196,7 @@ void ScalarSourceModel::removeAllDatasets()
205196
removeRows(DefaultRow::DatasetStart, rowCount() - DefaultRow::DatasetStart);
206197
}
207198

208-
const Datasets& ScalarSourceModel::getDatasets() const
199+
Datasets ScalarSourceModel::getDatasets() const
209200
{
210201
Datasets datasets;
211202

src/ScalarSourceModel.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,45 @@ class ScalarSourceModel : public QStandardItemModel
4444
public:
4545

4646
/**
47-
* Construct with pointer to \p scalarDataset
47+
* Construct with reference to \p scalarSourceModel and pointer to \p scalarDataset
48+
* @param scalarSourceModel Reference to the scalar source model
4849
* @param scalarDataset Pointer to scalar dataset (maybe nullptr)
4950
*/
50-
Item(const mv::Dataset<DatasetImpl>& scalarDataset);
51+
Item(const ScalarSourceModel& scalarSourceModel, const mv::Dataset<DatasetImpl>& scalarDataset);
5152

5253
/**
5354
* Get model data for \p role
5455
* @return Data for \p role in variant form
5556
*/
5657
QVariant data(int role = Qt::UserRole + 1) const override;
5758

59+
/**
60+
* Get reference to the scalar source model
61+
* @return Reference to the scalar source model
62+
*/
63+
const ScalarSourceModel& getScalarSourceModel() const;
64+
5865
/**
5966
* Get the scalar dataset associated with this item (if any)
6067
* @return Pointer to scalar dataset (maybe nullptr)
6168
*/
6269
const mv::Dataset<>& getScalarDataset() const;
6370

6471
private:
65-
mv::Dataset<> _scalarDataset; /** Pointer to scalar dataset (maybe nullptr) */
72+
const ScalarSourceModel& _scalarSourceModel; /** Reference to the scalar source model */
73+
mv::Dataset<> _scalarDataset; /** Pointer to scalar dataset (maybe nullptr) */
6674
};
6775

6876
/** Standard model item class for displaying the dataset GUI name */
6977
class NameItem final : public Item, public QObject {
7078
public:
7179

7280
/**
73-
* Construct with pointer to \p scalarDataset
81+
* Construct with reference to \p scalarSourceModel and pointer to \p scalarDataset
82+
* @param scalarSourceModel Reference to the scalar source model
7483
* @param scalarDataset Pointer to scalar dataset (maybe nullptr)
7584
*/
76-
NameItem(const mv::Dataset<>& scalarDataset);
85+
NameItem(const ScalarSourceModel& scalarSourceModel, const mv::Dataset<>& scalarDataset);
7786

7887
/**
7988
* Get model data for \p role
@@ -168,7 +177,7 @@ class ScalarSourceModel : public QStandardItemModel
168177
* Get datasets
169178
* @return Vector of smart pointers to datasets
170179
*/
171-
const Datasets& getDatasets() const;
180+
Datasets getDatasets() const;
172181

173182
/**
174183
* Get dataset at the specified row index
@@ -210,10 +219,11 @@ class ScalarSourceModel : public QStandardItemModel
210219
* Construct with pointer to \p scalarDataset
211220
* @param scalarDataset Pointer to scalar dataset (maybe nullptr)
212221
*/
213-
Row(const mv::Dataset<>& scalarDataset) : QList<QStandardItem*>()
222+
Row(const ScalarSourceModel& scalarSourceModel, const mv::Dataset<>& scalarDataset) :
223+
QList<QStandardItem*>()
214224
{
215-
append(new NameItem(scalarDataset));
216-
append(new IdItem(scalarDataset));
225+
append(new NameItem(scalarSourceModel, scalarDataset));
226+
append(new IdItem(scalarSourceModel, scalarDataset));
217227
}
218228
};
219229

0 commit comments

Comments
 (0)