OGS
DiagramPrefsDialog.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
5
6#include <QCheckBox>
7#include <QFileDialog>
8#include <QMessageBox>
9
10#include "Base/OGSError.h"
11#include "DetailWindow.h"
12#include "DiagramList.h"
13#include "GeoLib/Station.h"
14#include "GetDateTime.h"
15
17 const QString& listName,
18 // DatabaseConnection* db,
19 QDialog* parent)
20 : QDialog(parent), _window(nullptr)
21{
22 setAttribute(Qt::WA_DeleteOnClose);
23
24 setupUi(this);
25 stationNameLabel->setText(QString::fromStdString(stn->getName()));
26 stationTypeLabel->setText(listName);
27}
28
30 : QDialog(parent), _window(nullptr)
31{
32 setupUi(this);
33 stationNameLabel->setText(QString::fromStdString(stn->getName()));
34 stationTypeLabel->setText("");
36
37 fromDateLine->setText(
38 QString::number(stn->getSensorData()->getStartTime()));
39 toDateLine->setText(QString::number(stn->getSensorData()->getEndTime()));
41}
42
44 DetailWindow* window,
45 QDialog* parent)
46 : QDialog(parent), _window(window)
47{
48 QFileInfo fi(filename);
49 setupUi(this);
50 stationNameLabel->setText(fi.baseName());
51 stationTypeLabel->setText("");
52 this->loadFile(filename);
53}
54
56{
57 this->destroy();
58}
59
61{
62 QDateTime start_date(getDateTime(fromDateLine->text()));
63 QDateTime end_date(getDateTime(toDateLine->text()));
64
65 if (start_date == QDateTime() || end_date == QDateTime() ||
66 start_date > end_date || _list.empty())
67 {
68 OGSError::box("No data found...");
69 return;
70 }
71
72 if (_list[0]->size() == 0)
73 {
74 OGSError::box("Invalid station data.");
75 this->done(QDialog::Rejected);
76 }
77
78 // Data has been loaded.
79 // If loading lists beyond the first one fails at least nothing terrible
80 // will happen.
81 bool window_is_empty(false);
82 if (_window == nullptr)
83 {
84 _window = new DetailWindow();
85 _window->setAttribute(Qt::WA_DeleteOnClose);
86 window_is_empty = true;
87 }
88
89 for (std::size_t i = 0; i < _list.size(); i++)
90 {
91 if (_visability[i]->isChecked())
92 {
93 _list[i]->truncateToRange(start_date, end_date);
94 _window->addList(_list[i]);
95 window_is_empty = false;
96 }
97 }
98
99 if (!window_is_empty)
100 {
101 _window->show();
102 this->done(QDialog::Accepted);
103 }
104 else
105 {
106 delete _window;
107 _window = nullptr;
108 OGSError::box("No dataset selected.");
109 }
110}
111
113{
114 this->done(QDialog::Rejected);
115}
116
118{
119 QString fileName =
120 QFileDialog::getOpenFileName(this,
121 "Select time series file to open",
122 "",
123 "Time series files (*.stn *.txt)");
124 if (!fileName.isEmpty())
125 {
126 loadFile(fileName);
127 }
128}
129
130int DiagramPrefsDialog::loadFile(const QString& filename)
131{
132 if (DiagramList::readList(filename, _list))
133 {
134 for (auto& item : _list)
135 {
136 // item->setName(stationTypeLabel->text() + ": " +
137 // stationNameLabel->text());
138 item->setXLabel("Time");
139 // item->setYLabel("Water Level");
140 item->setXUnit("day");
141 // item->setYUnit("metres");
142 item->setColor(QColor(Qt::red));
143 }
144 fromDateLine->setText(_list[0]->getStartDate().toString("dd.MM.yyyy"));
145 QDateTime endDate = _list[0]->getStartDate().addSecs(
146 static_cast<int>(_list[0]->maxXValue()));
147 toDateLine->setText(endDate.toString("dd.MM.yyyy"));
149 return 1;
150 }
151
152 OGSError::box("Error reading file.");
153 return 0;
154}
155
157 const std::vector<std::pair<QDateTime, float>>& coords)
158{
159 if (!coords.empty())
160 {
161 auto* l = new DiagramList;
162 l->setName(stationTypeLabel->text() + ": " + stationNameLabel->text());
163 l->setXLabel("Time");
164 // l->setYLabel("Water Level");
165 l->setXUnit("day");
166 // l->setYUnit("metres");
167 l->setColor(QColor(Qt::red));
168 l->setList(coords);
169 _list.push_back(l);
170 return 1;
171 }
172 return 0;
173}
174
176{
177 for (auto& item : _list)
178 {
179 QCheckBox* box = new QCheckBox(item->getName());
180 box->setChecked(true);
181 this->CheckBoxLayout->addWidget(box);
182 _visability.push_back(box);
183 }
184}
QDateTime getDateTime(QString const &stringDate)
Converts string into QDateTime-format.
Definition GetDateTime.h:11
Creates a window containing a diagram.
A List of data points and all the necessary meta-information to draw a graph.
Definition DiagramList.h:18
static int readList(const QString &path, std::vector< DiagramList * > &list)
void setName(QString name)
Sets the name of the graph to be displayed in the caption.
Definition DiagramList.h:95
void on_loadFileButton_clicked()
Instructions if the "Load File"-Button has been pressed.
DiagramPrefsDialog(const GeoLib::Station *stn, const QString &listName, QDialog *parent=nullptr)
void reject() override
Instructions if the Cancel-Button has been pressed.
int loadFile(const QString &filename)
int loadList(const std::vector< std::pair< QDateTime, float > > &coords)
std::vector< QCheckBox * > _visability
std::vector< DiagramList * > _list
A Station (observation site) is basically a Point with some additional information.
Definition Station.h:26
std::string const & getName() const
Returns the name of the station.
Definition Station.h:49
const SensorData * getSensorData() const
Returns all the sensor data for this observation site.
Definition Station.h:78
static void box(const QString &e)
Definition OGSError.cpp:13
std::size_t getEndTime() const
Returns the last time step.
Definition SensorData.h:93
std::size_t getStartTime() const
Returns the first time step.
Definition SensorData.h:90