Isis 3 Programmer Reference
QHistogram.cpp
1 
7 /* SPDX-License-Identifier: CC0-1.0 */
8 
9 #include "QHistogram.h"
10 #include "Histogram.h"
11 #include "IException.h"
12 #include <QFileDialog>
13 #include <QPrintDialog>
14 #include <QPainter>
15 #include <QPrinter>
16 #include <QMessageBox>
17 #include <QFileInfo>
18 #include <QVBoxLayout>
19 #include <QLabel>
20 #include <qwt_plot_curve.h>
21 #include <qwt_plot_zoomer.h>
22 
23 using namespace std;
24 namespace Isis {
25 
26 
33  QHistogram::QHistogram(QWidget *parent) : QwtPlot(parent) {
34  p_zoomer = new QwtPlotZoomer(this->canvas());
35  setTitle("Histogram Plot");
36 
37  QwtText *leftLabel = new QwtText("Frequency", QwtText::PlainText);
38  leftLabel->setColor(Qt::red);
39  QFont font = leftLabel->font();
40  font.setPointSize(13);
41  font.setBold(true);
42  leftLabel->setFont(font);
43  setAxisTitle(QwtPlot::yLeft, *leftLabel);
44 
45  QwtText *rtLabel = new QwtText("Percentage");
46  rtLabel->setColor(Qt::blue);
47  rtLabel->setFont(font);
48  setAxisTitle(QwtPlot::yRight, *rtLabel);
49 
50  setAxisTitle(QwtPlot::xBottom, "Pixel Value (DN)");
51 
52  setAxisScale(QwtPlot::yRight, 0, 100);
53  enableAxis(QwtPlot::yRight, true);
54  setCanvasBackground(Qt::white);
55  }
56 
57 
64  p_histCurve = new QwtPlotCurve();
65  p_histCurve->setStyle(QwtPlotCurve::Lines);
66 
67  p_cdfCurve = new QwtPlotCurve();
68  p_cdfCurve->setStyle(QwtPlotCurve::Lines);
69 
70  //Transfer data from histogram to the plotcurve
71  QVector<QPointF> histData;
72  QVector<QPointF> cdfData;
73  double cumpct = 0.0;
74  for(int i = 0; i < hist.Bins(); i++) {
75  if(hist.BinCount(i) > 0) {
76  histData.append(QPointF(hist.BinMiddle(i), hist.BinCount(i)));
77 
78  double pct = (double)hist.BinCount(i) / hist.ValidPixels() * 100.;
79  cumpct += pct;
80  cdfData.append(QPointF(hist.BinMiddle(i), cumpct));
81  }
82  }
83  QPen *pen = new QPen(Qt::red);
84  pen->setWidth(2);
85  p_histCurve->setData(new QwtPointSeriesData(histData));
86  p_histCurve->setYAxis(QwtPlot::yLeft);
87  p_histCurve->setPen(*pen);
88  p_histCurve->attach(this);
89 
90  pen->setColor(Qt::blue);
91  p_cdfCurve->setData(new QwtPointSeriesData(cdfData));
92  p_cdfCurve->setYAxis(QwtPlot::yRight);
93  p_cdfCurve->setPen(*pen);
94  p_cdfCurve->attach(this);
95 
96  this->replot();
97  p_zoomer->setZoomBase();
98  }
99 
100 
106  if(p_zoomer->trackerMode() == QwtPicker::ActiveOnly) {
107  p_zoomer->setTrackerMode(QwtPicker::AlwaysOn);
108  }
109  else {
110  p_zoomer->setTrackerMode(QwtPicker::ActiveOnly);
111  }
112  }
113 
114 
120  p_cdfCurve->setVisible(!p_cdfCurve->isVisible());
121  this->replot();
122  }
123 
124 
130  // Initialize a printer
131  static QPrinter *printer = NULL;
132  if(printer == NULL) printer = new QPrinter;
133  printer->setPageSize(QPrinter::Letter);
134  printer->setColorMode(QPrinter::Color);
135 
136  QPrintDialog printDialog(printer, (QWidget *)parent());
137  if(printDialog.exec() == QDialog::Accepted) {
138  // Get display widget as a pixmap and convert to an image
139  QPixmap pixmap = this->grab();
140  QImage img = pixmap.toImage();
141 
142  // C++ Gui Programmign with Qt, page 201
143  QPainter painter(printer);
144  QRect rect = painter.viewport();
145  QSize size = img.size();
146  size.scale(rect.size(), Qt::KeepAspectRatio);
147  painter.setViewport(rect.x(), rect.y(),
148  size.width(), size.height());
149  painter.setWindow(img.rect());
150  painter.drawImage(0, 0, img);
151  }
152 
153  }
154 
155 
161  QString output =
162  QFileDialog::getSaveFileName((QWidget *)parent(),
163  "Choose output file",
164  "./",
165  QString("Images (*.png *.jpg *.tif)"));
166  if(output.isEmpty()) return;
167 
168  QString format = QFileInfo(output).suffix();
169  QPixmap pixmap = this->grab();
170  std::string formatString = format.toStdString();
171  if(!pixmap.save(output, formatString.c_str())) {
172  QMessageBox::information((QWidget *)parent(), "Error", "Unable to save" + output);
173  return;
174  }
175  }
176 
177 
183  QPen *pen = new QPen(Qt::white);
184  if(canvasBackground() == Qt::white) {
185  setCanvasBackground(Qt::black);
186  p_zoomer->setRubberBandPen(*pen);
187  p_zoomer->setTrackerPen(*pen);
188  }
189  else {
190  setCanvasBackground(Qt::white);
191  pen->setColor(Qt::black);
192  p_zoomer->setRubberBandPen(*pen);
193  p_zoomer->setTrackerPen(*pen);
194  }
195  replot();
196  }
197 
198 
204  QDialog *d = new QDialog(this);
205  d->setWindowTitle("Basic Help");
206 
207  QLabel *zoomLabel = new QLabel("Zoom Options:");
208  QLabel *zoomIn = new
209  QLabel(" <b>Left click</b> on the mouse, drag, and release to select an area to zoom in on");
210  QLabel *zoomOut = new
211  QLabel(" <b>Middle click</b> on the mouse to zoom out one level");
212  QLabel *zoomReset = new
213  QLabel(" <b>Right click</b> on the mouse to clear the zoom and return to the original plot");
214 
215  QVBoxLayout *layout = new QVBoxLayout();
216  layout->addWidget(zoomLabel);
217  layout->addWidget(zoomIn);
218  layout->addWidget(zoomOut);
219  layout->addWidget(zoomReset);
220 
221  d->setLayout(layout);
222  d->show();
223  }
224 
225 } // end namespace isis
QwtPlot
Isis::QHistogram::cdfCurveVisible
void cdfCurveVisible()
Hide/show the cdf curve.
Definition: QHistogram.cpp:119
Isis::QHistogram::p_zoomer
QwtPlotZoomer * p_zoomer
Plot Zoomer.
Definition: QHistogram.h:54
QWidget
Isis::QHistogram::trackerEnabled
void trackerEnabled()
Enables mouse tracking on the plot.
Definition: QHistogram.cpp:105
Isis::Histogram::BinMiddle
double BinMiddle(const int index) const
Returns the value represented by a bin.
Definition: Histogram.cpp:449
Isis::QHistogram::switchBackground
void switchBackground()
Switches the plot background color between black and white.
Definition: QHistogram.cpp:182
Isis::Statistics::ValidPixels
BigInt ValidPixels() const
Returns the total number of valid pixels processed.
Definition: Statistics.cpp:433
Isis::QHistogram::savePlot
void savePlot()
Allows user to save the plot to an image file.
Definition: QHistogram.cpp:160
Isis::Histogram::BinCount
BigInt BinCount(const int index) const
Returns the count at a bin position in the histogram.
Definition: Histogram.cpp:403
Isis::QHistogram::p_histCurve
QwtPlotCurve * p_histCurve
Historgram plot curve.
Definition: QHistogram.h:55
Isis::QHistogram::showHelp
void showHelp()
Provides help text in a dialog box.
Definition: QHistogram.cpp:203
QwtPlotCurve
Isis::QHistogram::printPlot
void printPlot()
Provide printing capabilities.
Definition: QHistogram.cpp:129
Isis::Histogram::Bins
int Bins() const
Returns the number of bins in the histogram.
Definition: Histogram.cpp:483
std
Namespace for the standard library.
Isis::Histogram
Container of a cube histogram.
Definition: Histogram.h:74
Isis::QHistogram::p_cdfCurve
QwtPlotCurve * p_cdfCurve
CDF plot curve.
Definition: QHistogram.h:56
QDialog
QVector
This is free and unencumbered software released into the public domain.
Definition: Calculator.h:18
Isis::QHistogram::Load
void Load(Histogram &hist)
Plots the given Isis Histogram in the plot window.
Definition: QHistogram.cpp:63
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16