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 29 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 26 of file TreeModel.cpp.

26 : QAbstractItemModel(parent)
27{
28 //_modelType = TREE_MODEL;
29 QList<QVariant> rootData;
30 rootData << "1"
31 << "2"
32 << "3";
33 _rootItem = new TreeItem(rootData, nullptr);
34 // setupModelData(data, _rootItem);
35}
Objects nodes for the TreeModel.
Definition TreeItem.h:28
QModelIndex parent(const QModelIndex &index) const override
Definition TreeModel.cpp:81
TreeItem * _rootItem
Definition TreeModel.h:58

References _rootItem.

◆ ~TreeModel()

TreeModel::~TreeModel ( )
override

Definition at line 37 of file TreeModel.cpp.

38{
39 delete _rootItem;
40}

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 127 of file TreeModel.cpp.

128{
129 if (parent.isValid())
130 {
131 return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
132 }
133
134 return _rootItem->columnCount();
135}
virtual int columnCount() const
Definition TreeItem.cpp:86
int columnCount(const QModelIndex &parent=QModelIndex()) const override

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

Referenced by columnCount().

◆ 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 142 of file TreeModel.cpp.

143{
144 if (!index.isValid())
145 {
146 return QVariant();
147 }
148
149 if (role == Qt::EditRole || role == Qt::DisplayRole)
150 {
151 auto* item = static_cast<TreeItem*>(index.internalPointer());
152
153 return item->data(index.column());
154 }
155
156 return QVariant();
157}
virtual QVariant data(int column) const
Definition TreeItem.cpp:94
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition TreeModel.cpp:50

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

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

◆ flags()

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

Definition at line 175 of file TreeModel.cpp.

176{
177 if (!index.isValid())
178 {
179 return Qt::NoItemFlags;
180 }
181
182 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
183}

References index().

◆ getItem()

TreeItem * TreeModel::getItem ( const QModelIndex & index) const

Returns the Item characterized by the given index.

Definition at line 188 of file TreeModel.cpp.

189{
190 if (index.isValid())
191 {
192 auto* item = static_cast<TreeItem*>(index.internalPointer());
193 if (item)
194 {
195 return item;
196 }
197 }
198 return _rootItem;
199}

References _rootItem, and index().

Referenced by VtkAddFilterDialog::VtkAddFilterDialog(), VtkVisPipeline::addPipelineItem(), VtkVisPipeline::addPipelineItem(), MeshModel::getMesh(), VtkAddFilterDialog::on_buttonBox_accepted(), MeshModel::removeMesh(), removeRows(), VtkVisPipeline::removeSourceItem(), VtkVisPipeline::removeSourceItem(), VtkVisPipeline::removeSourceItem(), StationTreeModel::removeStationList(), and MeshModel::vtkSource().

◆ headerData()

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

Definition at line 201 of file TreeModel.cpp.

203{
204 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
205 {
206 return _rootItem->data(section);
207 }
208
209 return QVariant();
210}

References _rootItem, and TreeItem::data().

◆ 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 50 of file TreeModel.cpp.

52{
53 if (!hasIndex(row, column, parent))
54 {
55 return QModelIndex();
56 }
57
58 TreeItem* parentItem;
59
60 if (!parent.isValid())
61 {
62 parentItem = _rootItem;
63 }
64 else
65 {
66 parentItem = static_cast<TreeItem*>(parent.internalPointer());
67 }
68
69 TreeItem* childItem = parentItem->child(row);
70 if (childItem)
71 {
72 return createIndex(row, column, childItem);
73 }
74
75 return QModelIndex();
76}
TreeItem * child(int row) const
Definition TreeItem.cpp:52

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

Referenced by AddFaultsToVoxelGridDialog::AddFaultsToVoxelGridDialog(), Layers2GridDialog::Layers2GridDialog(), TranslateDataDialog::TranslateDataDialog(), 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 81 of file TreeModel.cpp.

82{
83 if (!index.isValid())
84 {
85 return QModelIndex();
86 }
87
88 auto* childItem = static_cast<TreeItem*>(index.internalPointer());
89 TreeItem* parentItem = childItem->parentItem();
90
91 if (parentItem == _rootItem)
92 {
93 return QModelIndex();
94 }
95
96 return createIndex(parentItem->row(), 0, parentItem);
97}
TreeItem * parentItem() const
Definition TreeItem.cpp:115
int row() const
Definition TreeItem.cpp:73

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

Referenced by ProcessModel::addConditionItem(), VtkVisPipeline::addPipelineItem(), VtkVisPipeline::addPipelineItem(), GeoTreeModel::appendPolylines(), GeoTreeModel::appendSurfaces(), columnCount(), MeshModel::columnCount(), ProcessModel::columnCount(), index(), StationTreeModel::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 215 of file TreeModel.cpp.

216{
217 TreeItem* parentItem = getItem(parent);
218 bool success = true;
219
220 beginRemoveRows(parent, position, position + count - 1);
221 success = parentItem->removeChildren(position, count);
222 endRemoveRows();
223
224 return success;
225}
bool removeChildren(int position, int count)
Definition TreeItem.cpp:124
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 294 of file TreeModel.cpp.

295{
296 return _rootItem;
297}

References _rootItem.

Referenced by TreeModelIterator::TreeModelIterator(), and TreeModelIterator::next().

◆ 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 104 of file TreeModel.cpp.

105{
106 TreeItem* parentItem;
107 if (parent.column() > 0)
108 {
109 return 0;
110 }
111
112 if (!parent.isValid())
113 {
114 parentItem = _rootItem;
115 }
116 else
117 {
118 parentItem = static_cast<TreeItem*>(parent.internalPointer());
119 }
120
121 return parentItem->childCount();
122}
virtual int childCount() const
Definition TreeItem.cpp:65

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

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

◆ setData()

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

Definition at line 159 of file TreeModel.cpp.

161{
162 if (!index.isValid())
163 {
164 return false;
165 }
166
167 if (role == Qt::EditRole)
168 {
169 auto* item = static_cast<TreeItem*>(index.internalPointer());
170 item->setData(index.column(), value);
171 return true;
172 }
173 return false;
174}
virtual bool setData(int column, const QVariant &value)
Definition TreeItem.cpp:102

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 230 of file TreeModel.cpp.

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

References parent().

◆ updateData

void TreeModel::updateData ( )
slot

Definition at line 137 of file TreeModel.cpp.

137{}

Member Data Documentation

◆ _rootItem


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