Skip to content

Commit

Permalink
give an option for a toggle button on graphs to change y-axis min/max #…
Browse files Browse the repository at this point in the history
  • Loading branch information
ElderOrb committed May 11, 2024
1 parent 25ea0fe commit f99188b
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 12 deletions.
9 changes: 6 additions & 3 deletions Project/QtCreator/qctools-gui/qctools-gui.pro
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ HEADERS += \
$$SOURCES_PATH/GUI/filterselector.h \
$$SOURCES_PATH/GUI/playercontrol.h \
$$SOURCES_PATH/GUI/panelsview.h \
$$SOURCES_PATH/GUI/plotschooser.h
$$SOURCES_PATH/GUI/plotschooser.h \
$$SOURCES_PATH/GUI/yminmaxselector.h

SOURCES += \
$$SOURCES_PATH/GUI/FilesList.cpp \
Expand Down Expand Up @@ -108,7 +109,8 @@ SOURCES += \
$$SOURCES_PATH/GUI/filterselector.cpp \
$$SOURCES_PATH/GUI/playercontrol.cpp \
$$SOURCES_PATH/GUI/panelsview.cpp \
$$SOURCES_PATH/GUI/plotschooser.cpp
$$SOURCES_PATH/GUI/plotschooser.cpp \
$$SOURCES_PATH/GUI/yminmaxselector.cpp

include(../zlib.pri)

Expand All @@ -121,7 +123,8 @@ FORMS += \
$$SOURCES_PATH/GUI/barchartconditioninput.ui \
$$SOURCES_PATH/GUI/managebarchartconditions.ui \
$$SOURCES_PATH/GUI/playercontrol.ui \
$$SOURCES_PATH/GUI/plotschooser.ui
$$SOURCES_PATH/GUI/plotschooser.ui \
$$SOURCES_PATH/GUI/yminmaxselector.ui \
RESOURCES += \
$$SOURCES_PATH/Resource/Resources.qrc
Expand Down
87 changes: 78 additions & 9 deletions Source/GUI/Plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <qwt_symbol.h>

#include "Core/FileInformation.h"
#include <QMetaEnum>
#include <QSettings>
#include <cassert>

static double stepSize( double distance, int numSteps )
Expand Down Expand Up @@ -346,31 +348,35 @@ void Plot::initYAxis()
}
else
{
auto yMin = 0.0;
auto yMax = 0.0;
const size_t plotType = type();
const size_t plotGroup = group();

CommonStats* stat = stats( streamPos() );
const struct per_group& group = PerStreamType[plotType].PerGroup[plotGroup];

auto yMin = 0.0;
if(m_minValue.isNull() || m_minValue.isError() || m_minValue.isUndefined()) {
yMin = stat->y_Min[plotGroup]; // auto-select min
} else {
if(m_yminMaxMode == Formula)
{
if(m_minValue.isNumber())
yMin = m_minValue.toNumber();
else if(m_minValue.isCallable())
yMin = m_minValue.call().toNumber();
}

auto yMax = 0.0;
if(m_maxValue.isNull() || m_maxValue.isError() || m_maxValue.isUndefined()) {
yMax = stat->y_Max[plotGroup]; // auto-select min
} else {
if(m_maxValue.isNumber())
yMax = m_maxValue.toNumber();
else if(m_maxValue.isCallable())
yMax = m_maxValue.call().toNumber();
}
else if(m_yminMaxMode == MinMaxOfThePlot)
{
yMin = stat->y_Min[plotGroup]; // auto-select min
yMax = stat->y_Max[plotGroup]; // auto-select max
} else if(m_yminMaxMode == Custom)
{
yMin = m_customYMin;
yMax = m_customYMax;
}

setYAxis( yMin, yMax, group.StepsCount );
}
Expand Down Expand Up @@ -428,6 +434,47 @@ bool Plot::isBarchart() const
return m_barchart;
}

void Plot::setYAxisMinMaxMode(YMinMaxMode mode)
{
m_yminMaxMode = mode;
initYAxis();
}

Plot::YMinMaxMode Plot::yAxisMinMaxMode() const
{
return m_yminMaxMode;
}

void Plot::setYAxisCustomMinMax(double min, double max)
{
m_customYMin = min;
m_customYMax = max;
}

void Plot::getYAxisCustomMinMax(double &min, double &max)
{
min = m_customYMin;
max = m_customYMax;
}

bool Plot::hasMinMaxFormula() const
{
if(m_minValue.isNull() || m_minValue.isError() || m_minValue.isUndefined()) {
return false;
}

if(m_maxValue.isNull() || m_maxValue.isError() || m_maxValue.isUndefined()) {
return false;
}

return true;
}

const CommonStats *Plot::getStats() const
{
return stats( streamPos() );
}

void Plot::setBarchart(bool value)
{
if(m_barchart != value)
Expand Down Expand Up @@ -623,6 +670,28 @@ Plot::Plot( size_t streamPos, size_t Type, size_t Group, const FileInformation*
#else
panner->setMouseButton( Qt::MiddleButton );
#endif // QT_VERSION < QT_VERSION_CHECK(6, 0, 0)

QSettings settings;
settings.beginGroup("yminmax");

QMetaEnum metaEnum = QMetaEnum::fromType<Plot::YMinMaxMode>();
QString value = settings.value(QString::number(m_group)).toString();
if(!value.isEmpty()) {
auto splitted = value.split(";");
auto yMinMaxMode = (Plot::YMinMaxMode) metaEnum.keyToValue(splitted[0].toLatin1().constData());

if(yMinMaxMode == Plot::Custom) {
auto min = splitted[1].toDouble();
auto max = splitted[2].toDouble();

setYAxisCustomMinMax(min, max);
}

setYAxisMinMaxMode(yMinMaxMode);
}

settings.endGroup();

}

//---------------------------------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions Source/GUI/Plot.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,13 @@ class Plot : public QwtPlot
Q_OBJECT

public:
enum YMinMaxMode {
MinMaxOfThePlot,
Formula,
Custom
};
Q_ENUM(YMinMaxMode);

explicit Plot( size_t streamPos, size_t Type, size_t Group, const FileInformation* fileInformation, QWidget *parent );
virtual ~Plot();

Expand Down Expand Up @@ -472,6 +479,15 @@ class Plot : public QwtPlot
void updateSymbols();
bool isBarchart() const;

void setYAxisMinMaxMode(YMinMaxMode mode);
YMinMaxMode yAxisMinMaxMode() const;

void setYAxisCustomMinMax(double min, double max);
void getYAxisCustomMinMax(double& min, double& max);

bool hasMinMaxFormula() const;

const CommonStats* getStats() const;
Q_SIGNALS:
void cursorMoved(const QPointF& point, int index);
void visibilityChanged(bool visible);
Expand All @@ -488,6 +504,9 @@ private Q_SLOTS:
const QwtPlotCurve* curve( int index ) const;
QColor curveColor( int index ) const;

YMinMaxMode m_yminMaxMode { MinMaxOfThePlot };
double m_customYMin { 0.0 };
double m_customYMax { 0.0 };
QJSValue m_maxValue;
QJSValue m_minValue;
QJSEngine m_engine;
Expand Down
23 changes: 23 additions & 0 deletions Source/GUI/Plots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "Core/Core.h"
#include <QtAVPlayer/qavplayer.h>
#include "playercontrol.h"
#include "yminmaxselector.h"
#include <QComboBox>
#include <QGridLayout>
#include <QEvent>
Expand Down Expand Up @@ -123,6 +124,18 @@ void Plots::showEditBarchartProfileDialog(const size_t plotGroup, Plot* plot, co
}
}

void Plots::showYMinMaxConfigDialog(const size_t plotGroup, Plot *plot, const stream_info &streamInfo, QToolButton* button)
{
auto globalButtonPos = button->mapToGlobal(QPoint(0, 0));
auto geometry = m_yMinMaxSelector->geometry();

m_yMinMaxSelector->setPlot(plot);
m_yMinMaxSelector->enableFormulaMinMax(plot->hasMinMaxFormula());
m_yMinMaxSelector->move(QPoint(globalButtonPos.x() - geometry.width(), globalButtonPos.y()));
if(!m_yMinMaxSelector->isVisible())
m_yMinMaxSelector->show();
}

Plots::Plots( QWidget *parent, FileInformation* fileInformation ) :
QWidget( parent ),
m_zoomFactor ( 0 ),
Expand Down Expand Up @@ -282,11 +295,18 @@ Plots::Plots( QWidget *parent, FileInformation* fileInformation ) :
barchartPlotSwitch->setIcon(switchToBarcharts ? QIcon(":/icon/chart_chart.png") : QIcon(":/icon/bar_chart.png"));
});

QToolButton* yMinMaxConfigButton = new QToolButton();
connect(plot, SIGNAL(visibilityChanged(bool)), yMinMaxConfigButton, SLOT(setVisible(bool)));
connect(yMinMaxConfigButton, &QToolButton::clicked, [=]() {
showYMinMaxConfigDialog(plotGroup, plot, streamInfo, yMinMaxConfigButton);
});

QHBoxLayout* barchartAndConfigurationLayout = new QHBoxLayout();
barchartAndConfigurationLayout->setAlignment(Qt::AlignLeft);
barchartAndConfigurationLayout->setSpacing(5);
barchartAndConfigurationLayout->addWidget(barchartPlotSwitch);
barchartAndConfigurationLayout->addWidget(barchartConfigButton);
barchartAndConfigurationLayout->addWidget(yMinMaxConfigButton);

legendLayout->addItem(barchartAndConfigurationLayout);
legendLayout->addWidget(plot->plotLegend());
Expand Down Expand Up @@ -496,6 +516,9 @@ Plots::Plots( QWidget *parent, FileInformation* fileInformation ) :
m_scaleWidget->setScale( m_timeInterval.from, m_timeInterval.to);

setCursorPos( framePos() );

m_yMinMaxSelector = new YMinMaxSelector(this);
m_yMinMaxSelector->setWindowFlag(Qt::Popup);
}

//---------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions Source/GUI/Plots.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class QwtPlot;
class Plot;
class PlotScaleWidget;
class PlayerControl;
class YMinMaxSelector;

