OGS
TreeModel Class Reference

Detailed Description

A hierarchical model for a tree implemented as a double-linked list.

A hierarchical model for the pre-defined QTreeView included in QT. The tree as implemented as a double-linked list.

Definition at line 18 of file TreeModel.h.

#include <TreeModel.h>

Inheritance diagram for TreeModel:
[legend]
Collaboration diagram for TreeModel:
[legend]

Public Slots

void updateData ()

Public Member Functions

 TreeModel (QObject *parent=nullptr)
 ~TreeModel () override
QVariant data (const QModelIndex &index, int role) const override
bool setData (const QModelIndex &index, const QVariant &value, int role) override
Qt::ItemFlags flags (const QModelIndex &index) const override
TreeItemgetItem (const QModelIndex &index) const
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
QModelIndex parent (const QModelIndex &index) const override
bool removeRows (int position, int count, const QModelIndex &parent) override
int rowCount (const QModelIndex &parent=QModelIndex()) const override
int columnCount (const QModelIndex &parent=QModelIndex()) const override
TreeItemrootItem () const

Protected Attributes

TreeItem_rootItem

Private Member Functions

void setupModelData (const QStringList &lines, TreeItem *parent)

Constructor & Destructor Documentation

◆ TreeModel()

TreeModel::TreeModel ( QObject * parent = nullptr)
explicit

A model for the QTreeView implementing the tree as a double-linked list.

Definition at line 15 of file TreeModel.cpp.

15 : QAbstractItemModel(parent)
16{
17 //_modelType = TREE_MODEL;
18 QList<QVariant> rootData;
19 rootData << "1"
20 << "2"
21 << "3";
22 _rootItem = new TreeItem(rootData, nullptr);
23 // setupModelData(data, _rootItem);
24}
QModelIndex parent(const QModelIndex &index) const override
Definition TreeModel.cpp:70
TreeItem * _rootItem
Definition TreeModel.h:47

References _rootItem, and parent().

Referenced by ElementTreeModel::ElementTreeModel(), FemConditionModel::FemConditionModel(), GeoTreeModel::GeoTreeModel(), MeshModel::MeshModel(), ProcessModel::ProcessModel(), StationTreeModel::StationTreeModel(), VtkVisPipeline::VtkVisPipeline(), VtkVisPipeline::highlightGeoObject(), and VtkVisPipeline::highlightMeshComponent().

◆ ~TreeModel()

TreeModel::~TreeModel ( )
override

Definition at line 26 of file TreeModel.cpp.

27{
28 delete _rootItem;
29}

References _rootItem.

Member Function Documentation

◆ columnCount()

int TreeModel::columnCount ( const QModelIndex & parent = QModelIndex()) const
override

Determines how many columns are present for a given model index.

Definition at line 116 of file TreeModel.cpp.

117{
118 if (parent.isValid())
119 {
120 return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
121 }
122
123 return _rootItem->columnCount();
124}

References _rootItem, TreeItem::columnCount(), and parent().

◆ data()

QVariant TreeModel::data ( const QModelIndex & index,
int role ) const
override

Since each item manages its own columns, the column number is used to retrieve the data with the TreeItem::data() function

Definition at line 131 of file TreeModel.cpp.

132{
133 if (!index.isValid())
134 {
135 return QVariant();
136 }
137
138 if (role == Qt::EditRole || role == Qt::DisplayRole)
139 {
140 auto* item = static_cast<TreeItem*>(index.internalPointer());
141
142 return item->data(index.column());
143 }
144
145 return QVariant();
146}
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition TreeModel.cpp:39

References TreeItem::data(), and index().

Referenced by GeoTreeModel::removeGeoList(), GeoTreeModel::vtkSource(), and StationTreeModel::vtkSource().

◆ flags()

Qt::ItemFlags TreeModel::flags ( const QModelIndex & index) const
override

Definition at line 164 of file TreeModel.cpp.

165{
166 if (!index.isValid())
167 {
168 return Qt::NoItemFlags;
169 }
170
171 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
172}

References index().

◆ getItem()

◆ headerData()

