Skip to content

Commit

Permalink
ticket:4400 Toggle visibility with single click.
Browse files Browse the repository at this point in the history
Hide all except click curve with double click.
Don't show plot picker for hidden curves.
  • Loading branch information
adeas31 committed May 9, 2017
1 parent ba9d6fd commit 0466192
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 39 deletions.
44 changes: 34 additions & 10 deletions OMPlot/OMPlotGUI/Legend.cpp
Expand Up @@ -128,30 +128,54 @@ QWidget* Legend::createWidget(const QwtLegendData &data) const
return pWidget;
}

/*!
* \brief Legend::mousePressEvent
* Reimplementation of QWidget::mousePressEvent()
* Show/hide the PlotCurve item clicked in the legend.
* \param event
*/
void Legend::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
QwtLegend::mousePressEvent(event);
return;
}
QwtLegend::mousePressEvent(event);
#if QWT_VERSION >= 0x060100
QwtPlotItem *pQwtPlotItem = qvariant_cast<QwtPlotItem*>(itemInfo(childAt(event->pos())));
mpPlotCurve = dynamic_cast<PlotCurve*>(pQwtPlotItem);
#else
mpPlotCurve = dynamic_cast<PlotCurve*>(find(childAt(event->pos())));
#endif
if (mpPlotCurve) {
mpPlotCurve->toggleVisibility();
}

}

/*!
* \brief Legend::mouseDoubleClickEvent
* Reimplementation of QWidget::mouseDoubleClickEvent()
* Show/hide the PlotCurve item double clicked in the legend.
* Show the PlotCurve item double clicked in the legend and hide all other.
* \param event
*/
void Legend::mouseDoubleClickEvent(QMouseEvent *event)
{
QwtLegend::mouseDoubleClickEvent(event);
#if QWT_VERSION >= 0x060100
QwtPlotItem *pQwtPlotItem = qvariant_cast<QwtPlotItem*>(itemInfo(childAt(event->pos())));
mpPlotCurve = dynamic_cast<PlotCurve*>(pQwtPlotItem);
#else
mpPlotCurve = dynamic_cast<PlotCurve*>(find(childAt(event->pos())));
#endif
if (mpPlotCurve) {
/* set the curve visibility */
mpPlotCurve->setVisible(!mpPlotCurve->isVisible());
QwtText text = mpPlotCurve->title();
if (mpPlotCurve->isVisible()) {
text.setColor(QColor(Qt::black));
} else {
text.setColor(QColor(Qt::gray));
foreach (PlotCurve *pPlotCurve, mpPlot->getPlotCurvesList()) {
if (pPlotCurve == mpPlotCurve) {
pPlotCurve->setVisible(false);
} else {
pPlotCurve->setVisible(true);
}
pPlotCurve->toggleVisibility();
}
mpPlotCurve->setTitle(text);
}
QwtLegend::mouseDoubleClickEvent(event);
}
1 change: 1 addition & 0 deletions OMPlot/OMPlotGUI/Legend.h
Expand Up @@ -56,6 +56,7 @@ public slots:
QAction *mpSetupAction;
protected:
virtual QWidget *createWidget(const QwtLegendData &data) const;
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseDoubleClickEvent(QMouseEvent *event);
};
}
Expand Down
16 changes: 16 additions & 0 deletions OMPlot/OMPlotGUI/PlotCurve.cpp
Expand Up @@ -214,6 +214,22 @@ bool PlotCurve::hasCustomColor()
return mCustomColor;
}

/*!
* \brief PlotCurve::toggleVisibility
* Toggles the curve visibility.
*/
void PlotCurve::toggleVisibility()
{
setVisible(!isVisible());
QwtText text = title();
if (isVisible()) {
text.setColor(QColor(Qt::black));
} else {
text.setColor(QColor(Qt::gray));
}
setTitle(text);
}

