diff --git a/examples/legend/mainwidget.cpp b/examples/legend/mainwidget.cpp index 8e60a4d..81468e8 100644 --- a/examples/legend/mainwidget.cpp +++ b/examples/legend/mainwidget.cpp @@ -66,6 +66,14 @@ MainWidget::MainWidget(QWidget *parent) : connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom())); m_buttonLayout->addWidget(bottomButton, 7, 0); + QPushButton *boldButton = new QPushButton("Toggle bold"); + connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold())); + m_buttonLayout->addWidget(boldButton, 8, 0); + + QPushButton *italicButton = new QPushButton("Toggle italic"); + connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic())); + m_buttonLayout->addWidget(italicButton, 9, 0); + m_legendPosX = new QDoubleSpinBox(); m_legendPosY = new QDoubleSpinBox(); m_legendWidth = new QDoubleSpinBox(); @@ -90,9 +98,18 @@ MainWidget::MainWidget(QWidget *parent) : m_chart = new QChart(); m_chartView = new QChartView(m_chart, this); + // Create spinbox to modify font size + m_fontSize = new QDoubleSpinBox(); + m_fontSize->setValue(m_chart->legend()->font().pointSizeF()); + connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged())); + + QFormLayout* fontLayout = new QFormLayout(); + fontLayout->addRow("Legend font size", m_fontSize); + // Create layout for grid and detached legend m_mainLayout = new QGridLayout(); m_mainLayout->addLayout(m_buttonLayout, 0, 0); + m_mainLayout->addLayout(fontLayout,1,0); m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1); setLayout(m_mainLayout); @@ -212,6 +229,27 @@ void MainWidget::setLegendBottom() m_chart->legend()->setAlignment(Qt::AlignBottom); } +void MainWidget::toggleBold() +{ + QFont font = m_chart->legend()->font(); + font.setBold(!font.bold()); + m_chart->legend()->setFont(font); +} + +void MainWidget::toggleItalic() +{ + QFont font = m_chart->legend()->font(); + font.setItalic(!font.italic()); + m_chart->legend()->setFont(font); +} + +void MainWidget::fontSizeChanged() +{ + QFont font = m_chart->legend()->font(); + font.setPointSizeF(m_fontSize->value()); + m_chart->legend()->setFont(font); +} + void MainWidget::updateLegendLayout() { //![4] diff --git a/examples/legend/mainwidget.h b/examples/legend/mainwidget.h index fa77344..e0b2fbd 100644 --- a/examples/legend/mainwidget.h +++ b/examples/legend/mainwidget.h @@ -56,8 +56,13 @@ public slots: void setLegendTop(); void setLegendBottom(); + void toggleBold(); + void toggleItalic(); + void fontSizeChanged(); + void updateLegendLayout(); + private: QChart *m_chart; @@ -66,6 +71,9 @@ private: QChartView *m_chartView; QGridLayout *m_mainLayout; QGridLayout *m_buttonLayout; + QGridLayout *m_fontLayout; + + QDoubleSpinBox *m_fontSize; // For detached layout QGroupBox* m_legendSettings; diff --git a/src/legend/legendmarker.cpp b/src/legend/legendmarker.cpp index 497fdee..9962a06 100644 --- a/src/legend/legendmarker.cpp +++ b/src/legend/legendmarker.cpp @@ -71,6 +71,17 @@ QBrush LegendMarker::brush() const return m_rectItem->brush(); } +void LegendMarker::setFont(const QFont &font) +{ + m_textItem->setFont(font); + updateLayout(); +} + +QFont LegendMarker::font() const +{ + return m_textItem->font(); +} + void LegendMarker::setLabel(const QString label) { m_textItem->setText(label); diff --git a/src/legend/legendmarker_p.h b/src/legend/legendmarker_p.h index e269c32..611d5cb 100644 --- a/src/legend/legendmarker_p.h +++ b/src/legend/legendmarker_p.h @@ -59,6 +59,9 @@ public: void setBrush(const QBrush &brush); QBrush brush() const; + void setFont(const QFont &font); + QFont font() const; + void setSize(const QSize& size); void setLabel(const QString label); diff --git a/src/legend/qlegend.cpp b/src/legend/qlegend.cpp index 8e2a8d0..ec77509 100644 --- a/src/legend/qlegend.cpp +++ b/src/legend/qlegend.cpp @@ -121,6 +121,15 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ /*! + \property QLegend::font + The font of markers used by legend +*/ +/*! + \qmlproperty color Legend::font + The font of markers used by legend +*/ + +/*! \fn void QLegend::backgroundVisibleChanged(bool) The visibility of the legend background changed to \a visible. */ @@ -136,6 +145,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ /*! + \fn void QLegend::fontChanged(QFont) + The font of markers of the legend changed to \a font. +*/ + +/*! \fn qreal QLegend::minWidth() const Returns minimum width of the legend */ @@ -259,6 +273,19 @@ QColor QLegend::borderColor() return d_ptr->m_pen.color(); } +void QLegend::setFont(const QFont &font) +{ + if (d_ptr->m_font != font) { + d_ptr->setFont(font); + emit fontChanged(font); + } +} + +QFont QLegend::font() const +{ + return d_ptr->m_font; +} + void QLegend::setAlignment(Qt::Alignment alignment) { if(d_ptr->m_alignment!=alignment) { @@ -680,13 +707,27 @@ int QLegendPrivate::roundness(qreal size) return 100*m_diameter/int(size); } +void QLegendPrivate::setFont(const QFont &font) +{ + m_font = font; + QList items = m_markers->childItems(); + + foreach (QGraphicsItem *markers, items) { + LegendMarker *marker = static_cast(markers); + marker->setFont(m_font); + } + updateLayout(); +} + void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) { Q_UNUSED(domain) QList markers = series->d_ptr->createLegendMarker(q_ptr); - foreach(LegendMarker* marker, markers) + foreach(LegendMarker* marker, markers) { + marker->setFont(m_font); m_markers->addToGroup(marker); + } QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); diff --git a/src/legend/qlegend.h b/src/legend/qlegend.h index c6dbb72..313469c 100644 --- a/src/legend/qlegend.h +++ b/src/legend/qlegend.h @@ -47,6 +47,7 @@ class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged) Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged) + Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) private: explicit QLegend(QChart *chart); @@ -67,6 +68,9 @@ public: void setBorderColor(QColor color); QColor borderColor(); + void setFont(const QFont &font); + QFont font() const; + void setAlignment(Qt::Alignment alignment); Qt::Alignment alignment() const; @@ -89,6 +93,7 @@ Q_SIGNALS: void backgroundVisibleChanged(bool visible); void colorChanged(QColor color); void borderColorChanged(QColor color); + void fontChanged(QFont font); private: QScopedPointer d_ptr; diff --git a/src/legend/qlegend_p.h b/src/legend/qlegend_p.h index da2eafd..4969873 100644 --- a/src/legend/qlegend_p.h +++ b/src/legend/qlegend_p.h @@ -51,6 +51,7 @@ public: void updateDetachedLayout(); void attachToChart(); int roundness(qreal size); + void setFont(const QFont &font); public Q_SLOTS: void handleSeriesAdded(QAbstractSeries *series, Domain *domain); @@ -67,6 +68,7 @@ private: Qt::Alignment m_alignment; QBrush m_brush; QPen m_pen; + QFont m_font; QRectF m_rect; qreal m_offsetX; qreal m_offsetY;