QVariant TreeModel::headerData ( int section,
Qt::Orientation orientation,
int role = Qt::DisplayRole ) const
override

Definition at line 190 of file TreeModel.cpp.

192{
193 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
194 {
195 return _rootItem->data(section);
196 }
197
198 return QVariant();
199}

References _rootItem.

◆ index()

QModelIndex TreeModel::index ( int row,
int column,
const QModelIndex & parent = QModelIndex() ) const
override

Returns the index of a TreeItem given its parent and position. It is first checked if the model index is valid. If it is not, it is assumed that a top-level item is being referred to; otherwise, the data pointer from the model index is obtained with its internalPointer() function and is used to reference a TreeItem object

Parameters
rowRow of the tree object
columnColumn of the tree object
parentIndex of the parent object

Definition at line 39 of file TreeModel.cpp.

41{
42 if (!hasIndex(row, column, parent))
43 {
44 return QModelIndex();
45 }
46
47 TreeItem* parentItem;
48
49 if (!parent.isValid())
50 {
51 parentItem = _rootItem;
52 }
53 else
54 {
55 parentItem = static_cast<TreeItem*>(parent.internalPointer());
56 }
57
58 TreeItem* childItem = parentItem->child(row);
59 if (childItem)
60 {
61 return createIndex(row, column, childItem);
62 }
63
64 return QModelIndex();
65}
TreeItem * child(int row) const
Definition TreeItem.cpp:41

References _rootItem, TreeItem::child(), and parent().

Referenced by AddFaultsToVoxelGridDialog::AddFaultsToVoxelGridDialog(), Layers2GridDialog::Layers2GridDialog(), Vtu2GridDialog::Vtu2GridDialog(), MeshModel::addMeshObject(), VtkVisPipeline::addPipelineItem(), data(), flags(), VtkVisPipeline::flags(), getItem(), VtkVisPipeline::highlightGeoObject(), VtkVisPipeline::highlightMeshComponent(), parent(), GeoTreeModel::removeGeoList(), MeshModel::removeMesh(), VtkVisPipeline::removePipelineItem(), VtkVisPipeline::removeSourceItem(), VtkVisPipeline::removeSourceItem(), VtkVisPipeline::removeSourceItem(), setData(), VtkVisPipeline::setData(), and MeshModel::updateMesh().

◆ parent()

QModelIndex TreeModel::parent ( const QModelIndex & index) const
override

Returns the model index of a TreeItem based on its index.

Definition at line 70 of file TreeModel.cpp.

71{
72 if (!index.isValid())
73 {
74 return QModelIndex();
75 }
76
77 auto* childItem = static_cast<TreeItem*>(index.internalPointer());
78 TreeItem* parentItem = childItem->parentItem();
79
80 if (parentItem == _rootItem)
81 {
82 return QModelIndex();
83 }
84
85 return createIndex(parentItem->row(), 0, parentItem);
86}
int row() const
Definition TreeItem.cpp:62

References _rootItem, index(), TreeItem::parentItem(), and TreeItem::row().

Referenced by ElementTreeModel::ElementTreeModel(), FemConditionModel::FemConditionModel(), GeoTreeModel::GeoTreeModel(), MeshModel::MeshModel(), ProcessModel::ProcessModel(), StationTreeModel::StationTreeModel(), TreeModel(), VtkVisPipeline::VtkVisPipeline(), ProcessModel::addConditionItem(), VtkVisPipeline::addPipelineItem(), VtkVisPipeline::addPipelineItem(), GeoTreeModel::appendPolylines(), GeoTreeModel::appendSurfaces(), MeshModel::columnCount(), ProcessModel::columnCount(), columnCount(), StationTreeModel::index(), index(), GeoTreeModel::removeGeoList(), removeRows(), StationTreeModel::removeStationList(), rowCount(), and setupModelData().

◆ removeRows()

bool TreeModel::removeRows ( int position,
int count,
const QModelIndex & parent )
override

Removes item from the model.

Definition at line 204 of file TreeModel.cpp.

