OGS
GeoTreeView.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 "GeoTreeView.h"
5
6#include <QFileDialog>
7#include <QMenu>
8#include <QSettings>
9
12#include "Base/OGSError.h"
13#include "GeoObjectListItem.h"
14#include "GeoTreeItem.h"
15#include "GeoTreeModel.h"
16
17GeoTreeView::GeoTreeView(QWidget* parent) : QTreeView(parent) {}
18
20{
21 setAlternatingRowColors(true);
22 setColumnWidth(0, 150);
23 setColumnWidth(1, 75);
24 setColumnWidth(2, 75);
25 setColumnWidth(3, 75);
26}
27
28void GeoTreeView::on_Clicked(QModelIndex idx)
29{
30 qDebug("%d, %d", idx.parent().row(), idx.row());
31}
32
33void GeoTreeView::selectionChanged(const QItemSelection& selected,
34 const QItemSelection& deselected)
35{
36 Q_UNUSED(deselected);
37 if (!selected.isEmpty())
38 {
39 const QModelIndex idx = *(selected.indexes().begin());
40 const TreeItem* tree_item =
41 static_cast<TreeModel*>(this->model())->getItem(idx);
43
44 const GeoObjectListItem* geo_object =
45 dynamic_cast<GeoObjectListItem*>(tree_item->parentItem());
46 if (geo_object) // geometry object
47 {
48 emit enableSaveButton(false);
49 emit enableRemoveButton(false);
50 emit geoItemSelected(geo_object->vtkSource(), tree_item->row());
51 }
52 else
53 {
54 if (!idx.parent().isValid()) // geometry item
55 {
56 emit enableSaveButton(true);
57 emit enableRemoveButton(true);
58 }
59 else // line points or surface triangles
60 {
61 emit enableSaveButton(false);
62 const auto* geo_type =
63 dynamic_cast<const GeoObjectListItem*>(tree_item);
64 if (geo_type)
65 { // geometry list item
66 emit enableRemoveButton(true);
67 }
68 else
69 {
70 // highlight a point for an expanded polyline
71 auto* list_item = dynamic_cast<GeoObjectListItem*>(
72 tree_item->parentItem()->parentItem());
73 if (list_item &&
74 list_item->getType() == GeoLib::GEOTYPE::POLYLINE)
75 {
77 tree_item->parentItem()
78 ->parentItem()
79 ->parentItem()
80 ->child(0))
81 ->vtkSource(),
82 tree_item->data(0).toInt());
83 }
84
85 // highlight a point for an expanded surface
86 list_item = dynamic_cast<GeoObjectListItem*>(
87 tree_item->parentItem()->parentItem()->parentItem());
88 if (list_item &&
89 list_item->getType() == GeoLib::GEOTYPE::SURFACE)
90 {
92 tree_item->parentItem()
93 ->parentItem()
94 ->parentItem()
95 ->parentItem()
96 ->child(0))
97 ->vtkSource(),
98 tree_item->data(0).toInt());
99 }
100 emit enableRemoveButton(false);
101 }
102 }
103 }
104 }
105 // emit itemSelectionChanged(selected, deselected);
106 // return QTreeView::selectionChanged(selected, deselected);
107}
108
109void GeoTreeView::selectionChangedFromOutside(const QItemSelection& selected,
110 const QItemSelection& deselected)
111{
112 QItemSelectionModel* selModel = this->selectionModel();
113
114 selModel->blockSignals(true);
115 selModel->select(deselected, QItemSelectionModel::Deselect);
116 selModel->select(selected, QItemSelectionModel::Select);
117 selModel->blockSignals(false);
118
119 QTreeView::selectionChanged(selected, deselected);
120}
121
122void GeoTreeView::contextMenuEvent(QContextMenuEvent* event)
123{
124 QModelIndex index = this->selectionModel()->currentIndex();
125 auto* item = static_cast<TreeItem*>(index.internalPointer());
126
127 auto* list = dynamic_cast<GeoObjectListItem*>(item);
128 QMenu menu;
129
130 // The current index is a list of points/polylines/surfaces
131 if (list != nullptr)
132 {
133 if (list->getType() == GeoLib::GEOTYPE::POINT)
134 {
135 auto const* convertToStationAction =
136 menu.addAction("Convert to Stations");
137 connect(convertToStationAction, SIGNAL(triggered()), this,
139 }
140 if (list->getType() == GeoLib::GEOTYPE::POLYLINE)
141 {
142 auto const* connectPlyAction =
143 menu.addAction("Connect Polylines...");
144 connect(connectPlyAction, SIGNAL(triggered()), this,
145 SLOT(connectPolylines()));
146 }
147 menu.addSeparator();
148 // QAction* removeAction = menu.addAction("Remove " +
149 // item->data(0).toString()); connect(removeAction, SIGNAL(triggered()),
150 // this, SLOT(removeList()));
151 }
152 else
153 {
154 if (!item)
155 { // Otherwise sometimes it crashes when (unmotivated ;-) ) clicking in
156 // a treeview
157 return;
158 }
159
160 auto* parent = dynamic_cast<GeoObjectListItem*>(item->parentItem());
161
162 // The current index refers to a geo-object
163 if (parent != nullptr)
164 {
165 QMenu* cond_menu = new QMenu("Set FEM Condition");
166 // menu.addMenu(cond_menu);
167 QAction* addCondAction = cond_menu->addAction("On object...");
168 QAction* addCondPointAction =
169 cond_menu->addAction("On all points...");
170 QAction* addNameAction = menu.addAction("Set name...");
171 connect(addCondAction, SIGNAL(triggered()), this,
172 SLOT(setObjectAsCondition()));
173 connect(addNameAction, SIGNAL(triggered()), this,
174 SLOT(setNameForElement()));
175
176 if (parent->getType() == GeoLib::GEOTYPE::POINT)
177 {
178 addCondPointAction->setEnabled(false);
179 }
180 else
181 {
182 connect(addCondPointAction, SIGNAL(triggered()), this,
184 }
185 }
186 // The current index refers to the name of a geometry-object
187 else if (item->childCount() > 0)
188 {
189 if (item->child(0)->data(0).toString().compare("Points") ==
190 0) // clumsy way to find out
191 {
192 // QAction* saveAction = menu.addAction("Save geometry...");
193 QAction* mapAction = menu.addAction("Map geometry...");
194 // QAction* addCNDAction = menu.addAction("Load FEM
195 // Conditions..."); QAction* saveCondAction =
196 // menu.addAction("Save FEM conditions...");
197 menu.addSeparator();
198 // QAction* removeAction = menu.addAction("Remove geometry");
199 // connect(saveAction, SIGNAL(triggered()), this,
200 // SLOT(writeToFile()));
201 connect(mapAction, SIGNAL(triggered()), this,
202 SLOT(mapGeometry()));
203 // connect(addCNDAction, SIGNAL(triggered()), this,
204 // SLOT(loadFEMConditions())); connect(saveCondAction,
205 // SIGNAL(triggered()), this, SLOT(saveFEMConditions()));
206 // connect(removeAction, SIGNAL(triggered()), this,
207 // SLOT(removeList()));
208 }
209 }
210 }
211
212 menu.exec(event->globalPos());
213}
214
216{
217 TreeItem const* const item =
218 static_cast<GeoTreeModel*>(model())
219 ->getItem(this->selectionModel()->currentIndex())
220 ->parentItem();
222 item->data(0).toString().toStdString());
223}
224
226{
227 TreeItem* item = static_cast<GeoTreeModel*>(model())
228 ->getItem(this->selectionModel()->currentIndex())
229 ->parentItem();
230 emit requestLineEditDialog(item->data(0).toString().toStdString());
231}
232
237
239{
240 QModelIndex index(this->selectionModel()->currentIndex());
241 if (!index.isValid())
242 {
243 OGSError::box("No geometry selected.");
244 }
245 else
246 {
247 TreeItem* item = static_cast<GeoTreeModel*>(model())->getItem(index);
248 auto* list = dynamic_cast<GeoObjectListItem*>(item);
249 if (list)
250 {
251 emit listRemoved(
252 (item->parentItem()->data(0).toString()).toStdString(),
253 list->getType());
254 }
255 else
256 {
257 emit listRemoved((item->data(0).toString()).toStdString(),
259 emit listRemoved((item->data(0).toString()).toStdString(),
261 emit listRemoved((item->data(0).toString()).toStdString(),
263 }
264
265 if (this->selectionModel()->selectedIndexes().count() == 0)
266 {
267 emit enableSaveButton(false);
268 emit enableRemoveButton(false);
269 }
270 }
271}
272
274{
275 const TreeItem* item = static_cast<GeoTreeModel*>(model())->getItem(
276 this->selectionModel()->currentIndex());
277 const std::size_t id = item->row();
278 const GeoLib::GEOTYPE type =
279 static_cast<GeoObjectListItem*>(item->parentItem())->getType();
280 const std::string geometry_name =
281 item->parentItem()->parentItem()->data(0).toString().toStdString();
282 emit requestCondSetupDialog(geometry_name, type, id, set_on_points);
283}
284
286{
287 const TreeItem* item = static_cast<GeoTreeModel*>(model())->getItem(
288 this->selectionModel()->currentIndex());
289 const std::size_t id = item->row();
290 const GeoLib::GEOTYPE type =
291 static_cast<GeoObjectListItem*>(item->parentItem())->getType();
292 const std::string geometry_name =
293 item->parentItem()->parentItem()->data(0).toString().toStdString();
294 emit requestNameChangeDialog(geometry_name, type, id);
295}
296
298{
299 TreeItem* item = static_cast<GeoTreeModel*>(model())->getItem(
300 this->selectionModel()->currentIndex());
301 std::string geo_name(item->data(0).toString().toStdString());
302 emit geometryMappingRequested(geo_name);
303}
304
306{
307 QModelIndex index(this->selectionModel()->currentIndex());
308 if (!index.isValid())
309 {
310 OGSError::box("No geometry selected.");
311 }
312 else
313 {
314 TreeItem* item = static_cast<GeoTreeModel*>(model())->getItem(index);
315 QString file_type("GeoSys geometry file (*.gml)");
316#ifndef NDEBUG
317 file_type.append(";;Legacy geometry file (*.gli)");
318#endif // DEBUG
319 QString geoName = item->data(0).toString();
320 QString fileName = QFileDialog::getSaveFileName(
321 nullptr, "Save geometry as",
322 LastSavedFileDirectory::getDir() + geoName, file_type);
323 if (!fileName.isEmpty())
324 {
326 emit saveToFileRequested(geoName, fileName);
327 }
328 }
329}
330
332{
333 TreeItem* item = static_cast<GeoTreeModel*>(model())->getItem(
334 this->selectionModel()->currentIndex());
335 emit loadFEMCondFileRequested(item->data(0).toString().toStdString());
336}
337/*
338void GeoTreeView::saveFEMConditions()
339{
340 TreeItem* item = static_cast<GeoTreeModel*>(model())->getItem(
341 this->selectionModel()->currentIndex());
342 QString fileName =
343 QFileDialog::getSaveFileName(nullptr, "Save FEM Conditions as", "",
344 "OpenGeoSys FEM Condition file (*.cnd);; "
345 "GeoSys Boundary Condition (*.bc);; "
346 "GeoSys Initial Condition (*.ic);; "
347 "GeoSys Source Condition (*.st)");
348 emit saveFEMConditionsRequested(item->data(0).toString(), fileName);
349}
350*/
vtkPolyDataAlgorithm * vtkSource() const
Returns the Vtk polydata source object.
GeoLib::GEOTYPE getType()
Returns the type of geo-objects contained in the subtree of this item.
A model for the GeoTreeView implementing a tree as a double-linked list.
GeoTreeView(QWidget *parent=nullptr)
Constructor.
void enableSaveButton(bool)
Saves FEM Conditions associated with the given geometry.
void requestPointToStationConversion(std::string const &)
void mapGeometry()
void setObjectAsCondition()
Definition GeoTreeView.h:54
void addGeometry()
void on_Clicked(QModelIndex idx)
void setNameForElement()
Calls a SetNameDialog.
void listRemoved(std::string name, GeoLib::GEOTYPE)
void writeToFile() const
Saves a geometry in a file.
void selectionChangedFromOutside(const QItemSelection &selected, const QItemSelection &deselected)
Instructions if the selection of items in the view has changed by events outside the view (i....
void geoItemSelected(const vtkPolyDataAlgorithm *, int)
void enableRemoveButton(bool)
void requestNameChangeDialog(const std::string &, const GeoLib::GEOTYPE, const std::size_t)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override
Instructions if the selection of items in the view has changed.
void saveToFileRequested(QString, QString) const
void setElementAsCondition(bool set_on_points=false)
Calls a FEMConditionSetupDialog.
void removeGeoItemSelection()
void connectPolylines()
Calls a LineEditDialog.
void convertPointsToStations()
void requestLineEditDialog(const std::string &)
void removeGeometry()
Removes a whole geometry or parts of it.
void openGeometryFile(int)
void contextMenuEvent(QContextMenuEvent *e) override
Actions to be taken after a right mouse click is performed in the station view.
void loadFEMCondFileRequested(std::string)
void geometryMappingRequested(const std::string &)
void loadFEMConditions()
Allows to add FEM Conditions to a process.
void updateView()
Update the view to visualise changes made to the underlying data.
void setObjectPointsAsCondition()
Definition GeoTreeView.h:55
void requestCondSetupDialog(const std::string &, const GeoLib::GEOTYPE, const std::size_t, bool on_points)
static void setDir(const QString &path)
Sets the directory last used for saving a file.
static const QString getDir()
Returns the directory last used for saving a file.
static void box(const QString &e)
Definition OGSError.cpp:13
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
int row() const
Definition TreeItem.cpp:62
virtual QVariant data(int column) const
Definition TreeItem.cpp:83
A hierarchical model for a tree implemented as a double-linked list.
Definition TreeModel.h:19
GEOTYPE
Definition GeoType.h:12