OGS
StationTreeModel.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "StationTreeModel.h"
5
6#include <QDebug>
7
8#include "Base/OGSError.h"
9#include "BaseItem.h"
10#include "GeoLib/Station.h"
11
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}
25
27
36 int row, int column, const QModelIndex& parent /*= QModelIndex()*/) const
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}
69
77 QString& listName) const
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}
89
90vtkPolyDataAlgorithm* StationTreeModel::vtkSource(const std::string& name) const
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}
102
103void StationTreeModel::setNameForItem(const std::string& stn_vec_name,
104 std::size_t const id,
105 std::string const& item_name)
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}
121
129 QString listName, const std::vector<GeoLib::Point*>* stations)
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}
168
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}
191
195void StationTreeModel::removeStationList(const std::string& name)
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}
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:22
vtkPolyDataAlgorithm * vtkSource() const
Returns the Vtk polydata source object.
Definition BaseItem.h:47
void setModelIndex(QModelIndex index)
Sets the model index.
Definition BaseItem.h:42
A Station (observation site) is basically a Point with some additional information.
Definition Station.h:26
A TreeItem containing some additional information used in the StationModel.
void addStationList(QString listName, const std::vector< GeoLib::Point * > *stations)
void setNameForItem(const std::string &stn_vec_name, std::size_t id, std::string const &item_name)
~StationTreeModel() override
void removeStationList(QModelIndex index)
StationTreeModel(QObject *parent=nullptr)
std::vector< ModelTreeItem * > _lists
vtkPolyDataAlgorithm * vtkSource(const std::string &name) const
GeoLib::Station * stationFromIndex(const QModelIndex &index, QString &listName) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Objects nodes for the TreeModel.
Definition TreeItem.h:17
TreeItem * parentItem() const
Definition TreeItem.cpp:104
TreeItem * child(int row) const
Definition TreeItem.cpp:41
virtual bool setData(int column, const QVariant &value)
Definition TreeItem.cpp:91
virtual QVariant data(int column) const
Definition TreeItem.cpp:83
bool removeRows(int position, int count, const QModelIndex &parent) override
TreeItem * getItem(const QModelIndex &index) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition TreeModel.cpp:93
QVariant data(const QModelIndex &index, int role) const override
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