Skip to content

Commit

Permalink
Working on axis autoscale
Browse files Browse the repository at this point in the history
  • Loading branch information
martonmiklos committed Feb 13, 2019
1 parent 14952c2 commit 853194c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 8 deletions.
10 changes: 9 additions & 1 deletion include/rqt_plot_qtcharts/verticalaxis.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,22 @@ class VerticalAxis : public QValueAxis
qreal rangeMax() const;
void setRangeMax(const qreal &rangeMax);

void seriesPointAdded(const qreal y);

bool getInstantApplyAutoScale() const;
void setInstantApplyAutoScale(bool instantApplyAutoScale);

void applyAutoScale();

private:
QString m_name, m_label;
bool m_autoScale = false;
bool m_instantApplyAutoScale = true;
Qt::Alignment m_align;
QString m_uid;

qreal m_rangeMin = 0, m_rangeMax = 100.0;

qreal m_seriesMin, m_seriesMax;
signals:
void alignChanged(Qt::Alignment align);
};
Expand Down
5 changes: 2 additions & 3 deletions src/rqt_plot_qtcharts/dialogaxisproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ void DialogAxisProperties::showEvent(QShowEvent *event)

void DialogAxisProperties::on_checkBoxAutoScale_toggled(bool checked)
{
ui->doubleSpinBoxRangeMin->setEnabled(!checked);
ui->doubleSpinBoxRangeMax->setEnabled(!checked);

m_axis->setAutoScale(checked);
if (checked) {
// TODO set the current min max of the axis to the spinBoxes
}
Expand All @@ -48,6 +46,7 @@ void DialogAxisProperties::setAxis(VerticalAxis *axis)
ui->doubleSpinBoxRangeMin->setValue(m_axis->rangeMin());
ui->doubleSpinBoxRangeMax->setValue(m_axis->rangeMax());
ui->checkBoxAxisLabelVisible->setChecked(m_axis->isVisible());
ui->checkBoxAutoScale->setChecked(m_axis->autoScale());

if (m_axis->align() == Qt::AlignRight) {
ui->comboBoxAlignment->setCurrentIndex(1);
Expand Down
5 changes: 4 additions & 1 deletion src/rqt_plot_qtcharts/dialogplotseriesproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ void DialogPlotSeriesProperties::setSeries(PlotLineSeries *series)
m_color = m_series->color();
ui->toolButtonSelectColor->setStyleSheet(QString("background-color: %1")
.arg(m_color.name()));
ui->comboBoxYAxis->setCurrentIndex(VerticalAxesManager::instance()->axes().indexOf(series->verticalAxis()));
int index = VerticalAxesManager::instance()->axes().indexOf(series->verticalAxis());
if (index == -1)
index = 0;
ui->comboBoxYAxis->setCurrentIndex(index);
}

void DialogPlotSeriesProperties::updateAxesComboBox()
Expand Down
9 changes: 8 additions & 1 deletion src/rqt_plot_qtcharts/plotlineseries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ QT_CHARTS_USE_NAMESPACE
PlotLineSeries::PlotLineSeries(QObject *parent) :
QLineSeries(parent)
{

setUseOpenGL(true);
}

QString PlotLineSeries::dataSource() const
Expand Down Expand Up @@ -62,10 +62,17 @@ void PlotLineSeries::dataReceived(qreal y)
{
append(m_xIndex, y);
m_xIndex++;

if (m_verticalAxis)
m_verticalAxis->seriesPointAdded(y);
}

void PlotLineSeries::clear()
{
m_xIndex = 0;
QLineSeries::clear();
append(QPointF(0,0));

if (m_verticalAxis)
m_verticalAxis->resetRange();
}
18 changes: 17 additions & 1 deletion src/rqt_plot_qtcharts/rqt_plot_qtcharts_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void PlotQtChartsWidget::restoreAxes(const qt_gui_cpp::Settings &instance_settin
axis->setRangeMin(instance_settings.value(QString("axes/%1/min").arg(index), 0).toDouble());
axis->setRangeMax(instance_settings.value(QString("axes/%1/max").arg(index), 100).toDouble());
axis->setAutoScale(instance_settings.value(QString("axes/%1/autoScale").arg(index), true).toBool());
axis->setUid(instance_settings.value(QString("axes/%1/uid").arg(index), true).toString());
axis->setUid(instance_settings.value(QString("axes/%1/uid").arg(index), "").toString());
VerticalAxesManager::instance()->addAxis(axis);
}

Expand Down Expand Up @@ -101,6 +101,7 @@ void PlotQtChartsWidget::saveAxes(qt_gui_cpp::Settings &instance_settings) const
axis->autoScale());
instance_settings.setValue(QString("axes/%1/align").arg(index),
static_cast<int>(axis->align()));
instance_settings.setValue(QString("axes/%1/uid").arg(index), axis->getUid());
index++;
}
}
Expand Down Expand Up @@ -230,6 +231,12 @@ void PlotQtChartsWidget::seriesAdded(PlotLineSeries *newSeries)
ui->toolButtonRemoveTopic->setEnabled(ui->toolButtonRemoveTopic->actions().count());
delete topicRemoveAction;
});

