Vespucci  1.0.0
vespucciworkspace.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (C) 2014-2016 Wright State University - All Rights Reserved
3  Daniel P. Foose - Maintainer/Lead Developer
4 
5  This file is part of Vespucci.
6 
7  Vespucci is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Vespucci is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Vespucci. If not, see <http://www.gnu.org/licenses/>.
19 *******************************************************************************/
21 #include <iostream>
22 #include <QtSvg>
23 #include <H5Cpp.h>
24 
28 VespucciWorkspace::VespucciWorkspace(QString settings_file) :
29  settings_(settings_file, QSettings::IniFormat)
30 {
31  settings_.setIniCodec("UTF-8");
32  dataset_loading_count_ = 0;
33  directory_ = QDir::homePath();
34  CheckSettings();
35  data_model_ = new DataModel();
36 
37  gradients_ = {
38  {"ColorBrewer BlueGreen", QCPColorGradient::cbBlues},
39  {"ColorBrewer BluePurple", QCPColorGradient::cbBuPu},
40  {"ColorBrewer GreenBlue", QCPColorGradient::cbGnBu},
41  {"ColorBrewer OrangeRed", QCPColorGradient::cbOrRd},
42  {"ColorBrewer PurpleBlue", QCPColorGradient::cbPuBu},
43  {"ColorBrewer PurpleBlueGreen", QCPColorGradient::cbPuBuGn},
44  {"ColorBrewer PurpleRed", QCPColorGradient::cbPuRd},
45  {"ColorBrewer RedPurple", QCPColorGradient::cbRdPu},
46  {"ColorBrewer YellowGreen", QCPColorGradient::cbYlGn},
47  {"ColorBrewer YellowGreenBlue", QCPColorGradient::cbYlGnBu},
48  {"ColorBrewer YellowOrangeBrown", QCPColorGradient::cbYlOrBr},
49  {"ColorBrewer YellowOrangeRed", QCPColorGradient::cbYlOrRd},
50  {"ColorBrewer Blues", QCPColorGradient::cbBlues},
51  {"ColorBrewer Greens", QCPColorGradient::cbGreens},
52  {"ColorBrewer Oranges", QCPColorGradient::cbOranges},
53  {"ColorBrewer Purples", QCPColorGradient::cbPurples},
54  {"ColorBrewer Reds", QCPColorGradient::cbReds},
55  {"ColorBrewer Grayscale", QCPColorGradient::cbGreys},
56  {"QCustomPlot Grayscale", QCPColorGradient::gpGrayscale},
57  {"QCustomPlot Night", QCPColorGradient::gpNight},
58  {"QCustomPlot Candy", QCPColorGradient::gpCandy},
59  {"QCustomPlot Ion", QCPColorGradient::gpIon},
60  {"QCustomPlot Thermal", QCPColorGradient::gpThermal},
61  {"↔QCustomPlot Polar", QCPColorGradient::gpPolar},
62  {"↔QCustomPlot Spectrum", QCPColorGradient::gpSpectrum},
63  {"QCustomPlot Jet", QCPColorGradient::gpJet},
64  {"QCustomPlot Hues", QCPColorGradient::gpHues},
65  {"QCustomPlot Hot", QCPColorGradient::gpHot},
66  {"QCustomPlot Cold", QCPColorGradient::gpCold},
67  {"↔ColorBrewer BrownBlueGreen", QCPColorGradient::cbBrBG},
68  {"↔ColorBrewer PinkYellowGreen", QCPColorGradient::cbPiYG},
69  {"↔ColorBrewer PurpleGreen", QCPColorGradient::cbPRGn},
70  {"↔ColorBrewer PurpleOrange", QCPColorGradient::cbPuOr},
71  {"↔ColorBrewer RedBlue", QCPColorGradient::cbRdBu},
72  {"↔ColorBrewer RedGray", QCPColorGradient::cbRdGy},
73  {"↔ColorBrewer RedYellowBlue", QCPColorGradient::cbRdYlBu},
74  {"↔ColorBrewer RedYellowGreen", QCPColorGradient::cbRdYlGn},
75  {"↔ColorBrewer Spectral", QCPColorGradient::cbSpectral},
76  {"↔Vespucci Spectral", QCPColorGradient::vSpectral}
77  };
78 }
79 
84 {
85  cout << "Workspace destructor\n";
86 
87  delete data_model_;
88 }
89 
91 {
92  return data_model_->DatasetNames();
93 }
94 
101 {
102  main_window_ = main_window;
103  dataset_tree_model_ = tree_model;
104 }
105 
106 
107 // FUNCTIONS THAT ADD AND REMOVE WORKSPACE OBJECTS
108 
113 void VespucciWorkspace::AddDataset(QSharedPointer<VespucciDataset> dataset)
114 {
115  data_model_->AddDataset(dataset);
116  main_window_->RefreshTreeModel(data_model_);
117  ++dataset_loading_count_;
118 }
119 
128 {
129  data_model_->RemoveDataset(name);
130  main_window_->RefreshTreeModel(data_model_);
131 }
132 
137 {
138  main_window_->RefreshTreeModel(data_model_);
139 }
140 
141 
142 
143 // Useful when the index is known (as when the list is being iterated)
144 
150 
151 void VespucciWorkspace::RemoveDatasetAt(const QModelIndex &parent)
152 {
153  //we need to know how deep this thing goes
154  TreeItem *item = main_window_->dataset_tree_model()->getItem(parent);
155  QStringList keys = item->keys();
156  if (keys.size() == 1) RemoveDataset(keys[0]);
157  else return;
158 }
159 
160 QSharedPointer<VespucciDataset> VespucciWorkspace::DatasetAt(const QModelIndex &parent)
161 {
162  TreeItem *item = main_window_->dataset_tree_model()->getItem(parent);
163  QString dataset_key = item->keys()[0];
164  return data_model_->GetDataset(dataset_key);
165 }
166 
167 
168 
169 
170 //ACCESS FUNCTIONS
176 {
177  return dataset_loading_count_;
178 }
179 
185 {
186  return directory_;
187 }
188 
194 {
195  directory_ = directory;
196 }
197 
203 {
204  return main_window_;
205 }
206 
212 double VespucciWorkspace::GetWavelengthMax(const QString &key) const
213 {
214  double abscissa_max = 0;
215  try{
216  abscissa_max = data_model_->GetDataset(key)->abscissa_ptr()->max();
217  }catch(exception e){
218  main_window_->DisplayExceptionWarning("GetWavelengthMax", e);
219  }
220 
221  return abscissa_max;
222 
223 }
224 
230 QSharedPointer<VespucciDataset> VespucciWorkspace::GetDataset(const QString &key) const
231 {
232  return QSharedPointer<VespucciDataset>(data_model_->GetDataset(key));
233 }
234 
241 QSharedPointer<AnalysisResults> VespucciWorkspace::GetAnalysisResults(const QString &dataset_key, const QString &results_key) const
242 {
243  return data_model_->GetResults(dataset_key, results_key);
244 }
245 
246 QSharedPointer<MapData> VespucciWorkspace::GetMap(const QString &dataset_key, const QString &map_key) const
247 {
248  return data_model_->GetMap(dataset_key, map_key);
249 }
250 
251 QSharedPointer<MapData> VespucciWorkspace::GetMap(const QStringList &map_keys)
252 {
253  return data_model_->GetMap(map_keys);
254 }
255 
256 const mat & VespucciWorkspace::GetAuxiliaryMatrix(const QString &dataset_key, const QString &matrix_key) const
257 {
258  return data_model_->GetAuxiliaryMatrix(dataset_key, matrix_key);
259 }
260 
261 const mat & VespucciWorkspace::GetResultsMatrix(const QString &dataset_key, const QString &results_key, const QString &matrix_key) const
262 {
263  return data_model_->GetResultsMatrix(dataset_key, results_key, matrix_key);
264 }
265 
266 const mat & VespucciWorkspace::GetCoreMatrix(const QString &dataset_key, const QString &matrix_key) const
267 {
268  return data_model_->GetCoreMatrix(dataset_key, matrix_key);
269 }
270 
276 double VespucciWorkspace::GetWavelengthMin(const QString &key) const
277 {
278  double abscissa_min = 0;
279  try{
280  abscissa_min = data_model_->GetDataset(key)->abscissa_ptr()->min();
281  }catch(exception e){
282  main_window_->DisplayExceptionWarning("GetWavelengthMin", e);
283  }
284  return abscissa_min;
285 }
286 
292 {
293  return &directory_;
294 }
295 
296 
297 
305 {
306  if (global_gradients_.contains(name)) global_gradients_.remove(name);
307 }
308 
310 {
311  return global_gradients_.keys();
312 }
313 
314 QMap<QString, Vespucci::GlobalGradient> VespucciWorkspace::global_gradients()
315 {
316  return global_gradients_;
317 }
318 
320 {
321  if (key == "ColorBrewer Cluster"){
322  switch (count){
323  case 2: return QCPColorGradient::cbCluster2;
324  case 3: return QCPColorGradient::cbCluster3;
325  case 4: return QCPColorGradient::cbCluster4;
326  case 5: return QCPColorGradient::cbCluster5;
327  case 6: return QCPColorGradient::cbCluster6;
328  case 7: return QCPColorGradient::cbCluster7;
329  case 8: return QCPColorGradient::cbCluster8;
330  case 9: default: return QCPColorGradient::cbCluster9;
331  }
332  }
333  if (gradients_.contains(key))
334  return gradients_.value(key);
335  else
337 }
338 
340 {
341  return global_gradients_.value(key);
342 }
343 
344 QStringList VespucciWorkspace::GradientNames(bool include_cluster)
345 {
346  QStringList keys = gradients_.keys();
347  if (include_cluster)
348  keys.insert(0, "ColorBrewer Cluster");
349  return keys;
350 }
351 
352 
358 {
359  return &global_data_range_;
360 }
361 
367 {
368  return &global_color_gradient_;
369 }
370 
378 void VespucciWorkspace::AddGlobalGradient(QString name, QString gradient_key, double lower, double upper)
379 {
380  Vespucci::GlobalGradient gradient;
381  gradient.gradient = GetGradient(gradient_key);
382  gradient.range = QCPRange(lower, upper);
383  global_gradients_[name] = gradient;
384 }
385 
387 {
388  if (global_gradients_.contains(name)){
389  QList<QSharedPointer<MapData> > maps =
390  data_model_->GetMapsUsingColorRange(name);
391  if (maps.isEmpty()) return;
392  vec mins(maps.size());
393  vec maxes(maps.size());
394  for (uword i = 0; i < mins.n_elem; ++i){
395  mins(i) = maps[i]->min();
396  maxes(i) = maps[i]->max();
397  }
398  double new_min = mins.min();
399  double new_max = maxes.max();
400 
401  Vespucci::GlobalGradient gradient = global_gradients_.value(name);
402  gradient.range = QCPRange(new_min, new_max);
403 
404  for (auto map: maps){
405  map->SetGlobalGradient(name);
406  }
407  }
408 }
409 
414 {
415  main_window_->dataset_tree_model()->ClearDatasets();
416 }
417 
422 {
423  return ++dataset_loading_count_;
424 }
425 
431 {
432  return main_window_->dataset_tree_model();
433 }
434 
436 {
437  QString r_path;
438 #if defined Q_OS_WIN32
439  r_path = "C:/Program Files/R/R-3.1.3";
440 #elif defined Q_OS_MAC
441  r_path = "/Library/Frameworks/R.framework/Resources";
442 #else
443  r_path = "/usr/lib/R";
444 #endif
445  settings_.beginGroup("environment");
446  settings_.setValue("R_HOME", r_path);
447  settings_.endGroup();
448 
449  settings_.beginGroup("specdata");
450  settings_.setValue("absLabel", "Raman Shift");
451  settings_.setValue("absUnits", "cm⁻¹");
452  settings_.setValue("ordLabel", "Intensity");
453  settings_.setValue("ordUnits", "a.u.");
454  settings_.endGroup();
455 }
456 
458 {
459  QString r_path, r_home;
460 #if defined Q_OS_WIN32
461  r_path = "C:/Program Files/R/R-3.1.3";
462 #elif defined Q_OS_MAC
463  r_path = "/Library/Frameworks/R.framework/Resources";
464 #else
465  r_path = "/usr/lib/R";
466 #endif
467 
468  if (!settings_.childGroups().contains("environment")){
469  settings_.beginGroup("environment");
470  settings_.setValue("R_HOME", r_path);
471  settings_.endGroup();
472  }
473  else{
474  settings_.beginGroup("environment");
475  if (settings_.allKeys().contains("R_HOME")){
476  r_home = settings_.value("R_HOME").toString();
477  }
478  else{
479  r_home = r_path;
480  settings_.setValue("R_HOME", r_path);
481  }
482  settings_.endGroup();
483  }
484 
485  if (!settings_.childGroups().contains("specdata")){
486  settings_.beginGroup("specdata");
487  settings_.setValue("absLabel", "Raman Shift");
488  settings_.setValue("absUnits", "cm⁻¹");
489  settings_.setValue("ordLabel", "Intensity");
490  settings_.setValue("ordUnits", "a.u.");
491  settings_.endGroup();
492  }
493  else{
494  settings_.beginGroup("specdata");
495  if (!settings_.allKeys().contains("absLabel"))
496  settings_.setValue("absLabel", "Raman Shift");
497  if (!settings_.allKeys().contains("absUnits"))
498  settings_.setValue("absUnits", "cm⁻¹");
499  if (!settings_.allKeys().contains("ordLabel"))
500  settings_.setValue("ordLabel", "Intensity");
501  if (!settings_.allKeys().contains("ordUnits"))
502  settings_.setValue("ordUnits", "a.u.");
503  settings_.endGroup();
504  }
505 }
506 
508 {
509  return &settings_;
510 }
511 
513 {
514  return data_model_;
515 }
516 
522 const mat & VespucciWorkspace::GetMatrix(const QStringList &keys) const
523 {
524  if (keys.size() < 2) return data_model_->EmptyMatrix();
525  try{
526  return data_model_->GetMatrix(keys);
527  }catch(exception e){
528  main_window_->DisplayExceptionWarning("VespucciWorkspace::GetMatrix", e);
529  return data_model_->EmptyMatrix();
530  }
531 }
532 
533 bool VespucciWorkspace::HasMatrix(const QStringList &keys) const
534 {
535  return data_model_->HasMatrix(keys);
536 }
537 
538 bool VespucciWorkspace::Mappable(const QStringList &keys) const
539 {
540  return data_model_->Mappable(keys);
541 }
542 
543 bool VespucciWorkspace::Plottable(const QStringList &keys) const
544 {
545  try{
546  return data_model_->Plottable(keys);
547  }catch(exception e){
548  main_window_->DisplayExceptionWarning("VespucciWorkspace::Plottable", e);
549  return false;
550  }
551 }
552 
553 
554 void VespucciWorkspace::UpdateTreeModel()
555 {
556  main_window_->dataset_tree_model()->UpdateData(data_model_);
557 }
void RemoveDatasetAt(const QModelIndex &parent)
VespucciWorkspace::RemoveDatasetAt.
bool Plottable(const QStringList &keys) const
QStringList keys() const
Definition: treeitem.cpp:98
const mat & EmptyMatrix() const
Definition: datamodel.cpp:339
void set_directory(QString directory)
VespucciWorkspace::set_directory.
QSharedPointer< VespucciDataset > GetDataset(const QString &key) const
VespucciWorkspace::GetDataset.
QStringList dataset_names() const
void ClearDatasets()
VespucciWorkspace::ClearDatasets Clears the internal container for datasets in the list model...
unsigned int UpdateCount()
VespucciWorkspace::UpdateCount.
QSharedPointer< MapData > GetMap(const QString &dataset_key, const QString &map_key) const
DataModel::GetMap Get the MapData object named map_key from dataset named dataset_key.
Definition: datamodel.cpp:81
QStringList DatasetNames() const
DataModel::DatasetNames.
Definition: datamodel.cpp:266
DataModel * data_model()
Colors suitable for thermal imaging, ranging from dark blue over purple to orange, yellow and white.
Definition: qcustomplot.h:1930
void AddDataset(QSharedPointer< VespucciDataset > dataset)
VespucciWorkspace::AddDataset.
void DisplayExceptionWarning(std::exception e)
MainWindow::DisplayExceptionWarning.
Definition: mainwindow.cpp:845
QCPColorGradient gradient
Definition: global.h:30
const mat & GetAuxiliaryMatrix(const QString &dataset_key, const QString &matrix_key) const
DataModel::GetAuxiliaryMatrix.
Definition: datamodel.cpp:191
Vespucci::GlobalGradient GetGlobalGradient(QString key)
bool Mappable(const QStringList &keys) const
const mat & GetCoreMatrix(const QString &dataset_key, const QString &matrix_key) const
Hue variation similar to a spectrum, often used in numerical visualization (creates banding illusion ...
Definition: qcustomplot.h:1933
void UpdateData(const DataModel *data_model)
Continuous lightness from black over icey colors to white (suited for non-biased data representation)...
Definition: qcustomplot.h:1925
const mat & GetMatrix(const QStringList &keys) const
VespucciWorkspace::GetMatrix.
An approximation of the visible light spectrum (creates banding illusion but allows more precise magn...
Definition: qcustomplot.h:1932
bool Plottable(const QStringList &keys) const
Definition: datamodel.cpp:255
void AddDataset(QSharedPointer< VespucciDataset > dataset)
DataModel::AddDataset.
Definition: datamodel.cpp:320
MainWindow * main_window()
VespucciWorkspace::main_window.
QSharedPointer< VespucciDataset > DatasetAt(const QModelIndex &parent)
Half hue spectrum from black over purple to blue and finally green (creates banding illusion but allo...
Definition: qcustomplot.h:1929
void RemoveDataset(QString name)
VespucciWorkspace::RemoveDataset.
Continuous lightness from black over firey colors to white (suited for non-biased data representation...
Definition: qcustomplot.h:1924
QStringList GlobalGradientKeys()
QString * directory_ptr()
VespucciWorkspace::directory_ptr.
Continuous lightness from black to white (suited for non-biased data representation) ...
Definition: qcustomplot.h:1923
Blue over pink to white.
Definition: qcustomplot.h:1927
QCPRange * global_data_range()
VespucciWorkspace::global_data_range.
QSharedPointer< AnalysisResults > GetResults(const QString &dataset_key, const QString &results_key) const
DataModel::GetResults Get the AnalysisResults object named results_key from the dataset named dataset...
Definition: datamodel.cpp:49
QSharedPointer< VespucciDataset > GetDataset(const QString &key) const
DataModel::GetDataset Get the VespucciDataset named key.
Definition: datamodel.cpp:32
QMap< QString, Vespucci::GlobalGradient > global_gradients()
bool HasMatrix(const QStringList &keys) const
Definition: datamodel.cpp:351
TreeItem * getItem(const QModelIndex &index) const
int dataset_loading_count() const
VespucciWorkspace::dataset_loading_count.
QStringList GradientNames(bool include_cluster=false)
DatasetTreeModel * dataset_tree_model()
Definition: mainwindow.cpp:885
void SetPointers(MainWindow *main_window, DatasetTreeModel *tree_model)
VespucciWorkspace::SetPointers.
void AddGlobalGradient(QString name, QString gradient_key, double lower, double upper)
VespucciWorkspace::AddGlobalGradient.
const mat & GetCoreMatrix(const QString &dataset_key, const QString &matrix_key) const
DataModel::GetCoreMatrix.
Definition: datamodel.cpp:160
const mat & GetResultsMatrix(const QString &dataset_key, const QString &results_key, const QString &matrix_key) const
double GetWavelengthMax(const QString &key) const
VespucciWorkspace::GetWavelengthMax.
void RemoveColorRange(QString name)
VespucciWorkspace::RemoveColorRange.
Colors suitable to emphasize polarity around the center, with blue for negative, black in the middle ...
Definition: qcustomplot.h:1931
QSharedPointer< AnalysisResults > GetAnalysisResults(const QString &dataset_key, const QString &results_key) const
VespucciWorkspace::GetAnalysisResults.
Continuous lightness from black over weak blueish colors to white (suited for non-biased data represe...
Definition: qcustomplot.h:1926
QCPColorGradient * global_gradient()
VespucciWorkspace::global_gradient.
double GetWavelengthMin(const QString &key) const
VespucciWorkspace::GetWavelengthMin.
void RemoveDataset(const QString &name)
DataModel::RemoveDataset.
Definition: datamodel.cpp:333
bool HasMatrix(const QStringList &keys) const
VespucciWorkspace(QString settings_file)
VespucciWorkspace::VespucciWorkspace Constructor.
QCPColorGradient GetGradient(QString key, int count=0)
bool Mappable(const QStringList &keys) const
Definition: datamodel.cpp:242
QSharedPointer< MapData > GetMap(const QString &dataset_key, const QString &map_key) const
void UpdateModel()
VespucciWorkspace::UpdateModel Call when a VespucciDataset adds or removes a new object.
Defines a color gradient for use with e.g. QCPColorMap.
Definition: qcustomplot.h:1905
QString directory() const
VespucciWorkspace::directory.
Represents the range an axis is encompassing.
Definition: qcustomplot.h:481
The MainWindow class The main window of the program, this is where the user performs most operations...
Definition: mainwindow.h:58
~VespucciWorkspace()
VespucciWorkspace::~VespucciWorkspace Destructor.
Full hue cycle, with highest and lowest color red (suitable for periodic data, such as angles and pha...
Definition: qcustomplot.h:1934
const mat & GetMatrix(const QString &dataset_key, const QString &matrix_key) const
Definition: datamodel.cpp:215
void RefreshTreeModel(const DataModel *data_model)
MainWindow::RefreshTreeModel.
Definition: mainwindow.cpp:149
void RecalculateGlobalGradient(QString name)
QList< QSharedPointer< MapData > > GetMapsUsingColorRange(const QString &range_key)
Definition: datamodel.cpp:106
DatasetTreeModel * dataset_tree_model() const
dataset_list_model
const mat & GetAuxiliaryMatrix(const QString &dataset_key, const QString &matrix_key) const
const mat & GetResultsMatrix(const QString &dataset_key, const QString &results_key, const QString &matrix_key) const
Definition: datamodel.cpp:119