##// END OF EJS Templates
Adds missing license headers
Michal Klocek -
r1535:954f1e6a64d9
parent child
Show More
@@ -1,144 +1,164
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
1 #include "chartlayout_p.h"
21 #include "chartlayout_p.h"
2 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
3 #include "chartaxis_p.h"
23 #include "chartaxis_p.h"
4 #include <QDebug>
24
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
26
7 ChartLayout::ChartLayout(ChartPresenter* presenter):
27 ChartLayout::ChartLayout(ChartPresenter* presenter):
8 m_presenter(presenter),
28 m_presenter(presenter),
9 m_marginBig(60),
29 m_marginBig(60),
10 m_marginSmall(20),
30 m_marginSmall(20),
11 m_marginTiny(10),
31 m_marginTiny(10),
12 m_chartMargins(QPointF(m_marginBig,m_marginBig),QPointF(m_marginBig,m_marginBig))
32 m_chartMargins(QPointF(m_marginBig,m_marginBig),QPointF(m_marginBig,m_marginBig))
13 {
33 {
14
34
15 }
35 }
16
36
17 ChartLayout::~ChartLayout()
37 ChartLayout::~ChartLayout()
18 {
38 {
19
39
20 }
40 }
21
41
22 void ChartLayout::setGeometry(const QRectF& rect)
42 void ChartLayout::setGeometry(const QRectF& rect)
23 {
43 {
24 if (!rect.isValid()) return;
44 if (!rect.isValid()) return;
25
45
26 QGraphicsLayout::setGeometry(rect);
46 QGraphicsLayout::setGeometry(rect);
27
47
28 // check title size
48 // check title size
29
49
30 QSize titleSize = QSize(0,0);
50 QSize titleSize = QSize(0,0);
31
51
32 if (m_presenter->titleItem()) {
52 if (m_presenter->titleItem()) {
33 titleSize= m_presenter->titleItem()->boundingRect().size().toSize();
53 titleSize= m_presenter->titleItem()->boundingRect().size().toSize();
34 }
54 }
35
55
36 qreal axisHeight = 0;
56 qreal axisHeight = 0;
37 qreal axisWidth = 0;
57 qreal axisWidth = 0;
38
58
39 // check axis size
59 // check axis size
40
60
41 foreach (ChartAxis* axis,m_presenter->axisItems()){
61 foreach (ChartAxis* axis,m_presenter->axisItems()){
42 if(axis->axisType() == ChartAxis::X_AXIS)
62 if(axis->axisType() == ChartAxis::X_AXIS)
43 axisHeight = qMax(axis->minimumHeight(),axisHeight);
63 axisHeight = qMax(axis->minimumHeight(),axisHeight);
44 else
64 else
45 axisWidth = qMax(axis->minimumWidth(),axisWidth);
65 axisWidth = qMax(axis->minimumWidth(),axisWidth);
46 }
66 }
47
67
48 QLegend* legend = m_presenter->legend();
68 QLegend* legend = m_presenter->legend();
49
69
50 qreal titlePadding = m_chartMargins.top()/2;
70 qreal titlePadding = m_chartMargins.top()/2;
51
71
52 QRectF chartMargins = m_chartMargins;
72 QRectF chartMargins = m_chartMargins;
53
73
54 // recalculate legend position
74 // recalculate legend position
55 if (legend != 0 && legend->isVisible() && legend->isAttachedToChart()) {
75 if (legend != 0 && legend->isVisible() && legend->isAttachedToChart()) {
56
76
57 // Reserve some space for legend
77 // Reserve some space for legend
58 switch (legend->alignment()) {
78 switch (legend->alignment()) {
59
79
60 case Qt::AlignTop: {
80 case Qt::AlignTop: {
61 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(rect.width(),-1));
81 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(rect.width(),-1));
62 int topMargin = 2*m_marginTiny + titleSize.height() + legendSize.height() + m_marginTiny;
82 int topMargin = 2*m_marginTiny + titleSize.height() + legendSize.height() + m_marginTiny;
63 chartMargins = QRect(QPoint(m_chartMargins.left(),topMargin),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
83 chartMargins = QRect(QPoint(m_chartMargins.left(),topMargin),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
64 m_legendMargins = QRect(QPoint(chartMargins.left(),topMargin - (legendSize.height() + m_marginTiny)),QPoint(chartMargins.right(),rect.height()-topMargin + m_marginTiny));
84 m_legendMargins = QRect(QPoint(chartMargins.left(),topMargin - (legendSize.height() + m_marginTiny)),QPoint(chartMargins.right(),rect.height()-topMargin + m_marginTiny));
65 titlePadding = m_marginTiny + m_marginTiny;
85 titlePadding = m_marginTiny + m_marginTiny;
66 break;
86 break;
67 }
87 }
68 case Qt::AlignBottom: {
88 case Qt::AlignBottom: {
69 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(rect.width(),-1));
89 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(rect.width(),-1));
70 int bottomMargin = m_marginTiny + m_marginSmall + legendSize.height() + m_marginTiny + axisHeight;
90 int bottomMargin = m_marginTiny + m_marginSmall + legendSize.height() + m_marginTiny + axisHeight;
71 chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomMargin));
91 chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomMargin));
72 m_legendMargins = QRect(QPoint(chartMargins.left(),rect.height()-bottomMargin + m_marginTiny + axisHeight),QPoint(chartMargins.right(),m_marginTiny + m_marginSmall));
92 m_legendMargins = QRect(QPoint(chartMargins.left(),rect.height()-bottomMargin + m_marginTiny + axisHeight),QPoint(chartMargins.right(),m_marginTiny + m_marginSmall));
73 titlePadding = chartMargins.top()/2;
93 titlePadding = chartMargins.top()/2;
74 break;
94 break;
75 }
95 }
76 case Qt::AlignLeft: {
96 case Qt::AlignLeft: {
77 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,rect.height()));
97 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,rect.height()));
78 int leftPadding = m_marginTiny + m_marginSmall + legendSize.width() + m_marginTiny + axisWidth;
98 int leftPadding = m_marginTiny + m_marginSmall + legendSize.width() + m_marginTiny + axisWidth;
79 chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
99 chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
80 m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,chartMargins.top()),QPoint(rect.width()-leftPadding + m_marginTiny + axisWidth,chartMargins.bottom()));
100 m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,chartMargins.top()),QPoint(rect.width()-leftPadding + m_marginTiny + axisWidth,chartMargins.bottom()));
81 titlePadding = chartMargins.top()/2;
101 titlePadding = chartMargins.top()/2;
82 break;
102 break;
83 }
103 }
84 case Qt::AlignRight: {
104 case Qt::AlignRight: {
85 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,rect.height()));
105 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,rect.height()));
86 int rightPadding = m_marginTiny + m_marginSmall + legendSize.width() + m_marginTiny;
106 int rightPadding = m_marginTiny + m_marginSmall + legendSize.width() + m_marginTiny;
87 chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom()));
107 chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom()));
88 m_legendMargins = QRect(QPoint(rect.width()- rightPadding+ m_marginTiny ,chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,chartMargins.bottom()));
108 m_legendMargins = QRect(QPoint(rect.width()- rightPadding+ m_marginTiny ,chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,chartMargins.bottom()));
89 titlePadding = chartMargins.top()/2;
109 titlePadding = chartMargins.top()/2;
90 break;
110 break;
91 }
111 }
92 default: {
112 default: {
93 break;
113 break;
94 }
114 }
95 }
115 }
96
116
97 legend->setGeometry(rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom()));
117 legend->setGeometry(rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom()));
98 }
118 }
99
119
100 // recalculate title position
120 // recalculate title position
101 if (m_presenter->titleItem()) {
121 if (m_presenter->titleItem()) {
102 QPointF center = rect.center() - m_presenter->titleItem()->boundingRect().center();
122 QPointF center = rect.center() - m_presenter->titleItem()->boundingRect().center();
103 m_presenter->titleItem()->setPos(center.x(),titlePadding);
123 m_presenter->titleItem()->setPos(center.x(),titlePadding);
104 }
124 }
105
125
106 //recalculate background gradient
126 //recalculate background gradient
107 if (m_presenter->backgroundItem()) {
127 if (m_presenter->backgroundItem()) {
108 m_presenter->backgroundItem()->setRect(rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny));
128 m_presenter->backgroundItem()->setRect(rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny));
109 }
129 }
110
130
111 QRectF chartRect = rect.adjusted(chartMargins.left(),chartMargins.top(),-chartMargins.right(),-chartMargins.bottom());
131 QRectF chartRect = rect.adjusted(chartMargins.left(),chartMargins.top(),-chartMargins.right(),-chartMargins.bottom());
112
132
113 if(m_presenter->geometry()!=chartRect && chartRect.isValid()){
133 if(m_presenter->geometry()!=chartRect && chartRect.isValid()){
114 m_presenter->setGeometry(chartRect);
134 m_presenter->setGeometry(chartRect);
115 }else if(chartRect.size().isEmpty()){
135 }else if(chartRect.size().isEmpty()){
116 m_presenter->setGeometry(QRect(rect.width()/2,rect.height()/2,1,1));
136 m_presenter->setGeometry(QRect(rect.width()/2,rect.height()/2,1,1));
117 }
137 }
118
138
119 }
139 }
120
140
121
141
122 QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
142 QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
123 {
143 {
124 Q_UNUSED(constraint);
144 Q_UNUSED(constraint);
125 if(which == Qt::MinimumSize)
145 if(which == Qt::MinimumSize)
126 return QSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom()));
146 return QSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom()));
127 else
147 else
128 return QSize(-1,-1);
148 return QSize(-1,-1);
129 }
149 }
130
150
131 void ChartLayout::setMarginsMinimum(const QRectF& margins)
151 void ChartLayout::setMarginsMinimum(const QRectF& margins)
132 {
152 {
133 if(m_chartMargins != margins){
153 if(m_chartMargins != margins){
134 m_chartMargins = margins;
154 m_chartMargins = margins;
135 updateGeometry();
155 updateGeometry();
136 }
156 }
137 }
157 }
138
158
139 QRectF ChartLayout::margins() const
159 QRectF ChartLayout::margins() const
140 {
160 {
141 return m_chartMargins;
161 return m_chartMargins;
142 }
162 }
143
163
144 QTCOMMERCIALCHART_END_NAMESPACE
164 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,43 +1,63
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
1 #ifndef CHARTLAYOUT_H_
21 #ifndef CHARTLAYOUT_H_
2 #define CHARTLAYOUT_H_
22 #define CHARTLAYOUT_H_
3 #include <QGraphicsLayout>
23 #include <QGraphicsLayout>
4 #include "qchartglobal.h"
24 #include "qchartglobal.h"
5
25
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
27
8 class ChartPresenter;
28 class ChartPresenter;
9
29
10 class ChartLayout : public QGraphicsLayout
30 class ChartLayout : public QGraphicsLayout
11 {
31 {
12 public:
32 public:
13
33
14 ChartLayout(ChartPresenter* presenter);
34 ChartLayout(ChartPresenter* presenter);
15 virtual ~ChartLayout();
35 virtual ~ChartLayout();
16
36
17 void setMarginsMinimum(const QRectF& margins);
37 void setMarginsMinimum(const QRectF& margins);
18 QRectF margins() const;
38 QRectF margins() const;
19
39
20 void setGeometry(const QRectF& rect);
40 void setGeometry(const QRectF& rect);
21
41
22 protected:
42 protected:
23 QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
43 QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
24 int count() const { return 0; }
44 int count() const { return 0; }
25 QGraphicsLayoutItem* itemAt(int) const { return 0; };
45 QGraphicsLayoutItem* itemAt(int) const { return 0; };
26 void removeAt(int){};
46 void removeAt(int){};
27
47
28 private:
48 private:
29 ChartPresenter* m_presenter;
49 ChartPresenter* m_presenter;
30 int m_marginBig;
50 int m_marginBig;
31 int m_marginSmall;
51 int m_marginSmall;
32 int m_marginTiny;
52 int m_marginTiny;
33
53
34 QRectF m_chartMargins;
54 QRectF m_chartMargins;
35 QRectF m_legendMargins;
55 QRectF m_legendMargins;
36
56
37
57
38
58
39 };
59 };
40
60
41 QTCOMMERCIALCHART_END_NAMESPACE
61 QTCOMMERCIALCHART_END_NAMESPACE
42
62
43 #endif
63 #endif
@@ -1,348 +1,368
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
1 #include "legendlayout_p.h"
21 #include "legendlayout_p.h"
2 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
3 #include "legendmarker_p.h"
23 #include "legendmarker_p.h"
4 #include "qlegend_p.h"
24 #include "qlegend_p.h"
5 #include <QDebug>
25
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
27
8 LegendLayout::LegendLayout(QLegend* legend):
28 LegendLayout::LegendLayout(QLegend* legend):
9 m_legend(legend)
29 m_legend(legend)
10 {
30 {
11
31
12 }
32 }
13
33
14 LegendLayout::~LegendLayout()
34 LegendLayout::~LegendLayout()
15 {
35 {
16
36
17 }
37 }
18
38
19 void LegendLayout::setOffset(qreal x, qreal y)
39 void LegendLayout::setOffset(qreal x, qreal y)
20 {
40 {
21 bool scrollHorizontal = true;
41 bool scrollHorizontal = true;
22 switch(m_legend->alignment()) {
42 switch(m_legend->alignment()) {
23 case Qt::AlignTop:
43 case Qt::AlignTop:
24 case Qt::AlignBottom: {
44 case Qt::AlignBottom: {
25 scrollHorizontal = true;
45 scrollHorizontal = true;
26 break;
46 break;
27 }
47 }
28 case Qt::AlignLeft:
48 case Qt::AlignLeft:
29 case Qt::AlignRight: {
49 case Qt::AlignRight: {
30 scrollHorizontal = false;
50 scrollHorizontal = false;
31 break;
51 break;
32 }
52 }
33 }
53 }
34
54
35 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
55 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
36 if (!m_legend->isAttachedToChart()) {
56 if (!m_legend->isAttachedToChart()) {
37 scrollHorizontal = !scrollHorizontal;
57 scrollHorizontal = !scrollHorizontal;
38 }
58 }
39
59
40 QRectF boundingRect = geometry();
60 QRectF boundingRect = geometry();
41
61
42 // Limit offset between m_minOffset and m_maxOffset
62 // Limit offset between m_minOffset and m_maxOffset
43 if (scrollHorizontal) {
63 if (scrollHorizontal) {
44 if(m_width<=boundingRect.width()) return;
64 if(m_width<=boundingRect.width()) return;
45
65
46 if (x != m_offsetX) {
66 if (x != m_offsetX) {
47 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
67 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
48 m_legend->d_ptr->items()->setPos(-m_offsetX,boundingRect.top());
68 m_legend->d_ptr->items()->setPos(-m_offsetX,boundingRect.top());
49 }
69 }
50 }
70 }
51 else {
71 else {
52 if(m_height<=boundingRect.height()) return;
72 if(m_height<=boundingRect.height()) return;
53
73
54 if (y != m_offsetY) {
74 if (y != m_offsetY) {
55 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
75 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
56 m_legend->d_ptr->items()->setPos(boundingRect.left(),-m_offsetY);
76 m_legend->d_ptr->items()->setPos(boundingRect.left(),-m_offsetY);
57 }
77 }
58 }
78 }
59 }
79 }
60
80
61 QPointF LegendLayout::offset() const
81 QPointF LegendLayout::offset() const
62 {
82 {
63 return QPointF(m_offsetX,m_offsetY);
83 return QPointF(m_offsetX,m_offsetY);
64 }
84 }
65
85
66 void LegendLayout::setGeometry(const QRectF& rect)
86 void LegendLayout::setGeometry(const QRectF& rect)
67 {
87 {
68 if (!rect.isValid()) return;
88 if (!rect.isValid()) return;
69
89
70 QGraphicsLayout::setGeometry(rect);
90 QGraphicsLayout::setGeometry(rect);
71
91
72 if(m_legend->isAttachedToChart()) {
92 if(m_legend->isAttachedToChart()) {
73 setAttachedGeometry(rect);
93 setAttachedGeometry(rect);
74 }
94 }
75 else {
95 else {
76 setDettachedGeometry(rect);
96 setDettachedGeometry(rect);
77 }
97 }
78 }
98 }
79
99
80 void LegendLayout::setAttachedGeometry(const QRectF& rect)
100 void LegendLayout::setAttachedGeometry(const QRectF& rect)
81 {
101 {
82
102
83 m_offsetX=0;
103 m_offsetX=0;
84 m_offsetY=0;
104 m_offsetY=0;
85
105
86 QSizeF size(0,0);
106 QSizeF size(0,0);
87
107
88 if( m_legend->d_ptr->markers().isEmpty()) return;
108 if( m_legend->d_ptr->markers().isEmpty()) return;
89
109
90 m_width=0;
110 m_width=0;
91 m_height=0;
111 m_height=0;
92
112
93 switch(m_legend->alignment()) {
113 switch(m_legend->alignment()) {
94
114
95 case Qt::AlignTop:
115 case Qt::AlignTop:
96
116
97 case Qt::AlignBottom: {
117 case Qt::AlignBottom: {
98 QPointF point(0,0);
118 QPointF point(0,0);
99 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
119 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
100 if (marker->isVisible()) {
120 if (marker->isVisible()) {
101 marker->setGeometry(QRectF(QPoint(0,0),marker->effectiveSizeHint(Qt::PreferredSize)));
121 marker->setGeometry(QRectF(QPoint(0,0),marker->effectiveSizeHint(Qt::PreferredSize)));
102 marker->setPos(point.x(),rect.height()/2 - marker->boundingRect().height()/2);
122 marker->setPos(point.x(),rect.height()/2 - marker->boundingRect().height()/2);
103 const QRectF& rect = marker->boundingRect();
123 const QRectF& rect = marker->boundingRect();
104 size = size.expandedTo(rect.size());
124 size = size.expandedTo(rect.size());
105 qreal w = rect.width();
125 qreal w = rect.width();
106 m_width+=w;
126 m_width+=w;
107 point.setX(point.x() + w);
127 point.setX(point.x() + w);
108 }
128 }
109 }
129 }
110 if(m_width<rect.width()) {
130 if(m_width<rect.width()) {
111 m_legend->d_ptr->items()->setPos(rect.width()/2-m_width/2,rect.top());
131 m_legend->d_ptr->items()->setPos(rect.width()/2-m_width/2,rect.top());
112
132
113 }
133 }
114 else {
134 else {
115 m_legend->d_ptr->items()->setPos(rect.topLeft());
135 m_legend->d_ptr->items()->setPos(rect.topLeft());
116 }
136 }
117 m_height=size.height();
137 m_height=size.height();
118 }
138 }
119 break;
139 break;
120 case Qt::AlignLeft:
140 case Qt::AlignLeft:
121 case Qt::AlignRight: {
141 case Qt::AlignRight: {
122 QPointF point(0,0);
142 QPointF point(0,0);
123 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
143 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
124 if (marker->isVisible()) {
144 if (marker->isVisible()) {
125 marker->setGeometry(QRectF(QPoint(0,0),marker->effectiveSizeHint(Qt::PreferredSize)));
145 marker->setGeometry(QRectF(QPoint(0,0),marker->effectiveSizeHint(Qt::PreferredSize)));
126 marker->setPos(point);
146 marker->setPos(point);
127 const QRectF& rect = marker->boundingRect();
147 const QRectF& rect = marker->boundingRect();
128 qreal h = rect.height();
148 qreal h = rect.height();
129 size = size.expandedTo(rect.size());
149 size = size.expandedTo(rect.size());
130 m_height+=h;
150 m_height+=h;
131 point.setY(point.y() + h);
151 point.setY(point.y() + h);
132 }
152 }
133 }
153 }
134 if(m_height<rect.height()) {
154 if(m_height<rect.height()) {
135 m_legend->d_ptr->items()->setPos(rect.left(),rect.height()/2-m_height/2);
155 m_legend->d_ptr->items()->setPos(rect.left(),rect.height()/2-m_height/2);
136 }
156 }
137 else {
157 else {
138 m_legend->d_ptr->items()->setPos(rect.topLeft());
158 m_legend->d_ptr->items()->setPos(rect.topLeft());
139 }
159 }
140 m_width=size.width();
160 m_width=size.width();
141 }
161 }
142 break;
162 break;
143 }
163 }
144
164
145 m_minOffsetX = 0;
165 m_minOffsetX = 0;
146 m_minOffsetY = 0;
166 m_minOffsetY = 0;
147 m_maxOffsetX = m_width - rect.width();
167 m_maxOffsetX = m_width - rect.width();
148 m_maxOffsetY = m_height - rect.height();
168 m_maxOffsetY = m_height - rect.height();
149 }
169 }
150
170
151 void LegendLayout::setDettachedGeometry(const QRectF& rect)
171 void LegendLayout::setDettachedGeometry(const QRectF& rect)
152 {
172 {
153 // Detached layout is different.
173 // Detached layout is different.
154 // In detached mode legend may have multiple rows and columns, so layout calculations
174 // In detached mode legend may have multiple rows and columns, so layout calculations
155 // differ a log from attached mode.
175 // differ a log from attached mode.
156 // Also the scrolling logic is bit different.
176 // Also the scrolling logic is bit different.
157
177
158 m_offsetX=0;
178 m_offsetX=0;
159 m_offsetY=0;
179 m_offsetY=0;
160
180
161 QSizeF size(0,0);
181 QSizeF size(0,0);
162
182
163 if( m_legend->d_ptr->markers().isEmpty()) return;
183 if( m_legend->d_ptr->markers().isEmpty()) return;
164
184
165 QList<QGraphicsItem *> items = m_legend->d_ptr->items()->childItems();
185 QList<QGraphicsItem *> items = m_legend->d_ptr->items()->childItems();
166
186
167 switch (m_legend->alignment()) {
187 switch (m_legend->alignment()) {
168 case Qt::AlignTop: {
188 case Qt::AlignTop: {
169 QPointF point = rect.topLeft();
189 QPointF point = rect.topLeft();
170 m_width = 0;
190 m_width = 0;
171 m_height = 0;
191 m_height = 0;
172 for (int i=0; i<items.count(); i++) {
192 for (int i=0; i<items.count(); i++) {
173 QGraphicsItem *item = items.at(i);
193 QGraphicsItem *item = items.at(i);
174 if (item->isVisible()) {
194 if (item->isVisible()) {
175 const QRectF& boundingRect = item->boundingRect();
195 const QRectF& boundingRect = item->boundingRect();
176 qreal w = boundingRect.width();
196 qreal w = boundingRect.width();
177 qreal h = boundingRect.height();
197 qreal h = boundingRect.height();
178 m_width = qMax(m_width,w);
198 m_width = qMax(m_width,w);
179 m_height = qMax(m_height,h);
199 m_height = qMax(m_height,h);
180 item->setPos(point.x(),point.y());
200 item->setPos(point.x(),point.y());
181 point.setX(point.x() + w);
201 point.setX(point.x() + w);
182 if (point.x() + w > rect.topLeft().x() + rect.width()) {
202 if (point.x() + w > rect.topLeft().x() + rect.width()) {
183 // Next item would go off rect.
203 // Next item would go off rect.
184 point.setX(rect.topLeft().x());
204 point.setX(rect.topLeft().x());
185 point.setY(point.y() + h);
205 point.setY(point.y() + h);
186 if (i+1 < items.count()) {
206 if (i+1 < items.count()) {
187 m_height += h;
207 m_height += h;
188 }
208 }
189 }
209 }
190 }
210 }
191 }
211 }
192 m_legend->d_ptr->items()->setPos(rect.topLeft());
212 m_legend->d_ptr->items()->setPos(rect.topLeft());
193
213
194 m_minOffsetX = 0;
214 m_minOffsetX = 0;
195 m_minOffsetY = 0;
215 m_minOffsetY = 0;
196 m_maxOffsetX = m_width - rect.width();
216 m_maxOffsetX = m_width - rect.width();
197 m_maxOffsetY = m_height - rect.height();
217 m_maxOffsetY = m_height - rect.height();
198 }
218 }
199 break;
219 break;
200 case Qt::AlignBottom: {
220 case Qt::AlignBottom: {
201 QPointF point = rect.bottomLeft();
221 QPointF point = rect.bottomLeft();
202 m_width = 0;
222 m_width = 0;
203 m_height = 0;
223 m_height = 0;
204 for (int i=0; i<items.count(); i++) {
224 for (int i=0; i<items.count(); i++) {
205 QGraphicsItem *item = items.at(i);
225 QGraphicsItem *item = items.at(i);
206 if (item->isVisible()) {
226 if (item->isVisible()) {
207 const QRectF& boundingRect = item->boundingRect();
227 const QRectF& boundingRect = item->boundingRect();
208 qreal w = boundingRect.width();
228 qreal w = boundingRect.width();
209 qreal h = boundingRect.height();
229 qreal h = boundingRect.height();
210 m_width = qMax(m_width,w);
230 m_width = qMax(m_width,w);
211 m_height = qMax(m_height,h);
231 m_height = qMax(m_height,h);
212 item->setPos(point.x(),point.y() - h);
232 item->setPos(point.x(),point.y() - h);
213 point.setX(point.x() + w);
233 point.setX(point.x() + w);
214 if (point.x() + w > rect.bottomLeft().x() + rect.width()) {
234 if (point.x() + w > rect.bottomLeft().x() + rect.width()) {
215 // Next item would go off rect.
235 // Next item would go off rect.
216 point.setX(rect.bottomLeft().x());
236 point.setX(rect.bottomLeft().x());
217 point.setY(point.y() - h);
237 point.setY(point.y() - h);
218 if (i+1 < items.count()) {
238 if (i+1 < items.count()) {
219 m_height += h;
239 m_height += h;
220 }
240 }
221 }
241 }
222 }
242 }
223 }
243 }
224 m_legend->d_ptr->items()->setPos(rect.topLeft());
244 m_legend->d_ptr->items()->setPos(rect.topLeft());
225
245
226 m_minOffsetX = 0;
246 m_minOffsetX = 0;
227 m_minOffsetY = qMin(rect.topLeft().y(), rect.topLeft().y() - m_height + rect.height());
247 m_minOffsetY = qMin(rect.topLeft().y(), rect.topLeft().y() - m_height + rect.height());
228 m_maxOffsetX = m_width - rect.width();
248 m_maxOffsetX = m_width - rect.width();
229 m_maxOffsetY = 0;
249 m_maxOffsetY = 0;
230 }
250 }
231 break;
251 break;
232 case Qt::AlignLeft: {
252 case Qt::AlignLeft: {
233 QPointF point = rect.topLeft();
253 QPointF point = rect.topLeft();
234 m_width = 0;
254 m_width = 0;
235 m_height = 0;
255 m_height = 0;
236 qreal maxWidth = 0;
256 qreal maxWidth = 0;
237 for (int i=0; i<items.count(); i++) {
257 for (int i=0; i<items.count(); i++) {
238 QGraphicsItem *item = items.at(i);
258 QGraphicsItem *item = items.at(i);
239 if (item->isVisible()) {
259 if (item->isVisible()) {
240 const QRectF& boundingRect = item->boundingRect();
260 const QRectF& boundingRect = item->boundingRect();
241 qreal w = boundingRect.width();
261 qreal w = boundingRect.width();
242 qreal h = boundingRect.height();
262 qreal h = boundingRect.height();
243 m_height = qMax(m_height,h);
263 m_height = qMax(m_height,h);
244 maxWidth = qMax(maxWidth,w);
264 maxWidth = qMax(maxWidth,w);
245 item->setPos(point.x(),point.y());
265 item->setPos(point.x(),point.y());
246 point.setY(point.y() + h);
266 point.setY(point.y() + h);
247 if (point.y() + h > rect.topLeft().y() + rect.height()) {
267 if (point.y() + h > rect.topLeft().y() + rect.height()) {
248 // Next item would go off rect.
268 // Next item would go off rect.
249 point.setX(point.x() + maxWidth);
269 point.setX(point.x() + maxWidth);
250 point.setY(rect.topLeft().y());
270 point.setY(rect.topLeft().y());
251 if (i+1 < items.count()) {
271 if (i+1 < items.count()) {
252 m_width += maxWidth;
272 m_width += maxWidth;
253 maxWidth = 0;
273 maxWidth = 0;
254 }
274 }
255 }
275 }
256 }
276 }
257 }
277 }
258 m_width += maxWidth;
278 m_width += maxWidth;
259 m_legend->d_ptr->items()->setPos(rect.topLeft());
279 m_legend->d_ptr->items()->setPos(rect.topLeft());
260
280
261 m_minOffsetX = 0;
281 m_minOffsetX = 0;
262 m_minOffsetY = 0;
282 m_minOffsetY = 0;
263 m_maxOffsetX = m_width - rect.width();
283 m_maxOffsetX = m_width - rect.width();
264 m_maxOffsetY = m_height - rect.height();
284 m_maxOffsetY = m_height - rect.height();
265 }
285 }
266 break;
286 break;
267 case Qt::AlignRight: {
287 case Qt::AlignRight: {
268 QPointF point = rect.topRight();
288 QPointF point = rect.topRight();
269 m_width = 0;
289 m_width = 0;
270 m_height = 0;
290 m_height = 0;
271 qreal maxWidth = 0;
291 qreal maxWidth = 0;
272 for (int i=0; i<items.count(); i++) {
292 for (int i=0; i<items.count(); i++) {
273 QGraphicsItem *item = items.at(i);
293 QGraphicsItem *item = items.at(i);
274 if (item->isVisible()) {
294 if (item->isVisible()) {
275 const QRectF& boundingRect = item->boundingRect();
295 const QRectF& boundingRect = item->boundingRect();
276 qreal w = boundingRect.width();
296 qreal w = boundingRect.width();
277 qreal h = boundingRect.height();
297 qreal h = boundingRect.height();
278 m_height = qMax(m_height,h);
298 m_height = qMax(m_height,h);
279 maxWidth = qMax(maxWidth,w);
299 maxWidth = qMax(maxWidth,w);
280 item->setPos(point.x() - w,point.y());
300 item->setPos(point.x() - w,point.y());
281 point.setY(point.y() + h);
301 point.setY(point.y() + h);
282 if (point.y() + h > rect.topLeft().y() + rect.height()) {
302 if (point.y() + h > rect.topLeft().y() + rect.height()) {
283 // Next item would go off rect.
303 // Next item would go off rect.
284 point.setX(point.x() - maxWidth);
304 point.setX(point.x() - maxWidth);
285 point.setY(rect.topLeft().y());
305 point.setY(rect.topLeft().y());
286 if (i+1 < items.count()) {
306 if (i+1 < items.count()) {
287 m_width += maxWidth;
307 m_width += maxWidth;
288 maxWidth = 0;
308 maxWidth = 0;
289 }
309 }
290 }
310 }
291 }
311 }
292 }
312 }
293 m_width += maxWidth;
313 m_width += maxWidth;
294 m_legend->d_ptr->items()->setPos(rect.topLeft());
314 m_legend->d_ptr->items()->setPos(rect.topLeft());
295
315
296 m_minOffsetX = qMin(rect.topLeft().x(), rect.topLeft().x() - m_width + rect.width());
316 m_minOffsetX = qMin(rect.topLeft().x(), rect.topLeft().x() - m_width + rect.width());
297 m_minOffsetY = 0;
317 m_minOffsetY = 0;
298 m_maxOffsetX = 0;
318 m_maxOffsetX = 0;
299 m_maxOffsetY = m_height - rect.height();
319 m_maxOffsetY = m_height - rect.height();
300 }
320 }
301 break;
321 break;
302 default:
322 default:
303 break;
323 break;
304 }
324 }
305
325
306 }
326 }
307
327
308 QSizeF LegendLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
328 QSizeF LegendLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
309 {
329 {
310 QSizeF size(0, 0);
330 QSizeF size(0, 0);
311 qreal left, top, right, bottom;
331 qreal left, top, right, bottom;
312 getContentsMargins(&left, &top, &right, &bottom);
332 getContentsMargins(&left, &top, &right, &bottom);
313
333
314 if(constraint.isValid()) {
334 if(constraint.isValid()) {
315 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
335 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
316 size = size.expandedTo(marker->effectiveSizeHint(which));
336 size = size.expandedTo(marker->effectiveSizeHint(which));
317 }
337 }
318 size = size.boundedTo(constraint);
338 size = size.boundedTo(constraint);
319 }
339 }
320 else if (constraint.width() >= 0) {
340 else if (constraint.width() >= 0) {
321 qreal width = 0;
341 qreal width = 0;
322 qreal height = 0;
342 qreal height = 0;
323 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
343 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
324 width+=marker->effectiveSizeHint(which).width();
344 width+=marker->effectiveSizeHint(which).width();
325 height=qMax(height,marker->effectiveSizeHint(which).height());
345 height=qMax(height,marker->effectiveSizeHint(which).height());
326 }
346 }
327
347
328 size = QSizeF(qMin(constraint.width(),width), height);
348 size = QSizeF(qMin(constraint.width(),width), height);
329 }
349 }
330 else if (constraint.height() >= 0) {
350 else if (constraint.height() >= 0) {
331 qreal width = 0;
351 qreal width = 0;
332 qreal height = 0;
352 qreal height = 0;
333 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
353 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
334 width=qMax(width,marker->effectiveSizeHint(which).width());
354 width=qMax(width,marker->effectiveSizeHint(which).width());
335 height+=height,marker->effectiveSizeHint(which).height();
355 height+=height,marker->effectiveSizeHint(which).height();
336 }
356 }
337 size = QSizeF(width,qMin(constraint.height(),height));
357 size = QSizeF(width,qMin(constraint.height(),height));
338 }
358 }
339 else {
359 else {
340 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
360 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
341 size = size.expandedTo(marker->effectiveSizeHint(which));
361 size = size.expandedTo(marker->effectiveSizeHint(which));
342 }
362 }
343 }
363 }
344 size += QSize(left + right, top + bottom);
364 size += QSize(left + right, top + bottom);
345 return size;
365 return size;
346 }
366 }
347
367
348 QTCOMMERCIALCHART_END_NAMESPACE
368 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,50 +1,70
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
1 #ifndef LEGENDLAYOUT_H_
21 #ifndef LEGENDLAYOUT_H_
2 #define LEGENDLAYOUT_H_
22 #define LEGENDLAYOUT_H_
3 #include <QGraphicsLayout>
23 #include <QGraphicsLayout>
4 #include "qchartglobal.h"
24 #include "qchartglobal.h"
5
25
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
27
8 class QLegend;
28 class QLegend;
9
29
10 class LegendLayout : public QGraphicsLayout
30 class LegendLayout : public QGraphicsLayout
11 {
31 {
12 public:
32 public:
13
33
14 LegendLayout(QLegend* legend);
34 LegendLayout(QLegend* legend);
15 virtual ~LegendLayout();
35 virtual ~LegendLayout();
16
36
17 void setGeometry(const QRectF& rect);
37 void setGeometry(const QRectF& rect);
18
38
19 void setOffset(qreal x, qreal y);
39 void setOffset(qreal x, qreal y);
20 QPointF offset() const;
40 QPointF offset() const;
21
41
22 protected:
42 protected:
23 QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
43 QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
24 int count() const { return 0; }
44 int count() const { return 0; }
25 QGraphicsLayoutItem* itemAt(int) const { return 0; };
45 QGraphicsLayoutItem* itemAt(int) const { return 0; };
26 void removeAt(int){};
46 void removeAt(int){};
27
47
28 private:
48 private:
29 void setAttachedGeometry(const QRectF& rect);
49 void setAttachedGeometry(const QRectF& rect);
30 void setDettachedGeometry(const QRectF& rect);
50 void setDettachedGeometry(const QRectF& rect);
31
51
32 private:
52 private:
33 QLegend* m_legend;
53 QLegend* m_legend;
34 int m_marginBig;
54 int m_marginBig;
35 int m_marginSmall;
55 int m_marginSmall;
36 int m_marginTiny;
56 int m_marginTiny;
37 qreal m_offsetX;
57 qreal m_offsetX;
38 qreal m_offsetY;
58 qreal m_offsetY;
39 qreal m_minOffsetX;
59 qreal m_minOffsetX;
40 qreal m_minOffsetY;
60 qreal m_minOffsetY;
41 qreal m_maxOffsetX;
61 qreal m_maxOffsetX;
42 qreal m_maxOffsetY;
62 qreal m_maxOffsetY;
43 qreal m_width;
63 qreal m_width;
44 qreal m_height;
64 qreal m_height;
45
65
46 };
66 };
47
67
48 QTCOMMERCIALCHART_END_NAMESPACE
68 QTCOMMERCIALCHART_END_NAMESPACE
49
69
50 #endif
70 #endif
General Comments 0
You need to be logged in to leave comments. Login now