diff options
author | Keith Kyzivat <keith.kyzivat@qt.io> | 2021-04-29 19:40:15 -0400 |
---|---|---|
committer | Keith Kyzivat <keith.kyzivat@qt.io> | 2021-06-01 13:10:10 -0400 |
commit | 7f3ad23dc6e3703cc1c2cdfa926cefb412b027bb (patch) | |
tree | 1d64a7519f6a102a8fceb09230692fc2de756a45 /src/charts/legend/qlegend.cpp | |
parent | 1a0063ed1523ca7511e1801957c377b5bf40af27 (diff) |
Add move and resize to QLegend
This feature adds the ability to move and resize the chart legend when
it is not attached to the chart. It does not put a separate frame around
the legend, but instead takes control of mouse messages when the mouse
cursor is between the content area and the border of the legend.
The user can drag the legend around when they click and drag the top
edge of a detached legend. A visual rectangle is painted when the user
positions the cursor at the top of a detached legend. This area
identifies the region they can use to drag the legend around.
If the mouse pointer is moved beyond the edge of the chart, it will
attach to the edge that the mouse moved off of. The legend remains bound
to the area of the chart, even if dragging would have moved an edge of
the legend beyond the edge of the chart.
An attached legend can be detached by double clicking it.
Task-number: QTBUG-93477
Change-Id: I5adff45802ba326004f2ffebe27357c8f8b138d0
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Diffstat (limited to 'src/charts/legend/qlegend.cpp')
-rw-r--r-- | src/charts/legend/qlegend.cpp | 56 |
1 files changed, 52 insertions, 4 deletions
diff --git a/src/charts/legend/qlegend.cpp b/src/charts/legend/qlegend.cpp index 90fd3458..4c481641 100644 --- a/src/charts/legend/qlegend.cpp +++ b/src/charts/legend/qlegend.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Charts module of the Qt Toolkit. @@ -38,6 +38,7 @@ #include <QtCharts/QLegendMarker> #include <private/qlegendmarker_p.h> #include <private/legendmarkeritem_p.h> +#include <private/legendmoveresizehandler_p.h> #include <private/chartdataset_p.h> #include <QtGui/QPainter> #include <QtGui/QPen> @@ -309,8 +310,20 @@ void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q painter->setOpacity(opacity()); painter->setPen(d_ptr->m_pen); painter->setBrush(d_ptr->m_brush); - painter->drawRoundedRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()), - Qt::RelativeSize); + painter->drawRoundedRect(rect(), d_ptr->roundness(rect().width()), + d_ptr->roundness(rect().height()), Qt::RelativeSize); + + // Draw a border slightly larger than the contents rectangle + // of the layout so we don't incur overlap. The border gives the + // impression of a move/resize frame around the legend to the user + // and demarcates its boundaries. + if (!isAttachedToChart()) { + QRectF frameRect = d_ptr->m_layout->contentsRect(); + frameRect.adjust(-1., -1., 1., 1.); + painter->setBrush(Qt::NoBrush); + painter->drawRoundedRect(frameRect, d_ptr->roundness(rect().width()), + d_ptr->roundness(rect().height()), Qt::RelativeSize); + } } @@ -570,6 +583,39 @@ void QLegend::setShowToolTips(bool show) } } +/*! + Returns whether the legend can be dragged or resized using a mouse when it is detached. + + \sa QLegend::setInteractive() + \since 6.2 +*/ + +bool QLegend::isInteractive() const +{ + return d_ptr->m_interactive; +} + +/*! + When \a interactive is \c true and the legend is detached, the legend is able to be moved and + resized with a mouse in a similar way to a window. + + The legend will automatically attach to an edge of the chart by dragging it off of that edge. + Double clicking an attached legend will detach it. + This is \c false by default. + + \sa QLegend::isInteractive() + \since 6.2 +*/ + +void QLegend::setInteractive(bool interactive) +{ + if (d_ptr->m_interactive != interactive) { + d_ptr->m_interactive = interactive; + update(); + emit interactiveChanged(interactive); + } +} + QLegend::MarkerShape QLegend::markerShape() const { return d_ptr->m_markerShape; @@ -615,6 +661,7 @@ QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend : q_ptr(q), m_presenter(presenter), m_layout(new LegendLayout(q)), + m_resizer(new LegendMoveResizeHandler(q)), m_chart(chart), m_items(new QGraphicsItemGroup(q)), m_alignment(Qt::AlignTop), @@ -626,6 +673,7 @@ QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend m_backgroundVisible(false), m_reverseMarkers(false), m_showToolTips(false), + m_interactive(false), m_markerShape(QLegend::MarkerShapeRectangle) { m_items->setHandlesChildEvents(false); @@ -633,7 +681,7 @@ QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend QLegendPrivate::~QLegendPrivate() { - + delete m_resizer; } void QLegendPrivate::setOffset(const QPointF &offset) |