author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 20:15:53 +0300 | |
branch | RCL_3 |
changeset 13 | c0432d11811c |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the demonstration applications of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#ifdef QT_OPENGL_SUPPORT |
|
43 |
#include <QGLWidget> |
|
44 |
#endif |
|
45 |
||
46 |
#include "arthurwidgets.h" |
|
47 |
#include "hoverpoints.h" |
|
48 |
||
49 |
#define printf |
|
50 |
||
51 |
HoverPoints::HoverPoints(QWidget *widget, PointShape shape) |
|
52 |
: QObject(widget) |
|
53 |
{ |
|
54 |
m_widget = widget; |
|
55 |
widget->installEventFilter(this); |
|
56 |
widget->setAttribute(Qt::WA_AcceptTouchEvents); |
|
57 |
||
58 |
m_connectionType = CurveConnection; |
|
59 |
m_sortType = NoSort; |
|
60 |
m_shape = shape; |
|
61 |
m_pointPen = QPen(QColor(255, 255, 255, 191), 1); |
|
62 |
m_connectionPen = QPen(QColor(255, 255, 255, 127), 2); |
|
63 |
m_pointBrush = QBrush(QColor(191, 191, 191, 127)); |
|
64 |
m_pointSize = QSize(11, 11); |
|
65 |
m_currentIndex = -1; |
|
66 |
m_editable = true; |
|
67 |
m_enabled = true; |
|
68 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
69 |
connect(this, SIGNAL(pointsChanged(QPolygonF)), |
0 | 70 |
m_widget, SLOT(update())); |
71 |
} |
|
72 |
||
73 |
||
74 |
void HoverPoints::setEnabled(bool enabled) |
|
75 |
{ |
|
76 |
if (m_enabled != enabled) { |
|
77 |
m_enabled = enabled; |
|
78 |
m_widget->update(); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
||
83 |
bool HoverPoints::eventFilter(QObject *object, QEvent *event) |
|
84 |
{ |
|
85 |
if (object == m_widget && m_enabled) { |
|
86 |
switch (event->type()) { |
|
87 |
||
88 |
case QEvent::MouseButtonPress: |
|
89 |
{ |
|
90 |
if (!m_fingerPointMapping.isEmpty()) |
|
91 |
return true; |
|
92 |
QMouseEvent *me = (QMouseEvent *) event; |
|
93 |
||
94 |
QPointF clickPos = me->pos(); |
|
95 |
int index = -1; |
|
96 |
for (int i=0; i<m_points.size(); ++i) { |
|
97 |
QPainterPath path; |
|
98 |
if (m_shape == CircleShape) |
|
99 |
path.addEllipse(pointBoundingRect(i)); |
|
100 |
else |
|
101 |
path.addRect(pointBoundingRect(i)); |
|
102 |
||
103 |
if (path.contains(clickPos)) { |
|
104 |
index = i; |
|
105 |
break; |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
if (me->button() == Qt::LeftButton) { |
|
110 |
if (index == -1) { |
|
111 |
if (!m_editable) |
|
112 |
return false; |
|
113 |
int pos = 0; |
|
114 |
// Insert sort for x or y |
|
115 |
if (m_sortType == XSort) { |
|
116 |
for (int i=0; i<m_points.size(); ++i) |
|
117 |
if (m_points.at(i).x() > clickPos.x()) { |
|
118 |
pos = i; |
|
119 |
break; |
|
120 |
} |
|
121 |
} else if (m_sortType == YSort) { |
|
122 |
for (int i=0; i<m_points.size(); ++i) |
|
123 |
if (m_points.at(i).y() > clickPos.y()) { |
|
124 |
pos = i; |
|
125 |
break; |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
m_points.insert(pos, clickPos); |
|
130 |
m_locks.insert(pos, 0); |
|
131 |
m_currentIndex = pos; |
|
132 |
firePointChange(); |
|
133 |
} else { |
|
134 |
m_currentIndex = index; |
|
135 |
} |
|
136 |
return true; |
|
137 |
||
138 |
} else if (me->button() == Qt::RightButton) { |
|
139 |
if (index >= 0 && m_editable) { |
|
140 |
if (m_locks[index] == 0) { |
|
141 |
m_locks.remove(index); |
|
142 |
m_points.remove(index); |
|
143 |
} |
|
144 |
firePointChange(); |
|
145 |
return true; |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
} |
|
150 |
break; |
|
151 |
||
152 |
case QEvent::MouseButtonRelease: |
|
153 |
if (!m_fingerPointMapping.isEmpty()) |
|
154 |
return true; |
|
155 |
m_currentIndex = -1; |
|
156 |
break; |
|
157 |
||
158 |
case QEvent::MouseMove: |
|
159 |
if (!m_fingerPointMapping.isEmpty()) |
|
160 |
return true; |
|
161 |
if (m_currentIndex >= 0) |
|
162 |
movePoint(m_currentIndex, ((QMouseEvent *)event)->pos()); |
|
163 |
break; |
|
164 |
case QEvent::TouchBegin: |
|
165 |
case QEvent::TouchUpdate: |
|
166 |
{ |
|
167 |
const QTouchEvent *const touchEvent = static_cast<const QTouchEvent*>(event); |
|
168 |
const QList<QTouchEvent::TouchPoint> points = touchEvent->touchPoints(); |
|
169 |
const qreal pointSize = qMax(m_pointSize.width(), m_pointSize.height()); |
|
170 |
foreach (const QTouchEvent::TouchPoint &touchPoint, points) { |
|
171 |
const int id = touchPoint.id(); |
|
172 |
switch (touchPoint.state()) { |
|
173 |
case Qt::TouchPointPressed: |
|
174 |
{ |
|
175 |
// find the point, move it |
|
176 |
QSet<int> activePoints = QSet<int>::fromList(m_fingerPointMapping.values()); |
|
177 |
int activePoint = -1; |
|
178 |
qreal distance = -1; |
|
179 |
const int pointsCount = m_points.size(); |
|
180 |
const int activePointCount = activePoints.size(); |
|
181 |
if (pointsCount == 2 && activePointCount == 1) { // only two points |
|
182 |
activePoint = activePoints.contains(0) ? 1 : 0; |
|
183 |
} else { |
|
184 |
for (int i=0; i<pointsCount; ++i) { |
|
185 |
if (activePoints.contains(i)) |
|
186 |
continue; |
|
187 |
||
188 |
qreal d = QLineF(touchPoint.pos(), m_points.at(i)).length(); |
|
189 |
if ((distance < 0 && d < 12 * pointSize) || d < distance) { |
|
190 |
distance = d; |
|
191 |
activePoint = i; |
|
192 |
} |
|
193 |
||
194 |
} |
|
195 |
} |
|
196 |
if (activePoint != -1) { |
|
197 |
m_fingerPointMapping.insert(touchPoint.id(), activePoint); |
|
198 |
movePoint(activePoint, touchPoint.pos()); |
|
199 |
} |
|
200 |
} |
|
201 |
break; |
|
202 |
case Qt::TouchPointReleased: |
|
203 |
{ |
|
204 |
// move the point and release |
|
205 |
QHash<int,int>::iterator it = m_fingerPointMapping.find(id); |
|
206 |
movePoint(it.value(), touchPoint.pos()); |
|
207 |
m_fingerPointMapping.erase(it); |
|
208 |
} |
|
209 |
break; |
|
210 |
case Qt::TouchPointMoved: |
|
211 |
{ |
|
212 |
// move the point |
|
213 |
const int pointIdx = m_fingerPointMapping.value(id, -1); |
|
214 |
if (pointIdx >= 0) // do we track this point? |
|
215 |
movePoint(pointIdx, touchPoint.pos()); |
|
216 |
} |
|
217 |
break; |
|
218 |
default: |
|
219 |
break; |
|
220 |
} |
|
221 |
} |
|
222 |
if (m_fingerPointMapping.isEmpty()) { |
|
223 |
event->ignore(); |
|
224 |
return false; |
|
225 |
} else { |
|
226 |
return true; |
|
227 |
} |
|
228 |
} |
|
229 |
break; |
|
230 |
case QEvent::TouchEnd: |
|
231 |
if (m_fingerPointMapping.isEmpty()) { |
|
232 |
event->ignore(); |
|
233 |
return false; |
|
234 |
} |
|
235 |
return true; |
|
236 |
break; |
|
237 |
||
238 |
case QEvent::Resize: |
|
239 |
{ |
|
240 |
QResizeEvent *e = (QResizeEvent *) event; |
|
241 |
if (e->oldSize().width() == 0 || e->oldSize().height() == 0) |
|
242 |
break; |
|
243 |
qreal stretch_x = e->size().width() / qreal(e->oldSize().width()); |
|
244 |
qreal stretch_y = e->size().height() / qreal(e->oldSize().height()); |
|
245 |
for (int i=0; i<m_points.size(); ++i) { |
|
246 |
QPointF p = m_points[i]; |
|
247 |
movePoint(i, QPointF(p.x() * stretch_x, p.y() * stretch_y), false); |
|
248 |
} |
|
249 |
||
250 |
firePointChange(); |
|
251 |
break; |
|
252 |
} |
|
253 |
||
254 |
case QEvent::Paint: |
|
255 |
{ |
|
256 |
QWidget *that_widget = m_widget; |
|
257 |
m_widget = 0; |
|
258 |
QApplication::sendEvent(object, event); |
|
259 |
m_widget = that_widget; |
|
260 |
paintPoints(); |
|
261 |
#ifdef QT_OPENGL_SUPPORT |
|
262 |
ArthurFrame *af = qobject_cast<ArthurFrame *>(that_widget); |
|
263 |
if (af && af->usesOpenGL()) |
|
264 |
af->glWidget()->swapBuffers(); |
|
265 |
#endif |
|
266 |
return true; |
|
267 |
} |
|
268 |
default: |
|
269 |
break; |
|
270 |
} |
|
271 |
} |
|
272 |
||
273 |
return false; |
|
274 |
} |
|
275 |
||
276 |
||
277 |
void HoverPoints::paintPoints() |
|
278 |
{ |
|
279 |
QPainter p; |
|
280 |
#ifdef QT_OPENGL_SUPPORT |
|
281 |
ArthurFrame *af = qobject_cast<ArthurFrame *>(m_widget); |
|
282 |
if (af && af->usesOpenGL()) |
|
283 |
p.begin(af->glWidget()); |
|
284 |
else |
|
285 |
p.begin(m_widget); |
|
286 |
#else |
|
287 |
p.begin(m_widget); |
|
288 |
#endif |
|
289 |
||
290 |
p.setRenderHint(QPainter::Antialiasing); |
|
291 |
||
292 |
if (m_connectionPen.style() != Qt::NoPen && m_connectionType != NoConnection) { |
|
293 |
p.setPen(m_connectionPen); |
|
294 |
||
295 |
if (m_connectionType == CurveConnection) { |
|
296 |
QPainterPath path; |
|
297 |
path.moveTo(m_points.at(0)); |
|
298 |
for (int i=1; i<m_points.size(); ++i) { |
|
299 |
QPointF p1 = m_points.at(i-1); |
|
300 |
QPointF p2 = m_points.at(i); |
|
301 |
qreal distance = p2.x() - p1.x(); |
|
302 |
||
303 |
path.cubicTo(p1.x() + distance / 2, p1.y(), |
|
304 |
p1.x() + distance / 2, p2.y(), |
|
305 |
p2.x(), p2.y()); |
|
306 |
} |
|
307 |
p.drawPath(path); |
|
308 |
} else { |
|
309 |
p.drawPolyline(m_points); |
|
310 |
} |
|
311 |
} |
|
312 |
||
313 |
p.setPen(m_pointPen); |
|
314 |
p.setBrush(m_pointBrush); |
|
315 |
||
316 |
for (int i=0; i<m_points.size(); ++i) { |
|
317 |
QRectF bounds = pointBoundingRect(i); |
|
318 |
if (m_shape == CircleShape) |
|
319 |
p.drawEllipse(bounds); |
|
320 |
else |
|
321 |
p.drawRect(bounds); |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
static QPointF bound_point(const QPointF &point, const QRectF &bounds, int lock) |
|
326 |
{ |
|
327 |
QPointF p = point; |
|
328 |
||
329 |
qreal left = bounds.left(); |
|
330 |
qreal right = bounds.right(); |
|
331 |
qreal top = bounds.top(); |
|
332 |
qreal bottom = bounds.bottom(); |
|
333 |
||
334 |
if (p.x() < left || (lock & HoverPoints::LockToLeft)) p.setX(left); |
|
335 |
else if (p.x() > right || (lock & HoverPoints::LockToRight)) p.setX(right); |
|
336 |
||
337 |
if (p.y() < top || (lock & HoverPoints::LockToTop)) p.setY(top); |
|
338 |
else if (p.y() > bottom || (lock & HoverPoints::LockToBottom)) p.setY(bottom); |
|
339 |
||
340 |
return p; |
|
341 |
} |
|
342 |
||
343 |
void HoverPoints::setPoints(const QPolygonF &points) |
|
344 |
{ |
|
345 |
if (points.size() != m_points.size()) |
|
346 |
m_fingerPointMapping.clear(); |
|
347 |
m_points.clear(); |
|
348 |
for (int i=0; i<points.size(); ++i) |
|
349 |
m_points << bound_point(points.at(i), boundingRect(), 0); |
|
350 |
||
351 |
m_locks.clear(); |
|
352 |
if (m_points.size() > 0) { |
|
353 |
m_locks.resize(m_points.size()); |
|
354 |
||
355 |
m_locks.fill(0); |
|
356 |
} |
|
357 |
} |
|
358 |
||
359 |
||
360 |
void HoverPoints::movePoint(int index, const QPointF &point, bool emitUpdate) |
|
361 |
{ |
|
362 |
m_points[index] = bound_point(point, boundingRect(), m_locks.at(index)); |
|
363 |
if (emitUpdate) |
|
364 |
firePointChange(); |
|
365 |
} |
|
366 |
||
367 |
||
368 |
inline static bool x_less_than(const QPointF &p1, const QPointF &p2) |
|
369 |
{ |
|
370 |
return p1.x() < p2.x(); |
|
371 |
} |
|
372 |
||
373 |
||
374 |
inline static bool y_less_than(const QPointF &p1, const QPointF &p2) |
|
375 |
{ |
|
376 |
return p1.y() < p2.y(); |
|
377 |
} |
|
378 |
||
379 |
void HoverPoints::firePointChange() |
|
380 |
{ |
|
381 |
// printf("HoverPoints::firePointChange(), current=%d\n", m_currentIndex); |
|
382 |
||
383 |
if (m_sortType != NoSort) { |
|
384 |
||
385 |
QPointF oldCurrent; |
|
386 |
if (m_currentIndex != -1) { |
|
387 |
oldCurrent = m_points[m_currentIndex]; |
|
388 |
} |
|
389 |
||
390 |
if (m_sortType == XSort) |
|
391 |
qSort(m_points.begin(), m_points.end(), x_less_than); |
|
392 |
else if (m_sortType == YSort) |
|
393 |
qSort(m_points.begin(), m_points.end(), y_less_than); |
|
394 |
||
395 |
// Compensate for changed order... |
|
396 |
if (m_currentIndex != -1) { |
|
397 |
for (int i=0; i<m_points.size(); ++i) { |
|
398 |
if (m_points[i] == oldCurrent) { |
|
399 |
m_currentIndex = i; |
|
400 |
break; |
|
401 |
} |
|
402 |
} |
|
403 |
} |
|
404 |
||
405 |
// printf(" - firePointChange(), current=%d\n", m_currentIndex); |
|
406 |
||
407 |
} |
|
408 |
||
409 |
// for (int i=0; i<m_points.size(); ++i) { |
|
410 |
// printf(" - point(%2d)=[%.2f, %.2f], lock=%d\n", |
|
411 |
// i, m_points.at(i).x(), m_points.at(i).y(), m_locks.at(i)); |
|
412 |
// } |
|
413 |
||
414 |
emit pointsChanged(m_points); |
|
415 |
} |