@@ -28,10 +28,7 | |||||
28 |
|
28 | |||
29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
30 |
|
30 | |||
31 |
LegendScroller::LegendScroller(QChart *chart) : QLegend(chart) |
|
31 | LegendScroller::LegendScroller(QChart *chart) : QLegend(chart) | |
32 | m_lastPos(0, 0), |
|
|||
33 | m_state(Idle), |
|
|||
34 | m_treshold(10) |
|
|||
35 | { |
|
32 | { | |
36 | } |
|
33 | } | |
37 |
|
34 | |||
@@ -45,56 +42,21 QPointF LegendScroller::offset() const | |||||
45 | return d_ptr->offset(); |
|
42 | return d_ptr->offset(); | |
46 | } |
|
43 | } | |
47 |
|
44 | |||
48 | void LegendScroller::setMoveTreshold(qreal treshold) |
|
|||
49 | { |
|
|||
50 | m_treshold = treshold; |
|
|||
51 | } |
|
|||
52 |
|
||||
53 | void LegendScroller::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
45 | void LegendScroller::mousePressEvent(QGraphicsSceneMouseEvent *event) | |
54 | { |
|
46 | { | |
55 | m_pressPos = event->screenPos(); |
|
47 | Scroller::handleMousePressEvent(event); | |
56 | m_lastPos = m_pressPos; |
|
|||
57 | m_state = Pressed; |
|
|||
58 | event->accept(); |
|
|||
59 | } |
|
48 | } | |
60 |
|
49 | |||
61 | void LegendScroller::mouseMoveEvent(QGraphicsSceneMouseEvent *event) |
|
50 | void LegendScroller::mouseMoveEvent(QGraphicsSceneMouseEvent *event) | |
62 | { |
|
51 | { | |
63 | QPointF delta = event->screenPos() - m_lastPos; |
|
52 | Scroller::handleMouseMoveEvent(event); | |
64 |
|
||||
65 | switch (m_state) { |
|
|||
66 | case Pressed: { |
|
|||
67 | // calculate treshold. If enough, change to move state and send out move deltas. |
|
|||
68 | if (qAbs(delta.x()) > m_treshold || qAbs(delta.y()) > m_treshold) { |
|
|||
69 | m_state = Moved; |
|
|||
70 | m_lastPos = event->screenPos(); |
|
|||
71 | move(delta); |
|
|||
72 | } |
|
|||
73 | event->accept(); |
|
|||
74 | break; |
|
|||
75 | } |
|
|||
76 | case Moved: { |
|
|||
77 | m_lastPos = event->screenPos(); |
|
|||
78 | move(delta); |
|
|||
79 | event->accept(); |
|
|||
80 | break; |
|
|||
81 | } |
|
|||
82 | case Idle: |
|
|||
83 | default: { |
|
|||
84 | event->ignore(); |
|
|||
85 | break; |
|
|||
86 | } |
|
|||
87 | } |
|
|||
88 | } |
|
53 | } | |
89 |
|
54 | |||
90 | void LegendScroller::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
|
55 | void LegendScroller::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) | |
91 | { |
|
56 | { | |
92 | m_lastPos = event->screenPos(); |
|
57 | Scroller::handleMouseReleaseEvent(event); | |
93 |
|
58 | |||
94 | switch (m_state) { |
|
59 | if (!event->isAccepted()) { | |
95 | case Pressed: |
|
|||
96 | { |
|
|||
97 | m_state = Idle; |
|
|||
98 | QList<QGraphicsItem *> items = scene()->items(event->scenePos()); |
|
60 | QList<QGraphicsItem *> items = scene()->items(event->scenePos()); | |
99 |
|
61 | |||
100 | foreach (QGraphicsItem *i, items) { |
|
62 | foreach (QGraphicsItem *i, items) { | |
@@ -103,22 +65,7 void LegendScroller::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) | |||||
103 | emit marker->clicked(); |
|
65 | emit marker->clicked(); | |
104 | } |
|
66 | } | |
105 | } |
|
67 | } | |
106 |
|
68 | event->accept(); | ||
107 | event->accept(); |
|
|||
108 | break; |
|
|||
109 | } |
|
|||
110 | case Moved: |
|
|||
111 | { |
|
|||
112 | scrollTo(m_lastPos - m_pressPos); |
|
|||
113 | event->accept(); |
|
|||
114 | break; |
|
|||
115 | } |
|
|||
116 | default: |
|
|||
117 | { |
|
|||
118 | m_state = Idle; |
|
|||
119 | event->ignore(); |
|
|||
120 | break; |
|
|||
121 | } |
|
|||
122 | } |
|
69 | } | |
123 | } |
|
70 | } | |
124 |
|
71 |
@@ -47,25 +47,9 public: | |||||
47 | void setOffset(const QPointF &point); |
|
47 | void setOffset(const QPointF &point); | |
48 | QPointF offset() const; |
|
48 | QPointF offset() const; | |
49 |
|
49 | |||
50 | void setMoveTreshold(qreal treshold); |
|
|||
51 |
|
||||
52 | void mousePressEvent(QGraphicsSceneMouseEvent *event); |
|
50 | void mousePressEvent(QGraphicsSceneMouseEvent *event); | |
53 | void mouseMoveEvent(QGraphicsSceneMouseEvent *event); |
|
51 | void mouseMoveEvent(QGraphicsSceneMouseEvent *event); | |
54 | void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); |
|
52 | 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; |
|
|||
69 | }; |
|
53 | }; | |
70 |
|
54 | |||
71 | QTCOMMERCIALCHART_END_NAMESPACE |
|
55 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -29,7 +29,8 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 | m_treshold(10) | |||
33 | { |
|
34 | { | |
34 |
|
35 | |||
35 | } |
|
36 | } | |
@@ -41,14 +42,12 Scroller::~Scroller() | |||||
41 | void Scroller::move(const QPointF &delta) |
|
42 | void Scroller::move(const QPointF &delta) | |
42 | { |
|
43 | { | |
43 | switch (m_state) { |
|
44 | switch (m_state) { | |
44 |
case |
|
45 | case Pressed: | |
45 | m_timeStamp = QTime::currentTime(); |
|
46 | m_timeStamp = QTime::currentTime(); | |
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; |
|
|||
52 | break; |
|
51 | break; | |
53 | default: |
|
52 | default: | |
54 | break; |
|
53 | break; | |
@@ -86,6 +85,62 void Scroller::scrollTo(const QPointF &delta) | |||||
86 | } |
|
85 | } | |
87 | } |
|
86 | } | |
88 |
|
87 | |||
|
88 | void Scroller::handleMousePressEvent(QGraphicsSceneMouseEvent *event) | |||
|
89 | { | |||
|
90 | stopTicker(); | |||
|
91 | m_pressPos = event->screenPos(); | |||
|
92 | m_lastPos = m_pressPos; | |||
|
93 | m_state = Pressed; | |||
|
94 | event->accept(); | |||
|
95 | } | |||
|
96 | ||||
|
97 | void Scroller::handleMouseMoveEvent(QGraphicsSceneMouseEvent *event) | |||
|
98 | { | |||
|
99 | QPointF delta = event->screenPos() - m_lastPos; | |||
|
100 | ||||
|
101 | switch (m_state) { | |||
|
102 | case Pressed: { | |||
|
103 | // calculate treshold. If enough, change to move state and send out move deltas. | |||
|
104 | if (qAbs(delta.x()) > m_treshold || qAbs(delta.y()) > m_treshold) { | |||
|
105 | m_lastPos = event->screenPos(); | |||
|
106 | move(delta); | |||
|
107 | m_state = Move; | |||
|
108 | } | |||
|
109 | event->accept(); | |||
|
110 | break; | |||
|
111 | } | |||
|
112 | case Move: { | |||
|
113 | m_lastPos = event->screenPos(); | |||
|
114 | move(delta); | |||
|
115 | event->accept(); | |||
|
116 | break; | |||
|
117 | } | |||
|
118 | case Idle: | |||
|
119 | default: { | |||
|
120 | event->ignore(); | |||
|
121 | break; | |||
|
122 | } | |||
|
123 | } | |||
|
124 | } | |||
|
125 | ||||
|
126 | void Scroller::handleMouseReleaseEvent(QGraphicsSceneMouseEvent *event) | |||
|
127 | { | |||
|
128 | switch (m_state) { | |||
|
129 | case Move: | |||
|
130 | { | |||
|
131 | scrollTo(m_lastPos - m_pressPos); | |||
|
132 | event->accept(); | |||
|
133 | break; | |||
|
134 | } | |||
|
135 | default: | |||
|
136 | { | |||
|
137 | m_state = Idle; | |||
|
138 | event->ignore(); | |||
|
139 | break; | |||
|
140 | } | |||
|
141 | } | |||
|
142 | } | |||
|
143 | ||||
89 | void Scroller::startTicker(int interval) |
|
144 | void Scroller::startTicker(int interval) | |
90 | { |
|
145 | { | |
91 | m_state = Scroll; |
|
146 | m_state = Scroll; | |
@@ -109,8 +164,7 void Scroller::scrollTick() | |||||
109 | m_ticker.stop(); |
|
164 | m_ticker.stop(); | |
110 | } |
|
165 | } | |
111 | break; |
|
166 | break; | |
112 | case Idle: |
|
167 | default: | |
113 | case Move: |
|
|||
114 | qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state; |
|
168 | qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state; | |
115 | m_ticker.stop(); |
|
169 | m_ticker.stop(); | |
116 | m_state = Idle; |
|
170 | m_state = Idle; |
@@ -62,6 +62,7 class Scroller | |||||
62 | public: |
|
62 | public: | |
63 | enum State { |
|
63 | enum State { | |
64 | Idle, |
|
64 | Idle, | |
|
65 | Pressed, | |||
65 | Move, |
|
66 | Move, | |
66 | Scroll |
|
67 | Scroll | |
67 | }; |
|
68 | }; | |
@@ -75,6 +76,10 public: | |||||
75 | void move(const QPointF &delta); |
|
76 | void move(const QPointF &delta); | |
76 | void scrollTo(const QPointF &delta); |
|
77 | void scrollTo(const QPointF &delta); | |
77 |
|
78 | |||
|
79 | void handleMousePressEvent(QGraphicsSceneMouseEvent *event); | |||
|
80 | void handleMouseMoveEvent(QGraphicsSceneMouseEvent *event); | |||
|
81 | void handleMouseReleaseEvent(QGraphicsSceneMouseEvent *event); | |||
|
82 | ||||
78 | void scrollTick(); |
|
83 | void scrollTick(); | |
79 |
|
84 | |||
80 | private: |
|
85 | private: | |
@@ -94,6 +99,9 private: | |||||
94 | int m_timeTresholdMax; |
|
99 | int m_timeTresholdMax; | |
95 |
|
100 | |||
96 | State m_state; |
|
101 | State m_state; | |
|
102 | QPointF m_pressPos; | |||
|
103 | QPointF m_lastPos; | |||
|
104 | qreal m_treshold; | |||
97 | }; |
|
105 | }; | |
98 |
|
106 | |||
99 | QTCOMMERCIALCHART_END_NAMESPACE |
|
107 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now