ui->zoomableChartWidget->chart()->addSeries(newSeries);
ui->zoomableChartWidget->chart()->setAxisX(m_axisX, newSeries);
if (newSeries->verticalAxis())
ui->zoomableChartWidget->chart()->setAxisY(newSeries->verticalAxis(), newSeries);
ui->zoomableChartWidget->connectLegendMarkerEvents();
}

void PlotQtChartsWidget::seriesRemoved(PlotLineSeries *series)
Expand All @@ -244,7 +251,10 @@ void PlotQtChartsWidget::seriesRemoved(PlotLineSeries *series)

void PlotQtChartsWidget::axisAdded(VerticalAxis *axis)
{
axis->setTitleText(axis->name());
axis->setTitleVisible(true);
ui->zoomableChartWidget->chart()->addAxis(axis, axis->align());
axis->resetRange();
}

void PlotQtChartsWidget::axisRemoved(VerticalAxis *axis)
Expand Down Expand Up @@ -272,6 +282,12 @@ void PlotQtChartsWidget::debugCallback(const rescube_msgs::PIDDebug::ConstPtr &d
series->dataReceived(dbg.get()->outputPID);
if (series->dataSource() == "/tiva_arm/PIDoutput/outputAfterScale")
series->dataReceived(dbg.get()->outputAfterScale);
if (series->dataSource() == "/tiva_arm/PIDoutput/integrator")
series->dataReceived(dbg.get()->integrator);
}

for (VerticalAxis *axis : VerticalAxesManager::instance()->axes()) {

}
}

Expand Down
42 changes: 41 additions & 1 deletion src/rqt_plot_qtcharts/verticalaxis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ VerticalAxis::VerticalAxis(QObject *parent) :
// enable this lovely feature if we have
this->setLabelsEditable(true);
#endif
m_seriesMin = std::numeric_limits<qreal>::max();
m_seriesMax = std::numeric_limits<qreal>::min();
}

QString VerticalAxis::label() const
Expand Down Expand Up @@ -36,7 +38,13 @@ bool VerticalAxis::autoScale() const

void VerticalAxis::setAutoScale(bool value)
{
m_autoScale = value;
if (m_autoScale != value) {
m_autoScale = value;
if (m_autoScale)
applyAutoScale();
else
setRange(m_rangeMin, m_rangeMax);
}
}

Qt::Alignment VerticalAxis::align() const
Expand Down Expand Up @@ -64,6 +72,8 @@ void VerticalAxis::setUid(const QString &value)

void VerticalAxis::resetRange()
{
m_seriesMin = m_rangeMin;
m_seriesMax = m_rangeMax;
setRange(m_rangeMin, m_rangeMax);
}

Expand All @@ -86,3 +96,33 @@ void VerticalAxis::setRangeMax(const qreal &rangeMax)
{
m_rangeMax = rangeMax;
}

void VerticalAxis::applyAutoScale()
{
setRange(m_seriesMin, m_seriesMax);
}

void VerticalAxis::seriesPointAdded(const qreal y)
{
if (y < m_seriesMin) {
m_seriesMin = y;
if (m_autoScale)
applyAutoScale();
}

if (y > m_seriesMax) {
m_seriesMax = y;
if (m_autoScale)
applyAutoScale();
}
}

bool VerticalAxis::getInstantApplyAutoScale() const
{
return m_instantApplyAutoScale;
}

void VerticalAxis::setInstantApplyAutoScale(bool instantApplyAutoScale)
{
m_instantApplyAutoScale = instantApplyAutoScale;
}
1 change: 1 addition & 0 deletions src/rqt_plot_qtcharts/zoomablechartwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ QChart *ZoomableChartWidget::chart() const
return m_chart;
}

// FIXME sublclass QChart emit a signal when series is added
void ZoomableChartWidget::connectLegendMarkerEvents()
{
// Connect all markers to handler
Expand Down

0 comments on commit 853194c

Please sign in to comment.