##// END OF EJS Templates
Kinetic scrolling is back for legend
sauimone -
r2189:4cb139890ac2
parent child
Show More
@@ -1,199 +1,197
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QPainter>
21 #include <QPainter>
22 #include <QGraphicsSceneEvent>
22 #include <QGraphicsSceneEvent>
23 #include <QGraphicsSimpleTextItem>
23 #include <QGraphicsSimpleTextItem>
24 #include <QDebug>
24 #include <QDebug>
25
25
26 #include "qlegend.h"
26 #include "qlegend.h"
27 #include "qlegend_p.h"
27 #include "qlegend_p.h"
28 #include "qlegendmarker.h"
28 #include "qlegendmarker.h"
29 #include "qlegendmarker_p.h"
29 #include "qlegendmarker_p.h"
30 #include "legendmarkeritem_p.h"
30 #include "legendmarkeritem_p.h"
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) :
34 LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) :
35 QGraphicsObject(parent),
35 QGraphicsObject(parent),
36 m_marker(marker),
36 m_marker(marker),
37 m_markerRect(0,0,10.0,10.0),
37 m_markerRect(0,0,10.0,10.0),
38 m_boundingRect(0,0,0,0),
38 m_boundingRect(0,0,0,0),
39 m_textItem(new QGraphicsSimpleTextItem(this)),
39 m_textItem(new QGraphicsSimpleTextItem(this)),
40 m_rectItem(new QGraphicsRectItem(this)),
40 m_rectItem(new QGraphicsRectItem(this)),
41 m_margin(4),
41 m_margin(4),
42 m_space(4)
42 m_space(4),
43 m_pressPos(0, 0)
43 {
44 {
44 m_rectItem->setRect(m_markerRect);
45 m_rectItem->setRect(m_markerRect);
45 }
46 }
46
47
47 void LegendMarkerItem::setPen(const QPen &pen)
48 void LegendMarkerItem::setPen(const QPen &pen)
48 {
49 {
49 m_rectItem->setPen(pen);
50 m_rectItem->setPen(pen);
50 }
51 }
51
52
52 QPen LegendMarkerItem::pen() const
53 QPen LegendMarkerItem::pen() const
53 {
54 {
54 return m_rectItem->pen();
55 return m_rectItem->pen();
55 }
56 }
56
57
57 void LegendMarkerItem::setBrush(const QBrush &brush)
58 void LegendMarkerItem::setBrush(const QBrush &brush)
58 {
59 {
59 m_rectItem->setBrush(brush);
60 m_rectItem->setBrush(brush);
60 }
61 }
61
62
62 QBrush LegendMarkerItem::brush() const
63 QBrush LegendMarkerItem::brush() const
63 {
64 {
64 return m_rectItem->brush();
65 return m_rectItem->brush();
65 }
66 }
66
67
67 void LegendMarkerItem::setFont(const QFont &font)
68 void LegendMarkerItem::setFont(const QFont &font)
68 {
69 {
69 m_textItem->setFont(font);
70 m_textItem->setFont(font);
70 QFontMetrics fn(font);
71 QFontMetrics fn(font);
71 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
72 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
72 updateGeometry();
73 updateGeometry();
73 }
74 }
74
75
75 QFont LegendMarkerItem::font() const
76 QFont LegendMarkerItem::font() const
76 {
77 {
77 return m_textItem->font();
78 return m_textItem->font();
78 }
79 }
79
80
80 void LegendMarkerItem::setLabel(const QString label)
81 void LegendMarkerItem::setLabel(const QString label)
81 {
82 {
82 m_label = label;
83 m_label = label;
83 updateGeometry();
84 updateGeometry();
84 }
85 }
85
86
86 QString LegendMarkerItem::label() const
87 QString LegendMarkerItem::label() const
87 {
88 {
88 return m_label;
89 return m_label;
89 }
90 }
90
91
91 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
92 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
92 {
93 {
93 m_textItem->setBrush(brush);
94 m_textItem->setBrush(brush);
94 }
95 }
95
96
96 QBrush LegendMarkerItem::labelBrush() const
97 QBrush LegendMarkerItem::labelBrush() const
97 {
98 {
98 return m_textItem->brush();
99 return m_textItem->brush();
99 }
100 }
100
101
101 void LegendMarkerItem::setGeometry(const QRectF& rect)
102 void LegendMarkerItem::setGeometry(const QRectF& rect)
102 {
103 {
103 QFontMetrics fn (m_font);
104 QFontMetrics fn (m_font);
104
105
105 int width = rect.width();
106 int width = rect.width();
106 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
107 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
107 qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin);
108 qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin);
108
109
109 if (fn.boundingRect(m_label).width() + x > width)
110 if (fn.boundingRect(m_label).width() + x > width)
110 {
111 {
111 QString string = m_label + "...";
112 QString string = m_label + "...";
112 while(fn.boundingRect(string).width() + x > width && string.length() > 3)
113 while(fn.boundingRect(string).width() + x > width && string.length() > 3)
113 string.remove(string.length() - 4, 1);
114 string.remove(string.length() - 4, 1);
114 m_textItem->setText(string);
115 m_textItem->setText(string);
115 }
116 }
116 else
117 else
117 m_textItem->setText(m_label);
118 m_textItem->setText(m_label);
118
119
119 const QRectF& textRect = m_textItem->boundingRect();
120 const QRectF& textRect = m_textItem->boundingRect();
120
121
121
122
122 m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2);
123 m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2);
123 m_rectItem->setRect(m_markerRect);
124 m_rectItem->setRect(m_markerRect);
124 m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2);
125 m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2);
125
126
126 prepareGeometryChange();
127 prepareGeometryChange();
127 m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y);
128 m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y);
128 }
129 }
129
130
130 QRectF LegendMarkerItem::boundingRect() const
131 QRectF LegendMarkerItem::boundingRect() const
131 {
132 {
132 return m_boundingRect;
133 return m_boundingRect;
133 }
134 }
134
135
135 void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
136 void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
136 {
137 {
137 Q_UNUSED(option)
138 Q_UNUSED(option)
138 Q_UNUSED(widget)
139 Q_UNUSED(widget)
139 Q_UNUSED(painter)
140 Q_UNUSED(painter)
140 }
141 }
141
142
142 QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
143 QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
143 {
144 {
144 Q_UNUSED(constraint)
145 Q_UNUSED(constraint)
145
146
146 QFontMetrics fn(m_textItem->font());
147 QFontMetrics fn(m_textItem->font());
147 QSizeF sh;
148 QSizeF sh;
148
149
149 switch (which) {
150 switch (which) {
150 case Qt::MinimumSize:
151 case Qt::MinimumSize:
151 sh = QSizeF(fn.boundingRect("...").width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
152 sh = QSizeF(fn.boundingRect("...").width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
152 break;
153 break;
153 case Qt::PreferredSize:
154 case Qt::PreferredSize:
154 sh = QSizeF(fn.boundingRect(m_label).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
155 sh = QSizeF(fn.boundingRect(m_label).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
155 break;
156 break;
156 default:
157 default:
157 break;
158 break;
158 }
159 }
159
160
160 return sh;
161 return sh;
161 }
162 }
162
163
163 void LegendMarkerItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
164 void LegendMarkerItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
164 {
165 {
165 MouseEventHandler::handleMousePressEvent(event);
166 MouseEventHandler::handleMousePressEvent(event);
167 m_pressPos = event->screenPos();
166 }
168 }
167
169
168 void LegendMarkerItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
170 void LegendMarkerItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
169 {
171 {
170 MouseEventHandler::handleMouseMoveEvent(event);
172 MouseEventHandler::handleMouseMoveEvent(event);
171 }
173 }
172
174
173 void LegendMarkerItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
175 void LegendMarkerItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
174 {
176 {
175 MouseEventHandler::handleMouseReleaseEvent(event);
177 MouseEventHandler::handleMouseReleaseEvent(event);
176 }
178 }
177
179
178 void LegendMarkerItem::mouseClicked()
180 void LegendMarkerItem::mouseClicked()
179 {
181 {
180 emit m_marker->q_func()->clicked();
182 emit m_marker->q_func()->clicked();
181 }
183 }
182
184
183 void LegendMarkerItem::mouseMoved(QPointF delta)
185 void LegendMarkerItem::mouseMoved(const QPointF &delta)
184 {
186 {
185 qreal dx = m_marker->m_legend->d_ptr->offset().x() - delta.x();
187 m_marker->m_legend->d_ptr->move(delta);
186 qreal dy = m_marker->m_legend->d_ptr->offset().y() - delta.y();
187 m_marker->m_legend->d_ptr->setOffset(dx, dy);
188 }
188 }
189
189
190 void LegendMarkerItem::mouseReleased(QPointF delta)
190 void LegendMarkerItem::mouseReleased(const QPointF &pos)
191 {
191 {
192 qreal dx = m_marker->m_legend->d_ptr->offset().x() - delta.x();
192 m_marker->m_legend->d_ptr->release(pos - m_pressPos);
193 qreal dy = m_marker->m_legend->d_ptr->offset().y() - delta.y();
194 m_marker->m_legend->d_ptr->setOffset(dx, dy);
195 }
193 }
196
194
197 #include "moc_legendmarkeritem_p.cpp"
195 #include "moc_legendmarkeritem_p.cpp"
198
196
199 QTCOMMERCIALCHART_END_NAMESPACE
197 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,110 +1,112
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef LEGENDMARKERITEM_P_H
30 #ifndef LEGENDMARKERITEM_P_H
31 #define LEGENDMARKERITEM_P_H
31 #define LEGENDMARKERITEM_P_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include <QGraphicsObject>
34 #include <QGraphicsObject>
35 #include <QFont>
35 #include <QFont>
36 #include <QBrush>
36 #include <QBrush>
37 #include <QPen>
37 #include <QPen>
38 #include <QGraphicsSimpleTextItem>
38 #include <QGraphicsSimpleTextItem>
39 #include <QGraphicsLayoutItem>
39 #include <QGraphicsLayoutItem>
40 #include "mouseeventhandler_p.h"
40 #include "mouseeventhandler_p.h"
41
41
42 QTCOMMERCIALCHART_BEGIN_NAMESPACE
42 QTCOMMERCIALCHART_BEGIN_NAMESPACE
43
43
44 class QLegendMarkerPrivate;
44 class QLegendMarkerPrivate;
45
45
46 class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem, public MouseEventHandler
46 class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem, public MouseEventHandler
47 {
47 {
48 Q_OBJECT
48 Q_OBJECT
49 Q_INTERFACES(QGraphicsLayoutItem)
49 Q_INTERFACES(QGraphicsLayoutItem)
50 public:
50 public:
51 explicit LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent = 0);
51 explicit LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent = 0);
52
52
53 void setPen(const QPen &pen);
53 void setPen(const QPen &pen);
54 QPen pen() const;
54 QPen pen() const;
55
55
56 void setBrush(const QBrush &brush);
56 void setBrush(const QBrush &brush);
57 QBrush brush() const;
57 QBrush brush() const;
58
58
59 void setFont(const QFont &font);
59 void setFont(const QFont &font);
60 QFont font() const;
60 QFont font() const;
61
61
62 void setLabel(const QString label);
62 void setLabel(const QString label);
63 QString label() const;
63 QString label() const;
64
64
65 void setLabelBrush(const QBrush &brush);
65 void setLabelBrush(const QBrush &brush);
66 QBrush labelBrush() const;
66 QBrush labelBrush() const;
67
67
68 void setGeometry(const QRectF& rect);
68 void setGeometry(const QRectF& rect);
69
69
70 QRectF boundingRect() const;
70 QRectF boundingRect() const;
71
71
72 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
72 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
73
73
74 QSizeF sizeHint (Qt::SizeHint which, const QSizeF& constraint) const;
74 QSizeF sizeHint (Qt::SizeHint which, const QSizeF& constraint) const;
75
75
76 // Event handlers, logic delegated to MouseEventHandler
76 // Event handlers, logic delegated to MouseEventHandler
77 void mousePressEvent(QGraphicsSceneMouseEvent *event);
77 void mousePressEvent(QGraphicsSceneMouseEvent *event);
78 void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
78 void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
79 void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
79 void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
80
80
81 // Filtered callbacks from MouseEventHandler
81 // Filtered callbacks from MouseEventHandler
82 void mouseClicked();
82 void mouseClicked();
83 void mouseMoved(QPointF delta);
83 void mouseMoved(const QPointF &delta);
84 void mouseReleased(QPointF delta);
84 void mouseReleased(const QPointF &pos);
85
85
86 protected:
86 protected:
87 QLegendMarkerPrivate *m_marker; // Knows
87 QLegendMarkerPrivate *m_marker; // Knows
88 QRectF m_markerRect;
88 QRectF m_markerRect;
89 QRectF m_boundingRect;
89 QRectF m_boundingRect;
90 QGraphicsSimpleTextItem *m_textItem;
90 QGraphicsSimpleTextItem *m_textItem;
91 QGraphicsRectItem *m_rectItem;
91 QGraphicsRectItem *m_rectItem;
92 qreal m_margin;
92 qreal m_margin;
93 qreal m_space;
93 qreal m_space;
94 QString m_label;
94 QString m_label;
95
95
96 QBrush m_labelBrush;
96 QBrush m_labelBrush;
97 QFont m_font;
97 QFont m_font;
98 QPen m_pen;
98 QPen m_pen;
99 QBrush m_brush;
99 QBrush m_brush;
100 bool m_visible;
100 bool m_visible;
101
101
102 QPointF m_pressPos;
103
102 friend class QLegendMarker;
104 friend class QLegendMarker;
103 friend class QLegendMarkerPrivate;
105 friend class QLegendMarkerPrivate;
104 friend class LegendMarkerItem;
106 friend class LegendMarkerItem;
105 friend class LegendLayout;
107 friend class LegendLayout;
106 };
108 };
107
109
108 QTCOMMERCIALCHART_END_NAMESPACE
110 QTCOMMERCIALCHART_END_NAMESPACE
109
111
110 #endif // LEGENDMARKERITEM_P_H
112 #endif // LEGENDMARKERITEM_P_H
@@ -1,59 +1,52
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30
30
31 #ifndef LEGENDSCROLLER_P_H
31 #ifndef LEGENDSCROLLER_P_H
32 #define LEGENDSCROLLER_P_H
32 #define LEGENDSCROLLER_P_H
33
33
34 #include "qlegend.h"
34 #include "qlegend.h"
35 #include "qlegend_p.h"
35 #include "qlegend_p.h"
36 #include "scroller_p.h"
37
36
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
38
40 class LegendScroller: public QLegend, public Scroller
39 class LegendScroller: public QLegend
41 {
40 {
42
41
43 public:
42 public:
44 LegendScroller(QChart *chart): QLegend(chart) { }
43 LegendScroller(QChart *chart): QLegend(chart) { }
45
44
46 void setOffset(const QPointF &point) { d_ptr->setOffset(point.x(), point.y()); }
45 void setOffset(const QPointF &point) { d_ptr->setOffset(point); }
47
46
48 QPointF offset() const { return d_ptr->offset(); }
47 QPointF offset() const { return d_ptr->offset(); }
49
50 void mousePressEvent(QGraphicsSceneMouseEvent *event) { Scroller::mousePressEvent(event); }
51
52 void mouseMoveEvent(QGraphicsSceneMouseEvent *event) { Scroller::mouseMoveEvent(event); }
53
54 void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { Scroller::mouseReleaseEvent(event); }
55 };
48 };
56
49
57 QTCOMMERCIALCHART_END_NAMESPACE
50 QTCOMMERCIALCHART_END_NAMESPACE
58
51
59 #endif
52 #endif
@@ -1,107 +1,106
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "mouseeventhandler_p.h"
21 #include "mouseeventhandler_p.h"
22 #include <QGraphicsSceneMouseEvent>
22 #include <QGraphicsSceneMouseEvent>
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 MouseEventHandler::MouseEventHandler() :
26 MouseEventHandler::MouseEventHandler() :
27 m_lastPos(0,0),
27 m_lastPos(0, 0),
28 m_state(Idle),
28 m_state(Idle),
29 m_treshold(10)
29 m_treshold(10)
30 {
30 {
31 }
31 }
32
32
33 MouseEventHandler::~MouseEventHandler()
33 MouseEventHandler::~MouseEventHandler()
34 {
34 {
35 }
35 }
36
36
37 void MouseEventHandler::setMoveTreshold(qreal treshold)
37 void MouseEventHandler::setMoveTreshold(qreal treshold)
38 {
38 {
39 m_treshold = treshold;
39 m_treshold = treshold;
40 }
40 }
41
41
42 void MouseEventHandler::handleMousePressEvent(QGraphicsSceneMouseEvent* event)
42 void MouseEventHandler::handleMousePressEvent(QGraphicsSceneMouseEvent* event)
43 {
43 {
44 m_lastPos = event->screenPos();
44 m_lastPos = event->screenPos();
45 m_state = Pressed;
45 m_state = Pressed;
46 event->accept();
46 event->accept();
47 }
47 }
48
48
49 void MouseEventHandler::handleMouseMoveEvent(QGraphicsSceneMouseEvent* event)
49 void MouseEventHandler::handleMouseMoveEvent(QGraphicsSceneMouseEvent* event)
50 {
50 {
51 QPointF delta = event->screenPos() - m_lastPos;
51 QPointF delta = event->screenPos() - m_lastPos;
52
52
53 switch (m_state) {
53 switch (m_state) {
54 case Pressed: {
54 case Pressed: {
55 // calculate treshold. If enough, change to move state and send out move deltas.
55 // calculate treshold. If enough, change to move state and send out move deltas.
56 if (qAbs(delta.x()) > m_treshold || qAbs(delta.y()) > m_treshold) {
56 if (qAbs(delta.x()) > m_treshold || qAbs(delta.y()) > m_treshold) {
57 m_state = Moved;
57 m_state = Moved;
58 m_lastPos = event->screenPos();
58 m_lastPos = event->screenPos();
59 mouseMoved(delta);
59 mouseMoved(delta);
60 }
60 }
61 event->accept();
61 event->accept();
62 break;
62 break;
63 }
63 }
64 case Moved: {
64 case Moved: {
65 m_lastPos = event->screenPos();
65 m_lastPos = event->screenPos();
66 mouseMoved(delta);
66 mouseMoved(delta);
67 event->accept();
67 event->accept();
68 break;
68 break;
69 }
69 }
70 case Idle:
70 case Idle:
71 default: {
71 default: {
72 event->ignore();
72 event->ignore();
73 break;
73 break;
74 }
74 }
75 }
75 }
76 }
76 }
77
77
78 void MouseEventHandler::handleMouseReleaseEvent(QGraphicsSceneMouseEvent* event)
78 void MouseEventHandler::handleMouseReleaseEvent(QGraphicsSceneMouseEvent* event)
79 {
79 {
80 QPointF delta = event->screenPos() - m_lastPos;
81 m_lastPos = event->screenPos();
80 m_lastPos = event->screenPos();
82
81
83 switch (m_state) {
82 switch (m_state) {
84 case Pressed:
83 case Pressed:
85 {
84 {
86 m_state = Idle;
85 m_state = Idle;
87 mouseClicked();
86 mouseClicked();
88 event->accept();
87 event->accept();
89 break;
88 break;
90 }
89 }
91 case Moved:
90 case Moved:
92 {
91 {
93 m_state = Idle;
92 m_state = Idle;
94 mouseReleased(delta);
93 mouseReleased(m_lastPos);
95 event->accept();
94 event->accept();
96 break;
95 break;
97 }
96 }
98 default:
97 default:
99 {
98 {
100 m_state = Idle;
99 m_state = Idle;
101 event->ignore();
100 event->ignore();
102 break;
101 break;
103 }
102 }
104 }
103 }
105 }
104 }
106
105
107 QTCOMMERCIALCHART_END_NAMESPACE
106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,89 +1,89
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 /*
30 /*
31 Purpose of this class is to resolve what happens during MousePress - MouseMove - MouseRelease event chain.
31 Purpose of this class is to resolve what happens during MousePress - MouseMove - MouseRelease event chain.
32 Threre can be many MouseMove events and if they all are below our treshold, the result is clicked call.
32 Threre can be many MouseMove events and if they all are below our treshold, the result is clicked call.
33 If any move event goes over treshold, result will be many moved(QPointF delta) calls,
33 If any move event goes over treshold, result will be many moved(QPointF delta) calls,
34 where delta is calculated from original position.
34 where delta is calculated from original position.
35
35
36 Reason for this is, that legend marker don't know when it should emit clicked and when it should
36 Reason for this is, that legend marker don't know when it should emit clicked and when it should
37 allow legend to scroll. We only get this information when some move goes beyond treshold, but
37 allow legend to scroll. We only get this information when some move goes beyond treshold, but
38 then it will be too late to let the scroller handle events, because marker is already handling.
38 then it will be too late to let the scroller handle events, because marker is already handling.
39
39
40 By handling clicked calls in markers and moved calls in scroller, the problem is solved.
40 By handling clicked calls in markers and moved calls in scroller, the problem is solved.
41 Derived class descides, should the virtual method be implemented as signal or not.
41 Derived class descides, should the virtual method be implemented as signal or not.
42
42
43 This could be implemented in LegendMarkerItem, but this way the code is reusable and in one place only.
43 This could be implemented in LegendMarkerItem, but this way the code is reusable and in one place only.
44 */
44 */
45
45
46 #ifndef MOUSEEVENTHANDLER_H
46 #ifndef MOUSEEVENTHANDLER_H
47 #define MOUSEEVENTHANDLER_H
47 #define MOUSEEVENTHANDLER_H
48
48
49 #include "qchartglobal.h"
49 #include "qchartglobal.h"
50 #include <QBasicTimer>
50 #include <QBasicTimer>
51 #include <QTime>
51 #include <QTime>
52 #include <QPointF>
52 #include <QPointF>
53
53
54 class QGraphicsSceneMouseEvent;
54 class QGraphicsSceneMouseEvent;
55
55
56 QTCOMMERCIALCHART_BEGIN_NAMESPACE
56 QTCOMMERCIALCHART_BEGIN_NAMESPACE
57
57
58 class MouseEventHandler
58 class MouseEventHandler
59 {
59 {
60 public:
60 public:
61 enum State {
61 enum State {
62 Idle,
62 Idle,
63 Pressed,
63 Pressed,
64 Moved,
64 Moved,
65 Released
65 Released
66 };
66 };
67
67
68 MouseEventHandler();
68 MouseEventHandler();
69 virtual ~MouseEventHandler();
69 virtual ~MouseEventHandler();
70
70
71 void setMoveTreshold(qreal treshold);
71 void setMoveTreshold(qreal treshold);
72
72
73 virtual void mouseClicked() = 0;
73 virtual void mouseClicked() = 0;
74 virtual void mouseMoved(QPointF delta) = 0;
74 virtual void mouseMoved(const QPointF &delta) = 0;
75 virtual void mouseReleased(QPointF delta) = 0;
75 virtual void mouseReleased(const QPointF &pos) = 0;
76
76
77 void handleMousePressEvent(QGraphicsSceneMouseEvent* event);
77 void handleMousePressEvent(QGraphicsSceneMouseEvent* event);
78 void handleMouseMoveEvent(QGraphicsSceneMouseEvent* event);
78 void handleMouseMoveEvent(QGraphicsSceneMouseEvent* event);
79 void handleMouseReleaseEvent(QGraphicsSceneMouseEvent* event);
79 void handleMouseReleaseEvent(QGraphicsSceneMouseEvent* event);
80
80
81 private:
81 private:
82 QPointF m_lastPos;
82 QPointF m_lastPos;
83 State m_state;
83 State m_state;
84 qreal m_treshold;
84 qreal m_treshold;
85 };
85 };
86
86
87 QTCOMMERCIALCHART_END_NAMESPACE
87 QTCOMMERCIALCHART_END_NAMESPACE
88
88
89 #endif // MOUSEEVENTHANDLER_H
89 #endif // MOUSEEVENTHANDLER_H
@@ -1,669 +1,669
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qlegend.h"
21 #include "qlegend.h"
22 #include "qlegend_p.h"
22 #include "qlegend_p.h"
23 #include "qabstractseries.h"
23 #include "qabstractseries.h"
24 #include "qabstractseries_p.h"
24 #include "qabstractseries_p.h"
25 #include "qchart_p.h"
25 #include "qchart_p.h"
26 #include "legendlayout_p.h"
26 #include "legendlayout_p.h"
27 #include "qxyseries.h"
27 #include "qxyseries.h"
28 #include "qlineseries.h"
28 #include "qlineseries.h"
29 #include "qareaseries.h"
29 #include "qareaseries.h"
30 #include "qscatterseries.h"
30 #include "qscatterseries.h"
31 #include "qsplineseries.h"
31 #include "qsplineseries.h"
32 #include "qabstractbarseries.h"
32 #include "qabstractbarseries.h"
33 #include "qstackedbarseries.h"
33 #include "qstackedbarseries.h"
34 #include "qpercentbarseries.h"
34 #include "qpercentbarseries.h"
35 #include "qbarset.h"
35 #include "qbarset.h"
36 #include "qpieseries.h"
36 #include "qpieseries.h"
37 #include "qpieseries_p.h"
37 #include "qpieseries_p.h"
38 #include "qpieslice.h"
38 #include "qpieslice.h"
39 #include "chartpresenter_p.h"
39 #include "chartpresenter_p.h"
40 #include "chartlayout_p.h"
40 #include "chartlayout_p.h"
41 #include <QPainter>
41 #include <QPainter>
42 #include <QPen>
42 #include <QPen>
43 #include <QTimer>
43 #include <QTimer>
44 #include <QGraphicsSceneEvent>
44 #include <QGraphicsSceneEvent>
45 #include <QGraphicsItemGroup>
45 #include <QGraphicsItemGroup>
46
46
47 #include "qlegendmarker.h"
47 #include "qlegendmarker.h"
48 #include "qlegendmarker_p.h"
48 #include "qlegendmarker_p.h"
49 #include "legendmarkeritem_p.h"
49 #include "legendmarkeritem_p.h"
50
50
51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
52
52
53 /*!
53 /*!
54 \class QLegend
54 \class QLegend
55 \brief Legend object
55 \brief Legend object
56 \mainclass
56 \mainclass
57
57
58 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
58 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
59 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
59 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
60 handle the drawing manually.
60 handle the drawing manually.
61 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
61 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
62
62
63 \image examples_percentbarchart_legend.png
63 \image examples_percentbarchart_legend.png
64
64
65 \sa QChart
65 \sa QChart
66 */
66 */
67 /*!
67 /*!
68 \qmlclass Legend QLegend
68 \qmlclass Legend QLegend
69 \brief Legend is part of QtCommercial Chart QML API.
69 \brief Legend is part of QtCommercial Chart QML API.
70
70
71 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
71 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
72 series have been changed. Legend is used via ChartView class. For example:
72 series have been changed. Legend is used via ChartView class. For example:
73 \code
73 \code
74 ChartView {
74 ChartView {
75 legend.visible: true
75 legend.visible: true
76 legend.alignment: Qt.AlignBottom
76 legend.alignment: Qt.AlignBottom
77 // Add a few series...
77 // Add a few series...
78 }
78 }
79 \endcode
79 \endcode
80
80
81 \image examples_percentbarchart_legend.png
81 \image examples_percentbarchart_legend.png
82 */
82 */
83
83
84 /*!
84 /*!
85 \property QLegend::alignment
85 \property QLegend::alignment
86 \brief The alignment of the legend.
86 \brief The alignment of the legend.
87
87
88 Legend paints on the defined position in the chart. The following alignments are supported:
88 Legend paints on the defined position in the chart. The following alignments are supported:
89 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
89 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
90 */
90 */
91 /*!
91 /*!
92 \qmlproperty Qt.Alignment Legend::alignment
92 \qmlproperty Qt.Alignment Legend::alignment
93 \brief The alignment of the legend.
93 \brief The alignment of the legend.
94
94
95 Legend paints on the defined position in the chart. The following alignments are supported:
95 Legend paints on the defined position in the chart. The following alignments are supported:
96 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
96 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
97 */
97 */
98
98
99 /*!
99 /*!
100 \property QLegend::backgroundVisible
100 \property QLegend::backgroundVisible
101 Whether the legend background is visible or not.
101 Whether the legend background is visible or not.
102 */
102 */
103 /*!
103 /*!
104 \qmlproperty bool Legend::backgroundVisible
104 \qmlproperty bool Legend::backgroundVisible
105 Whether the legend background is visible or not.
105 Whether the legend background is visible or not.
106 */
106 */
107
107
108 /*!
108 /*!
109 \property QLegend::color
109 \property QLegend::color
110 The color of the legend, i.e. the background (brush) color. Note that if you change the color
110 The color of the legend, i.e. the background (brush) color. Note that if you change the color
111 of the legend, the style of the legend brush is set to Qt::SolidPattern.
111 of the legend, the style of the legend brush is set to Qt::SolidPattern.
112 */
112 */
113 /*!
113 /*!
114 \qmlproperty color Legend::color
114 \qmlproperty color Legend::color
115 The color of the legend, i.e. the background (brush) color.
115 The color of the legend, i.e. the background (brush) color.
116 */
116 */
117
117
118 /*!
118 /*!
119 \property QLegend::borderColor
119 \property QLegend::borderColor
120 The border color of the legend, i.e. the line color.
120 The border color of the legend, i.e. the line color.
121 */
121 */
122 /*!
122 /*!
123 \qmlproperty color Legend::borderColor
123 \qmlproperty color Legend::borderColor
124 The border color of the legend, i.e. the line color.
124 The border color of the legend, i.e. the line color.
125 */
125 */
126
126
127 /*!
127 /*!
128 \property QLegend::font
128 \property QLegend::font
129 The font of markers used by legend
129 The font of markers used by legend
130 */
130 */
131 /*!
131 /*!
132 \qmlproperty Font Legend::font
132 \qmlproperty Font Legend::font
133 The font of markers used by legend
133 The font of markers used by legend
134 */
134 */
135
135
136 /*!
136 /*!
137 \property QLegend::labelColor
137 \property QLegend::labelColor
138 The color of brush used to draw labels.
138 The color of brush used to draw labels.
139 */
139 */
140 /*!
140 /*!
141 \qmlproperty color QLegend::labelColor
141 \qmlproperty color QLegend::labelColor
142 The color of brush used to draw labels.
142 The color of brush used to draw labels.
143 */
143 */
144
144
145 /*!
145 /*!
146 \fn void QLegend::backgroundVisibleChanged(bool)
146 \fn void QLegend::backgroundVisibleChanged(bool)
147 The visibility of the legend background changed to \a visible.
147 The visibility of the legend background changed to \a visible.
148 */
148 */
149
149
150 /*!
150 /*!
151 \fn void QLegend::colorChanged(QColor)
151 \fn void QLegend::colorChanged(QColor)
152 The color of the legend background changed to \a color.
152 The color of the legend background changed to \a color.
153 */
153 */
154
154
155 /*!
155 /*!
156 \fn void QLegend::borderColorChanged(QColor)
156 \fn void QLegend::borderColorChanged(QColor)
157 The border color of the legend background changed to \a color.
157 The border color of the legend background changed to \a color.
158 */
158 */
159
159
160 /*!
160 /*!
161 \fn void QLegend::fontChanged(QFont)
161 \fn void QLegend::fontChanged(QFont)
162 The font of markers of the legend changed to \a font.
162 The font of markers of the legend changed to \a font.
163 */
163 */
164
164
165 /*!
165 /*!
166 \fn void QLegend::labelColorChanged(QColor color)
166 \fn void QLegend::labelColorChanged(QColor color)
167 This signal is emitted when the color of brush used to draw labels has changed to \a color.
167 This signal is emitted when the color of brush used to draw labels has changed to \a color.
168 */
168 */
169
169
170 /*!
170 /*!
171 Constructs the legend object and sets the parent to \a parent
171 Constructs the legend object and sets the parent to \a parent
172 */
172 */
173
173
174 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
174 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
175 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
175 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
176 {
176 {
177 setZValue(ChartPresenter::LegendZValue);
177 setZValue(ChartPresenter::LegendZValue);
178 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
178 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
179 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*)));
179 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*)));
180 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
180 QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*)));
181 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleCountChanged(QAbstractSeries*)));
181 // QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleCountChanged(QAbstractSeries*)));
182 setLayout(d_ptr->m_layout);
182 setLayout(d_ptr->m_layout);
183 }
183 }
184
184
185 /*!
185 /*!
186 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
186 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
187 */
187 */
188 QLegend::~QLegend()
188 QLegend::~QLegend()
189 {
189 {
190 }
190 }
191
191
192 /*!
192 /*!
193 \internal
193 \internal
194 */
194 */
195 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
195 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
196 {
196 {
197 Q_UNUSED(option)
197 Q_UNUSED(option)
198 Q_UNUSED(widget)
198 Q_UNUSED(widget)
199
199
200 if (!d_ptr->m_backgroundVisible)
200 if (!d_ptr->m_backgroundVisible)
201 return;
201 return;
202
202
203 painter->setOpacity(opacity());
203 painter->setOpacity(opacity());
204 painter->setPen(d_ptr->m_pen);
204 painter->setPen(d_ptr->m_pen);
205 painter->setBrush(d_ptr->m_brush);
205 painter->setBrush(d_ptr->m_brush);
206 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
206 painter->drawRoundRect(rect(), d_ptr->roundness(rect().width()), d_ptr->roundness(rect().height()));
207 }
207 }
208
208
209
209
210 /*!
210 /*!
211 Sets the \a brush of legend. Brush affects the background of legend.
211 Sets the \a brush of legend. Brush affects the background of legend.
212 */
212 */
213 void QLegend::setBrush(const QBrush &brush)
213 void QLegend::setBrush(const QBrush &brush)
214 {
214 {
215 if (d_ptr->m_brush != brush) {
215 if (d_ptr->m_brush != brush) {
216 d_ptr->m_brush = brush;
216 d_ptr->m_brush = brush;
217 update();
217 update();
218 emit colorChanged(brush.color());
218 emit colorChanged(brush.color());
219 }
219 }
220 }
220 }
221
221
222 /*!
222 /*!
223 Returns the brush used by legend.
223 Returns the brush used by legend.
224 */
224 */
225 QBrush QLegend::brush() const
225 QBrush QLegend::brush() const
226 {
226 {
227 return d_ptr->m_brush;
227 return d_ptr->m_brush;
228 }
228 }
229
229
230 void QLegend::setColor(QColor color)
230 void QLegend::setColor(QColor color)
231 {
231 {
232 QBrush b = d_ptr->m_brush;
232 QBrush b = d_ptr->m_brush;
233 if (b.style() != Qt::SolidPattern || b.color() != color) {
233 if (b.style() != Qt::SolidPattern || b.color() != color) {
234 b.setStyle(Qt::SolidPattern);
234 b.setStyle(Qt::SolidPattern);
235 b.setColor(color);
235 b.setColor(color);
236 setBrush(b);
236 setBrush(b);
237 }
237 }
238 }
238 }
239
239
240 QColor QLegend::color()
240 QColor QLegend::color()
241 {
241 {
242 return d_ptr->m_brush.color();
242 return d_ptr->m_brush.color();
243 }
243 }
244
244
245 /*!
245 /*!
246 Sets the \a pen of legend. Pen affects the legend borders.
246 Sets the \a pen of legend. Pen affects the legend borders.
247 */
247 */
248 void QLegend::setPen(const QPen &pen)
248 void QLegend::setPen(const QPen &pen)
249 {
249 {
250 if (d_ptr->m_pen != pen) {
250 if (d_ptr->m_pen != pen) {
251 d_ptr->m_pen = pen;
251 d_ptr->m_pen = pen;
252 update();
252 update();
253 emit borderColorChanged(pen.color());
253 emit borderColorChanged(pen.color());
254 }
254 }
255 }
255 }
256
256
257 /*!
257 /*!
258 Returns the pen used by legend
258 Returns the pen used by legend
259 */
259 */
260
260
261 QPen QLegend::pen() const
261 QPen QLegend::pen() const
262 {
262 {
263 return d_ptr->m_pen;
263 return d_ptr->m_pen;
264 }
264 }
265
265
266 void QLegend::setFont(const QFont &font)
266 void QLegend::setFont(const QFont &font)
267 {
267 {
268 if (d_ptr->m_font != font)
268 if (d_ptr->m_font != font)
269 d_ptr->m_font = font;
269 d_ptr->m_font = font;
270
270
271 foreach (QLegendMarker *marker, d_ptr->markers()) {
271 foreach (QLegendMarker *marker, d_ptr->markers()) {
272 marker->setFont(d_ptr->m_font);
272 marker->setFont(d_ptr->m_font);
273 }
273 }
274 layout()->invalidate();
274 layout()->invalidate();
275 emit fontChanged(font);
275 emit fontChanged(font);
276 }
276 }
277
277
278 QFont QLegend::font() const
278 QFont QLegend::font() const
279 {
279 {
280 return d_ptr->m_font;
280 return d_ptr->m_font;
281 }
281 }
282
282
283 void QLegend::setBorderColor(QColor color)
283 void QLegend::setBorderColor(QColor color)
284 {
284 {
285 QPen p = d_ptr->m_pen;
285 QPen p = d_ptr->m_pen;
286 if (p.color() != color) {
286 if (p.color() != color) {
287 p.setColor(color);
287 p.setColor(color);
288 setPen(p);
288 setPen(p);
289 }
289 }
290 }
290 }
291
291
292 QColor QLegend::borderColor()
292 QColor QLegend::borderColor()
293 {
293 {
294 return d_ptr->m_pen.color();
294 return d_ptr->m_pen.color();
295 }
295 }
296
296
297 /*!
297 /*!
298 Set brush used to draw labels to \a brush.
298 Set brush used to draw labels to \a brush.
299 */
299 */
300 void QLegend::setLabelBrush(const QBrush &brush)
300 void QLegend::setLabelBrush(const QBrush &brush)
301 {
301 {
302 if (d_ptr->m_labelBrush != brush) {
302 if (d_ptr->m_labelBrush != brush) {
303 d_ptr->m_labelBrush = brush;
303 d_ptr->m_labelBrush = brush;
304 foreach (QLegendMarker *marker, d_ptr->markers()) {
304 foreach (QLegendMarker *marker, d_ptr->markers()) {
305 marker->setLabelBrush(d_ptr->m_labelBrush);
305 marker->setLabelBrush(d_ptr->m_labelBrush);
306 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
306 // Note: The pen of the marker rectangle could be exposed in the public QLegend API
307 // instead of mapping it from label brush color
307 // instead of mapping it from label brush color
308 marker->setPen(brush.color());
308 marker->setPen(brush.color());
309 }
309 }
310 emit labelColorChanged(brush.color());
310 emit labelColorChanged(brush.color());
311 }
311 }
312 }
312 }
313
313
314 /*!
314 /*!
315 Brush used to draw labels.
315 Brush used to draw labels.
316 */
316 */
317 QBrush QLegend::labelBrush() const
317 QBrush QLegend::labelBrush() const
318 {
318 {
319 return d_ptr->m_labelBrush;
319 return d_ptr->m_labelBrush;
320 }
320 }
321
321
322 void QLegend::setLabelColor(QColor color)
322 void QLegend::setLabelColor(QColor color)
323 {
323 {
324 QBrush b = d_ptr->m_labelBrush;
324 QBrush b = d_ptr->m_labelBrush;
325 if (b.style() != Qt::SolidPattern || b.color() != color) {
325 if (b.style() != Qt::SolidPattern || b.color() != color) {
326 b.setStyle(Qt::SolidPattern);
326 b.setStyle(Qt::SolidPattern);
327 b.setColor(color);
327 b.setColor(color);
328 setLabelBrush(b);
328 setLabelBrush(b);
329 }
329 }
330 }
330 }
331
331
332 QColor QLegend::labelColor() const
332 QColor QLegend::labelColor() const
333 {
333 {
334 return d_ptr->m_labelBrush.color();
334 return d_ptr->m_labelBrush.color();
335 }
335 }
336
336
337
337
338 void QLegend::setAlignment(Qt::Alignment alignment)
338 void QLegend::setAlignment(Qt::Alignment alignment)
339 {
339 {
340 if (d_ptr->m_alignment != alignment) {
340 if (d_ptr->m_alignment != alignment) {
341 d_ptr->m_alignment = alignment;
341 d_ptr->m_alignment = alignment;
342 layout()->invalidate();
342 layout()->invalidate();
343 }
343 }
344 }
344 }
345
345
346 Qt::Alignment QLegend::alignment() const
346 Qt::Alignment QLegend::alignment() const
347 {
347 {
348 return d_ptr->m_alignment;
348 return d_ptr->m_alignment;
349 }
349 }
350
350
351 /*!
351 /*!
352 Detaches the legend from chart. Chart won't change layout of the legend.
352 Detaches the legend from chart. Chart won't change layout of the legend.
353 */
353 */
354 void QLegend::detachFromChart()
354 void QLegend::detachFromChart()
355 {
355 {
356 d_ptr->m_attachedToChart = false;
356 d_ptr->m_attachedToChart = false;
357 layout()->invalidate();
357 layout()->invalidate();
358 setParent(0);
358 setParent(0);
359
359
360 }
360 }
361
361
362 /*!
362 /*!
363 Attaches the legend to chart. Chart may change layout of the legend.
363 Attaches the legend to chart. Chart may change layout of the legend.
364 */
364 */
365 void QLegend::attachToChart()
365 void QLegend::attachToChart()
366 {
366 {
367 d_ptr->m_attachedToChart = true;
367 d_ptr->m_attachedToChart = true;
368 layout()->invalidate();
368 layout()->invalidate();
369 setParent(d_ptr->m_chart);
369 setParent(d_ptr->m_chart);
370 }
370 }
371
371
372 /*!
372 /*!
373 Returns true, if legend is attached to chart.
373 Returns true, if legend is attached to chart.
374 */
374 */
375 bool QLegend::isAttachedToChart()
375 bool QLegend::isAttachedToChart()
376 {
376 {
377 return d_ptr->m_attachedToChart;
377 return d_ptr->m_attachedToChart;
378 }
378 }
379
379
380 /*!
380 /*!
381 Sets the visibility of legend background to \a visible
381 Sets the visibility of legend background to \a visible
382 */
382 */
383 void QLegend::setBackgroundVisible(bool visible)
383 void QLegend::setBackgroundVisible(bool visible)
384 {
384 {
385 if (d_ptr->m_backgroundVisible != visible) {
385 if (d_ptr->m_backgroundVisible != visible) {
386 d_ptr->m_backgroundVisible = visible;
386 d_ptr->m_backgroundVisible = visible;
387 update();
387 update();
388 emit backgroundVisibleChanged(visible);
388 emit backgroundVisibleChanged(visible);
389 }
389 }
390 }
390 }
391
391
392 /*!
392 /*!
393 Returns the visibility of legend background
393 Returns the visibility of legend background
394 */
394 */
395 bool QLegend::isBackgroundVisible() const
395 bool QLegend::isBackgroundVisible() const
396 {
396 {
397 return d_ptr->m_backgroundVisible;
397 return d_ptr->m_backgroundVisible;
398 }
398 }
399
399
400 QList<QLegendMarker*> QLegend::markers() const
400 QList<QLegendMarker*> QLegend::markers() const
401 {
401 {
402 return d_ptr->markers();
402 return d_ptr->markers();
403 }
403 }
404
404
405 void QLegend::addSeries(QAbstractSeries* series)
405 void QLegend::addSeries(QAbstractSeries* series)
406 {
406 {
407 d_ptr->addSeries(series);
407 d_ptr->addSeries(series);
408 }
408 }
409
409
410 void QLegend::removeSeries(QAbstractSeries* series)
410 void QLegend::removeSeries(QAbstractSeries* series)
411 {
411 {
412 d_ptr->removeSeries(series);
412 d_ptr->removeSeries(series);
413 }
413 }
414
414
415 /*!
415 /*!
416 \internal \a event see QGraphicsWidget for details
416 \internal \a event see QGraphicsWidget for details
417 */
417 */
418 void QLegend::hideEvent(QHideEvent *event)
418 void QLegend::hideEvent(QHideEvent *event)
419 {
419 {
420 if (isAttachedToChart())
420 if (isAttachedToChart())
421 d_ptr->m_presenter->layout()->invalidate();
421 d_ptr->m_presenter->layout()->invalidate();
422 QGraphicsWidget::hideEvent(event);
422 QGraphicsWidget::hideEvent(event);
423 }
423 }
424 /*!
424 /*!
425 \internal \a event see QGraphicsWidget for details
425 \internal \a event see QGraphicsWidget for details
426 */
426 */
427 void QLegend::showEvent(QShowEvent *event)
427 void QLegend::showEvent(QShowEvent *event)
428 {
428 {
429 if (isAttachedToChart()) {
429 if (isAttachedToChart()) {
430 d_ptr->items()->setVisible(false);
430 d_ptr->items()->setVisible(false);
431 layout()->invalidate();
431 layout()->invalidate();
432 }
432 }
433 QGraphicsWidget::showEvent(event);
433 QGraphicsWidget::showEvent(event);
434 //layout activation will show the items
434 //layout activation will show the items
435 }
435 }
436
436
437 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
437 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
438
438
439 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
439 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q)
440 : q_ptr(q),
440 : q_ptr(q),
441 m_presenter(presenter),
441 m_presenter(presenter),
442 m_layout(new LegendLayout(q)),
442 m_layout(new LegendLayout(q)),
443 m_chart(chart),
443 m_chart(chart),
444 m_items(new QGraphicsItemGroup(q)),
444 m_items(new QGraphicsItemGroup(q)),
445 m_alignment(Qt::AlignTop),
445 m_alignment(Qt::AlignTop),
446 m_brush(QBrush()),
446 m_brush(QBrush()),
447 m_pen(QPen()),
447 m_pen(QPen()),
448 m_labelBrush(QBrush()),
448 m_labelBrush(QBrush()),
449 m_diameter(5),
449 m_diameter(5),
450 m_attachedToChart(true),
450 m_attachedToChart(true),
451 m_backgroundVisible(false)
451 m_backgroundVisible(false)
452 {
452 {
453 m_items->setHandlesChildEvents(false);
453 m_items->setHandlesChildEvents(false);
454 }
454 }
455
455
456 QLegendPrivate::~QLegendPrivate()
456 QLegendPrivate::~QLegendPrivate()
457 {
457 {
458
458
459 }
459 }
460
460
461 void QLegendPrivate::setOffset(qreal x, qreal y)
461 void QLegendPrivate::setOffset(const QPointF &offset)
462 {
462 {
463 m_layout->setOffset(x, y);
463 m_layout->setOffset(offset.x(), offset.y());
464 }
464 }
465
465
466 QPointF QLegendPrivate::offset() const
466 QPointF QLegendPrivate::offset() const
467 {
467 {
468 return m_layout->offset();
468 return m_layout->offset();
469 }
469 }
470
470
471 int QLegendPrivate::roundness(qreal size)
471 int QLegendPrivate::roundness(qreal size)
472 {
472 {
473 return 100 * m_diameter / int(size);
473 return 100 * m_diameter / int(size);
474 }
474 }
475
475
476 void QLegendPrivate::addSeries(QAbstractSeries* series)
476 void QLegendPrivate::addSeries(QAbstractSeries* series)
477 {
477 {
478 // Only allow one instance of series
478 // Only allow one instance of series
479 if (m_series.contains(series)) {
479 if (m_series.contains(series)) {
480 qDebug() << "series already added" << series;
480 qDebug() << "series already added" << series;
481 return;
481 return;
482 }
482 }
483
483
484 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
484 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
485 decorateMarkers(newMarkers);
485 decorateMarkers(newMarkers);
486 addMarkers(newMarkers);
486 addMarkers(newMarkers);
487
487
488 // TODO: This is the part I don't like. There should be better solution.
488 // TODO: This is the part I don't like. There should be better solution.
489 // On the other hand. It is only one switch case for appending and another for removing series
489 // On the other hand. It is only one switch case for appending and another for removing series
490 // If countChanged signal were on QAbstractSeries, there would be no need for switch at all.
490 // If countChanged signal were on QAbstractSeries, there would be no need for switch at all.
491 switch (series->type())
491 switch (series->type())
492 {
492 {
493 case QAbstractSeries::SeriesTypePie: {
493 case QAbstractSeries::SeriesTypePie: {
494 QPieSeries *s = qobject_cast<QPieSeries *> (series);
494 QPieSeries *s = qobject_cast<QPieSeries *> (series);
495 QObject::connect(s, SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
495 QObject::connect(s, SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
496 break;
496 break;
497 }
497 }
498 case QAbstractSeries::SeriesTypeBar:
498 case QAbstractSeries::SeriesTypeBar:
499 case QAbstractSeries::SeriesTypeStackedBar:
499 case QAbstractSeries::SeriesTypeStackedBar:
500 case QAbstractSeries::SeriesTypePercentBar:
500 case QAbstractSeries::SeriesTypePercentBar:
501 case QAbstractSeries::SeriesTypeHorizontalBar:
501 case QAbstractSeries::SeriesTypeHorizontalBar:
502 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
502 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
503 case QAbstractSeries::SeriesTypeHorizontalPercentBar: {
503 case QAbstractSeries::SeriesTypeHorizontalPercentBar: {
504 QAbstractBarSeries *s = qobject_cast<QAbstractBarSeries *> (series);
504 QAbstractBarSeries *s = qobject_cast<QAbstractBarSeries *> (series);
505 QObject::connect(s, SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
505 QObject::connect(s, SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
506 break;
506 break;
507 }
507 }
508 case QAbstractSeries::SeriesTypeLine:
508 case QAbstractSeries::SeriesTypeLine:
509 case QAbstractSeries::SeriesTypeArea:
509 case QAbstractSeries::SeriesTypeArea:
510 case QAbstractSeries::SeriesTypeScatter:
510 case QAbstractSeries::SeriesTypeScatter:
511 case QAbstractSeries::SeriesTypeSpline:
511 case QAbstractSeries::SeriesTypeSpline:
512 default: {
512 default: {
513 // No need to connect any series related signals. We have no series level
513 // No need to connect any series related signals. We have no series level
514 // changes, that would generate or delete markers
514 // changes, that would generate or delete markers
515 }
515 }
516 }
516 }
517
517
518 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
518 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
519
519
520 m_series.append(series);
520 m_series.append(series);
521 m_items->setVisible(false);
521 m_items->setVisible(false);
522 m_layout->invalidate();
522 m_layout->invalidate();
523 }
523 }
524
524
525 void QLegendPrivate::removeSeries(QAbstractSeries* series)
525 void QLegendPrivate::removeSeries(QAbstractSeries* series)
526 {
526 {
527 if (m_series.contains(series)) {
527 if (m_series.contains(series)) {
528 m_series.removeOne(series);
528 m_series.removeOne(series);
529 }
529 }
530
530
531 // Find out, which markers to remove
531 // Find out, which markers to remove
532 QList<QLegendMarker *> removed;
532 QList<QLegendMarker *> removed;
533 foreach (QLegendMarker *m, m_markers) {
533 foreach (QLegendMarker *m, m_markers) {
534 if (m->series() == series) {
534 if (m->series() == series) {
535 removed << m;
535 removed << m;
536 }
536 }
537 }
537 }
538 removeMarkers(removed);
538 removeMarkers(removed);
539
539
540 switch (series->type())
540 switch (series->type())
541 {
541 {
542 case QAbstractSeries::SeriesTypePie: {
542 case QAbstractSeries::SeriesTypePie: {
543 QPieSeries *s = qobject_cast<QPieSeries *> (series);
543 QPieSeries *s = qobject_cast<QPieSeries *> (series);
544 QObject::disconnect(s, SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
544 QObject::disconnect(s, SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
545 break;
545 break;
546 }
546 }
547 case QAbstractSeries::SeriesTypeBar:
547 case QAbstractSeries::SeriesTypeBar:
548 case QAbstractSeries::SeriesTypeStackedBar:
548 case QAbstractSeries::SeriesTypeStackedBar:
549 case QAbstractSeries::SeriesTypePercentBar:
549 case QAbstractSeries::SeriesTypePercentBar:
550 case QAbstractSeries::SeriesTypeHorizontalBar:
550 case QAbstractSeries::SeriesTypeHorizontalBar:
551 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
551 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
552 case QAbstractSeries::SeriesTypeHorizontalPercentBar: {
552 case QAbstractSeries::SeriesTypeHorizontalPercentBar: {
553 QAbstractBarSeries *s = qobject_cast<QAbstractBarSeries *> (series);
553 QAbstractBarSeries *s = qobject_cast<QAbstractBarSeries *> (series);
554 QObject::disconnect(s, SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
554 QObject::disconnect(s, SIGNAL(countChanged()), this, SLOT(handleCountChanged()));
555 break;
555 break;
556 }
556 }
557 // TODO:
557 // TODO:
558 case QAbstractSeries::SeriesTypeLine:
558 case QAbstractSeries::SeriesTypeLine:
559 case QAbstractSeries::SeriesTypeArea:
559 case QAbstractSeries::SeriesTypeArea:
560 case QAbstractSeries::SeriesTypeScatter:
560 case QAbstractSeries::SeriesTypeScatter:
561 case QAbstractSeries::SeriesTypeSpline:
561 case QAbstractSeries::SeriesTypeSpline:
562 default: {
562 default: {
563 // No need to disconnect any series related signals
563 // No need to disconnect any series related signals
564 break;
564 break;
565 }
565 }
566 }
566 }
567
567
568 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
568 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
569
569
570 m_layout->invalidate();
570 m_layout->invalidate();
571 // q_ptr->layout()->activate();
571 // q_ptr->layout()->activate();
572 }
572 }
573
573
574 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series)
574 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series)
575 {
575 {
576 // Moved to appendSeries
576 // Moved to appendSeries
577 // This slot is just to make old code work for now.
577 // This slot is just to make old code work for now.
578 addSeries(series);
578 addSeries(series);
579 }
579 }
580
580
581 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
581 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
582 {
582 {
583 // Moved to removeSeries
583 // Moved to removeSeries
584 // This slot is just to make old code work for now.
584 // This slot is just to make old code work for now.
585 removeSeries(series);
585 removeSeries(series);
586 }
586 }
587
587
588 void QLegendPrivate::handleSeriesVisibleChanged()
588 void QLegendPrivate::handleSeriesVisibleChanged()
589 {
589 {
590 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
590 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
591 Q_ASSERT(series);
591 Q_ASSERT(series);
592
592
593 foreach (QLegendMarker* marker, m_markers) {
593 foreach (QLegendMarker* marker, m_markers) {
594 if (marker->series() == series) {
594 if (marker->series() == series) {
595 marker->setVisible(series->isVisible());
595 marker->setVisible(series->isVisible());
596 }
596 }
597 }
597 }
598 m_layout->invalidate();
598 m_layout->invalidate();
599 }
599 }
600
600
601 void QLegendPrivate::handleCountChanged()
601 void QLegendPrivate::handleCountChanged()
602 {
602 {
603 // Here we handle the changes in marker count.
603 // Here we handle the changes in marker count.
604 // Can happen for example when pieslice(s) have been added to or removed from pieseries.
604 // Can happen for example when pieslice(s) have been added to or removed from pieseries.
605
605
606 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
606 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
607 QList<QLegendMarker *> createdMarkers = series->d_ptr->createLegendMarkers(q_ptr);
607 QList<QLegendMarker *> createdMarkers = series->d_ptr->createLegendMarkers(q_ptr);
608
608
609 // Find out removed markers and created markers
609 // Find out removed markers and created markers
610 QList<QLegendMarker *> removedMarkers;
610 QList<QLegendMarker *> removedMarkers;
611 foreach (QLegendMarker *oldMarker, m_markers) {
611 foreach (QLegendMarker *oldMarker, m_markers) {
612 // we have marker, which is related to sender.
612 // we have marker, which is related to sender.
613 if (oldMarker->series() == series) {
613 if (oldMarker->series() == series) {
614 bool found = false;
614 bool found = false;
615 foreach(QLegendMarker *newMarker, createdMarkers) {
615 foreach(QLegendMarker *newMarker, createdMarkers) {
616 // New marker considered existing if:
616 // New marker considered existing if:
617 // - d_ptr->relatedObject() is same for both markers.
617 // - d_ptr->relatedObject() is same for both markers.
618 if (newMarker->d_ptr->relatedObject() == oldMarker->d_ptr->relatedObject()) {
618 if (newMarker->d_ptr->relatedObject() == oldMarker->d_ptr->relatedObject()) {
619 // Delete the new marker, since we already have existing marker, that might be connected on user side.
619 // Delete the new marker, since we already have existing marker, that might be connected on user side.
620 found = true;
620 found = true;
621 createdMarkers.removeOne(newMarker);
621 createdMarkers.removeOne(newMarker);
622 delete newMarker;
622 delete newMarker;
623 }
623 }
624 }
624 }
625 if (!found) {
625 if (!found) {
626 // No related object found for marker, add to removedMarkers list
626 // No related object found for marker, add to removedMarkers list
627 removedMarkers << oldMarker;
627 removedMarkers << oldMarker;
628 }
628 }
629 }
629 }
630 }
630 }
631
631
632 removeMarkers(removedMarkers);
632 removeMarkers(removedMarkers);
633 decorateMarkers(createdMarkers);
633 decorateMarkers(createdMarkers);
634 addMarkers(createdMarkers);
634 addMarkers(createdMarkers);
635
635
636 q_ptr->layout()->invalidate();
636 q_ptr->layout()->invalidate();
637 }
637 }
638
638
639 void QLegendPrivate::addMarkers(QList<QLegendMarker *> markers)
639 void QLegendPrivate::addMarkers(QList<QLegendMarker *> markers)
640 {
640 {
641 foreach (QLegendMarker* marker, markers) {
641 foreach (QLegendMarker* marker, markers) {
642 m_items->addToGroup(marker->d_ptr.data()->item());
642 m_items->addToGroup(marker->d_ptr.data()->item());
643 m_markers << marker;
643 m_markers << marker;
644 }
644 }
645 }
645 }
646
646
647 void QLegendPrivate::removeMarkers(QList<QLegendMarker *> markers)
647 void QLegendPrivate::removeMarkers(QList<QLegendMarker *> markers)
648 {
648 {
649 foreach (QLegendMarker *marker, markers) {
649 foreach (QLegendMarker *marker, markers) {
650 marker->d_ptr->item()->setVisible(false);
650 marker->d_ptr->item()->setVisible(false);
651 m_items->removeFromGroup(marker->d_ptr->item());
651 m_items->removeFromGroup(marker->d_ptr->item());
652 delete marker;
652 delete marker;
653 m_markers.removeOne(marker);
653 m_markers.removeOne(marker);
654 }
654 }
655 }
655 }
656
656
657 void QLegendPrivate::decorateMarkers(QList<QLegendMarker *> markers)
657 void QLegendPrivate::decorateMarkers(QList<QLegendMarker *> markers)
658 {
658 {
659 foreach (QLegendMarker* marker, markers) {
659 foreach (QLegendMarker* marker, markers) {
660 marker->setFont(m_font);
660 marker->setFont(m_font);
661 marker->setLabelBrush(m_labelBrush);
661 marker->setLabelBrush(m_labelBrush);
662 }
662 }
663 }
663 }
664
664
665
665
666 #include "moc_qlegend.cpp"
666 #include "moc_qlegend.cpp"
667 #include "moc_qlegend_p.cpp"
667 #include "moc_qlegend_p.cpp"
668
668
669 QTCOMMERCIALCHART_END_NAMESPACE
669 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,101 +1,102
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QLEGEND_P_H
30 #ifndef QLEGEND_P_H
31 #define QLEGEND_P_H
31 #define QLEGEND_P_H
32
32
33 #include "qlegend.h"
33 #include "qlegend.h"
34 #include "scroller_p.h"
34
35
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
37
37 class QChart;
38 class QChart;
38 class ChartPresenter;
39 class ChartPresenter;
39 class QAbstractSeries;
40 class QAbstractSeries;
40 class LegendLayout;
41 class LegendLayout;
41 class Domain;
42 class Domain;
42 class QLegendMarker;
43 class QLegendMarker;
43
44
44 class QLegendPrivate : public QObject
45 class QLegendPrivate : public QObject, public Scroller
45 {
46 {
46 Q_OBJECT
47 Q_OBJECT
47 public:
48 public:
48 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
49 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
49 ~QLegendPrivate();
50 ~QLegendPrivate();
50
51
51 void setOffset(qreal x, qreal y);
52 void setOffset(const QPointF &offset);
52 QPointF offset() const;
53 QPointF offset() const;
53 int roundness(qreal size);
54 int roundness(qreal size);
54
55
55 QGraphicsItemGroup* items() { return m_items; }
56 QGraphicsItemGroup* items() { return m_items; }
56
57
57 // New stuff:
58 // New stuff:
58 QList<QLegendMarker*> markers() { return m_markers; }
59 QList<QLegendMarker*> markers() { return m_markers; }
59 void addSeries(QAbstractSeries* series);
60 void addSeries(QAbstractSeries* series);
60 void removeSeries(QAbstractSeries* series);
61 void removeSeries(QAbstractSeries* series);
61
62
62 public Q_SLOTS:
63 public Q_SLOTS:
63 void handleSeriesAdded(QAbstractSeries *series);
64 void handleSeriesAdded(QAbstractSeries *series);
64 void handleSeriesRemoved(QAbstractSeries *series);
65 void handleSeriesRemoved(QAbstractSeries *series);
65 void handleSeriesVisibleChanged();
66 void handleSeriesVisibleChanged();
66 void handleCountChanged();
67 void handleCountChanged();
67
68
68 private:
69 private:
69 // Internal helpers
70 // Internal helpers
70 void addMarkers(QList<QLegendMarker *> markers);
71 void addMarkers(QList<QLegendMarker *> markers);
71 void removeMarkers(QList<QLegendMarker *> markers);
72 void removeMarkers(QList<QLegendMarker *> markers);
72 void decorateMarkers(QList<QLegendMarker *> markers);
73 void decorateMarkers(QList<QLegendMarker *> markers);
73
74
74 private:
75 private:
75 QLegend *q_ptr;
76 QLegend *q_ptr;
76 ChartPresenter *m_presenter;
77 ChartPresenter *m_presenter;
77 LegendLayout *m_layout;
78 LegendLayout *m_layout;
78 QChart* m_chart;
79 QChart* m_chart;
79 QGraphicsItemGroup* m_items;
80 QGraphicsItemGroup* m_items;
80 Qt::Alignment m_alignment;
81 Qt::Alignment m_alignment;
81 QBrush m_brush;
82 QBrush m_brush;
82 QPen m_pen;
83 QPen m_pen;
83 QFont m_font;
84 QFont m_font;
84 QBrush m_labelBrush;
85 QBrush m_labelBrush;
85
86
86 qreal m_diameter;
87 qreal m_diameter;
87 bool m_attachedToChart;
88 bool m_attachedToChart;
88 bool m_backgroundVisible;
89 bool m_backgroundVisible;
89
90
90 friend class QLegend;
91 friend class QLegend;
91 friend class LegendLayout;
92 friend class LegendLayout;
92 QList<QLegendMarker*> m_markers;
93 QList<QLegendMarker*> m_markers;
93 QList<QAbstractSeries*> m_series;
94 QList<QAbstractSeries*> m_series;
94
95
95 friend class QLegend;
96 friend class QLegend;
96 friend class LegendMarkerItem;
97 friend class LegendMarkerItem;
97 };
98 };
98
99
99 QTCOMMERCIALCHART_END_NAMESPACE
100 QTCOMMERCIALCHART_END_NAMESPACE
100
101
101 #endif
102 #endif
@@ -1,247 +1,159
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "scroller_p.h"
21 #include "scroller_p.h"
22 #include "qlegend.h"
22 #include "qlegend.h"
23 #include <QGraphicsSceneMouseEvent>
23 #include <QGraphicsSceneMouseEvent>
24 #include <QDebug>
24 #include <QDebug>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 Scroller::Scroller()
28 Scroller::Scroller()
29 : m_ticker(this),
29 : m_ticker(this),
30 m_state(Idle),
30 m_timeTresholdMin(50),
31 m_moveThreshold(10),
31 m_timeTresholdMax(300),
32 m_timeTreshold(50)
32 m_state(Idle)
33 {
33 {
34
34
35 }
35 }
36
36
37 Scroller::~Scroller()
37 Scroller::~Scroller()
38 {
38 {
39 }
39 }
40
40
41 void Scroller::mousePressEvent(QGraphicsSceneMouseEvent *event)
41 void Scroller::move(const QPointF &delta)
42 {
42 {
43 if (event->button() == Qt::LeftButton) {
44 switch (m_state) {
43 switch (m_state) {
45 case Idle:
44 case Idle:
46 m_state = Pressed;
47 m_offset = offset();
48 m_press = event->pos();
49 m_timeStamp = QTime::currentTime();
45 m_timeStamp = QTime::currentTime();
50 event->accept();
46 m_state = Move;
51 break;
47 break;
52 case Scroll:
48 case Scroll:
53 m_state = Stop;
49 stopTicker();
54 m_speed = QPointF(0, 0);
50 m_timeStamp = QTime::currentTime();
55 m_offset = offset();
51 m_state = Move;
56 m_press = event->pos();
57 event->accept();
58 break;
52 break;
59 case Pressed:
53 default:
60 case Move:
61 case Stop:
62 qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state;
63 event->ignore();
64 break;
54 break;
65 }
55 }
66 }
56 setOffset(offset() - delta);
67 }
57 }
68
58
69 void Scroller::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
59 void Scroller::release(const QPointF &delta)
70 {
60 {
71 QPointF delta = event->pos() - m_press;
61 // Starts scrolling, if at least m_timeTresholdMin msecs has gone since timestamp
62 // current time is no more than m_timeTresholdMax from timestamp
72
63
73 switch (m_state) {
64 if ((m_timeStamp.elapsed() > m_timeTresholdMin) && (m_timeStamp.msecsTo(QTime::currentTime()) < m_timeTresholdMax)) {
74 case Pressed:
65 // Release was quick enough. Start scrolling.
75 case Stop:
66 // Magic number is to make scroll bit slower (the resolution of screen may affect this)
76 if (qAbs(delta.x()) > m_moveThreshold || qAbs(delta.y()) > m_moveThreshold) {
67 m_speed = delta / 5;
77 m_state = Move;
68
78 m_timeStamp = QTime::currentTime();
69 qreal fraction = qMax(qAbs(m_speed.x()), qAbs(m_speed.y()));
79 m_distance = QPointF(0, 0);
70
80 m_press = event->pos();
71 if (!qFuzzyIsNull(fraction)) {
81 event->accept();
72 m_fraction.setX(qAbs(m_speed.x() / fraction));
73 m_fraction.setY(qAbs(m_speed.y() / fraction));
82 } else {
74 } else {
83 event->ignore();
75 m_fraction.setX(1);
76 m_fraction.setY(1);
84 }
77 }
85 break;
78 startTicker(25);
86 case Move:
79 m_state = Scroll;
87 setOffset(m_offset - delta);
80 } else {
88 calculateSpeed(event->pos());
81 stopTicker(); // Stop ticker, if one is running.
89 event->accept();
82 m_state = Idle;
90 break;
91 case Idle:
92 case Scroll:
93 qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state;
94 event->ignore();
95 break;
96 }
83 }
97 }
84 }
98
85
99 void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
86 void Scroller::startTicker(int interval)
100 {
87 {
101 if (event->button() == Qt::LeftButton) {
102
103 switch (m_state) {
104
105 case Scroll:
106 m_state = Stop;
107 m_speed = QPointF(0, 0);
108 m_offset = offset();
109 event->accept();
110 break;
111 case Pressed:
112 m_state = Idle;
113 //if (m_timeStamp.elapsed() < m_clickedPressDelay) {
114 //emit clicked(m_offset.toPoint());
115 //}
116 event->accept();
117 break;
118 case Move:
119 calculateSpeed(event->pos());
120 m_offset = offset();
121 m_press = event->pos();
122 if (m_speed == QPointF(0, 0)) {
123 m_state = Idle;
124 } else {
125 m_speed /= 3.75;
126 m_state = Scroll;
88 m_state = Scroll;
127 m_ticker.start(25);
89 m_ticker.start(interval);
128 }
129 event->accept();
130 break;
131 case Stop:
132 case Idle:
133 qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state;
134 event->ignore();
135 break;
136 }
137 }
138 }
90 }
139
91
140 void Scroller::scroll(const QPointF &velocity)
92 void Scroller::stopTicker()
141 {
93 {
142 Q_UNUSED(velocity);
143 // TODO:
144 /*
145 m_offset = offset();
146 m_speed = velocity;
147 if (m_speed == QPointF(0, 0)) {
148 m_state = Idle;
94 m_state = Idle;
149 }
95 m_ticker.stop();
150 else {
151 m_speed /= 3.75;
152 m_state = Scroll;
153 m_ticker.start(25);
154 }
155 */
156 }
96 }
157
97
158
159 void Scroller::scrollTick()
98 void Scroller::scrollTick()
160 {
99 {
161 switch (m_state) {
100 switch (m_state) {
162 case Scroll:
101 case Scroll:
163 lowerSpeed(m_speed);
102 lowerSpeed(m_speed);
164 setOffset(m_offset - m_speed);
103 setOffset(offset() - m_speed);
165 m_offset = offset();
166 if (m_speed == QPointF(0, 0)) {
104 if (m_speed == QPointF(0, 0)) {
167 m_state = Idle;
105 m_state = Idle;
168 m_ticker.stop();
106 m_ticker.stop();
169 }
107 }
170 break;
108 break;
171 case Stop:
172 m_ticker.stop();
173 break;
174 case Idle:
109 case Idle:
175 case Move:
110 case Move:
176 case Pressed:
177 qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state;
111 qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state;
178 m_ticker.stop();
112 m_ticker.stop();
113 m_state = Idle;
179 break;
114 break;
180
181 }
115 }
182 }
116 }
183
117
184 void Scroller::lowerSpeed(QPointF &speed, qreal maxSpeed)
118 void Scroller::lowerSpeed(QPointF &speed, qreal maxSpeed)
185 {
119 {
186 qreal x = qBound(-maxSpeed, speed.x(), maxSpeed);
120 qreal x = qBound(-maxSpeed, speed.x(), maxSpeed);
187 qreal y = qBound(-maxSpeed, speed.y(), maxSpeed);
121 qreal y = qBound(-maxSpeed, speed.y(), maxSpeed);
188
122
189 x = (x == 0) ? x :
123 x = (x == 0) ? x :
190 (x > 0) ? qMax(qreal(0), x - m_fraction.x()) : qMin(qreal(0), x + m_fraction.x());
124 (x > 0) ? qMax(qreal(0), x - m_fraction.x()) : qMin(qreal(0), x + m_fraction.x());
191 y = (y == 0) ? y :
125 y = (y == 0) ? y :
192 (y > 0) ? qMax(qreal(0), y - m_fraction.y()) : qMin(qreal(0), y + m_fraction.y());
126 (y > 0) ? qMax(qreal(0), y - m_fraction.y()) : qMin(qreal(0), y + m_fraction.y());
193 speed.setX(x);
127 speed.setX(x);
194 speed.setY(y);
128 speed.setY(y);
195 }
129 }
196
130
197 void Scroller::calculateSpeed(const QPointF &position)
198 {
199 if (m_timeStamp.elapsed() > m_timeTreshold) {
200
201 QPointF distance = position - m_press;
202
203 m_timeStamp = QTime::currentTime();
204 m_speed = distance - m_distance;
205 m_distance = distance;
206
207 qreal fraction = qMax(qAbs(m_speed.x()), qAbs(m_speed.y()));
208
209 if (fraction != 0) {
210 m_fraction.setX(qAbs(m_speed.x() / fraction));
211 m_fraction.setY(qAbs(m_speed.y() / fraction));
212 } else {
213 m_fraction.setX(1);
214 m_fraction.setY(1);
215 }
216 }
217 }
218
219 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
131 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
220
132
221 ScrollTicker::ScrollTicker(Scroller *scroller, QObject *parent)
133 ScrollTicker::ScrollTicker(Scroller *scroller, QObject *parent)
222 : QObject(parent),
134 : QObject(parent),
223 m_scroller(scroller)
135 m_scroller(scroller)
224 {
136 {
225
137
226 }
138 }
227
139
228 void ScrollTicker::start(int interval)
140 void ScrollTicker::start(int interval)
229 {
141 {
230 if (!m_timer.isActive())
142 if (!m_timer.isActive())
231 m_timer.start(interval, this);
143 m_timer.start(interval, this);
232 }
144 }
233
145
234 void ScrollTicker::stop()
146 void ScrollTicker::stop()
235 {
147 {
236 m_timer.stop();
148 m_timer.stop();
237 }
149 }
238
150
239 void ScrollTicker::timerEvent(QTimerEvent *event)
151 void ScrollTicker::timerEvent(QTimerEvent *event)
240 {
152 {
241 Q_UNUSED(event);
153 Q_UNUSED(event);
242 m_scroller->scrollTick();
154 m_scroller->scrollTick();
243 }
155 }
244
156
245 #include "moc_scroller_p.cpp"
157 #include "moc_scroller_p.cpp"
246
158
247 QTCOMMERCIALCHART_END_NAMESPACE
159 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,106 +1,102
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef SCROLLER_P_H
30 #ifndef SCROLLER_P_H
31 #define SCROLLER_P_H
31 #define SCROLLER_P_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include <QBasicTimer>
34 #include <QBasicTimer>
35 #include <QTime>
35 #include <QTime>
36 #include <QPointF>
36 #include <QPointF>
37
37
38 class QGraphicsSceneMouseEvent;
38 class QGraphicsSceneMouseEvent;
39
39
40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41
41
42 class Scroller;
42 class Scroller;
43 class QLegend;
43 class QLegend;
44
44
45 class ScrollTicker : public QObject
45 class ScrollTicker : public QObject
46 {
46 {
47 Q_OBJECT
47 Q_OBJECT
48 public:
48 public:
49 explicit ScrollTicker(Scroller *scroller, QObject *parent = 0);
49 explicit ScrollTicker(Scroller *scroller, QObject *parent = 0);
50 void start(int interval);
50 void start(int interval);
51 void stop();
51 void stop();
52 protected:
52 protected:
53 void timerEvent(QTimerEvent *event);
53 void timerEvent(QTimerEvent *event);
54
54
55 private:
55 private:
56 QBasicTimer m_timer;
56 QBasicTimer m_timer;
57 Scroller *m_scroller;
57 Scroller *m_scroller;
58 };
58 };
59
59
60 class Scroller
60 class Scroller
61 {
61 {
62 public:
62 public:
63 enum State {
63 enum State {
64 Idle,
64 Idle,
65 Pressed,
66 Move,
65 Move,
67 Scroll,
66 Scroll
68 Stop
69 };
67 };
70
68
71 Scroller();
69 Scroller();
72 virtual ~Scroller();
70 virtual ~Scroller();
73
71
74 virtual void setOffset(const QPointF &point) = 0;
72 virtual void setOffset(const QPointF &point) = 0;
75 virtual QPointF offset() const = 0;
73 virtual QPointF offset() const = 0;
76
74
77 void scroll(const QPointF& velocity);
75 void move(const QPointF &delta);
76 void release(const QPointF &delta);
78
77
79 public:
80 void scrollTick();
78 void scrollTick();
81
79
82 public:
80 private:
83 void mousePressEvent(QGraphicsSceneMouseEvent *event);
81 void startTicker(int interval);
84 void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
82 void stopTicker();
85 void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
83
86
84
87 private:
85 private:
88 void calculateSpeed(const QPointF &position);
86 void calculateSpeed(const QPointF &position);
89 void lowerSpeed(QPointF &speed, qreal maxSpeed = 100);
87 void lowerSpeed(QPointF &speed, qreal maxSpeed = 100);
90
88
91 private:
89 private:
92 ScrollTicker m_ticker;
90 ScrollTicker m_ticker;
93 State m_state;
94 QTime m_timeStamp;
91 QTime m_timeStamp;
95 QPointF m_press;
96 QPointF m_offset;
97 QPointF m_speed;
92 QPointF m_speed;
98 QPointF m_distance;
99 QPointF m_fraction;
93 QPointF m_fraction;
100 int m_moveThreshold;
94 int m_timeTresholdMin;
101 int m_timeTreshold;
95 int m_timeTresholdMax;
96
97 State m_state;
102 };
98 };
103
99
104 QTCOMMERCIALCHART_END_NAMESPACE
100 QTCOMMERCIALCHART_END_NAMESPACE
105
101
106 #endif /* SCROLLER_P_H */
102 #endif /* SCROLLER_P_H */
General Comments 0
You need to be logged in to leave comments. Login now