205{
206 TreeItem* parentItem = getItem(parent);
207 bool success = true;
208
209 beginRemoveRows(parent, position, position + count - 1);
210 success = parentItem->removeChildren(position, count);
211 endRemoveRows();
212
213 return success;
214}
bool removeChildren(int position, int count)
Definition TreeItem.cpp:113
TreeItem * getItem(const QModelIndex &index) const

References getItem(), parent(), and TreeItem::removeChildren().

Referenced by GeoTreeModel::removeGeoList(), VtkVisPipeline::removePipelineItem(), and StationTreeModel::removeStationList().

◆ rootItem()

TreeItem * TreeModel::rootItem ( ) const

Definition at line 283 of file TreeModel.cpp.

284{
285 return _rootItem;
286}

References _rootItem.

◆ rowCount()

int TreeModel::rowCount ( const QModelIndex & parent = QModelIndex()) const
override

Returns the number of child items for the TreeItem that corresponds to a given model index, or the number of top-level items if an invalid index is specified.

Definition at line 93 of file TreeModel.cpp.

94{
95 TreeItem* parentItem;
96 if (parent.column() > 0)
97 {
98 return 0;
99 }
100
101 if (!parent.isValid())
102 {
103 parentItem = _rootItem;
104 }
105 else
106 {
107 parentItem = static_cast<TreeItem*>(parent.internalPointer());
108 }
109
110 return parentItem->childCount();
111}
virtual int childCount() const
Definition TreeItem.cpp:54

References _rootItem, TreeItem::childCount(), and parent().

Referenced by AddFaultsToVoxelGridDialog::AddFaultsToVoxelGridDialog(), Layers2GridDialog::Layers2GridDialog(), Vtu2GridDialog::Vtu2GridDialog(), and StationTreeModel::addStationList().

◆ setData()

bool TreeModel::setData ( const QModelIndex & index,
const QVariant & value,
int role )
override

Definition at line 148 of file TreeModel.cpp.

150{
151 if (!index.isValid())
152 {
153 return false;
154 }
155
156 if (role == Qt::EditRole)
157 {
158 auto* item = static_cast<TreeItem*>(index.internalPointer());
159 item->setData(index.column(), value);
160 return true;
161 }
162 return false;
163}

References index(), and TreeItem::setData().

Referenced by VtkVisPipeline::setData().

◆ setupModelData()

void TreeModel::setupModelData ( const QStringList & lines,
TreeItem * parent )
private

Test method for creating a tree model

Definition at line 219 of file TreeModel.cpp.

220{
221 QList<TreeItem*> parents;
222 QList<int> indentations;
223 parents << parent;
224 indentations << 0;
225
226 int number = 0;
227
228 while (number < lines.count())
229 {
230 int position = 0;
231 while (position < lines[number].length())
232 {
233 if (lines[number].mid(position, 1) != " ")
234 {
235 break;
236 }
237 position++;
238 }
239
240 QString lineData = lines[number].mid(position).trimmed();
241
242 if (!lineData.isEmpty())
243 {
244 // Read the column data from the rest of the line.
245 QStringList columnStrings =
246 lineData.split("\t", Qt::SkipEmptyParts);
247 QList<QVariant> columnData;
248 for (int column = 0; column < columnStrings.count(); ++column)
249 {
250 columnData << columnStrings[column];
251 }
252
253 if (position > indentations.last())
254 {
255 // The last child of the current parent is now the new parent
256 // unless the current parent has no children.
257
258 if (parents.last()->childCount() > 0)
259 {
260 parents << parents.last()->child(
261 parents.last()->childCount() - 1);
262 indentations << position;
263 }
264 }
265 else
266 {
267 while (position < indentations.last() && parents.count() > 0)
268 {
269 parents.pop_back();
270 indentations.pop_back();
271 }
272 }
273
274 // Append a new item to the current parent's list of children.
275 parents.last()->appendChild(
276 new TreeItem(columnData, parents.last()));
277 }
278
279 number++;
280 }
281}

References parent().

◆ updateData

void TreeModel::updateData ( )
slot

Definition at line 126 of file TreeModel.cpp.

126{}

Member Data Documentation

◆ _rootItem


The documentation for this class was generated from the following files: