source: trunk/demos/shared/hoverpoints.cpp@ 265

Last change on this file since 265 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 9.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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
51HoverPoints::HoverPoints(QWidget *widget, PointShape shape)
52 : QObject(widget)
53{
54 m_widget = widget;
55 widget->installEventFilter(this);
56
57 m_connectionType = CurveConnection;
58 m_sortType = NoSort;
59 m_shape = shape;
60 m_pointPen = QPen(QColor(255, 255, 255, 191), 1);
61 m_connectionPen = QPen(QColor(255, 255, 255, 127), 2);
62 m_pointBrush = QBrush(QColor(191, 191, 191, 127));
63 m_pointSize = QSize(11, 11);
64 m_currentIndex = -1;
65 m_editable = true;
66 m_enabled = true;
67
68 connect(this, SIGNAL(pointsChanged(const QPolygonF &)),
69 m_widget, SLOT(update()));
70}
71
72
73void HoverPoints::setEnabled(bool enabled)
74{
75 if (m_enabled != enabled) {
76 m_enabled = enabled;
77 m_widget->update();
78 }
79}
80
81
82bool HoverPoints::eventFilter(QObject *object, QEvent *event)
83{
84 if (object == m_widget && m_enabled) {
85 switch (event->type()) {
86
87 case QEvent::MouseButtonPress:
88 {
89 QMouseEvent *me = (QMouseEvent *) event;
90
91 QPointF clickPos = me->pos();
92 int index = -1;
93 for (int i=0; i<m_points.size(); ++i) {
94 QPainterPath path;
95 if (m_shape == CircleShape)
96 path.addEllipse(pointBoundingRect(i));
97 else
98 path.addRect(pointBoundingRect(i));
99
100 if (path.contains(clickPos)) {
101 index = i;
102 break;
103 }
104 }
105
106 if (me->button() == Qt::LeftButton) {
107 if (index == -1) {
108 if (!m_editable)
109 return false;
110 int pos = 0;
111 // Insert sort for x or y
112 if (m_sortType == XSort) {
113 for (int i=0; i<m_points.size(); ++i)
114 if (m_points.at(i).x() > clickPos.x()) {
115 pos = i;
116 break;
117 }
118 } else if (m_sortType == YSort) {
119 for (int i=0; i<m_points.size(); ++i)
120 if (m_points.at(i).y() > clickPos.y()) {
121 pos = i;
122 break;
123 }
124 }
125
126 m_points.insert(pos, clickPos);
127 m_locks.insert(pos, 0);
128 m_currentIndex = pos;
129 firePointChange();
130 } else {
131 m_currentIndex = index;
132 }
133 return true;
134
135 } else if (me->button() == Qt::RightButton) {
136 if (index >= 0 && m_editable) {
137 if (m_locks[index] == 0) {
138 m_locks.remove(index);
139 m_points.remove(index);
140 }
141 firePointChange();
142 return true;
143 }
144 }
145
146 }
147 break;
148
149 case QEvent::MouseButtonRelease:
150 m_currentIndex = -1;
151 break;
152
153 case QEvent::MouseMove:
154 if (m_currentIndex >= 0)
155 movePoint(m_currentIndex, ((QMouseEvent *)event)->pos());
156 break;
157
158 case QEvent::Resize:
159 {
160 QResizeEvent *e = (QResizeEvent *) event;
161 if (e->oldSize().width() == 0 || e->oldSize().height() == 0)
162 break;
163 qreal stretch_x = e->size().width() / qreal(e->oldSize().width());
164 qreal stretch_y = e->size().height() / qreal(e->oldSize().height());
165 for (int i=0; i<m_points.size(); ++i) {
166 QPointF p = m_points[i];
167 movePoint(i, QPointF(p.x() * stretch_x, p.y() * stretch_y), false);
168 }
169
170 firePointChange();
171 break;
172 }
173
174 case QEvent::Paint:
175 {
176 QWidget *that_widget = m_widget;
177 m_widget = 0;
178 QApplication::sendEvent(object, event);
179 m_widget = that_widget;
180 paintPoints();
181#ifdef QT_OPENGL_SUPPORT
182 ArthurFrame *af = qobject_cast<ArthurFrame *>(that_widget);
183 if (af && af->usesOpenGL())
184 af->glWidget()->swapBuffers();
185#endif
186 return true;
187 }
188 default:
189 break;
190 }
191 }
192
193 return false;
194}
195
196
197void HoverPoints::paintPoints()
198{
199 QPainter p;
200#ifdef QT_OPENGL_SUPPORT
201 ArthurFrame *af = qobject_cast<ArthurFrame *>(m_widget);
202 if (af && af->usesOpenGL())
203 p.begin(af->glWidget());
204 else
205 p.begin(m_widget);
206#else
207 p.begin(m_widget);
208#endif
209
210 p.setRenderHint(QPainter::Antialiasing);
211
212 if (m_connectionPen.style() != Qt::NoPen && m_connectionType != NoConnection) {
213 p.setPen(m_connectionPen);
214
215 if (m_connectionType == CurveConnection) {
216 QPainterPath path;
217 path.moveTo(m_points.at(0));
218 for (int i=1; i<m_points.size(); ++i) {
219 QPointF p1 = m_points.at(i-1);
220 QPointF p2 = m_points.at(i);
221 qreal distance = p2.x() - p1.x();
222
223 path.cubicTo(p1.x() + distance / 2, p1.y(),
224 p1.x() + distance / 2, p2.y(),
225 p2.x(), p2.y());
226 }
227 p.drawPath(path);
228 } else {
229 p.drawPolyline(m_points);
230 }
231 }
232
233 p.setPen(m_pointPen);
234 p.setBrush(m_pointBrush);
235
236 for (int i=0; i<m_points.size(); ++i) {
237 QRectF bounds = pointBoundingRect(i);
238 if (m_shape == CircleShape)
239 p.drawEllipse(bounds);
240 else
241 p.drawRect(bounds);
242 }
243}
244
245static QPointF bound_point(const QPointF &point, const QRectF &bounds, int lock)
246{
247 QPointF p = point;
248
249 qreal left = bounds.left();
250 qreal right = bounds.right();
251 qreal top = bounds.top();
252 qreal bottom = bounds.bottom();
253
254 if (p.x() < left || (lock & HoverPoints::LockToLeft)) p.setX(left);
255 else if (p.x() > right || (lock & HoverPoints::LockToRight)) p.setX(right);
256
257 if (p.y() < top || (lock & HoverPoints::LockToTop)) p.setY(top);
258 else if (p.y() > bottom || (lock & HoverPoints::LockToBottom)) p.setY(bottom);
259
260 return p;
261}
262
263void HoverPoints::setPoints(const QPolygonF &points)
264{
265 m_points.clear();
266 for (int i=0; i<points.size(); ++i)
267 m_points << bound_point(points.at(i), boundingRect(), 0);
268
269 m_locks.clear();
270 if (m_points.size() > 0) {
271 m_locks.resize(m_points.size());
272
273 m_locks.fill(0);
274 }
275}
276
277
278void HoverPoints::movePoint(int index, const QPointF &point, bool emitUpdate)
279{
280 m_points[index] = bound_point(point, boundingRect(), m_locks.at(index));
281 if (emitUpdate)
282 firePointChange();
283}
284
285
286inline static bool x_less_than(const QPointF &p1, const QPointF &p2)
287{
288 return p1.x() < p2.x();
289}
290
291
292inline static bool y_less_than(const QPointF &p1, const QPointF &p2)
293{
294 return p1.y() < p2.y();
295}
296
297void HoverPoints::firePointChange()
298{
299// printf("HoverPoints::firePointChange(), current=%d\n", m_currentIndex);
300
301 if (m_sortType != NoSort) {
302
303 QPointF oldCurrent;
304 if (m_currentIndex != -1) {
305 oldCurrent = m_points[m_currentIndex];
306 }
307
308 if (m_sortType == XSort)
309 qSort(m_points.begin(), m_points.end(), x_less_than);
310 else if (m_sortType == YSort)
311 qSort(m_points.begin(), m_points.end(), y_less_than);
312
313 // Compensate for changed order...
314 if (m_currentIndex != -1) {
315 for (int i=0; i<m_points.size(); ++i) {
316 if (m_points[i] == oldCurrent) {
317 m_currentIndex = i;
318 break;
319 }
320 }
321 }
322
323// printf(" - firePointChange(), current=%d\n", m_currentIndex);
324
325 }
326
327// for (int i=0; i<m_points.size(); ++i) {
328// printf(" - point(%2d)=[%.2f, %.2f], lock=%d\n",
329// i, m_points.at(i).x(), m_points.at(i).y(), m_locks.at(i));
330// }
331
332 emit pointsChanged(m_points);
333}
Note: See TracBrowser for help on using the repository browser.