OGS
StationTreeModel Class Reference

Detailed Description

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

A model for the StationTreeView implementing a tree as a double-linked list. In addition to a simple TreeModel each item also contains a 2D / 3D GraphicsItem for visualization.

See also
TreeModel, StationTreeView, TreeItem, ModelTreeItem

Definition at line 31 of file StationTreeModel.h.

#include <StationTreeModel.h>

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

Public Member Functions

 StationTreeModel (QObject *parent=nullptr)
 ~StationTreeModel () override
void addStationList (QString listName, const std::vector< GeoLib::Point * > *stations)
const std::vector< ModelTreeItem * > & getLists ()
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
void removeStationList (QModelIndex index)
void removeStationList (const std::string &name)
void setNameForItem (const std::string &stn_vec_name, std::size_t id, std::string const &item_name)
GeoLib::StationstationFromIndex (const QModelIndex &index, QString &listName) const
vtkPolyDataAlgorithm * vtkSource (const std::string &name) const
Public Member Functions inherited from TreeModel
 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

Private Attributes

std::vector< ModelTreeItem * > _lists

Additional Inherited Members

Public Slots inherited from TreeModel
void updateData ()
Protected Attributes inherited from TreeModel
TreeItem_rootItem

Constructor & Destructor Documentation

◆ StationTreeModel()

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

Constructor.

Definition at line 15 of file StationTreeModel.cpp.

16{
17 QList<QVariant> rootData;
18 delete _rootItem;
19 rootData << "Station Name"
20 << "x"
21 << "y"
22 << "z";
23 _rootItem = new ModelTreeItem(rootData, nullptr, nullptr);
24}
QModelIndex parent(const QModelIndex &index) const override
Definition TreeModel.cpp:70
TreeModel(QObject *parent=nullptr)
Definition TreeModel.cpp:15
TreeItem * _rootItem
Definition TreeModel.h:47

References TreeModel::TreeModel(), TreeModel::_rootItem, and TreeModel::parent().

◆ ~StationTreeModel()

StationTreeModel::~StationTreeModel ( )
overridedefault

Member Function Documentation

◆ addStationList()

void StationTreeModel::addStationList ( QString listName,
const std::vector< GeoLib::Point * > * stations )

Inserts a subtree under _rootItem.

Parameters
listNameName of the new subtree. If no name is given a default name is assigned.
stationsThe list with stations to be added as children of that subtree

Definition at line 128 of file StationTreeModel.cpp.

130{
131 beginResetModel();
132
133 QList<QVariant> grpName;
134 if (listName.compare("") ==
135 0) // if no name is given a default name is assigned
136 {
137 listName = "List";
138 listName.append(QString::number(rowCount() + 1));
139 }
140 grpName << listName << ""
141 << ""
142 << "";
143 auto* group =
144 new ModelTreeItem(grpName, _rootItem, new BaseItem(listName, stations));
145 _lists.push_back(group);
146 _rootItem->appendChild(group);
147 int vectorSize = stations->size();
148
149 for (int i = 0; i < vectorSize; i++)
150 {
151 QList<QVariant> stn;
152 stn << QString::fromStdString(
153 static_cast<GeoLib::Station*>((*stations)[i])->getName())
154 << QString::number((*(*stations)[i])[0], 'f')
155 << QString::number((*(*stations)[i])[1], 'f')
156 << QString::number((*(*stations)[i])[2], 'f');
157
158 auto* child = new ModelTreeItem(stn, group);
159 child->setStation(static_cast<GeoLib::Station*>((*stations)[i]));
160 group->appendChild(child);
161 }
162
163 qDebug() << "List" << listName << "loaded, " << stations->size()
164 << "items added.";
165
166 endResetModel();
167}
std::string getName(std::string const &line)
Returns the name/title from the "Zone"-description.
std::vector< ModelTreeItem * > _lists
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition TreeModel.cpp:93

References _lists, TreeModel::_rootItem, getName(), and TreeModel::rowCount().

◆ getLists()

const std::vector< ModelTreeItem * > & StationTreeModel::getLists ( )
inline

Definition at line 40 of file StationTreeModel.h.

40{ return _lists; }

References _lists.

◆ index()

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

Returns the model index of an item in the tree.

Parameters
rowThe row where the item is located
columnThe column where the item is located
parentThe parent of the item
Returns
The model index of the item

Definition at line 35 of file StationTreeModel.cpp.

37{
38 if (!hasIndex(row, column, parent))
39 {
40 return QModelIndex();
41 }
42
43 ModelTreeItem* parentItem;
44
45 if (!parent.isValid())
46 {
47 parentItem = static_cast<ModelTreeItem*>(_rootItem);
48 }
49 else
50 {
51 parentItem = static_cast<ModelTreeItem*>(parent.internalPointer());
52 }
53
54 auto* childItem = static_cast<ModelTreeItem*>(parentItem->child(row));
55 if (childItem)
56 {
57 QModelIndex newIndex = createIndex(row, column, childItem);
58 // assign ModelIndex to BaseItem so it can communicate with the model
59 BaseItem* item = childItem->getItem();
60 if (item != nullptr)
61 {
62 item->setModelIndex(newIndex);
63 }
64 return newIndex;
65 }
66
67 return QModelIndex();
68}
void setModelIndex(QModelIndex index)
Sets the model index.
Definition BaseItem.h:42
TreeItem * child(int row) const
Definition TreeItem.cpp:41

