| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
|---|
| 4 | ** Contact: https://www.qt.io/licensing/ | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the Qt Charts module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:GPL$ | 
|---|
| 9 | ** Commercial License Usage | 
|---|
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
|---|
| 11 | ** accordance with the commercial license agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
|---|
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
|---|
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
|---|
| 16 | ** | 
|---|
| 17 | ** GNU General Public License Usage | 
|---|
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 19 | ** General Public License version 3 or (at your option) any later version | 
|---|
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by | 
|---|
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 | 
|---|
| 22 | ** included in the packaging of this file. Please review the following | 
|---|
| 23 | ** information to ensure the GNU General Public License requirements will | 
|---|
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
|---|
| 25 | ** | 
|---|
| 26 | ** $QT_END_LICENSE$ | 
|---|
| 27 | ** | 
|---|
| 28 | ****************************************************************************/ | 
|---|
| 29 |  | 
|---|
| 30 | #include <QtCharts/QLegendMarker> | 
|---|
| 31 | #include <private/qlegendmarker_p.h> | 
|---|
| 32 | #include <private/legendmarkeritem_p.h> | 
|---|
| 33 | #include <QtCharts/QLegend> | 
|---|
| 34 | #include <private/qlegend_p.h> | 
|---|
| 35 | #include <private/legendlayout_p.h> | 
|---|
| 36 | #include <QtGui/QFontMetrics> | 
|---|
| 37 | #include <QtWidgets/QGraphicsSceneEvent> | 
|---|
| 38 | #include <QtCharts/QAbstractSeries> | 
|---|
| 39 |  | 
|---|
| 40 | QT_CHARTS_BEGIN_NAMESPACE | 
|---|
| 41 |  | 
|---|
| 42 | /*! | 
|---|
| 43 | \class QLegendMarker | 
|---|
| 44 | \inmodule QtCharts | 
|---|
| 45 | \brief The QLegendMarker class is an abstract object that can be used to access | 
|---|
| 46 | markers within a legend. | 
|---|
| 47 |  | 
|---|
| 48 | A legend marker consists of an icon and a label. The icon color corresponds to the color | 
|---|
| 49 | used to draw a series and the label displays the name of the series (or the label of the | 
|---|
| 50 | slice for a pie series or bar set for a bar series). A legend marker is always related to | 
|---|
| 51 | one series, slice, or bar set. | 
|---|
| 52 |  | 
|---|
| 53 | \image examples_percentbarchart_legend.png | 
|---|
| 54 |  | 
|---|
| 55 | \sa QLegend | 
|---|
| 56 | */ | 
|---|
| 57 | /*! | 
|---|
| 58 | \enum QLegendMarker::LegendMarkerType | 
|---|
| 59 | \since 5.8 | 
|---|
| 60 |  | 
|---|
| 61 | The type of the legend marker object. | 
|---|
| 62 |  | 
|---|
| 63 | \value LegendMarkerTypeArea | 
|---|
| 64 | A legend marker for an area series. | 
|---|
| 65 | \value LegendMarkerTypeBar | 
|---|
| 66 | A legend marker for a bar set. | 
|---|
| 67 | \value LegendMarkerTypePie | 
|---|
| 68 | A legend marker for a pie slice. | 
|---|
| 69 | \value LegendMarkerTypeXY | 
|---|
| 70 | A legend marker for a line, spline, or scatter series. | 
|---|
| 71 | \value LegendMarkerTypeBoxPlot | 
|---|
| 72 | A legend marker for a box plot series. | 
|---|
| 73 | \value LegendMarkerTypeCandlestick | 
|---|
| 74 | A legend marker for a candlestick series. | 
|---|
| 75 | */ | 
|---|
| 76 |  | 
|---|
| 77 | /*! | 
|---|
| 78 | \fn virtual LegendMarkerType QLegendMarker::type() = 0; | 
|---|
| 79 | Returns the type of the legend marker for the related series, pie slice, or bar set. | 
|---|
| 80 |  | 
|---|
| 81 | \sa LegendMarkerType | 
|---|
| 82 | */ | 
|---|
| 83 |  | 
|---|
| 84 | /*! | 
|---|
| 85 | \fn virtual QAbstractSeries* QLegendMarker::series() = 0; | 
|---|
| 86 | Returns a pointer to the series that is related to this legend marker. A legend marker | 
|---|
| 87 | is always related to a series. | 
|---|
| 88 | */ | 
|---|
| 89 |  | 
|---|
| 90 | /*! | 
|---|
| 91 | \fn void QLegendMarker::clicked(); | 
|---|
| 92 | This signal is emitted when the legend marker is clicked. | 
|---|
| 93 | */ | 
|---|
| 94 |  | 
|---|
| 95 | /*! | 
|---|
| 96 | \fn void QLegendMarker::hovered(bool status); | 
|---|
| 97 | This signal is emitted when a mouse is hovered over the legend marker. | 
|---|
| 98 | When the mouse moves over the marker, \a status turns \c true, and when | 
|---|
| 99 | the mouse moves away again, it turns \c false. | 
|---|
| 100 | */ | 
|---|
| 101 |  | 
|---|
| 102 | /*! | 
|---|
| 103 | \fn void QLegendMarker::labelChanged() | 
|---|
| 104 | This signal is emitted when the label of the legend marker has changed. | 
|---|
| 105 | */ | 
|---|
| 106 |  | 
|---|
| 107 | /*! | 
|---|
| 108 | \fn void QLegendMarker::labelBrushChanged() | 
|---|
| 109 | This signal is emitted when the label brush of the legend marker has changed. | 
|---|
| 110 | */ | 
|---|
| 111 |  | 
|---|
| 112 | /*! | 
|---|
| 113 | \fn void QLegendMarker::fontChanged() | 
|---|
| 114 | This signal is emitted when the (label) font of the legend marker has changed. | 
|---|
| 115 | */ | 
|---|
| 116 |  | 
|---|
| 117 | /*! | 
|---|
| 118 | \fn void QLegendMarker::penChanged() | 
|---|
| 119 | This signal is emitted when the pen of the legend marker has changed. | 
|---|
| 120 | */ | 
|---|
| 121 |  | 
|---|
| 122 | /*! | 
|---|
| 123 | \fn void QLegendMarker::brushChanged() | 
|---|
| 124 | This signal is emitted when the brush of the legend marker has changed. | 
|---|
| 125 | */ | 
|---|
| 126 |  | 
|---|
| 127 | /*! | 
|---|
| 128 | \fn void QLegendMarker::visibleChanged() | 
|---|
| 129 | This signal is emitted when the visibility of the legend marker has changed. | 
|---|
| 130 | */ | 
|---|
| 131 |  | 
|---|
| 132 | /*! | 
|---|
| 133 | \property QLegendMarker::label | 
|---|
| 134 | \brief The text shown in the legend for a legend marker. | 
|---|
| 135 | */ | 
|---|
| 136 |  | 
|---|
| 137 | /*! | 
|---|
| 138 | \property QLegendMarker::labelBrush | 
|---|
| 139 | \brief The brush of the label. | 
|---|
| 140 | */ | 
|---|
| 141 |  | 
|---|
| 142 | /*! | 
|---|
| 143 | \property QLegendMarker::font | 
|---|
| 144 | \brief The font of the label. | 
|---|
| 145 | */ | 
|---|
| 146 |  | 
|---|
| 147 | /*! | 
|---|
| 148 | \property QLegendMarker::pen | 
|---|
| 149 | \brief The pen used to draw the outline of the icon. | 
|---|
| 150 | */ | 
|---|
| 151 |  | 
|---|
| 152 | /*! | 
|---|
| 153 | \property QLegendMarker::brush | 
|---|
| 154 | \brief The brush used to fill the icon. | 
|---|
| 155 | */ | 
|---|
| 156 |  | 
|---|
| 157 | /*! | 
|---|
| 158 | \property QLegendMarker::visible | 
|---|
| 159 | \brief The visibility of the legend marker. | 
|---|
| 160 |  | 
|---|
| 161 | The visibility affects both the legend marker label and the icon. | 
|---|
| 162 | */ | 
|---|
| 163 |  | 
|---|
| 164 | /*! | 
|---|
| 165 | \property QLegendMarker::shape | 
|---|
| 166 |  | 
|---|
| 167 | The shape of the legend marker. Defaults to QLegend::MarkerShapeDefault, which indicates | 
|---|
| 168 | the shape is determined by QLegend::markerShape property. | 
|---|
| 169 | */ | 
|---|
| 170 |  | 
|---|
| 171 | /*! | 
|---|
| 172 | \internal | 
|---|
| 173 | */ | 
|---|
| 174 | QLegendMarker::QLegendMarker(QLegendMarkerPrivate &d, QObject *parent) : | 
|---|
| 175 | QObject(parent), | 
|---|
| 176 | d_ptr(&d) | 
|---|
| 177 | { | 
|---|
| 178 | d_ptr->m_item->setVisible(d_ptr->series()->isVisible()); | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | /*! | 
|---|
| 182 | Removes the legend marker. | 
|---|
| 183 | */ | 
|---|
| 184 | QLegendMarker::~QLegendMarker() | 
|---|
| 185 | { | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | /*! | 
|---|
| 189 | Returns the label of the marker. | 
|---|
| 190 | */ | 
|---|
| 191 | QString QLegendMarker::label() const | 
|---|
| 192 | { | 
|---|
| 193 | return d_ptr->m_item->label(); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | /*! | 
|---|
| 197 | Sets the label of the marker to \a label. | 
|---|
| 198 |  | 
|---|
| 199 | \note Changing the name of a series also changes the label of its marker. | 
|---|
| 200 | */ | 
|---|
| 201 | void QLegendMarker::setLabel(const QString &label) | 
|---|
| 202 | { | 
|---|
| 203 | if (label.isEmpty()) { | 
|---|
| 204 | d_ptr->m_customLabel = false; | 
|---|
| 205 | } else { | 
|---|
| 206 | d_ptr->m_customLabel = true; | 
|---|
| 207 | d_ptr->m_item->setLabel(label); | 
|---|
| 208 | } | 
|---|
| 209 | } | 
|---|
| 210 | /*! | 
|---|
| 211 | Returns the brush that is used to draw the label. | 
|---|
| 212 | */ | 
|---|
| 213 | QBrush QLegendMarker::labelBrush() const | 
|---|
| 214 | { | 
|---|
| 215 | return d_ptr->m_item->labelBrush(); | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | /*! | 
|---|
| 219 | Sets the the brush used to draw to label to \a brush. | 
|---|
| 220 | */ | 
|---|
| 221 | void QLegendMarker::setLabelBrush(const QBrush &brush) | 
|---|
| 222 | { | 
|---|
| 223 | d_ptr->m_item->setLabelBrush(brush); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | /*! | 
|---|
| 227 | Retuns the font of the label. | 
|---|
| 228 | */ | 
|---|
| 229 | QFont QLegendMarker::font() const | 
|---|
| 230 | { | 
|---|
| 231 | return d_ptr->m_item->font(); | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | /*! | 
|---|
| 235 | Sets the font of the label to \a font. | 
|---|
| 236 | */ | 
|---|
| 237 | void QLegendMarker::setFont(const QFont &font) | 
|---|
| 238 | { | 
|---|
| 239 | d_ptr->m_item->setFont(font); | 
|---|
| 240 | } | 
|---|
| 241 |  | 
|---|
| 242 | /*! | 
|---|
| 243 | Returns the pen used to draw the outline of the icon. | 
|---|
| 244 | */ | 
|---|
| 245 | QPen QLegendMarker::pen() const | 
|---|
| 246 | { | 
|---|
| 247 | return d_ptr->m_item->pen(); | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | /*! | 
|---|
| 251 | Sets the \a pen used to draw the outline of the icon to \a pen. | 
|---|
| 252 | */ | 
|---|
| 253 | void QLegendMarker::setPen(const QPen &pen) | 
|---|
| 254 | { | 
|---|
| 255 | if (pen == QPen(Qt::NoPen)) { | 
|---|
| 256 | d_ptr->m_customPen = false; | 
|---|
| 257 | } else { | 
|---|
| 258 | d_ptr->m_customPen = true; | 
|---|
| 259 | d_ptr->m_item->setPen(pen); | 
|---|
| 260 | } | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | /*! | 
|---|
| 264 | Returns the brush used to fill the icon. | 
|---|
| 265 | */ | 
|---|
| 266 | QBrush QLegendMarker::brush() const | 
|---|
| 267 | { | 
|---|
| 268 | return d_ptr->m_item->brush(); | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | /*! | 
|---|
| 272 | Sets the brush used to fill the icon to \a brush. | 
|---|
| 273 |  | 
|---|
| 274 | \note Changing the color of the series also changes the color of the icon. | 
|---|
| 275 | */ | 
|---|
| 276 | void QLegendMarker::setBrush(const QBrush &brush) | 
|---|
| 277 | { | 
|---|
| 278 | if (brush == QBrush(Qt::NoBrush)) { | 
|---|
| 279 | d_ptr->m_customBrush = false; | 
|---|
| 280 | } else { | 
|---|
| 281 | d_ptr->m_customBrush = true; | 
|---|
| 282 | d_ptr->m_item->setBrush(brush); | 
|---|
| 283 | } | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | /*! | 
|---|
| 287 | Returns the visibility of the marker. | 
|---|
| 288 | */ | 
|---|
| 289 | bool QLegendMarker::isVisible() const | 
|---|
| 290 | { | 
|---|
| 291 | return d_ptr->m_item->isVisible(); | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 | /*! | 
|---|
| 295 | Sets the marker's visibility to \a visible. | 
|---|
| 296 | */ | 
|---|
| 297 | void QLegendMarker::setVisible(bool visible) | 
|---|
| 298 | { | 
|---|
| 299 | d_ptr->m_item->setVisible(visible); | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | QLegend::MarkerShape QLegendMarker::shape() const | 
|---|
| 303 | { | 
|---|
| 304 | return d_ptr->m_item->markerShape(); | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|
| 307 | void QLegendMarker::setShape(QLegend::MarkerShape shape) | 
|---|
| 308 | { | 
|---|
| 309 | if (shape != d_ptr->m_item->markerShape()) { | 
|---|
| 310 | d_ptr->m_item->setMarkerShape(shape); | 
|---|
| 311 | d_ptr->handleShapeChange(); | 
|---|
| 312 | emit shapeChanged(); | 
|---|
| 313 | } | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 317 | QLegendMarkerPrivate::QLegendMarkerPrivate(QLegendMarker *q, QLegend *legend) : | 
|---|
| 318 | m_legend(legend), | 
|---|
| 319 | m_customLabel(false), | 
|---|
| 320 | m_customBrush(false), | 
|---|
| 321 | m_customPen(false), | 
|---|
| 322 | q_ptr(q) | 
|---|
| 323 | { | 
|---|
| 324 | m_item = new LegendMarkerItem(this); | 
|---|
| 325 |  | 
|---|
| 326 | connect(sender: legend, signal: &QLegend::markerShapeChanged, receiver: this, | 
|---|
| 327 | slot: &QLegendMarkerPrivate::handleShapeChange); | 
|---|
| 328 | } | 
|---|
| 329 |  | 
|---|
| 330 | QLegendMarkerPrivate::~QLegendMarkerPrivate() | 
|---|
| 331 | { | 
|---|
| 332 | delete m_item; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | void QLegendMarkerPrivate::invalidateLegend() | 
|---|
| 336 | { | 
|---|
| 337 | m_item->updateGeometry(); | 
|---|
| 338 | m_legend->d_ptr->m_layout->invalidate(); | 
|---|
| 339 | } | 
|---|
| 340 |  | 
|---|
| 341 | void QLegendMarkerPrivate::invalidateAllItems() | 
|---|
| 342 | { | 
|---|
| 343 | QList<QLegendMarker *> markers = m_legend->markers(); | 
|---|
| 344 | for (int i = 0; i < markers.size(); i++) | 
|---|
| 345 | markers.at(i)->d_ptr->m_item->updateGeometry(); | 
|---|
| 346 | m_legend->d_ptr->m_layout->invalidate(); | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | void QLegendMarkerPrivate::handleShapeChange() | 
|---|
| 350 | { | 
|---|
| 351 | m_item->updateMarkerShapeAndSize(); | 
|---|
| 352 | m_legend->d_ptr->m_layout->invalidate(); | 
|---|
| 353 | } | 
|---|
| 354 |  | 
|---|
| 355 | QT_CHARTS_END_NAMESPACE | 
|---|
| 356 |  | 
|---|
| 357 | #include "moc_qlegendmarker.cpp" | 
|---|
| 358 | #include "moc_qlegendmarker_p.cpp" | 
|---|
| 359 |  | 
|---|