OGS
StationTreeModel.cpp
Go to the documentation of this file.
1
15#include "StationTreeModel.h"
16
17#include <QDebug>
18
19#include "Base/OGSError.h"
20#include "BaseItem.h"
21#include "GeoLib/Station.h"
22
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}
36
38
47 int row, int column, const QModelIndex& parent /*= QModelIndex()*/) const
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}
80
88 QString& listName) const
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}
100
101vtkPolyDataAlgorithm* StationTreeModel::vtkSource(const std::string& name) const
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}
113
114void StationTreeModel::setNameForItem(const std::string& stn_vec_name,
115 std::size_t const id,
116 std::string const& item_name)
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}
132
140 QString listName, const std::vector<GeoLib::Point*>* stations)
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}
179
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}
202
206void StationTreeModel::removeStationList(const std::string& name)
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}
Definition of the BaseItem class.
Definition of the OGSError class.
Definition of the StationTreeModel class.
Definition of the Station class.
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
void setModelIndex(QModelIndex index)
Sets the model index.
Definition BaseItem.h:52
A Station (observation site) is basically a Point with some additional information.
Definition Station.h:37
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:28
void appendChild(TreeItem *item)
Definition TreeItem.cpp:42
TreeItem * parentItem() const
Definition TreeItem.cpp:115
TreeItem * child(int row) const
Definition TreeItem.cpp:52
virtual bool setData(int column, const QVariant &value)
Definition TreeItem.cpp:102
virtual QVariant data(int column) const
Definition TreeItem.cpp:94
A hierarchical model for a tree implemented as a double-linked list.
Definition TreeModel.h:30
bool removeRows(int position, int count, const QModelIndex &parent) override
TreeItem * getItem(const QModelIndex &index) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
QModelIndex parent(const QModelIndex &index) const override
Definition TreeModel.cpp:81
TreeItem * _rootItem
Definition TreeModel.h:58