##// END OF EJS Templates
legend font fix
sauimone -
r1522:7ac28ceb0acb
parent child
Show More
@@ -66,6 +66,14 MainWidget::MainWidget(QWidget *parent) :
66 66 connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
67 67 m_buttonLayout->addWidget(bottomButton, 7, 0);
68 68
69 QPushButton *boldButton = new QPushButton("Toggle bold");
70 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
71 m_buttonLayout->addWidget(boldButton, 8, 0);
72
73 QPushButton *italicButton = new QPushButton("Toggle italic");
74 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
75 m_buttonLayout->addWidget(italicButton, 9, 0);
76
69 77 m_legendPosX = new QDoubleSpinBox();
70 78 m_legendPosY = new QDoubleSpinBox();
71 79 m_legendWidth = new QDoubleSpinBox();
@@ -90,9 +98,18 MainWidget::MainWidget(QWidget *parent) :
90 98 m_chart = new QChart();
91 99 m_chartView = new QChartView(m_chart, this);
92 100
101 // Create spinbox to modify font size
102 m_fontSize = new QDoubleSpinBox();
103 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
104 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
105
106 QFormLayout* fontLayout = new QFormLayout();
107 fontLayout->addRow("Legend font size", m_fontSize);
108
93 109 // Create layout for grid and detached legend
94 110 m_mainLayout = new QGridLayout();
95 111 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
112 m_mainLayout->addLayout(fontLayout,1,0);
96 113 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
97 114 setLayout(m_mainLayout);
98 115
@@ -212,6 +229,27 void MainWidget::setLegendBottom()
212 229 m_chart->legend()->setAlignment(Qt::AlignBottom);
213 230 }
214 231
232 void MainWidget::toggleBold()
233 {
234 QFont font = m_chart->legend()->font();
235 font.setBold(!font.bold());
236 m_chart->legend()->setFont(font);
237 }
238
239 void MainWidget::toggleItalic()
240 {
241 QFont font = m_chart->legend()->font();
242 font.setItalic(!font.italic());
243 m_chart->legend()->setFont(font);
244 }
245
246 void MainWidget::fontSizeChanged()
247 {
248 QFont font = m_chart->legend()->font();
249 font.setPointSizeF(m_fontSize->value());
250 m_chart->legend()->setFont(font);
251 }
252
215 253 void MainWidget::updateLegendLayout()
216 254 {
217 255 //![4]
@@ -56,8 +56,13 public slots:
56 56 void setLegendTop();
57 57 void setLegendBottom();
58 58
59 void toggleBold();
60 void toggleItalic();
61 void fontSizeChanged();
62
59 63 void updateLegendLayout();
60 64
65
61 66 private:
62 67
63 68 QChart *m_chart;
@@ -66,6 +71,9 private:
66 71 QChartView *m_chartView;
67 72 QGridLayout *m_mainLayout;
68 73 QGridLayout *m_buttonLayout;
74 QGridLayout *m_fontLayout;
75
76 QDoubleSpinBox *m_fontSize;
69 77
70 78 // For detached layout
71 79 QGroupBox* m_legendSettings;
@@ -71,6 +71,17 QBrush LegendMarker::brush() const
71 71 return m_rectItem->brush();
72 72 }
73 73
74 void LegendMarker::setFont(const QFont &font)
75 {
76 m_textItem->setFont(font);
77 updateLayout();
78 }
79
80 QFont LegendMarker::font() const
81 {
82 return m_textItem->font();
83 }
84
74 85 void LegendMarker::setLabel(const QString label)
75 86 {
76 87 m_textItem->setText(label);
@@ -59,6 +59,9 public:
59 59 void setBrush(const QBrush &brush);
60 60 QBrush brush() const;
61 61
62 void setFont(const QFont &font);
63 QFont font() const;
64
62 65 void setSize(const QSize& size);
63 66
64 67 void setLabel(const QString label);
@@ -121,6 +121,15 QTCOMMERCIALCHART_BEGIN_NAMESPACE
121 121 */
122 122
123 123 /*!
124 \property QLegend::font
125 The font of markers used by legend
126 */
127 /*!
128 \qmlproperty color Legend::font
129 The font of markers used by legend
130 */
131
132 /*!
124 133 \fn void QLegend::backgroundVisibleChanged(bool)
125 134 The visibility of the legend background changed to \a visible.
126 135 */
@@ -136,6 +145,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
136 145 */
137 146
138 147 /*!
148 \fn void QLegend::fontChanged(QFont)
149 The font of markers of the legend changed to \a font.
150 */
151
152 /*!
139 153 \fn qreal QLegend::minWidth() const
140 154 Returns minimum width of the legend
141 155 */
@@ -259,6 +273,19 QColor QLegend::borderColor()
259 273 return d_ptr->m_pen.color();
260 274 }
261 275
276 void QLegend::setFont(const QFont &font)
277 {
278 if (d_ptr->m_font != font) {
279 d_ptr->setFont(font);
280 emit fontChanged(font);
281 }
282 }
283
284 QFont QLegend::font() const
285 {
286 return d_ptr->m_font;
287 }
288
262 289 void QLegend::setAlignment(Qt::Alignment alignment)
263 290 {
264 291 if(d_ptr->m_alignment!=alignment) {
@@ -680,13 +707,27 int QLegendPrivate::roundness(qreal size)
680 707 return 100*m_diameter/int(size);
681 708 }
682 709
710 void QLegendPrivate::setFont(const QFont &font)
711 {
712 m_font = font;
713 QList<QGraphicsItem *> items = m_markers->childItems();
714
715 foreach (QGraphicsItem *markers, items) {
716 LegendMarker *marker = static_cast<LegendMarker*>(markers);
717 marker->setFont(m_font);
718 }
719 updateLayout();
720 }
721
683 722 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
684 723 {
685 724 Q_UNUSED(domain)
686 725
687 726 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
688 foreach(LegendMarker* marker, markers)
727 foreach(LegendMarker* marker, markers) {
728 marker->setFont(m_font);
689 729 m_markers->addToGroup(marker);
730 }
690 731
691 732 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
692 733
@@ -47,6 +47,7 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
47 47 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
48 48 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
49 49 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
50 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
50 51
51 52 private:
52 53 explicit QLegend(QChart *chart);
@@ -67,6 +68,9 public:
67 68 void setBorderColor(QColor color);
68 69 QColor borderColor();
69 70
71 void setFont(const QFont &font);
72 QFont font() const;
73
70 74 void setAlignment(Qt::Alignment alignment);
71 75 Qt::Alignment alignment() const;
72 76
@@ -89,6 +93,7 Q_SIGNALS:
89 93 void backgroundVisibleChanged(bool visible);
90 94 void colorChanged(QColor color);
91 95 void borderColorChanged(QColor color);
96 void fontChanged(QFont font);
92 97
93 98 private:
94 99 QScopedPointer<QLegendPrivate> d_ptr;
@@ -51,6 +51,7 public:
51 51 void updateDetachedLayout();
52 52 void attachToChart();
53 53 int roundness(qreal size);
54 void setFont(const QFont &font);
54 55
55 56 public Q_SLOTS:
56 57 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
@@ -67,6 +68,7 private:
67 68 Qt::Alignment m_alignment;
68 69 QBrush m_brush;
69 70 QPen m_pen;
71 QFont m_font;
70 72 QRectF m_rect;
71 73 qreal m_offsetX;
72 74 qreal m_offsetY;
General Comments 0
You need to be logged in to leave comments. Login now