diff options
author | Keith Kyzivat <keith.kyzivat@qt.io> | 2021-03-04 15:27:02 -0500 |
---|---|---|
committer | Keith Kyzivat <keith.kyzivat@qt.io> | 2021-05-31 08:47:29 -0400 |
commit | b1d4053d5d932a37fe76857193ee6911f5471b78 (patch) | |
tree | 595ab2f787ae9e38d514c228c175bcf45bc15e1b /src | |
parent | 198c7d663145f17ceee5e97cc5eec5764caba82a (diff) |
Chart item mouse click handling if chart rubber band enabled
Pass left mouse clicks on to underlying Qt Chart items if those items
accept left mouse clicks, in all cases.
Previous to this change if QChartView's setRubberBand feature was
enabled, left clicks were always handled by the QChartView to start
showing the rubber band selection.
With this change, if the mouse is over a QGraphicsItem in the scene that
accepts Qt::LeftMouse button, then the click is passed on to the item.
To make this work, some QGraphicsItems that are created in the chart
needed to properly mark their acceptance of mouse buttons using
QGraphicsItem::setAcceptedMouseButtons, as the default is to accept all
mouse buttons.
Task-number: QTBUG-94035
Change-Id: I4f2ec3823b10acbb5ef5195454ab5461d04b75da
Reviewed-by: Tomi Korpipää <tomi.korpipaa@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/charts/axis/cartesianchartaxis.cpp | 6 | ||||
-rw-r--r-- | src/charts/chartbackground.cpp | 1 | ||||
-rw-r--r-- | src/charts/chartpresenter.cpp | 1 | ||||
-rw-r--r-- | src/charts/charttitle.cpp | 1 | ||||
-rw-r--r-- | src/charts/qchartview.cpp | 27 | ||||
-rw-r--r-- | src/charts/qchartview.h | 3 |
6 files changed, 30 insertions, 9 deletions
diff --git a/src/charts/axis/cartesianchartaxis.cpp b/src/charts/axis/cartesianchartaxis.cpp index 60bcbbcb..2bcd31ee 100644 --- a/src/charts/axis/cartesianchartaxis.cpp +++ b/src/charts/axis/cartesianchartaxis.cpp @@ -57,6 +57,7 @@ void CartesianChartAxis::createItems(int count) { if (arrowItems().size() == 0) { QGraphicsLineItem *arrow = new LineArrowItem(this, this); + arrow->setAcceptedMouseButtons({}); arrow->setPen(axis()->linePen()); arrowGroup()->addToGroup(arrow); } @@ -64,9 +65,11 @@ void CartesianChartAxis::createItems(int count) if (intervalAxis() && gridItems().size() == 0) { for (int i = 0 ; i < 2 ; i ++){ QGraphicsLineItem *item = new QGraphicsLineItem(this); + item->setAcceptedMouseButtons({}); item->setPen(axis()->gridLinePen()); gridGroup()->addToGroup(item); QGraphicsRectItem *shades = new QGraphicsRectItem(this); + shades->setAcceptedMouseButtons({}); shades->setPen(axis()->shadesPen()); shades->setBrush(axis()->shadesBrush()); shadeGroup()->addToGroup(shades); @@ -80,7 +83,9 @@ void CartesianChartAxis::createItems(int count) for (int i = 0; i < count; ++i) { QGraphicsLineItem *arrow = new QGraphicsLineItem(this); + arrow->setAcceptedMouseButtons({}); QGraphicsLineItem *grid = new QGraphicsLineItem(this); + grid->setAcceptedMouseButtons({}); QGraphicsTextItem *label; if (axis()->type() == QAbstractAxis::AxisTypeValue) { label = new ValueAxisLabel(this); @@ -102,6 +107,7 @@ void CartesianChartAxis::createItems(int count) label = new QGraphicsTextItem(this); } + label->setAcceptedMouseButtons({}); label->document()->setDocumentMargin(ChartPresenter::textMargin()); arrow->setPen(axis()->linePen()); grid->setPen(axis()->gridLinePen()); diff --git a/src/charts/chartbackground.cpp b/src/charts/chartbackground.cpp index 75f92c3c..6f4a837a 100644 --- a/src/charts/chartbackground.cpp +++ b/src/charts/chartbackground.cpp @@ -41,6 +41,7 @@ ChartBackground::ChartBackground(QGraphicsItem *parent) m_diameter(5), m_dropShadow(0) { + setAcceptedMouseButtons({}); } ChartBackground::~ChartBackground() diff --git a/src/charts/chartpresenter.cpp b/src/charts/chartpresenter.cpp index f9e7a481..39ba35f6 100644 --- a/src/charts/chartpresenter.cpp +++ b/src/charts/chartpresenter.cpp @@ -253,6 +253,7 @@ void ChartPresenter::createPlotAreaBackgroundItem() m_plotAreaBackground = new QGraphicsEllipseItem(rootItem()); // Use transparent pen instead of Qt::NoPen, as Qt::NoPen causes // antialising artifacts with axis lines for some reason. + m_plotAreaBackground->setAcceptedMouseButtons({}); m_plotAreaBackground->setPen(QPen(Qt::transparent)); m_plotAreaBackground->setBrush(Qt::NoBrush); m_plotAreaBackground->setZValue(ChartPresenter::PlotAreaZValue); diff --git a/src/charts/charttitle.cpp b/src/charts/charttitle.cpp index 22fa689d..04f9902a 100644 --- a/src/charts/charttitle.cpp +++ b/src/charts/charttitle.cpp @@ -40,6 +40,7 @@ ChartTitle::ChartTitle(QGraphicsItem *parent) : QGraphicsTextItem(parent) { document()->setDocumentMargin(ChartPresenter::textMargin()); + setAcceptedMouseButtons({}); } ChartTitle::~ChartTitle() diff --git a/src/charts/qchartview.cpp b/src/charts/qchartview.cpp index f61798da..e7ca9db1 100644 --- a/src/charts/qchartview.cpp +++ b/src/charts/qchartview.cpp @@ -32,6 +32,7 @@ #include <private/qchart_p.h> #include <QtWidgets/QGraphicsScene> #include <QtWidgets/QRubberBand> +#include <QtWidgets/QGraphicsItem> /*! \enum QChartView::RubberBand @@ -50,6 +51,11 @@ \value RectangleRubberBand The rubber band is fixed to the point that was clicked and can be pulled both vertically and horizontally. + \value ClickThroughRubberBand + An option on the above rubber band choices that allows left clicks + to be passed on to chart items if those chart items accept clicks. + To select this, OR it with one of the rubber band selection modes. + \since 6.2 */ /*! @@ -128,7 +134,7 @@ void QChartView::setRubberBand(const RubberBands &rubberBand) #ifndef QT_NO_RUBBERBAND d_ptr->m_rubberBandFlags = rubberBand; - if (!d_ptr->m_rubberBandFlags) { + if (!(d_ptr->m_rubberBandFlags & ~RubberBands(ClickThroughRubberBand))) { delete d_ptr->m_rubberBand; d_ptr->m_rubberBand = nullptr; return; @@ -163,9 +169,14 @@ QChartView::RubberBands QChartView::rubberBand() const void QChartView::mousePressEvent(QMouseEvent *event) { #ifndef QT_NO_RUBBERBAND + QGraphicsItem *itemUnderCursor = itemAt(event->pos()); + bool itemUnderCursorAcceptsLMB = (itemUnderCursor->acceptedMouseButtons() & Qt::LeftButton); + bool clickThrough = d_ptr->m_rubberBandFlags.testFlag(ClickThroughRubberBand); QRectF plotArea = d_ptr->m_chart->plotArea(); if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() - && event->button() == Qt::LeftButton && plotArea.contains(event->pos())) { + && event->button() == Qt::LeftButton + && plotArea.contains(event->pos()) + && !(clickThrough && itemUnderCursorAcceptsLMB)) { d_ptr->m_rubberBandOrigin = event->pos(); d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize())); d_ptr->m_rubberBand->show(); @@ -223,10 +234,10 @@ void QChartView::mouseReleaseEvent(QMouseEvent *event) // Since plotArea uses QRectF and rubberband uses QRect, we can't just blindly use // rubberband's dimensions for vertical and horizontal rubberbands, where one // dimension must match the corresponding plotArea dimension exactly. - if (d_ptr->m_rubberBandFlags == VerticalRubberBand) { + if (d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) { rect.setX(d_ptr->m_chart->plotArea().x()); rect.setWidth(d_ptr->m_chart->plotArea().width()); - } else if (d_ptr->m_rubberBandFlags == HorizontalRubberBand) { + } else if (d_ptr->m_rubberBandFlags.testFlag(HorizontalRubberBand)) { rect.setY(d_ptr->m_chart->plotArea().y()); rect.setHeight(d_ptr->m_chart->plotArea().height()); } @@ -238,13 +249,13 @@ void QChartView::mouseReleaseEvent(QMouseEvent *event) // If vertical or horizontal rubberband mode, restrict zoom out to specified axis. // Since there is no suitable API for that, use zoomIn with rect bigger than the // plot area. - if (d_ptr->m_rubberBandFlags == VerticalRubberBand - || d_ptr->m_rubberBandFlags == HorizontalRubberBand) { + if (d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand) + || d_ptr->m_rubberBandFlags.testFlag(HorizontalRubberBand)) { QRectF rect = d_ptr->m_chart->plotArea(); - if (d_ptr->m_rubberBandFlags == VerticalRubberBand) { + if (d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) { qreal adjustment = rect.height() / 2; rect.adjust(0, -adjustment, 0, adjustment); - } else if (d_ptr->m_rubberBandFlags == HorizontalRubberBand) { + } else if (d_ptr->m_rubberBandFlags.testFlag(HorizontalRubberBand)) { qreal adjustment = rect.width() / 2; rect.adjust(-adjustment, 0, adjustment, 0); } diff --git a/src/charts/qchartview.h b/src/charts/qchartview.h index 6ebbf475..b6b4e1e8 100644 --- a/src/charts/qchartview.h +++ b/src/charts/qchartview.h @@ -54,7 +54,8 @@ public: NoRubberBand = 0x0, VerticalRubberBand = 0x1, HorizontalRubberBand = 0x2, - RectangleRubberBand = 0x3 + RectangleRubberBand = 0x3, + ClickThroughRubberBand = 0x80 }; Q_DECLARE_FLAGS(RubberBands, RubberBand) |