qlegendmarker.cpp
294 lines
| 6.7 KiB
| text/x-c
|
CppLexer
|
r2845 | /****************************************************************************** | ||
** | ||||
** Copyright (C) 2015 The Qt Company Ltd. | ||||
** Contact: http://www.qt.io/licensing/ | ||||
** | ||||
** This file is part of the Qt Charts module. | ||||
** | ||||
** $QT_BEGIN_LICENSE:COMM$ | ||||
** | ||||
** Commercial License Usage | ||||
** Licensees holding valid commercial Qt licenses may use this file in | ||||
** accordance with the commercial license agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and The Qt Company. For licensing terms | ||||
** and conditions see http://www.qt.io/terms-conditions. For further | ||||
** information use the contact form at http://www.qt.io/contact-us. | ||||
** | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
******************************************************************************/ | ||||
|
r2160 | |||
|
r2714 | #include <QtCharts/QLegendMarker> | ||
#include <private/qlegendmarker_p.h> | ||||
#include <private/legendmarkeritem_p.h> | ||||
#include <QtCharts/QLegend> | ||||
#include <private/qlegend_p.h> | ||||
#include <private/legendlayout_p.h> | ||||
#include <QtGui/QFontMetrics> | ||||
#include <QtWidgets/QGraphicsSceneEvent> | ||||
#include <QtCharts/QAbstractSeries> | ||||
|
r2160 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r2174 | |||
|
r2216 | /*! | ||
\class QLegendMarker | ||||
|
r2639 | \inmodule Qt Charts | ||
|
r2520 | \brief LegendMarker object. | ||
|
r2216 | |||
|
r2217 | QLegendMarker is abstract object that can be used to access markers inside QLegend. Legend marker consists of two | ||
items: The colored box, which reflects the color of series and label, which is the name of series (or label of slice/barset | ||||
in case of pie or bar series) | ||||
The QLegendMarker is always related to one series. | ||||
|
r2216 | |||
|
r2217 | \image examples_percentbarchart_legend.png | ||
|
r2216 | |||
\sa QLegend | ||||
*/ | ||||
/*! | ||||
\enum QLegendMarker::LegendMarkerType | ||||
The type of the legendmarker object. | ||||
\value LegendMarkerTypeArea | ||||
\value LegendMarkerTypeBar | ||||
\value LegendMarkerTypePie | ||||
\value LegendMarkerTypeXY | ||||
|
r2548 | \value LegendMarkerTypeBoxPlot | ||
|
r2216 | */ | ||
/*! | ||||
\fn virtual LegendMarkerType QLegendMarker::type() = 0; | ||||
Returns the type of legendmarker. Type depends of the related series. LegendMarkerTypeXY is used for all QXYSeries derived | ||||
classes. | ||||
*/ | ||||
/*! | ||||
\fn virtual QAbstractSeries* QLegendMarker::series() = 0; | ||||
Returns pointer to series, which is related to this marker. Marker is always related to some series. | ||||
*/ | ||||
/*! | ||||
\fn void QLegendMarker::clicked(); | ||||
This signal is emitted, when marker is clicked with mouse. | ||||
*/ | ||||
/*! | ||||
\fn void QLegendMarker::hovered(bool status); | ||||
This signal is emitted, when mouse is hovered over marker. \a status is true, when mouse enters the marker | ||||
and false when it leaves the marker. | ||||
*/ | ||||
|
r2265 | /*! | ||
\fn void QLegendMarker::labelChanged() | ||||
This signal is emitted when the label of the legend marker has changed. | ||||
*/ | ||||
/*! | ||||
\fn void QLegendMarker::labelBrushChanged() | ||||
This signal is emitted when the label brush of the legend marker has changed. | ||||
*/ | ||||
/*! | ||||
\fn void QLegendMarker::fontChanged() | ||||
This signal is emitted when the (label) font of the legend marker has changed. | ||||
*/ | ||||
/*! | ||||
\fn void QLegendMarker::penChanged() | ||||
This signal is emitted when the pen of the legend marker has changed. | ||||
*/ | ||||
/*! | ||||
\fn void QLegendMarker::brushChanged() | ||||
This signal is emitted when the brush of the legend marker has changed. | ||||
*/ | ||||
/*! | ||||
\fn void QLegendMarker::visibleChanged() | ||||
This signal is emitted when the visibility of the legend marker has changed. | ||||
*/ | ||||
|
r2225 | /*! | ||
\property QLegendMarker::label | ||||
Label of the marker. This is the text that is shown in legend. | ||||
*/ | ||||
|
r2226 | /*! | ||
\property QLegendMarker::labelBrush | ||||
Brush of the label | ||||
*/ | ||||
/*! | ||||
\property QLegendMarker::font | ||||
Font of the label | ||||
*/ | ||||
/*! | ||||
\property QLegendMarker::pen | ||||
Pen of the marker. This is the outline of the colored square. | ||||
*/ | ||||
/*! | ||||
\property QLegendMarker::brush | ||||
Brush of the marker. This is the inside of the colored square. | ||||
*/ | ||||
/*! | ||||
\property QLegendMarker::visible | ||||
Visibility of the legend marker. Affects label and the colored square. | ||||
*/ | ||||
|
r2216 | /*! | ||
|
r2265 | \internal | ||
|
r2216 | */ | ||
|
r2167 | QLegendMarker::QLegendMarker(QLegendMarkerPrivate &d, QObject *parent) : | ||
QObject(parent), | ||||
d_ptr(&d) | ||||
{ | ||||
|
r2182 | d_ptr->m_item->setVisible(d_ptr->series()->isVisible()); | ||
|
r2167 | } | ||
|
r2160 | |||
|
r2216 | /*! | ||
Destructor of marker | ||||
*/ | ||||
|
r2160 | QLegendMarker::~QLegendMarker() | ||
{ | ||||
} | ||||
|
r2216 | /*! | ||
Returns the label of the marker. | ||||
*/ | ||||
|
r2160 | QString QLegendMarker::label() const | ||
{ | ||||
|
r2174 | return d_ptr->m_item->label(); | ||
|
r2160 | } | ||
|
r2216 | /*! | ||
Sets the \a label of marker. Note that changing name of series will also change label of its marker. | ||||
*/ | ||||
|
r2160 | void QLegendMarker::setLabel(const QString &label) | ||
{ | ||||
|
r2429 | if (label.isEmpty()) { | ||
d_ptr->m_customLabel = false; | ||||
} else { | ||||
d_ptr->m_customLabel = true; | ||||
d_ptr->m_item->setLabel(label); | ||||
} | ||||
|
r2163 | } | ||
|
r2216 | /*! | ||
Returns the brush which is used to draw label. | ||||
*/ | ||||
|
r2163 | QBrush QLegendMarker::labelBrush() const | ||
{ | ||||
|
r2195 | return d_ptr->m_item->labelBrush(); | ||
|
r2163 | } | ||
|
r2216 | /*! | ||
Sets the \a brush of label | ||||
*/ | ||||
|
r2163 | void QLegendMarker::setLabelBrush(const QBrush &brush) | ||
{ | ||||
|
r2174 | d_ptr->m_item->setLabelBrush(brush); | ||
|
r2163 | } | ||
|
r2216 | /*! | ||
Retuns the font of label | ||||
*/ | ||||
|
r2163 | QFont QLegendMarker::font() const | ||
{ | ||||
|
r2174 | return d_ptr->m_item->font(); | ||
|
r2163 | } | ||
|
r2216 | /*! | ||
Sets the \a font of label | ||||
*/ | ||||
|
r2163 | void QLegendMarker::setFont(const QFont &font) | ||
{ | ||||
|
r2174 | d_ptr->m_item->setFont(font); | ||
|
r2160 | } | ||
|
r2216 | /*! | ||
Returns the pen of marker item | ||||
*/ | ||||
|
r2160 | QPen QLegendMarker::pen() const | ||
{ | ||||
|
r2174 | return d_ptr->m_item->pen(); | ||
|
r2160 | } | ||
|
r2216 | /*! | ||
Sets the \a pen of marker item | ||||
*/ | ||||
|
r2160 | void QLegendMarker::setPen(const QPen &pen) | ||
{ | ||||
|
r2429 | if (pen == QPen(Qt::NoPen)) { | ||
d_ptr->m_customPen = false; | ||||
} else { | ||||
d_ptr->m_customPen = true; | ||||
d_ptr->m_item->setPen(pen); | ||||
} | ||||
|
r2160 | } | ||
|
r2216 | /*! | ||
Returns the brush of marker item | ||||
*/ | ||||
|
r2160 | QBrush QLegendMarker::brush() const | ||
{ | ||||
|
r2174 | return d_ptr->m_item->brush(); | ||
|
r2160 | } | ||
|
r2216 | /*! | ||
Sets the \a brush of marker item. Note that changing color of the series also changes this. | ||||
*/ | ||||
|
r2160 | void QLegendMarker::setBrush(const QBrush &brush) | ||
{ | ||||
|
r2429 | if (brush == QBrush(Qt::NoBrush)) { | ||
d_ptr->m_customBrush = false; | ||||
} else { | ||||
d_ptr->m_customBrush = true; | ||||
d_ptr->m_item->setBrush(brush); | ||||
} | ||||
|
r2160 | } | ||
|
r2216 | /*! | ||
Returns visibility of the marker | ||||
*/ | ||||
|
r2160 | bool QLegendMarker::isVisible() const | ||
{ | ||||
|
r2174 | return d_ptr->m_item->isVisible(); | ||
|
r2160 | } | ||
|
r2216 | /*! | ||
Sets markers visibility to \a visible | ||||
*/ | ||||
|
r2160 | void QLegendMarker::setVisible(bool visible) | ||
{ | ||||
|
r2174 | d_ptr->m_item->setVisible(visible); | ||
|
r2160 | } | ||
|
r2163 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
r2171 | QLegendMarkerPrivate::QLegendMarkerPrivate(QLegendMarker *q, QLegend *legend) : | ||
|
r2202 | m_legend(legend), | ||
|
r2429 | m_customLabel(false), | ||
m_customBrush(false), | ||||
m_customPen(false), | ||||
|
r2202 | q_ptr(q) | ||
|
r2167 | { | ||
m_item = new LegendMarkerItem(this); | ||||
} | ||||
|
r2163 | |||
|
r2166 | QLegendMarkerPrivate::~QLegendMarkerPrivate() | ||
{ | ||||
|
r2381 | delete m_item; | ||
|
r2166 | } | ||
|
r2194 | void QLegendMarkerPrivate::invalidateLegend() | ||
{ | ||||
m_legend->d_ptr->m_layout->invalidate(); | ||||
} | ||||
|
r2160 | #include "moc_qlegendmarker.cpp" | ||
|
r2163 | #include "moc_qlegendmarker_p.cpp" | ||
|
r2160 | |||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||