References TreeModel::_rootItem, TreeItem::child(), TreeModel::parent(), and BaseItem::setModelIndex().

Referenced by removeStationList(), and stationFromIndex().

◆ removeStationList() [1/2]

void StationTreeModel::removeStationList ( const std::string & name)

Removes the TreeItem with the given name including all its children

Definition at line 195 of file StationTreeModel.cpp.

196{
197 for (auto& list : _lists)
198 {
199 if (name == list->data(0).toString().toStdString())
200 {
201 removeStationList(createIndex(list->row(), 0, list));
202 }
203 }
204}
void removeStationList(QModelIndex index)

References _lists, and removeStationList().

◆ removeStationList() [2/2]

void StationTreeModel::removeStationList ( QModelIndex index)

Removes the TreeItem with the given Index including all its children

Definition at line 172 of file StationTreeModel.cpp.

173{
174 if (index.isValid()) //
175 {
176 auto* item = static_cast<ModelTreeItem*>(getItem(index));
177
178 // also delete the lists entry in the list directory of the model
179 for (std::size_t i = 0; i < _lists.size(); i++)
180 {
181 if (item == _lists[i])
182 {
183 _lists.erase(_lists.begin() + i);
184 }
185 }
186
187 removeRows(0, item->childCount(), index);
188 removeRows(item->row(), 1, parent(index));
189 }
190}
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
bool removeRows(int position, int count, const QModelIndex &parent) override
TreeItem * getItem(const QModelIndex &index) const

References _lists, TreeModel::getItem(), index(), TreeModel::parent(), and TreeModel::removeRows().

Referenced by removeStationList().

◆ setNameForItem()

void StationTreeModel::setNameForItem ( const std::string & stn_vec_name,
std::size_t id,
std::string const & item_name )

Definition at line 103 of file StationTreeModel.cpp.

106{
107 auto const stn_list = find_if(
108 _lists.begin(),
109 _lists.end(),
110 [&stn_vec_name](ModelTreeItem* item)
111 { return (stn_vec_name == item->data(0).toString().toStdString()); });
112
113 if (stn_list == _lists.end() ||
114 id >= static_cast<std::size_t>((*stn_list)->childCount()))
115 {
116 return;
117 }
118 TreeItem* const item = (*stn_list)->child(id);
119 item->setData(0, QString::fromStdString(item_name));
120}
virtual bool setData(int column, const QVariant &value)
Definition TreeItem.cpp:91

References _lists, TreeItem::child(), and TreeItem::setData().

Referenced by MainWindow::showStationNameDialog().

◆ stationFromIndex()

GeoLib::Station * StationTreeModel::stationFromIndex ( const QModelIndex & index,
QString & listName ) const

Returns the Station-Object of the ModelTreeItem with the given index and the name of the list this station belongs to.

Parameters
indexIndex of the requested item
listNameHere, the method will put the name of the list this station belongs to.
Returns
The station object associated with the tree item

Definition at line 76 of file StationTreeModel.cpp.

78{
79 if (index.isValid())
80 {
81 auto* treeItem = static_cast<ModelTreeItem*>(index.internalPointer());
82 TreeItem* parentItem = treeItem->parentItem();
83 listName = parentItem->data(0).toString();
84 return treeItem->getStation();
85 }
86
87 return nullptr;
88}
virtual QVariant data(int column) const
Definition TreeItem.cpp:83

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

◆ vtkSource()

vtkPolyDataAlgorithm * StationTreeModel::vtkSource ( const std::string & name) const

Definition at line 90 of file StationTreeModel.cpp.

91{
92 std::size_t nLists = _lists.size();
93 for (std::size_t i = 0; i < nLists; i++)
94 {
95 if (name == _lists[i]->data(0).toString().toStdString())
96 {
97 return dynamic_cast<BaseItem*>(_lists[i]->getItem())->vtkSource();
98 }
99 }
100 return nullptr;
101}
QVariant data(const QModelIndex &index, int role) const override
const char * toString(mgis::behaviour::Behaviour::Kinematic kin)

References _lists, TreeModel::data(), and BaseItem::vtkSource().

Referenced by VtkVisPipeline::addPipelineItem(), StationTreeView::displayStratigraphy(), VtkVisPipeline::removeSourceItem(), and StationTreeView::writeStratigraphiesAsImages().

Member Data Documentation

◆ _lists

std::vector<ModelTreeItem*> StationTreeModel::_lists
private

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