Isis 3 Programmer Reference
QHistogram.cpp
Go to the documentation of this file.
1 
23 #include "QHistogram.h"
24 #include "Histogram.h"
25 #include "IException.h"
26 #include <QFileDialog>
27 #include <QPrintDialog>
28 #include <QPainter>
29 #include <QPrinter>
30 #include <QMessageBox>
31 #include <QFileInfo>
32 #include <QVBoxLayout>
33 #include <QLabel>
34 #include <qwt_plot_curve.h>
35 #include <qwt_plot_zoomer.h>
36 
37 using namespace std;
38 namespace Isis {
39 
40 
47  QHistogram::QHistogram(QWidget *parent) : QwtPlot(parent) {
48  p_zoomer = new QwtPlotZoomer(this->canvas());
49  setTitle("Histogram Plot");
50 
51  QwtText *leftLabel = new QwtText("Frequency", QwtText::PlainText);
52  leftLabel->setColor(Qt::red);
53  QFont font = leftLabel->font();
54  font.setPointSize(13);
55  font.setBold(true);
56  leftLabel->setFont(font);
57  setAxisTitle(QwtPlot::yLeft, *leftLabel);
58 
59  QwtText *rtLabel = new QwtText("Percentage");
60  rtLabel->setColor(Qt::blue);
61  rtLabel->setFont(font);
62  setAxisTitle(QwtPlot::yRight, *rtLabel);
63 
64  setAxisTitle(QwtPlot::xBottom, "Pixel Value (DN)");
65 
66  setAxisScale(QwtPlot::yRight, 0, 100);
67  enableAxis(QwtPlot::yRight, true);
68  setCanvasBackground(Qt::white);
69  }
70 
71 
78  p_histCurve = new QwtPlotCurve();
79  p_histCurve->setStyle(QwtPlotCurve::Lines);
80 
81  p_cdfCurve = new QwtPlotCurve();
82  p_cdfCurve->setStyle(QwtPlotCurve::Lines);
83 
84  //Transfer data from histogram to the plotcurve
85  QVector<QPointF> histData;
86  QVector<QPointF> cdfData;
87  double cumpct = 0.0;
88  for(int i = 0; i < hist.Bins(); i++) {
89  if(hist.BinCount(i) > 0) {
90  histData.append(QPointF(hist.BinMiddle(i), hist.BinCount(i)));
91 
92  double pct = (double)hist.BinCount(i) / hist.ValidPixels() * 100.;
93  cumpct += pct;
94  cdfData.append(QPointF(hist.BinMiddle(i), cumpct));
95  }
96  }
97  QPen *pen = new QPen(Qt::red);
98  pen->setWidth(2);
99  p_histCurve->setData(new QwtPointSeriesData(histData));
100  p_histCurve->setYAxis(QwtPlot::yLeft);
101  p_histCurve->setPen(*pen);
102  p_histCurve->attach(this);
103 
104  pen->setColor(Qt::blue);
105  p_cdfCurve->setData(new QwtPointSeriesData(cdfData));
106  p_cdfCurve->setYAxis(QwtPlot::yRight);
107  p_cdfCurve->setPen(*pen);
108  p_cdfCurve->attach(this);
109 
110  this->replot();
111  p_zoomer->setZoomBase();
112  }
113 
114 
120  if(p_zoomer->trackerMode() == QwtPicker::ActiveOnly) {
121  p_zoomer->setTrackerMode(QwtPicker::AlwaysOn);
122  }
123  else {
124  p_zoomer->setTrackerMode(QwtPicker::ActiveOnly);
125  }
126  }
127 
128 
134  p_cdfCurve->setVisible(!p_cdfCurve->isVisible());
135  this->replot();
136  }
137 
138 
144  // Initialize a printer
145  static QPrinter *printer = NULL;
146  if(printer == NULL) printer = new QPrinter;
147  printer->setPageSize(QPrinter::Letter);
148  printer->setColorMode(QPrinter::Color);
149 
150  QPrintDialog printDialog(printer, (QWidget *)parent());
151  if(printDialog.exec() == QDialog::Accepted) {
152  // Get display widget as a pixmap and convert to an image
153  QPixmap pixmap = this->grab();
154  QImage img = pixmap.toImage();
155 
156  // C++ Gui Programmign with Qt, page 201
157  QPainter painter(printer);
158  QRect rect = painter.viewport();
159  QSize size = img.size();
160  size.scale(rect.size(), Qt::KeepAspectRatio);
161  painter.setViewport(rect.x(), rect.y(),
162  size.width(), size.height());
163  painter.setWindow(img.rect());
164  painter.drawImage(0, 0, img);
165  }
166 
167  }
168 
169 
175  QString output =
176  QFileDialog::getSaveFileName((QWidget *)parent(),
177  "Choose output file",
178  "./",
179  QString("Images (*.png *.jpg *.tif)"));
180  if(output.isEmpty()) return;
181 
182  QString format = QFileInfo(output).suffix();
183  QPixmap pixmap = this->grab();
184  std::string formatString = format.toStdString();
185  if(!pixmap.save(output, formatString.c_str())) {
186  QMessageBox::information((QWidget *)parent(), "Error", "Unable to save" + output);
187  return;
188  }
189  }
190 
191 
197  QPen *pen = new QPen(Qt::white);
198  if(canvasBackground() == Qt::white) {
199  setCanvasBackground(Qt::black);
200  p_zoomer->setRubberBandPen(*pen);
201  p_zoomer->setTrackerPen(*pen);
202  }
203  else {
204  setCanvasBackground(Qt::white);
205  pen->setColor(Qt::black);
206  p_zoomer->setRubberBandPen(*pen);
207  p_zoomer->setTrackerPen(*pen);
208  }
209  replot();
210  }
211 
212 
218  QDialog *d = new QDialog(this);
219  d->setWindowTitle("Basic Help");
220 
221  QLabel *zoomLabel = new QLabel("Zoom Options:");
222  QLabel *zoomIn = new
223  QLabel(" <b>Left click</b> on the mouse, drag, and release to select an area to zoom in on");
224  QLabel *zoomOut = new
225  QLabel(" <b>Middle click</b> on the mouse to zoom out one level");
226  QLabel *zoomReset = new
227  QLabel(" <b>Right click</b> on the mouse to clear the zoom and return to the original plot");
228 
229  QVBoxLayout *layout = new QVBoxLayout();
230  layout->addWidget(zoomLabel);
231  layout->addWidget(zoomIn);
232  layout->addWidget(zoomOut);
233  layout->addWidget(zoomReset);
234 
235  d->setLayout(layout);
236  d->show();
237  }
238 
239 } // end namespace isis
240 
void trackerEnabled()
Enables mouse tracking on the plot.
Definition: QHistogram.cpp:119
Namespace for the standard library.
QwtPlotCurve * p_histCurve
Historgram plot curve.
Definition: QHistogram.h:68
void showHelp()
Provides help text in a dialog box.
Definition: QHistogram.cpp:217
BigInt ValidPixels() const
Returns the total number of valid pixels processed.
Definition: Statistics.cpp:446
BigInt BinCount(const int index) const
Returns the count at a bin position in the histogram.
Definition: Histogram.cpp:624
double BinMiddle(const int index) const
Returns the value represented by a bin.
Definition: Histogram.cpp:670
void cdfCurveVisible()
Hide/show the cdf curve.
Definition: QHistogram.cpp:133
QwtPlotZoomer * p_zoomer
Plot Zoomer.
Definition: QHistogram.h:67
Container of a cube histogram.
Definition: Histogram.h:86
QwtPlotCurve * p_cdfCurve
CDF plot curve.
Definition: QHistogram.h:69
void printPlot()
Provide printing capabilities.
Definition: QHistogram.cpp:143
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void switchBackground()
Switches the plot background color between black and white.
Definition: QHistogram.cpp:196
void Load(Histogram &hist)
Plots the given Isis Histogram in the plot window.
Definition: QHistogram.cpp:77
int Bins() const
Returns the number of bins in the histogram.
Definition: Histogram.cpp:704
void savePlot()
Allows user to save the plot to an image file.
Definition: QHistogram.cpp:174