void PlotCurve::setData(const double* xData, const double* yData, int size)
{
#if QWT_VERSION >= 0x060000
Expand Down
1 change: 1 addition & 0 deletions OMPlot/OMPlotGUI/PlotCurve.h
Expand Up @@ -93,6 +93,7 @@ class PlotCurve : public QwtPlotCurve
QString getYVariable();
void setCustomColor(bool value);
bool hasCustomColor();
void toggleVisibility();
void setData(const double* xData, const double* yData, int size);
#if QWT_VERSION < 0x060000
virtual void updateLegend(QwtLegend *legend) const;
Expand Down
60 changes: 31 additions & 29 deletions OMPlot/OMPlotGUI/PlotPicker.cpp
Expand Up @@ -109,37 +109,39 @@ bool PlotPicker::curveAtPosition(const QPoint pos, PlotCurve *&pPlotCurve, int &
const QwtPlotItemList plotCurves = plot()->itemList(QwtPlotItem::Rtti_PlotCurve);
for (int i = 0 ; i < plotCurves.size() ; i++) {
pPlotCurve = static_cast<PlotCurve*>(plotCurves[i]);
// find the closest point
index = pPlotCurve->closestPoint(pos);
if (index > -1) {
int index1, previousIndex, nextIndex;
if (index == 0) {
index1 = 1;
} else if (index == pPlotCurve->mXAxisVector.size()) {
index1 = index - 1;
} else {
previousIndex = index - 1;
nextIndex = index + 1;
QPointF previousCurvePoint(pPlotCurve->mXAxisVector.at(previousIndex), pPlotCurve->mYAxisVector.at(previousIndex));
QPointF nextCurvePoint(pPlotCurve->mXAxisVector.at(nextIndex), pPlotCurve->mYAxisVector.at(nextIndex));
// find which point is closest to mouse point.
qreal pseudoDistance1 = qPow(posF.x() - previousCurvePoint.x(), 2) + qPow(posF.y() - previousCurvePoint.y(), 2);
qreal pseudoDistance2 = qPow(posF.x() - nextCurvePoint.x(), 2) + qPow(posF.y() - nextCurvePoint.y(), 2);
if (pseudoDistance1 < pseudoDistance2) {
index1 = previousIndex;
if (pPlotCurve->isVisible()) {
// find the closest point
index = pPlotCurve->closestPoint(pos);
if (index > -1) {
int index1, previousIndex, nextIndex;
if (index == 0) {
index1 = 1;
} else if (index == pPlotCurve->mXAxisVector.size()) {
index1 = index - 1;
} else {
index1 = nextIndex;
previousIndex = index - 1;
nextIndex = index + 1;
QPointF previousCurvePoint(pPlotCurve->mXAxisVector.at(previousIndex), pPlotCurve->mYAxisVector.at(previousIndex));
QPointF nextCurvePoint(pPlotCurve->mXAxisVector.at(nextIndex), pPlotCurve->mYAxisVector.at(nextIndex));
// find which point is closest to mouse point.
qreal pseudoDistance1 = qPow(posF.x() - previousCurvePoint.x(), 2) + qPow(posF.y() - previousCurvePoint.y(), 2);
qreal pseudoDistance2 = qPow(posF.x() - nextCurvePoint.x(), 2) + qPow(posF.y() - nextCurvePoint.y(), 2);
if (pseudoDistance1 < pseudoDistance2) {
index1 = previousIndex;
} else {
index1 = nextIndex;
}
}
}
QList<double> xMajorTicks = mpPlot->getPlotGrid()->xScaleDiv().ticks(QwtScaleDiv::MajorTick);
QList<double> yMajorTicks = mpPlot->getPlotGrid()->yScaleDiv().ticks(QwtScaleDiv::MajorTick);
if (xMajorTicks.size() > 1 && yMajorTicks.size() > 1) {
double x = (xMajorTicks[1] - xMajorTicks[0]) / mpPlot->axisMaxMinor(QwtPlot::xBottom);
double y = (yMajorTicks[1] - yMajorTicks[0]) / mpPlot->axisMaxMinor(QwtPlot::yLeft);
QPointF curvePointA(pPlotCurve->mXAxisVector.at(index), pPlotCurve->mYAxisVector.at(index));
QPointF curvePointB(pPlotCurve->mXAxisVector.at(index1), pPlotCurve->mYAxisVector.at(index1));
if (containsPoint(posF, curvePointA, curvePointB, x, y)) {
return true;
QList<double> xMajorTicks = mpPlot->getPlotGrid()->xScaleDiv().ticks(QwtScaleDiv::MajorTick);
QList<double> yMajorTicks = mpPlot->getPlotGrid()->yScaleDiv().ticks(QwtScaleDiv::MajorTick);
if (xMajorTicks.size() > 1 && yMajorTicks.size() > 1) {
double x = (xMajorTicks[1] - xMajorTicks[0]) / mpPlot->axisMaxMinor(QwtPlot::xBottom);
double y = (yMajorTicks[1] - yMajorTicks[0]) / mpPlot->axisMaxMinor(QwtPlot::yLeft);
QPointF curvePointA(pPlotCurve->mXAxisVector.at(index), pPlotCurve->mYAxisVector.at(index));
QPointF curvePointB(pPlotCurve->mXAxisVector.at(index1), pPlotCurve->mYAxisVector.at(index1));
if (containsPoint(posF, curvePointA, curvePointB, x, y)) {
return true;
}
}
}
}
Expand Down

0 comments on commit 0466192

Please sign in to comment.