24 #include <QCloseEvent>
26 #include <QFileDialog>
33 #include "ui_histogramDlg.h"
36 : QCustomPlot(parent),
38 m_colorScheme(USE_SOLID_COLOR),
39 m_solidColor(Qt::
blue),
41 m_associatedSF(nullptr),
42 m_numberOfClassesCanBeChanged(false),
47 m_overlayCurve(nullptr),
49 m_drawVerticalIndicator(false),
50 m_verticalIndicatorPositionPercent(0),
55 m_areaLeftlastValue(
std::numeric_limits<double>::quiet_NaN()),
57 m_areaRightlastValue(
std::numeric_limits<double>::quiet_NaN()),
59 m_arrowLeftlastValue(
std::numeric_limits<double>::quiet_NaN()),
60 m_arrowRight(nullptr),
61 m_arrowRightlastValue(
std::numeric_limits<double>::quiet_NaN()),
62 m_lastMouseClick(0, 0),
63 m_refreshAfterResize(true) {
64 setWindowTitle(
"Histogram");
65 setFocusPolicy(Qt::StrongFocus);
67 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
69 setAutoAddPlottableToLegend(
false);
70 setAntialiasedElements(QCP::AntialiasedElement::aeAll);
78 assert(xAxis && yAxis);
79 xAxis->setTickLength(0, 5);
80 xAxis->setSubTickLength(0, 3);
81 yAxis->setTickLength(0, 5);
82 yAxis->setSubTickLength(0, 3);
92 void ccHistogramWindow::clearInternal() {
109 const QString& yLabel) {
110 if (xLabel.isNull()) {
111 xAxis->setVisible(
false);
114 xAxis->setLabel(xLabel);
115 xAxis->setVisible(
true);
118 if (xLabel.isNull()) {
119 yAxis->setVisible(
false);
122 yAxis->setLabel(yLabel);
123 yAxis->setVisible(
true);
128 unsigned initialNumberOfClasses ,
129 bool numberOfClassesCanBeChanged ,
130 bool showNaNValuesInGrey ) {
158 }
catch (
const std::bad_alloc&) {
159 CVLog::Warning(
"[ccHistogramWindow::fromBinArray] Not enough memory!");
173 }
catch (
const std::bad_alloc&) {
175 "[ccHistogramWindow::setCurveValues] Not enough memory!");
186 "[ccHistogramWindow::computeBinArrayFromSF] Need an associated "
194 "[ccHistogramWindow::computeBinArrayFromSF] Invalid number of "
203 }
catch (
const std::bad_alloc&) {
205 "[ccHistogramWindow::computeBinArrayFromSF] Not enough "
215 }
catch (
const std::bad_alloc&) {
217 "[ccHistogramWindow::computeBinArrayFromSF] Not enough "
225 double step = range /
static_cast<double>(binCount);
226 for (
unsigned i = 0; i <
count; ++i) {
285 QVector<double> keyData(histoSize);
286 QVector<double> valueData(histoSize);
287 QVector<QColor>
colors(histoSize);
289 for (
int i = 0; i < histoSize; ++i) {
291 double normVal = (
static_cast<double>(i) + 0.5) / histoSize;
297 static_cast<ScalarType
>(keyData[i]));
300 colors[i] = QColor(col->
r, col->
g, col->
b);
308 replot(QCustomPlot::rpImmediateRefresh);
346 plotLayout()->insertRow(0);
353 this, QStringLiteral(
"%0 [%1 classes]")
371 this->clearPlottables();
383 double partialSum = 0;
391 QVector<double> keyData(histoSize);
392 QVector<double> valueData(histoSize);
395 switch (colorScheme) {
424 for (
int i = 0; i < histoSize; ++i) {
426 double normVal = (
static_cast<double>(i) + 0.5) / histoSize;
443 static_cast<ScalarType
>(keyData[i]));
447 col = colorScale->getColorByRelativePos(normVal);
453 colors[i] = QColor(col->
r, col->
g, col->
b);
467 QVector<double> x(curveSize);
468 QVector<double> y(curveSize);
471 for (
int i = 0; i < curveSize; ++i) {
472 x[i] =
m_minVal + (
static_cast<double>(i) ) * step;
483 QPen pen(QColor(col.
r, col.
g, col.
b));
500 : dispRange.
start());
519 const ecvColor::Rgb* col = colorScale->getColorByRelativePos(
533 const ecvColor::Rgb* col = colorScale->getColorByRelativePos(
550 QVector<double> keyData(1);
551 QVector<double> valueData(1);
563 unsigned bin =
static_cast<unsigned>(
565 QString valueStr = QString(
"bin %0").arg(bin);
568 QString(
"< %0 %").arg(100.0 *
static_cast<double>(partialSum) /
569 static_cast<double>(totalSum),
658 int penWidth =
std::max(w, h) / 200;
661 pen.setWidth(penWidth);
668 QCustomPlot::resizeEvent(
event);
711 if (
event->buttons() & Qt::LeftButton) {
713 QPoint mousePos =
event->pos();
802 if (roi.contains(
event->pos(),
false)) {
805 int verticalIndicatorPosition =
807 (
event->x() - roi.x())) /
810 static_cast<double>(verticalIndicatorPosition) /
843 : QDialog(parent, Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint),
845 m_gui(new Ui_HistogramDialog) {
846 m_gui->setupUi(
this);
848 auto hboxLayout =
new QHBoxLayout;
850 hboxLayout->setContentsMargins(0, 0, 0, 0);
851 hboxLayout->addWidget(
m_win);
853 m_gui->histoFrame->setLayout(hboxLayout);
855 connect(
m_gui->exportCSVToolButton, &QAbstractButton::clicked,
this,
857 connect(
m_gui->exportImageToolButton, &QAbstractButton::clicked,
this,
869 "[Histogram] Histogram has no associated values (can't save "
875 if (!file.open(QFile::WriteOnly | QFile::Text)) {
877 QString(
"[Histogram] Failed to save histogram to file '%1'")
882 QTextStream stream(&file);
883 stream.setRealNumberPrecision(12);
884 stream.setRealNumberNotation(QTextStream::FixedNotation);
887 stream <<
"Class; Value; Class start; Class end;" <<
QtCompat::endl;
892 int histoSize =
static_cast<int>(histoValues.size());
894 for (
int i = 0; i < histoSize; ++i) {
898 stream << histoValues[i];
902 stream << minVal + step;
924 QString currentPath =
928 currentPath += QString(
"/") +
m_win->windowTitle() +
".csv";
931 QString
filename = QFileDialog::getSaveFileName(
this,
"Select output file",
932 currentPath,
"*.csv");
955 QString currentPath =
960 "Select output file",
m_win->windowTitle(), currentPath,
this);
962 if (outputFilename.isEmpty()) {
969 QFileInfo(outputFilename).absolutePath());
974 if (
image.save(outputFilename)) {
975 CVLog::Print(QString(
"[Histogram] Image '%1' successfully saved")
976 .arg(outputFilename));
978 CVLog::Error(QString(
"Failed to save file '%1'").arg(outputFilename));
std::shared_ptr< core::Tensor > image
double qtCompatWheelEventDelta(const QWheelEvent *event) noexcept
virtual void link()
Increase counter.
virtual void release()
Decrease counter and deletes object when 0.
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
static bool Print(const char *format,...)
Prints out a formatted message in console.
static bool Error(const char *format,...)
Display an error dialog with formatted message.
static QString GetSaveFilename(const QString &dialogTitle, const QString &baseName, const QString &imageSavePath, QWidget *parentWidget=nullptr)
Helper: select an output image filename.
QCustomPlot: small arrows at the bottom.
void setColor(int r, int g, int b)
Sets triangle 'inside' color.
QCustomPlot: vertical bar with text along side.
void setText(QString text)
void setTextAlignment(bool left)
void appendText(QString text)
QCustomPlot: colored histogram.
void setData(const QVector< double > &key, const QVector< double > &value)
QCustomPlot: greyed areas.
double pixelToKey(int pixX) const
Converts a pixel value (X) to the equivalent key.
void setRange(double minVal, double maxVal)
virtual bool isSelectable(QPoint click) const
void setCurrentVal(double val)
double currentVal() const
QSharedPointer< ccColorScale > Shared
Shared pointer type.
Color scales manager/container.
static ccColorScale::Shared GetDefaultScale(DEFAULT_SCALES scale=BGYR)
Returns a pre-defined color scale (static shortcut)
virtual ~ccHistogramWindowDlg()
Destructor.
void onExportToImage()
When the export to Image file button is pressed.
ccHistogramWindowDlg(QWidget *parent=0)
Default constructor.
ccHistogramWindow * m_win
void onExportToCSV()
When the export to CSV file button is pressed.
bool exportToCSV(QString filename) const
Exports histogram to a CSV file.
Ui_HistogramDialog * m_gui
Associated widgets.
HISTOGRAM_COLOR_SCHEME m_colorScheme
Color scheme.
void refreshBars()
Updates the histogram bars only.
void setMinDispValue(double)
void setTitle(const QString &str)
Sets title.
void setCurveValues(const std::vector< double > &curveValues)
Sets overlay curve values.
void sfMaxSatValChanged(double)
double m_areaLeftlastValue
const std::vector< unsigned > & histoValues() const
Returns the current histogram bins.
virtual ~ccHistogramWindow()
Destructor.
double minVal() const
Returns the current histogram min value.
void resizeEvent(QResizeEvent *event)
bool m_refreshAfterResize
QCPHiddenArea * m_areaRight
Right greyed area.
unsigned getMaxHistoVal()
Returns current maximum bin size.
void sfMinSatValChanged(double)
void wheelEvent(QWheelEvent *event)
QPoint m_lastMouseClick
Last mouse click.
QCPBarsWithText * m_vertBar
void setMinSatValue(double)
ccHistogramWindow(QWidget *parent=0)
Default constructor.
void refresh()
Updates the display.
double m_areaRightlastValue
void setColorScheme(HISTOGRAM_COLOR_SCHEME scheme)
Sets how the gradient bars should be colored.
void fromSF(ccScalarField *sf, unsigned initialNumberOfClasses=0, bool numberOfClassesCanBeChanged=true, bool showNaNValuesInGrey=true)
Computes histogram from a scalar field.
ccScalarField * m_associatedSF
Associated scalar field.
ccColorScale::Shared m_colorScale
Gradient color scale.
void sfMinDispValChanged(double)
AxisDisplayOptions m_axisDisplayOptions
void fromBinArray(const std::vector< unsigned > &histoValues, double minVal, double maxVal)
void setAxisDisplayOption(AxisDisplayOptions axisOptions)
void setRefreshAfterResize(bool refreshAfterResize)
SELECTABLE_ITEMS m_selectedItem
Currently selected item.
QCPHiddenArea * m_areaLeft
Left greyed area.
void setNumberOfClasses(size_t n)
Changes the current number of classes.
double m_verticalIndicatorPositionPercent
QCPGraph * m_overlayCurve
Overlay curve.
double m_arrowLeftlastValue
void mouseMoveEvent(QMouseEvent *event)
void setSFInteractionMode(SFInteractionModes modes)
Enables SF interaction mode.
QCPArrow * m_arrowRight
Right arrow.
void sfMaxDispValChanged(double)
bool computeBinArrayFromSF(size_t binCount)
Dynamically computes histogram bins from scalar field.
QCPColoredBars * m_histogram
std::vector< double > m_curveValues
void mousePressEvent(QMouseEvent *event)
double maxVal() const
Returns the current histogram max value.
QFont m_renderingFont
Rendering font.
QColor m_solidColor
Solid color.
void setMaxSatValue(double)
void setMaxDispValue(double)
bool m_numberOfClassesCanBeChanged
SFInteractionModes m_sfInteractionModes
Which SF interaction modes are enabled.
void clear()
Clears the display.
QCPArrow * m_arrowLeft
Left arrow.
bool m_drawVerticalIndicator
std::vector< unsigned > m_histoValues
void setAxisLabels(const QString &xLabel, const QString &yLabel)
Sets axis labels.
QCPTextElement * m_titlePlot
void updateOverlayCurveWidth(int w, int h)
Updates overlay curve width depending on the widget display size.
double m_arrowRightlastValue
Scalar field range structure.
A scalar field associated to display-related parameters.
const ccColorScale::Shared & getColorScale() const
Returns associated color scale.
void setSaturationStop(ScalarType val)
Sets the value at which to stop color gradient.
void setMinDisplayed(ScalarType val)
Sets the minimum displayed value.
unsigned getColorRampSteps() const
Returns number of color ramp steps.
const Range & saturationRange() const
Access to the range of saturation values.
const Histogram & getHistogram() const
Returns associated histogram values (for display)
const Range & displayRange() const
Access to the range of displayed values.
const ecvColor::Rgb * getColor(ScalarType value) const
bool symmetricalScale() const
Returns whether the color scale s symmetrical or not.
void setSaturationStart(ScalarType val)
Sets the value at which to start color gradient.
void setMaxDisplayed(ScalarType val)
Sets the maximum displayed value.
ScalarType getMin() const
Returns the minimum value.
ScalarType & getValue(std::size_t index)
unsigned currentSize() const
ScalarType getMax() const
Returns the maximum value.
static const ParamStruct & Parameters()
Returns the stored values of each parameter.
static const QString CurrentPath()
static const QString SaveFile()
static const QChar s_csvSep(';')
QTextStream & endl(QTextStream &stream)
constexpr utility::nullopt_t None
MiniVec< float, N > floor(const MiniVec< float, N > &a)
constexpr Rgb red(MAX, 0, 0)
constexpr Rgb darkGrey(MAX/2, MAX/2, MAX/2)
constexpr Rgb blue(0, 0, MAX)
constexpr Rgb lightGrey(static_cast< ColorCompType >(MAX *0.8), static_cast< ColorCompType >(MAX *0.8), static_cast< ColorCompType >(MAX *0.8))
QString defaultDocPath()
Shortcut for getting the documents location path.
unsigned displayedNumPrecision
Displayed numbers precision.