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