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 42 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 26 of file StationTreeModel.cpp.

27{
28 QList<QVariant> rootData;
29 delete _rootItem;
30 rootData << "Station Name"
31 << "x"
32 << "y"
33 << "z";
34 _rootItem = new ModelTreeItem(rootData, nullptr, nullptr);
35}
A TreeItem containing some additional information used in the StationModel.
QModelIndex parent(const QModelIndex &index) const override
Definition TreeModel.cpp:81
TreeModel(QObject *parent=nullptr)
Definition TreeModel.cpp:26
TreeItem * _rootItem
Definition TreeModel.h:58

References TreeModel::_rootItem.

◆ ~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 139 of file StationTreeModel.cpp.

141{
142 beginResetModel();
143
144 QList<QVariant> grpName;
145 if (listName.compare("") ==
146 0) // if no name is given a default name is assigned
147 {
148 listName = "List";
149 listName.append(QString::number(rowCount() + 1));
150 }
151 grpName << listName << ""
152 << ""
153 << "";
154 auto* group =
155 new ModelTreeItem(grpName, _rootItem, new BaseItem(listName, stations));
156 _lists.push_back(group);
157 _rootItem->appendChild(group);
158 int vectorSize = stations->size();
159
160 for (int i = 0; i < vectorSize; i++)
161 {
162 QList<QVariant> stn;
163 stn << QString::fromStdString(
164 static_cast<GeoLib::Station*>((*stations)[i])->getName())
165 << QString::number((*(*stations)[i])[0], 'f')
166 << QString::number((*(*stations)[i])[1], 'f')
167 << QString::number((*(*stations)[i])[2], 'f');
168
169 auto* child = new ModelTreeItem(stn, group);
170 child->setStation(static_cast<GeoLib::Station*>((*stations)[i]));
171 group->appendChild(child);
172 }
173
174 qDebug() << "List" << listName << "loaded, " << stations->size()
175 << "items added.";
176
177 endResetModel();
178}
std::string getName(std::string const &line)
Returns the name/title from the "Zone"-description.
A BaseItem contains additional Information about a subtree in the StationTreeModel.
Definition BaseItem.h:32
A Station (observation site) is basically a Point with some additional information.
Definition Station.h:37
std::vector< ModelTreeItem * > _lists
void appendChild(TreeItem *item)
Definition TreeItem.cpp:42
int rowCount(const QModelIndex &parent=QModelIndex()) const override

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

Referenced by GEOModels::addStationVec(), and GEOModels::updateGeometry().

◆ getLists()

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

Definition at line 51 of file StationTreeModel.h.

51{ 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 46 of file StationTreeModel.cpp.

48{
49 if (!hasIndex(row, column, parent))
50 {
51 return QModelIndex();
52 }
53
54 ModelTreeItem* parentItem;
55
56 if (!parent.isValid())
57 {
58 parentItem = static_cast<ModelTreeItem*>(_rootItem);
59 }
60 else
61 {
62 parentItem = static_cast<ModelTreeItem*>(parent.internalPointer());
63 }
64
65 auto* childItem = static_cast<ModelTreeItem*>(parentItem->child(row));
66 if (childItem)
67 {
68 QModelIndex newIndex = createIndex(row, column, childItem);
69 // assign ModelIndex to BaseItem so it can communicate with the model
70 BaseItem* item = childItem->getItem();
71 if (item != nullptr)
72 {
73 item->setModelIndex(newIndex);
74 }
75 return newIndex;
76 }
77
78 return QModelIndex();
79}
void setModelIndex(QModelIndex index)
Sets the model index.
Definition BaseItem.h:52
TreeItem * child(int row) const
Definition TreeItem.cpp:52

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 206 of file StationTreeModel.cpp.

207{
208 for (auto& list : _lists)
209 {
210 if (name == list->data(0).toString().toStdString())
211 {
212 removeStationList(createIndex(list->row(), 0, list));
213 }
214 }
215}
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 183 of file StationTreeModel.cpp.

184{
185 if (index.isValid()) //
186 {
187 auto* item = static_cast<ModelTreeItem*>(getItem(index));
188
189 // also delete the lists entry in the list directory of the model
190 for (std::size_t i = 0; i < _lists.size(); i++)
191 {
192 if (item == _lists[i])
193 {
194 _lists.erase(_lists.begin() + i);
195 }
196 }
197
198 removeRows(0, item->childCount(), index);
199 removeRows(item->row(), 1, parent(index));
200 }
201}
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(), GEOModels::removeStationVec(), and GEOModels::updateGeometry().

◆ setNameForItem()

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

Definition at line 114 of file StationTreeModel.cpp.

117{
118 auto const stn_list = find_if(
119 _lists.begin(),
120 _lists.end(),
121 [&stn_vec_name](ModelTreeItem* item)
122 { return (stn_vec_name == item->data(0).toString().toStdString()); });
123
124 if (stn_list == _lists.end() ||
125 id >= static_cast<std::size_t>((*stn_list)->childCount()))
126 {
127 return;
128 }
129 TreeItem* const item = (*stn_list)->child(id);
130 item->setData(0, QString::fromStdString(item_name));
131}
Objects nodes for the TreeModel.
Definition TreeItem.h:28
virtual bool setData(int column, const QVariant &value)
Definition TreeItem.cpp:102

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

◆ 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 87 of file StationTreeModel.cpp.

89{
90 if (index.isValid())
91 {
92 auto* treeItem = static_cast<ModelTreeItem*>(index.internalPointer());
93 TreeItem* parentItem = treeItem->parentItem();
94 listName = parentItem->data(0).toString();
95 return treeItem->getStation();
96 }
97
98 return nullptr;
99}
TreeItem * parentItem() const
Definition TreeItem.cpp:115
virtual QVariant data(int column) const
Definition TreeItem.cpp:94

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

◆ vtkSource()

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

Definition at line 101 of file StationTreeModel.cpp.

102{
103 std::size_t nLists = _lists.size();
104 for (std::size_t i = 0; i < nLists; i++)
105 {
106 if (name == _lists[i]->data(0).toString().toStdString())
107 {
108 return dynamic_cast<BaseItem*>(_lists[i]->getItem())->vtkSource();
109 }
110 }
111 return nullptr;
112}
vtkPolyDataAlgorithm * vtkSource(const std::string &name) const
QVariant data(const QModelIndex &index, int role) const override
const char * toString(mgis::behaviour::Behaviour::Kinematic kin)

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

Referenced by VtkVisPipeline::addPipelineItem(), VtkVisPipeline::removeSourceItem(), and vtkSource().

Member Data Documentation

◆ _lists

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

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