Vespucci  1.0.0
mapdata.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 *******************************************************************************/
20 #include "Data/Imaging/mapdata.h"
21 #include "Global/global.h"
22 #include <QtSvg>
23 
24 
29 MapData::MapData(QString name,
30  QString type,
31  QStringList data_keys,
32  uword data_column,
33  QSharedPointer<VespucciWorkspace> workspace)
34  :name_(name), type_(type)
35 {
36  workspace_ = workspace;
37  if (workspace_->GetMatrix(data_keys).n_cols <= data_column)
38  throw invalid_argument("Requested column out of bounds");
39  data_keys_ = data_keys;
40  data_column_ = data_column;
41  dataset_ = workspace_->GetDataset(data_keys.first());
42  QStringList map_keys = {dataset_->name(), name_};
43 
44  map_display_ = new MapViewer(workspace_->main_window(), map_keys, workspace_);
45  map_display_->setWindowTitle(name_);
46  map_qcp_ = map_display_->mapPlot();
47  vec x = dataset_->x();
48  vec y = dataset_->y();
49  vec z = workspace_->GetMatrix(data_keys_).col(data_column_);
50  map_qcp_->SetMapData(x, y, z);
51  vec unique_z = unique(z);
52  if (unique_z.n_rows < 10) map_qcp_->SetClusterTicks(unique_z.n_rows);
53 }
54 
56 {
57  delete map_display_;
58 }
59 
64 QString MapData::name()
65 {
66  return name_;
67 }
68 
73 QString MapData::type()
74 {
75  return type_;
76 }
77 
79 {
80  map_qcp_ = plot;
81 }
82 
83 
89 void MapData::SetName(QString name, QString type)
90 {
91  name_ = name;
92  type_ = type;
93  QString window_title = dataset_->name() + "—" + name + " " + type;
94  map_display_->setWindowTitle(window_title);
95 }
96 
98 {
99  if (workspace_->GlobalGradientKeys().contains(name)){
100  global_gradient_key_ = name;
102  }
103 }
104 
106 {
107  Vespucci::GlobalGradient gradient = workspace_->GetGlobalGradient(global_gradient_key_);
108  map_qcp_->setDataRange(gradient.range);
109  map_qcp_->SetGradient(gradient.gradient);
110 }
111 
112 QStringList MapData::keys()
113 {
114  return QStringList({dataset_->name(), name_});
115 }
116 
118 {
119  return map_qcp_;
120 }
121 
125 void MapData::ShowMapWindow(bool show)
126 {
127  map_display_->setVisible(show);
128  map_display_->raise();
129 }
130 
135 {
136  if(map_display_->isVisible())
137  map_display_->close();
138 }
139 
145 {
146  return map_display_->isVisible();
147 }
148 
154 void MapData::CreateImage(QCPColorGradient color_scheme, bool interpolation, int tick_count)
155 {
156  map_qcp_->SetGradient(color_scheme);
157  int key_size = map_qcp_->keySize();
158  int value_size = map_qcp_->valueSize();
159  key_size *= 9;
160  value_size *= 9;
161  map_qcp_->resize(key_size, value_size);
162  key_size +=50;
163  value_size +=50;
164  map_display_->resize(key_size, value_size);
165 }
166 
171 void MapData::SetXDescription(QString description)
172 {
173  x_axis_description_ = description;
174 }
175 
180 void MapData::SetYDescription(QString description)
181 {
182  y_axis_description_ = description;
183 }
184 
190 {
191  return map_qcp_->interpolate();
192 }
193 
198 void MapData::setInterpolate(bool enabled)
199 {
200  map_qcp_->setInterpolate(enabled);
201  map_qcp_->replot(QCustomPlot::rpImmediate);
202 }
203 
208 void MapData::ShowColorScale(bool enabled)
209 {
210  map_qcp_->ColorScaleVisible(enabled);
211 }
212 
217 void MapData::ShowAxes(bool enabled)
218 {
219  if (enabled){
220  if (!map_qcp_->xAxis->visible()){
221  map_qcp_->xAxis->setVisible(true);
222  map_qcp_->yAxis->setVisible(true);
223  map_qcp_->replot(QCustomPlot::rpImmediate);
224  map_qcp_->repaint();
225  }
226  }
227 
228  else{
229  if (map_qcp_->xAxis->visible()){
230  map_qcp_->xAxis->setVisible(false);
231  map_qcp_->yAxis->setVisible(false);
232  map_qcp_->replot(QCustomPlot::rpImmediate);
233  map_qcp_->repaint();
234  }
235  }
236 }
237 
246 bool MapData::saveBmp(const QString &fileName, int width, int height, double scale)
247 {
248  map_qcp_->setBackground(Qt::white);
249  map_qcp_->ShowCrosshairs(false);
250  map_qcp_->replot(QCustomPlot::rpImmediate);
251  bool success = map_qcp_->saveBmp(fileName, width, height, scale);
252 
253  map_qcp_->setBackground(map_display_->palette().window());
254  map_qcp_->ShowCrosshairs(true);
255  map_qcp_->replot(QCustomPlot::rpImmediate);
256  return success;
257 }
258 
259 
268 bool MapData::savePdf(const QString &fileName, int width, int height)
269 {
270  map_qcp_->ShowCrosshairs(false);
271  map_qcp_->replot(QCustomPlot::rpImmediate);
272  bool success = map_qcp_->savePdf(fileName, true, width, height, "Vespucci", name());
273  map_qcp_->ShowCrosshairs(true);
274  map_qcp_->replot(QCustomPlot::rpImmediate);
275  return success;
276 }
277 
287 bool MapData::savePng(const QString &fileName, int width, int height, double scale, int quality)
288 {
289  map_qcp_->setBackground(Qt::transparent);
290  map_qcp_->ShowCrosshairs(false);
291  map_qcp_->replot(QCustomPlot::rpImmediate);
292  bool success = map_qcp_->savePng(fileName, width, height, scale, quality);
293  map_qcp_->setBackground(map_display_->palette().window());
294  map_qcp_->ShowCrosshairs(true);
295  map_qcp_->replot(QCustomPlot::rpImmediate);
296  return success;
297 }
298 
308 bool MapData::saveJpg(const QString &fileName, int width, int height, double scale, int quality)
309 {
310  map_qcp_->setBackground(Qt::white);
311  map_qcp_->ShowCrosshairs(false);
312  map_qcp_->replot(QCustomPlot::rpImmediate);
313  bool success = map_qcp_->saveJpg(fileName, width, height, scale, quality);
314  map_qcp_->setBackground(map_display_->palette().window());
315  map_qcp_->ShowCrosshairs(true);
316  map_qcp_->replot(QCustomPlot::rpImmediate);
317  return success;
318 }
319 
329 bool MapData::saveTiff(const QString &fileName, int width, int height, double scale, int quality)
330 {
331  map_qcp_->setBackground(Qt::transparent);
332  map_qcp_->ShowCrosshairs(false);
333  map_qcp_->replot(QCustomPlot::rpImmediate);
334  bool success = map_qcp_->saveRastered(fileName, width, height, scale, "TIF", quality);
335  map_qcp_->setBackground(map_display_->palette().window());
336  map_qcp_->ShowCrosshairs(true);
337  map_qcp_->replot(QCustomPlot::rpImmediate);
338  return success;
339 }
340 
341 
342 
348 {
349  global_gradient_key_ = QString();
350  map_qcp_->SetGradient(gradient);
351  map_qcp_->rescaleDataRange();
352  map_qcp_->replot(QCustomPlot::rpImmediate);
353 }
354 
356 {
357  map_qcp_->SetColorScaleTickCount(ticks);
358 }
359 
369 void MapData::DrawScaleBar(double width,
370  double height,
371  QString units,
372  QColor color,
373  QString position,
374  QFont font)
375 {
376  //instantiate shape
377 
378  //QCustomPlot object will take ownership of these and make sure they're deleted.
379  QCPItemRect *scale_bar = new QCPItemRect(map_qcp_);
380  QCPItemText *scale_bar_text = new QCPItemText(map_qcp_);
381 
382 
383  map_qcp_->addItem(scale_bar);
384  map_qcp_->addItem(scale_bar_text);
385 
386  QString text = QString::number(width) + " " + units;
387  scale_bar_text->setText(text);
388  scale_bar_text->setPositionAlignment(Qt::AlignCenter);
389  scale_bar_text->setColor(color);
390  scale_bar_text->setFont(font);
391 
392  QPen pen(color);
393  QBrush brush(color, Qt::SolidPattern);
394  scale_bar->setPen(pen);
395  scale_bar->setBrush(brush);
396  QCPRange key_range = map_qcp_->keyRange();
397  QCPRange value_range = map_qcp_->valueRange();
398 
399  //find corners of image
400  double key_max = key_range.upper;
401  double key_min = key_range.lower;
402  double value_max = value_range.upper;
403  double value_min = value_range.lower;
404 
405  double x_min, y_min, x_max, y_max;
406 
407  if (position == "Top Left"){
408  x_min = key_min + 2.0;
409  x_max = x_min + width;
410  y_max = value_max - 4.0;
411  y_min = y_max - height;
412  }
413 
414  else if (position == "Top Right"){
415  x_max = key_max - 2.0;
416  x_min = x_max - width;
417  y_max = value_max - 4.0;
418  y_min = y_max - height;
419  }
420 
421  else if (position == "Bottom Left"){
422  x_min = key_min + 2.0;
423  x_max = x_min + width;
424  y_min = value_min + 2.0;
425  y_max = y_min + height;
426  }
427 
428  else{
429  x_max = key_max - 2.0;
430  x_min = x_max - width;
431  y_min = value_min + 2.0;
432  y_max = y_min + height;
433  }
434 
435  scale_bar->bottomRight->setCoords(x_max, y_min);
436  scale_bar->topLeft->setCoords(x_min, y_max);
437  double x_mid = ((x_max - x_min) / 2.0) + x_min;
438  double y_text = y_max + 1;
439  if (font.pointSize() >= 32){
440  y_text += (font.pointSize() - 32) / 4;
441  }
442 
443  scale_bar_text->position->setCoords(x_mid, y_text);
444 
445  map_qcp_->replot(QCustomPlot::rpImmediate);
446 }
447 
455 {
456  map_qcp_->setDataRange(new_range);
457 }
458 
465 {
466  double width_ratio = map_qcp_->width()/initial_map_size_.width();
467  double height_ratio = map_qcp_->height()/initial_map_size_.height();
468  double max_ratio = std::max(width_ratio, height_ratio);
469  double new_height = max_ratio * initial_map_size_.height();
470  double new_width = max_ratio * initial_map_size_.width();
471  map_qcp_->resize(new_width, new_height);
472  map_display_->resize(new_width + 50, new_height + 50);
473  return;
474 }
475 
481 {
482  if (lock)
483  map_display_->setFixedSize(map_display_->size());
484  else
485  map_display_->setFixedSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
486 }
487 
492 {
493  map_qcp_->resize(initial_map_size_);
494  map_display_->resize(initial_map_size_.width() + 50, initial_map_size_.height() + 50);
495  return;
496 }
497 
498 
499 void MapData::SetFonts(const QFont &font)
500 {
501  map_qcp_->SetFonts(font);
502 }
503 
505 {
506  return global_gradient_key_;
507 }
508 
513 double MapData::min()
514 {
515  return map_qcp_->min();
516 }
517 
518 double MapData::max()
519 {
520  return map_qcp_->max();
521 }
bool saveBmp(const QString &fileName, int width=0, int height=0, double scale=1.0)
MapData::saveBmp.
Definition: mapdata.cpp:246
~MapData()
Definition: mapdata.cpp:55
void SetGradient(QCPColorGradient gradient)
Definition: mapplot.cpp:119
void ColorScaleVisible(bool visible)
Definition: mapplot.cpp:275
void DrawScaleBar(double width, double height, QString units, QColor color, QString position, QFont font)
MapData::DrawScaleBar.
Definition: mapdata.cpp:369
void SetMapPlot(MapPlot *plot)
Definition: mapdata.cpp:78
MapPlot * map_qcp()
Definition: mapdata.cpp:117
void UpdateGlobalGradient()
Definition: mapdata.cpp:105
bool savePng(const QString &fileName, int width=0, int height=0, double scale=1.0, int quality=-1)
void SetYDescription(QString description)
MapData::SetYDescription.
Definition: mapdata.cpp:180
void RescaleMapWidget()
MapData::RescaleMapWidget Changes the proportions of the map window back to the initial proportions...
Definition: mapdata.cpp:464
QCPItemPosition *const topLeft
Definition: qcustomplot.h:3445
bool saveJpg(const QString &fileName, int width=0, int height=0, double scale=1.0, int quality=-1)
MapData::saveJpg.
Definition: mapdata.cpp:308
void SetFonts(const QFont &font)
Definition: mapdata.cpp:499
bool visible() const
Definition: qcustomplot.h:428
void ResetMapWidgetSize()
MapData::ResetMapWidgetSize Returns the size of the map window to the original proportions.
Definition: mapdata.cpp:491
void setGradient(const QCPColorGradient &gradient)
MapData::setGradient.
Definition: mapdata.cpp:347
void SetMapData(const vec &x, const vec &y, const vec &z)
MapPlot::SetMapData.
Definition: mapplot.cpp:77
bool saveRastered(const QString &fileName, int width, int height, double scale, const char *format, int quality=-1)
void SetFonts(const QFont &font)
Definition: mapplot.cpp:286
double min()
MapData::min.
Definition: mapdata.cpp:513
double min() const
Definition: mapplot.cpp:192
QCPColorGradient gradient
Definition: global.h:30
VESPUCCI_EXPORT arma::uword max(arma::uword a, arma::uword b)
Vespucci::Math::max.
Definition: accessory.cpp:237
void setInterpolate(bool interpolate)
Definition: mapplot.cpp:353
QCPAxis * xAxis
Definition: qcustomplot.h:1824
bool interpolate() const
Definition: mapplot.cpp:358
void setCoords(double key, double value)
void setInterpolate(bool enabled)
MapData::setInterpolate.
Definition: mapdata.cpp:198
void SetDataRange(QCPRange new_range)
MapData::SetDataRange.
Definition: mapdata.cpp:454
void setColor(const QColor &color)
bool saveBmp(const QString &fileName, int width=0, int height=0, double scale=1.0)
QString type()
MapData::type.
Definition: mapdata.cpp:73
bool savePdf(const QString &fileName, int width, int height)
MapData::savePDF.
Definition: mapdata.cpp:268
void setBackground(const QPixmap &pm)
Q_SLOT void replot(QCustomPlot::RefreshPriority refreshPriority=QCustomPlot::rpHint)
void ShowCrosshairs(bool show)
Definition: mapplot.cpp:383
bool savePng(const QString &fileName, int width=0, int height=0, double scale=1.0, int quality=-1)
MapData::savePng.
Definition: mapdata.cpp:287
VESPUCCI_EXPORT void position(arma::uword index, arma::uword n_rows, arma::uword n_cols, arma::uword &i, arma::uword &j)
Vespucci::Math::position Find row and column numbers for index.
Definition: accessory.cpp:469
void HideMapWindow()
MapData::HideMapWindow Closes the map window.
Definition: mapdata.cpp:134
bool addItem(QCPAbstractItem *item)
void ShowMapWindow(bool show)
MapData::ShowMapWindow Shows or hides the map window.
Definition: mapdata.cpp:125
QString global_gradient_key()
Definition: mapdata.cpp:504
QStringList keys()
Definition: mapdata.cpp:112
double max()
Definition: mapdata.cpp:518
A rectangle.
Definition: qcustomplot.h:3417
void SetClusterTicks(size_t count)
Definition: mapplot.cpp:334
void setDataRange(const QCPRange &dataRange)
Definition: mapplot.cpp:268
void SetColorScaleTickCount(int ticks)
Definition: mapplot.cpp:319
bool MapWindowVisible()
MapData::MapWindowVisible.
Definition: mapdata.cpp:144
The MapPlot class A subclass of QCustomPlot for handling a specfic kind of color map. Provides a wrapper for a QCPColorScale and a QCPColorMap which are child widgets.
Definition: mapplot.h:33
void setVisible(bool on)
void ShowColorScale(bool enabled)
MapData::ShowColorScale.
Definition: mapdata.cpp:208
The MapViewer class Displays the image created by MapData.
Definition: mapviewer.h:45
void CreateImage(QCPColorGradient color_scheme, bool interpolation, int tick_count)
MapData::CreateImage.
Definition: mapdata.cpp:154
MapData(QString name, QString type, QStringList data_keys, uword data_column, QSharedPointer< VespucciWorkspace > workspace)
MapData::~MapData Deletes everything the new keyword is used on in this object. Destructor triggered ...
Definition: mapdata.cpp:29
QCPRange valueRange()
Definition: mapplot.cpp:378
void setBrush(const QBrush &brush)
bool savePdf(const QString &fileName, bool noCosmeticPen=false, int width=0, int height=0, const QString &pdfCreator=QString(), const QString &pdfTitle=QString())
bool saveJpg(const QString &fileName, int width=0, int height=0, double scale=1.0, int quality=-1)
The QCustomPlot surface is immediately refreshed, by calling QWidget::repaint() after the replot...
Definition: qcustomplot.h:1715
void SetColorScaleTickCount(int ticks)
Definition: mapdata.cpp:355
void ShowAxes(bool enabled)
MapData::ShowAxes.
Definition: mapdata.cpp:217
void LockMapDisplaySize(bool lock)
MapData::LockMapDisplaySize.
Definition: mapdata.cpp:480
int valueSize()
Definition: mapplot.cpp:368
double max() const
Definition: mapplot.cpp:197
void rescaleDataRange(bool onlyVisibleMaps=false)
Definition: mapplot.cpp:263
A text label.
Definition: qcustomplot.h:3471
QCPRange keyRange()
Definition: mapplot.cpp:373
void SetXDescription(QString description)
MapData::SetXDescription.
Definition: mapdata.cpp:171
QCPItemPosition *const position
Definition: qcustomplot.h:3526
MapPlot * mapPlot()
Definition: mapviewer.cpp:55
void setText(const QString &text)
bool interpolate()
MapData::interpolate.
Definition: mapdata.cpp:189
Defines a color gradient for use with e.g. QCPColorMap.
Definition: qcustomplot.h:1905
double upper
Definition: qcustomplot.h:484
Represents the range an axis is encompassing.
Definition: qcustomplot.h:481
void setPositionAlignment(Qt::Alignment alignment)
double lower
Definition: qcustomplot.h:484
void SetName(QString name, QString type)
MapData::set_name.
Definition: mapdata.cpp:89
void SetGlobalGradient(QString name)
Definition: mapdata.cpp:97
QCPAxis * yAxis
Definition: qcustomplot.h:1824
void setPen(const QPen &pen)
bool saveTiff(const QString &fileName, int width=0, int height=0, double scale=1.0, int quality=0)
MapData::saveTiff.
Definition: mapdata.cpp:329
QCPItemPosition *const bottomRight
Definition: qcustomplot.h:3446
QString name()
MapData::name.
Definition: mapdata.cpp:64
int keySize()
Definition: mapplot.cpp:363
void setFont(const QFont &font)