##// END OF EJS Templates
legend pos to theme example, legend padding
sauimone -
r803:a2705c21cb29
parent child
Show More
@@ -50,6 +50,7
50 #include <QSplineSeries>
50 #include <QSplineSeries>
51 #include <QScatterSeries>
51 #include <QScatterSeries>
52 #include <QAreaSeries>
52 #include <QAreaSeries>
53 #include <QLegend>
53 #include <QGridLayout>
54 #include <QGridLayout>
54 #include <QFormLayout>
55 #include <QFormLayout>
55 #include <QComboBox>
56 #include <QComboBox>
@@ -67,7 +68,9 ThemeWidget::ThemeWidget(QWidget* parent) :
67 m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)),
68 m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)),
68 m_themeComboBox(createThemeBox()),
69 m_themeComboBox(createThemeBox()),
69 m_antialiasCheckBox(new QCheckBox("Anti aliasing")),
70 m_antialiasCheckBox(new QCheckBox("Anti aliasing")),
70 m_animatedComboBox(createAnimationBox())
71 m_animatedComboBox(createAnimationBox()),
72 m_legendComboBox(createLegendBox())
73
71 {
74 {
72
75
73 connectSignals();
76 connectSignals();
@@ -78,6 +81,7 ThemeWidget::ThemeWidget(QWidget* parent) :
78 settingsLayout->addWidget(m_themeComboBox);
81 settingsLayout->addWidget(m_themeComboBox);
79 settingsLayout->addWidget(new QLabel("Animation:"));
82 settingsLayout->addWidget(new QLabel("Animation:"));
80 settingsLayout->addWidget(m_animatedComboBox);
83 settingsLayout->addWidget(m_animatedComboBox);
84 settingsLayout->addWidget(m_legendComboBox);
81 settingsLayout->addWidget(m_antialiasCheckBox);
85 settingsLayout->addWidget(m_antialiasCheckBox);
82 settingsLayout->addStretch();
86 settingsLayout->addStretch();
83 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
87 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
@@ -123,6 +127,7 void ThemeWidget::connectSignals()
123 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
127 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
124 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
128 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
125 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
129 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
130 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
126 }
131 }
127
132
128 DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const
133 DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const
@@ -172,6 +177,16 QComboBox* ThemeWidget::createAnimationBox() const
172 return animationComboBox;
177 return animationComboBox;
173 }
178 }
174
179
180 QComboBox* ThemeWidget::createLegendBox() const
181 {
182 QComboBox* legendComboBox = new QComboBox();
183 legendComboBox->addItem("Legend Top", QLegend::AlignmentTop);
184 legendComboBox->addItem("Legend Bottom", QLegend::AlignmentBottom);
185 legendComboBox->addItem("Legend Left", QLegend::AlignmentLeft);
186 legendComboBox->addItem("Legend Right", QLegend::AlignmentRight);
187 return legendComboBox;
188 }
189
175 QChart* ThemeWidget::createAreaChart() const
190 QChart* ThemeWidget::createAreaChart() const
176 {
191 {
177 // area chart
192 // area chart
@@ -342,5 +357,11 void ThemeWidget::updateUI()
342 foreach (QChartView *chartView, m_charts)
357 foreach (QChartView *chartView, m_charts)
343 chartView->chart()->setAnimationOptions(options);
358 chartView->chart()->setAnimationOptions(options);
344 }
359 }
360
361 QLegend::Alignments alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
362 foreach (QChartView *chartView, m_charts) {
363 qDebug() << alignment;
364 chartView->chart()->legend()->setAlignmnent(alignment);
365 }
345 }
366 }
346
367
@@ -70,6 +70,7 private:
70 DataTable generateRandomData(int listCount,int valueMax,int valueCount) const;
70 DataTable generateRandomData(int listCount,int valueMax,int valueCount) const;
71 QComboBox* createThemeBox() const;
71 QComboBox* createThemeBox() const;
72 QComboBox* createAnimationBox() const;
72 QComboBox* createAnimationBox() const;
73 QComboBox* createLegendBox() const;
73 void connectSignals();
74 void connectSignals();
74 QChart* createAreaChart() const;
75 QChart* createAreaChart() const;
75 QChart* createBarChart(int valueCount) const;
76 QChart* createBarChart(int valueCount) const;
@@ -88,6 +89,7 private:
88 QComboBox *m_themeComboBox;
89 QComboBox *m_themeComboBox;
89 QCheckBox *m_antialiasCheckBox;
90 QCheckBox *m_antialiasCheckBox;
90 QComboBox *m_animatedComboBox;
91 QComboBox *m_animatedComboBox;
92 QComboBox *m_legendComboBox;
91 };
93 };
92
94
93 #endif /* THEMEWINDOW_H_ */
95 #endif /* THEMEWINDOW_H_ */
@@ -19,7 +19,6
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qchart.h"
21 #include "qchart.h"
22 #include "qlegend.h"
23 #include "qchartaxis.h"
22 #include "qchartaxis.h"
24 #include "chartpresenter_p.h"
23 #include "chartpresenter_p.h"
25 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
@@ -55,7 +54,6 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(char
55 m_rect(QRectF(QPoint(0,0),m_chart->size())),
54 m_rect(QRectF(QPoint(0,0),m_chart->size())),
56 m_options(QChart::NoAnimation),
55 m_options(QChart::NoAnimation),
57 m_themeForce(false),
56 m_themeForce(false),
58 m_padding(50),
59 m_backgroundPadding(10)
57 m_backgroundPadding(10)
60 {
58 {
61 createConnections();
59 createConnections();
@@ -79,55 +77,8 void ChartPresenter::createConnections()
79 void ChartPresenter::handleGeometryChanged()
77 void ChartPresenter::handleGeometryChanged()
80 {
78 {
81 QRectF rect(QPoint(0,0),m_chart->size());
79 QRectF rect(QPoint(0,0),m_chart->size());
82 QLegend* legend = m_chart->legend();
80 QRectF padding = m_chart->padding();
83 if ((legend->attachedToChart()) && (legend->isVisible())) {
81 rect.adjust(padding.left(), padding.top(), -padding.right(), -padding.bottom());
84
85 // Reserve some space for legend
86 switch (m_chart->legend()->alignment()) {
87 case QLegend::AlignmentTop: {
88 rect.adjust(m_padding,
89 m_padding + legend->size().height(),
90 -m_padding,
91 -m_padding);
92 break;
93 }
94 case QLegend::AlignmentBottom: {
95 rect.adjust(m_padding,
96 m_padding,
97 -m_padding,
98 -m_padding - legend->size().height());
99 break;
100 }
101 case QLegend::AlignmentLeft: {
102 rect.adjust(m_padding + legend->size().width(),
103 m_padding,
104 -m_padding,
105 -m_padding);
106 break;
107 }
108 case QLegend::AlignmentRight: {
109 rect.adjust(m_padding,
110 m_padding,
111 -m_padding - legend->size().width(),
112 -m_padding);
113 break;
114 }
115 default: {
116 rect.adjust(m_padding,
117 m_padding,
118 -m_padding,
119 -m_padding);
120 break;
121 }
122 }
123 } else {
124
125 // Legend is detached, or not visible
126 rect.adjust(m_padding,
127 m_padding,
128 -m_padding,
129 -m_padding);
130 }
131
82
132 //rewrite zoom stack
83 //rewrite zoom stack
133 /*
84 /*
@@ -393,7 +344,7 void ChartPresenter::zoomIn()
393 void ChartPresenter::zoomIn(const QRectF& rect)
344 void ChartPresenter::zoomIn(const QRectF& rect)
394 {
345 {
395 QRectF r = rect.normalized();
346 QRectF r = rect.normalized();
396 r.translate(-m_padding, -m_padding);
347 r.translate(-m_chart->padding().topLeft());
397 if(m_animator) {
348 if(m_animator) {
398
349
399 QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height());
350 QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height());
@@ -414,7 +365,7 void ChartPresenter::zoomOut()
414
365
415 QSizeF size = geometry().size();
366 QSizeF size = geometry().size();
416 QRectF rect = geometry();
367 QRectF rect = geometry();
417 rect.translate(-m_padding, -m_padding);
368 rect.translate(-m_chart->padding().topLeft());
418 m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size);
369 m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size);
419 //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
370 //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
420
371
@@ -55,7 +55,6 public:
55 ChartPresenter(QChart* chart,ChartDataSet *dataset);
55 ChartPresenter(QChart* chart,ChartDataSet *dataset);
56 virtual ~ChartPresenter();
56 virtual ~ChartPresenter();
57
57
58 int padding() const { return m_padding; }
59 int backgroundPadding() const { return m_backgroundPadding; }
58 int backgroundPadding() const { return m_backgroundPadding; }
60 QRectF geometry() const { return m_rect; }
59 QRectF geometry() const { return m_rect; }
61 ChartAnimator* animator() const { return m_animator; }
60 ChartAnimator* animator() const { return m_animator; }
@@ -98,7 +97,6 private:
98 QRectF m_rect;
97 QRectF m_rect;
99 QChart::AnimationOptions m_options;
98 QChart::AnimationOptions m_options;
100 bool m_themeForce;
99 bool m_themeForce;
101 int m_padding;
102 int m_backgroundPadding;
100 int m_backgroundPadding;
103
101
104 };
102 };
@@ -73,8 +73,7 d_ptr(new QChartPrivate(this))
73 d_ptr->m_legend = new QLegend(this);
73 d_ptr->m_legend = new QLegend(this);
74 d_ptr->m_dataset = new ChartDataSet(this);
74 d_ptr->m_dataset = new ChartDataSet(this);
75 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
75 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
76 int padding = d_ptr->m_presenter->padding();
76 setMinimumSize(d_ptr->m_padding.left() * 3, d_ptr->m_padding.top() * 3);
77 setMinimumSize(3*padding,3*padding);
78 connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
77 connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
79 connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
78 connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
80 }
79 }
@@ -269,6 +268,12 QLegend* QChart::legend() const
269 return d_ptr->m_legend;
268 return d_ptr->m_legend;
270 }
269 }
271
270
271 QRectF QChart::padding() const
272 {
273 return d_ptr->m_padding;
274 }
275
276
272 /*!
277 /*!
273 Resizes and updates the chart area using the \a event data
278 Resizes and updates the chart area using the \a event data
274 */
279 */
@@ -336,7 +341,8 m_backgroundItem(0),
336 m_titleItem(0),
341 m_titleItem(0),
337 m_legend(0),
342 m_legend(0),
338 m_dataset(0),
343 m_dataset(0),
339 m_presenter(0)
344 m_presenter(0),
345 m_padding(QRectF(50,50,50,50))
340 {
346 {
341
347
342 }
348 }
@@ -365,10 +371,65 void QChartPrivate::createChartTitleItem()
365
371
366 void QChartPrivate::updateLegendLayout()
372 void QChartPrivate::updateLegendLayout()
367 {
373 {
368 int padding = m_presenter->padding();
374 //int legendPadding = m_chart->legend()->padding();
369 QRectF plotRect = m_rect.adjusted(padding,padding,-padding,-padding);
375 int legendPadding = 30;
376 QRectF rect = m_rect;
377
378 if ((m_legend->attachedToChart()) && (m_legend->isVisible())) {
379
380 // Reserve some space for legend
381 switch (m_legend->alignment()) {
382 case QLegend::AlignmentTop: {
383 rect.adjust(m_padding.left(),
384 m_padding.top() + legendPadding,
385 -m_padding.right(),
386 -m_padding.bottom());
387 break;
388 }
389 case QLegend::AlignmentBottom: {
390 rect.adjust(m_padding.left(),
391 m_padding.top(),
392 -m_padding.right(),
393 -m_padding.bottom() - legendPadding);
394 break;
395 }
396 case QLegend::AlignmentLeft: {
397 rect.adjust(m_padding.left() + legendPadding,
398 m_padding.top(),
399 -m_padding.right(),
400 -m_padding.bottom());
401 break;
402 }
403 case QLegend::AlignmentRight: {
404 rect.adjust(m_padding.left(),
405 m_padding.top(),
406 -m_padding.right() - legendPadding,
407 -m_padding.bottom());
408 break;
409 }
410 default: {
411 rect.adjust(m_padding.left(),
412 m_padding.top(),
413 -m_padding.right(),
414 -m_padding.bottom());
415 break;
416 }
417 }
418 } else {
419
420 rect.adjust(m_padding.left(),
421 m_padding.top(),
422 -m_padding.right(),
423 -m_padding.bottom());
424 }
425
426 QRectF plotRect = m_rect.adjusted(m_padding.left()
427 ,m_padding.top()
428 ,-m_padding.right()
429 ,-m_padding.bottom());
370 QRectF legendRect;
430 QRectF legendRect;
371
431
432 int padding = 0; // TODO: fix this
372 switch (m_legend->alignment())
433 switch (m_legend->alignment())
373 {
434 {
374 case QLegend::AlignmentTop: {
435 case QLegend::AlignmentTop: {
@@ -406,7 +467,6 void QChartPrivate::updateLegendLayout()
406 pos.setY(pos.y() + height/2);
467 pos.setY(pos.y() + height/2);
407 }
468 }
408
469
409 qDebug() << "lenged topleft:" << pos;
410 m_legend->setPos(pos);
470 m_legend->setPos(pos);
411 }
471 }
412
472
@@ -414,7 +474,7 void QChartPrivate::updateLayout()
414 {
474 {
415 if (!m_rect.isValid()) return;
475 if (!m_rect.isValid()) return;
416
476
417 int padding = m_presenter->padding();
477 int padding = m_padding.top();
418 int backgroundPadding = m_presenter->backgroundPadding();
478 int backgroundPadding = m_presenter->backgroundPadding();
419
479
420 // recalculate title position
480 // recalculate title position
@@ -99,6 +99,8 public:
99
99
100 QLegend* legend() const;
100 QLegend* legend() const;
101
101
102 QRectF padding() const;
103
102 protected:
104 protected:
103 void resizeEvent(QGraphicsSceneResizeEvent *event);
105 void resizeEvent(QGraphicsSceneResizeEvent *event);
104
106
@@ -57,6 +57,7 struct QChartPrivate
57 QLegend* m_legend;
57 QLegend* m_legend;
58 ChartDataSet *m_dataset;
58 ChartDataSet *m_dataset;
59 ChartPresenter *m_presenter;
59 ChartPresenter *m_presenter;
60 QRectF m_padding;
60 };
61 };
61
62
62 QTCOMMERCIALCHART_END_NAMESPACE
63 QTCOMMERCIALCHART_END_NAMESPACE
@@ -117,7 +117,7 void QChartView::mousePressEvent(QMouseEvent *event)
117 {
117 {
118 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
118 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
119
119
120 int padding = d_ptr->m_presenter->padding();
120 int padding = d_ptr->m_chart->padding().top();
121 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
121 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
122
122
123 if (rect.contains(event->pos())) {
123 if (rect.contains(event->pos())) {
@@ -139,7 +139,7 void QChartView::mousePressEvent(QMouseEvent *event)
139 void QChartView::mouseMoveEvent(QMouseEvent *event)
139 void QChartView::mouseMoveEvent(QMouseEvent *event)
140 {
140 {
141 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
141 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
142 int padding = d_ptr->m_presenter->padding();
142 int padding = d_ptr->m_chart->padding().top();
143 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
143 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
144 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
144 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
145 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
145 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
@@ -95,7 +95,8 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
95 m_brush(Qt::darkGray), // TODO: default should come from theme
95 m_brush(Qt::darkGray), // TODO: default should come from theme
96 m_alignment(QLegend::AlignmentTop),
96 m_alignment(QLegend::AlignmentTop),
97 mFirstMarker(0),
97 mFirstMarker(0),
98 m_attachedToChart(true)
98 m_attachedToChart(true),
99 m_chart(chart)
99 {
100 {
100 m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this);
101 m_scrollButtonLeft = new LegendScrollButton(LegendScrollButton::ScrollButtonIdLeft, this);
101 m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this);
102 m_scrollButtonRight = new LegendScrollButton(LegendScrollButton::ScrollButtonIdRight, this);
@@ -169,16 +170,19 QPen QLegend::pen() const
169 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
170 Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart.
170 \sa QLegend::Layout
171 \sa QLegend::Layout
171 */
172 */
172 void QLegend::setAlignmnent(QLegend::Alignment alignment)
173 void QLegend::setAlignmnent(QLegend::Alignments alignment)
173 {
174 {
175 // if (!m_attachedToChart) {
174 m_alignment = alignment;
176 m_alignment = alignment;
175 updateLayout();
177 updateLayout();
178 m_chart->resize(m_chart->size());
179 // }
176 }
180 }
177
181
178 /*!
182 /*!
179 Returns the preferred layout for legend
183 Returns the preferred layout for legend
180 */
184 */
181 QLegend::Alignment QLegend::alignment() const
185 QLegend::Alignments QLegend::alignment() const
182 {
186 {
183 return m_alignment;
187 return m_alignment;
184 }
188 }
@@ -52,6 +52,9 public:
52 AlignmentLeft = Qt::AlignLeft,
52 AlignmentLeft = Qt::AlignLeft,
53 AlignmentRight = Qt::AlignRight
53 AlignmentRight = Qt::AlignRight
54 };
54 };
55
56 Q_DECLARE_FLAGS(Alignments, Alignment)
57
55 private:
58 private:
56 explicit QLegend(QChart *chart);
59 explicit QLegend(QChart *chart);
57
60
@@ -65,8 +68,8 public:
65 void setPen(const QPen &pen);
68 void setPen(const QPen &pen);
66 QPen pen() const;
69 QPen pen() const;
67
70
68 void setAlignmnent(QLegend::Alignment alignment);
71 void setAlignmnent(QLegend::Alignments alignment);
69 QLegend::Alignment alignment() const;
72 QLegend::Alignments alignment() const;
70
73
71 QSizeF maximumSize() const;
74 QSizeF maximumSize() const;
72 void setMaximumSize(const QSizeF size);
75 void setMaximumSize(const QSizeF size);
@@ -86,6 +89,7 Q_SIGNALS:
86 void clicked(QSeries *series, Qt::MouseButton button);
89 void clicked(QSeries *series, Qt::MouseButton button);
87 void clicked(QBarSet *barset, Qt::MouseButton button);
90 void clicked(QBarSet *barset, Qt::MouseButton button);
88 void clicked(QPieSlice *slice, Qt::MouseButton button);
91 void clicked(QPieSlice *slice, Qt::MouseButton button);
92 void legendGeometryChanged();
89
93
90 public Q_SLOTS:
94 public Q_SLOTS:
91 // PIMPL --->
95 // PIMPL --->
@@ -120,7 +124,7 private:
120
124
121 QBrush m_brush;
125 QBrush m_brush;
122 QPen m_pen;
126 QPen m_pen;
123 QLegend::Alignment m_alignment;
127 QLegend::Alignments m_alignment;
124
128
125 int mFirstMarker;
129 int mFirstMarker;
126
130
@@ -131,6 +135,7 private:
131
135
132 bool m_attachedToChart;
136 bool m_attachedToChart;
133
137
138 QChart *m_chart;
134 friend class QChart;
139 friend class QChart;
135 // <--- PIMPL
140 // <--- PIMPL
136 };
141 };
General Comments 0
You need to be logged in to leave comments. Login now