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