Vespucci  1.0.0
global.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 "global.h"
22 #include <QtSvg>
23 #include <EmfEngine.h>
30 bool Vespucci::SavePlot(QCustomPlot *plot, QString filename)
31 {
32  QStringList filename_list = filename.split(".");
33  QString extension = filename_list.last();
34 
35  //this method of determining type may not be valid on non-Win platforms
36  //check this on GNU/Linux and Mac OSX later.
37 
38  bool success = true;
39 
40  if (extension == "bmp")
41  success = plot->saveBmp(filename, 0, 0, 1.0);
42  else if (extension == "pdf")
43  success = plot->savePdf(filename, false, 0, 0, "Vespucci 1.0", "Plot");
44  else if (extension == "png"){
45  bool ok;
46  plot->setBackground(Qt::transparent);
48  int quality = QInputDialog::getInt(plot->parentWidget(), "Enter Quality",
49  "Quality (%)",
50  80, 0, 100, 1, &ok);
51  if (ok)
52  plot->savePng(filename, 0, 0, 1.0, quality);
53  plot->setBackground(Qt::white);
55  }
56 
57  else if (extension == "jpg"){
58  bool ok;
59  int quality = QInputDialog::getInt(plot->parentWidget(), "Enter Quality",
60  "Quality (%)",
61  80, 0, 100, 1, &ok);
62  if (ok)
63  plot->saveJpg(filename, 0, 0, 1.0, quality);
64  }
65  else if (extension == "svg"){
66  QPicture picture;
67  QCPPainter qcp_painter(&picture);
68  qcp_painter.setMode(QCPPainter::pmVectorized);
69 
70  plot->setBackground(Qt::transparent);
72 
73  plot->toPainter(&qcp_painter);
74  qcp_painter.end();
75 
76  QSvgGenerator generator;
77  generator.setFileName(filename);
78 
79  QPainter painter;
80 
81  painter.begin(&generator);
82  painter.drawPicture(0, 0, picture);
83  painter.end();
84 
85  plot->setBackground(Qt::white);
87  }
88  else if (extension == "emf"){
89  QPicture picture;
90  QCPPainter qcp_painter(&picture);
91  qcp_painter.setMode(QCPPainter::pmVectorized);
92 
93  plot->setBackground(Qt::transparent);
95 
96  plot->toPainter(&qcp_painter);
97  qcp_painter.end();
98 
99  EmfPaintDevice emf(plot->size(), filename);
100  QPainter painter;
101  painter.begin(&emf);
102  painter.drawPicture(0, 0, picture);
103  painter.end();
104 
105  plot->setBackground(Qt::white);
107  }
108 
109  else{
110  //default to tif, force extension (for Windows compatability)
111  if (extension != "tif")
112  filename.append(".tif");
113  bool ok;
114  int quality = QInputDialog::getInt(plot->parentWidget(),
115  "Compression",
116  "Enter 0 for no compression,"
117  "1 for LZW lossless compression",
118  0, 0, 1, 1, &ok);
119  if (ok){
121  success = plot->saveRastered(filename, 0, 0, 1.0, "TIF", quality);
122  plot->setBackground(Qt::white);
124  plot->repaint();
125  }
126  }
127 
128  return success;
129 }
130 
136 void Vespucci::SetQCPFonts(QCustomPlot *plot, const QFont &font)
137 {
138  plot->setFont(font);
139 
140  plot->xAxis->setLabelFont(font);
141  plot->xAxis->setTickLabelFont(font);
142 
143  plot->xAxis2->setLabelFont(font);
144  plot->xAxis2->setTickLabelFont(font);
145 
146  plot->yAxis->setLabelFont(font);
147  plot->yAxis->setTickLabelFont(font);
148 
149  plot->yAxis2->setLabelFont(font);
150  plot->yAxis2->setTickLabelFont(font);
151 
152  plot->legend->setFont(font);
153 }
154 
155 
156 QProgressDialog *Vespucci::DisplayProgressDialog(QWidget *parent, QString title, QString text)
157 {
158  QProgressDialog *progress = new QProgressDialog(parent);
159  progress->setWindowModality(Qt::WindowModal);
160  progress->setLabelText(text);
161  progress->setWindowTitle(title);
162  progress->setCancelButton(0);
163  progress->setRange(0,0);
164  progress->exec();
165  return progress;
166 }
167 
168 bool Vespucci::KeysAreEqual(QStringList &keys1, QStringList &keys2)
169 {
170  if (keys1.size() != keys2.size()) return false;
171  else{
172  bool key_equal = false;
173  for (int i = 0; i < keys1.size(); ++i){
174  key_equal = (keys1[i] == keys2[i]);
175  }
176  return key_equal;
177  }
178 }
179 
180 QVector<double> Vespucci::FromArmaVec(const arma::vec &data)
181 {
182  return QVector<double>::fromStdVector(conv_to<std::vector<double> >::from(data));
183 }
0x01 Mode for vectorized painting (e.g. PDF export). For example, this prevents some antialiasing fix...
Definition: qcustomplot.h:326
bool savePng(const QString &fileName, int width=0, int height=0, double scale=1.0, int quality=-1)
bool saveRastered(const QString &fileName, int width, int height, double scale, const char *format, int quality=-1)
QCPAxis * xAxis
Definition: qcustomplot.h:1824
QProgressDialog * DisplayProgressDialog(QWidget *parent, QString title, QString text)
Definition: global.cpp:156
bool saveBmp(const QString &fileName, int width=0, int height=0, double scale=1.0)
bool begin(QPaintDevice *device)
void setBackground(const QPixmap &pm)
Q_SLOT void replot(QCustomPlot::RefreshPriority refreshPriority=QCustomPlot::rpHint)
void setTickLabelFont(const QFont &font)
QVector< double > FromArmaVec(const arma::vec &data)
Definition: global.cpp:180
The central class of the library. This is the QWidget which displays the plot and interacts with the ...
Definition: qcustomplot.h:1685
void setFont(const QFont &font)
QPainter subclass used internally.
Definition: qcustomplot.h:317
QCPAxis * yAxis2
Definition: qcustomplot.h:1824
QCPAxis * xAxis2
Definition: qcustomplot.h:1824
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)
void SetQCPFonts(QCustomPlot *plot, const QFont &font)
Vespucci::SetQCPFonts.
Definition: global.cpp:136
bool KeysAreEqual(QStringList &keys1, QStringList &keys2)
Definition: global.cpp:168
The QCustomPlot surface is immediately refreshed, by calling QWidget::repaint() after the replot...
Definition: qcustomplot.h:1715
void setLabelFont(const QFont &font)
void setMode(PainterMode mode, bool enabled=true)
QCPAxis * yAxis
Definition: qcustomplot.h:1824
void toPainter(QCPPainter *painter, int width=0, int height=0)
QCPLegend * legend
Definition: qcustomplot.h:1825
bool SavePlot(QCustomPlot *plot, QString filename)
Vespucci::SavePlot.
Definition: global.cpp:30