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