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