OGS
StationTreeView.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 "StationTreeView.h"
5
6#include <QFileDialog>
7#include <QMenu>
8
12#include "Base/OGSError.h"
15#include "GeoLib/Station.h"
16#include "ModelTreeItem.h"
17#include "StationTreeModel.h"
18
19StationTreeView::StationTreeView(QWidget* parent) : QTreeView(parent)
20{
21 // setContextMenuPolicy(Qt::CustomContextMenu);
22 // connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
23 // this, SLOT(showContextMenu(const QPoint &)));
24}
25
27{
28 setAlternatingRowColors(true);
29 setColumnWidth(0, 150);
30 setColumnWidth(1, 75);
31 setColumnWidth(2, 75);
32 setColumnWidth(3, 75);
33}
34
35void StationTreeView::selectionChanged(const QItemSelection& selected,
36 const QItemSelection& deselected)
37{
38 Q_UNUSED(deselected);
39 if (!selected.isEmpty())
40 {
41 const QModelIndex idx = *(selected.indexes().begin());
42 const TreeItem* tree_item =
43 static_cast<TreeModel*>(this->model())->getItem(idx);
44
45 const auto* list_item =
46 dynamic_cast<const ModelTreeItem*>(tree_item->parentItem());
47 if (list_item->getItem())
48 {
49 if (list_item)
50 {
51 emit geoItemSelected(list_item->getItem()->vtkSource(),
52 tree_item->row());
53 }
54 emit enableRemoveButton(false);
55 emit enableSaveButton(false);
56 }
57 else
58 {
60 emit enableSaveButton(true);
61 emit enableRemoveButton(true);
62 }
63 }
64 // emit itemSelectionChanged(selected, deselected);
65 // return QTreeView::selectionChanged(selected, deselected);
66}
67
69 const QItemSelection& selected, const QItemSelection& deselected)
70{
71 QItemSelectionModel* selModel = this->selectionModel();
72
73 selModel->blockSignals(true);
74 selModel->select(deselected, QItemSelectionModel::Deselect);
75 selModel->select(selected, QItemSelectionModel::Select);
76 selModel->blockSignals(false);
77
78 return QTreeView::selectionChanged(selected, deselected);
79}
80
81void StationTreeView::contextMenuEvent(QContextMenuEvent* event)
82{
83 QModelIndex index = this->selectionModel()->currentIndex();
84 auto* item = static_cast<ModelTreeItem*>(index.internalPointer());
85
86 if (!item)
87 { // Otherwise sometimes it crashes when (unmotivated ;-) ) clicking in a
88 // treeview
89 return;
90 }
91
92 // The current index refers to a parent item (e.g. a listname)
93 if (item->childCount() > 0)
94 {
95 QMenu menu;
96 QAction* mapAction = menu.addAction("Map stations...");
97 QAction* exportAction = menu.addAction("Export to GMS...");
98 menu.addSeparator();
99
100 connect(mapAction, SIGNAL(triggered()), this, SLOT(mapStations()));
101 connect(exportAction, SIGNAL(triggered()), this, SLOT(exportList()));
102 menu.exec(event->globalPos());
103 }
104 // The current index refers to a station object
105 else
106 {
107 QString temp_name;
108 QMenu menu;
109
110 QAction* setNameAction = menu.addAction("Set name...");
111 connect(setNameAction, SIGNAL(triggered()), this,
112 SLOT(setNameForElement()));
113 if (dynamic_cast<GeoLib::StationBorehole*>(
114 static_cast<StationTreeModel*>(model())->stationFromIndex(
115 index, temp_name)))
116 {
117 QAction* stratAction = menu.addAction("Display Stratigraphy...");
118 QAction* exportAction = menu.addAction("Export to GMS...");
119 connect(stratAction, SIGNAL(triggered()), this,
120 SLOT(displayStratigraphy()));
121 connect(exportAction, SIGNAL(triggered()), this,
122 SLOT(exportStation()));
123 menu.exec(event->globalPos());
124 }
125 else
126 {
127 QAction* showDiagramAction = menu.addAction("View Diagram...");
128 connect(showDiagramAction, SIGNAL(triggered()), this,
129 SLOT(showDiagramPrefsDialog()));
130 menu.exec(event->globalPos());
131 }
132 }
133}
134
136{
137 TreeItem const* const item =
138 static_cast<StationTreeModel*>(model())->getItem(
139 this->selectionModel()->currentIndex());
140 std::string const stn_vec_name =
141 item->parentItem()->data(0).toString().toStdString();
142 emit requestNameChangeDialog(stn_vec_name, item->row());
143}
144
146{
147 TreeItem const* const item =
148 static_cast<StationTreeModel*>(model())->getItem(
149 this->selectionModel()->currentIndex());
150 std::string const& geo_name(item->data(0).toString().toStdString());
151 emit geometryMappingRequested(geo_name);
152}
153
155{
156 QModelIndex index = this->selectionModel()->currentIndex();
157
158 QString temp_name;
159 // get list name
160 static_cast<StationTreeModel*>(model())->stationFromIndex(
161 this->selectionModel()->currentIndex(), temp_name);
162 // get color table (horrible way to do it but there you go ...)
163 std::map<std::string, DataHolderLib::Color> colorLookupTable =
164 static_cast<VtkStationSource*>(
165 static_cast<StationTreeModel*>(model())->vtkSource(
166 temp_name.toStdString()))
168 auto* stratView = new StratWindow(
169 static_cast<GeoLib::StationBorehole*>(
170 static_cast<StationTreeModel*>(model())->stationFromIndex(
171 index, temp_name)),
172 &colorLookupTable);
173 stratView->setAttribute(
174 Qt::WA_DeleteOnClose); // this fixes the memory leak shown by cppcheck
175 stratView->show();
176}
177
182
184{
185 QModelIndex index(this->selectionModel()->currentIndex());
186 if (!index.isValid())
187 {
188 OGSError::box("No station list selected.");
189 }
190 else
191 {
192 TreeItem* item =
193 static_cast<StationTreeModel*>(model())->getItem(index);
194 QString listName = item->data(0).toString();
195 QString fileName = QFileDialog::getSaveFileName(
196 this, "Save station list",
197 LastSavedFileDirectory::getDir() + listName, "*.stn");
198 if (!fileName.isEmpty())
199 {
201 emit stationListSaved(listName, fileName);
202 }
203 }
204}
205
207{
208 // only a test for the stratigraphy screenshot tool and writer!!
209 // QString Name =
210 // static_cast<StationTreeModel*>(model())->getItem(this->selectionModel()->currentIndex())->data(0).toString();
211 // writeStratigraphiesAsImages(Name);
212
213 TreeItem* item = static_cast<StationTreeModel*>(model())->getItem(
214 this->selectionModel()->currentIndex());
215 QString listName = item->data(0).toString();
216 QString fileName = QFileDialog::getSaveFileName(
217 this, "Export Boreholes to GMS-Format",
218 LastSavedFileDirectory::getDir() + listName, "*.txt");
219 if (!fileName.isEmpty())
220 {
222 emit stationListExportRequested(listName.toStdString(),
223 fileName.toStdString());
224 }
225}
226
228{
229 QModelIndex index = this->selectionModel()->currentIndex();
230 QString fileName =
231 QFileDialog::getSaveFileName(this, "Export Borehole to GMS-Format",
233 if (!fileName.isEmpty())
234 {
235 QString temp_name;
236 std::vector<GeoLib::Point*> stations;
237 stations.push_back(static_cast<GeoLib::StationBorehole*>(
238 static_cast<StationTreeModel*>(model())->stationFromIndex(
239 index, temp_name)));
241 fileName.toStdString());
243 }
244}
245
247{
248 QModelIndex index(this->selectionModel()->currentIndex());
249 if (!index.isValid())
250 {
251 OGSError::box("No station list selected.");
252 }
253 else
254 {
255 TreeItem* item =
256 static_cast<StationTreeModel*>(model())->getItem(index);
257 emit stationListRemoved((item->data(0).toString()).toStdString());
258
259 if (this->selectionModel()->selectedIndexes().count() == 0)
260 {
261 emit enableSaveButton(false);
262 emit enableRemoveButton(false);
263 }
264 }
265}
266
268{
269 QModelIndex index = this->selectionModel()->currentIndex();
270 emit diagramRequested(index);
271}
272
274{
275 std::map<std::string, DataHolderLib::Color> colorLookupTable =
276 static_cast<VtkStationSource*>(
277 static_cast<StationTreeModel*>(model())->vtkSource(
278 listName.toStdString()))
280 std::vector<ModelTreeItem*> lists =
281 static_cast<StationTreeModel*>(model())->getLists();
282 std::size_t nLists = lists.size();
283 for (std::size_t i = 0; i < nLists; i++)
284 {
285 if (listName.compare(lists[i]->data(0).toString()) != 0)
286 {
287 continue;
288 }
289
290 std::vector<GeoLib::Point*> const& stations =
291 *dynamic_cast<BaseItem*>(lists[i]->getItem())->getStations();
292
293 for (auto station : stations)
294 {
295 auto* stratView =
296 new StratWindow(static_cast<GeoLib::StationBorehole*>(station),
297 &colorLookupTable);
298 stratView->setAttribute(Qt::WA_DeleteOnClose);
299 stratView->show();
300 stratView->stationView->saveAsImage(
301 QString::fromStdString(
302 static_cast<GeoLib::StationBorehole*>(station)->getName()) +
303 ".jpg");
304 stratView->close();
305 }
306 }
307}
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
const std::vector< GeoLib::Point * > * getStations()
Definition BaseItem.h:44
static void writeBoreholesToGMS(const std::vector< GeoLib::Point * > *stations, const std::string &filename)
A borehole as a geometric object.
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.
A TreeItem containing some additional information used in the StationModel.
static void box(const QString &e)
Definition OGSError.cpp:13
A model for the StationTreeView implementing a tree as a double-linked list.
vtkPolyDataAlgorithm * vtkSource(const std::string &name) const
void updateView()
Update the view to visualise changes made to the underlying data.
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 requestNameChangeDialog(const std::string &, std::size_t)
void openStationListFile(int)
void stationListExportRequested(std::string listName, std::string fileName)
void geometryMappingRequested(const std::string &)
void geoItemSelected(const vtkPolyDataAlgorithm *, int)
void diagramRequested(QModelIndex &)
void enableRemoveButton(bool)
void contextMenuEvent(QContextMenuEvent *e) override
Actions to be taken after a right mouse click is performed in the station view.
void setNameForElement()
Calls a SetNameDialog.
void stationListSaved(QString listName, QString fileName)
void removeGeoItemSelection()
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override
Instructions if the selection of items in the view has changed.
StationTreeView(QWidget *parent=nullptr)
Constructor.
void enableSaveButton(bool)
void writeStratigraphiesAsImages(QString listName)
Create image files from all stratigraphies in a borehole vector.
void stationListRemoved(std::string name)
Creates a window to visualise the stratigraphy of a borehole.
Definition StratWindow.h:18
Objects nodes for the TreeModel.
Definition TreeItem.h:17
TreeItem * parentItem() const
Definition TreeItem.cpp:104
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
VTK source object for the visualisation of station data (including boreholes)
const std::map< std::string, DataHolderLib::Color > & getColorLookupTable() const