void showEditFrameCommentsDialog(QWidget* parentWidget, FileInformation* info, CommonStats* stats, size_t frameIndex);

Expand Down Expand Up @@ -116,6 +117,7 @@ class Plots : public QWidget
void loadBarchartsProfile(const QJsonObject& profile);

void showEditBarchartProfileDialog(const size_t plotGroup, Plot* plot, const stream_info& streamInfo);
void showYMinMaxConfigDialog(const size_t plotGroup, Plot* plot, const stream_info& streamInfo, QToolButton* button);

Q_SIGNALS:
void visibleFramesChanged(int from, int to);
Expand Down Expand Up @@ -146,6 +148,7 @@ private Q_SLOTS:
void setFramePos( size_t framePos, size_t statsPos = (size_t)-1 ) const { m_fileInfoData->Frames_Pos_Set(framePos, statsPos); }

private:
YMinMaxSelector* m_yMinMaxSelector;
PlotScaleWidget* m_scaleWidget;
CommentsPlot* m_commentsPlot;
std::vector<PanelsView*> m_PanelsViews;
Expand Down
118 changes: 118 additions & 0 deletions Source/GUI/yminmaxselector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "yminmaxselector.h"
#include "ui_yminmaxselector.h"
#include "Plot.h"

#include <QMetaEnum>
#include <QSettings>

YMinMaxSelector::YMinMaxSelector(QWidget *parent)
: QWidget(parent)
, ui(new Ui::YMinMaxSelector)
{
ui->setupUi(this);

connect(ui->customMinMax_radioButton, &QRadioButton::toggled, this, &YMinMaxSelector::enableCustomMinMax);
}

YMinMaxSelector::~YMinMaxSelector()
{
delete ui;
}

bool YMinMaxSelector::isMinMaxFromThePlot() const
{
return ui->minMaxOfThePlot_radioButton->isChecked();
}

bool YMinMaxSelector::isFormula() const
{
return ui->minMaxSystemProvided_radioButton->isChecked();
}

bool YMinMaxSelector::isCustom() const
{
return ui->customMinMax_radioButton->isChecked();
}

void YMinMaxSelector::enableCustomMinMax(bool value)
{
ui->min_label->setEnabled(value);
ui->min_doubleSpinBox->setEnabled(value);
ui->max_label->setEnabled(value);
ui->max_doubleSpinBox->setEnabled(value);
}

void YMinMaxSelector::enableFormulaMinMax(bool value)
{
ui->minMaxSystemProvided_radioButton->setEnabled(value);
}

void YMinMaxSelector::setPlot(Plot *plot)
{
m_plot = plot;

if(plot->yAxisMinMaxMode() == Plot::MinMaxOfThePlot) {
ui->minMaxOfThePlot_radioButton->setChecked(true);
} else if(plot->yAxisMinMaxMode() == Plot::Formula) {
ui->minMaxSystemProvided_radioButton->setChecked(true);
} else if(plot->yAxisMinMaxMode() == Plot::Custom) {
ui->customMinMax_radioButton->setChecked(true);
}

double min, max;
m_plot->getYAxisCustomMinMax(min, max);

ui->min_doubleSpinBox->setValue(min);
ui->max_doubleSpinBox->setValue(max);

if(qFuzzyCompare(ui->min_doubleSpinBox->value(), 0) && qFuzzyCompare(ui->max_doubleSpinBox->value(), 0)) {
auto stat = m_plot->getStats();
auto plotGroup = m_plot->group();

auto yMin = stat->y_Min[plotGroup]; // auto-select min
auto yMax = stat->y_Max[plotGroup]; // auto-select max

ui->min_doubleSpinBox->setValue(yMin);
ui->max_doubleSpinBox->setValue(yMax);
}
}

Plot *YMinMaxSelector::getPlot() const
{
return m_plot;
}

void YMinMaxSelector::on_apply_pushButton_clicked()
{
if(isFormula())
{
m_plot->setYAxisMinMaxMode(Plot::Formula);
}
else if(isMinMaxFromThePlot())
{
m_plot->setYAxisMinMaxMode(Plot::MinMaxOfThePlot);
}
else if(isCustom())
{
m_plot->setYAxisCustomMinMax(ui->min_doubleSpinBox->value(), ui->max_doubleSpinBox->value());
m_plot->setYAxisMinMaxMode(Plot::Custom);
}

QSettings settings;
settings.beginGroup("yminmax");

QMetaEnum metaEnum = QMetaEnum::fromType<Plot::YMinMaxMode>();
QString stringValue = metaEnum.valueToKey(m_plot->yAxisMinMaxMode());
if(m_plot->yAxisMinMaxMode() == Plot::Custom) {
stringValue.append(";");
stringValue.append(QString::number(ui->min_doubleSpinBox->value()));
stringValue.append(";");
stringValue.append(QString::number(ui->max_doubleSpinBox->value()));
}

settings.setValue(QString::number(m_plot->group()), stringValue);
settings.endGroup();

m_plot->replot();
}

Loading

0 comments on commit f99188b

Please sign in to comment.