source: trunk/src/gui/graphicsview/qgraphicssceneevent.cpp@ 135

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

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

File size: 40.2 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 QtGui module 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/*!
43 \class QGraphicsSceneEvent
44 \brief The QGraphicsSceneEvent class provides a base class for all
45 graphics view related events.
46 \since 4.2
47 \ingroup multimedia
48 \ingroup graphicsview-api
49
50 When a QGraphicsView receives Qt mouse, keyboard, and drag and
51 drop events (QMouseEvent, QKeyEvent, QDragEvent, etc.), it
52 translates them into instances of QGraphicsSceneEvent subclasses
53 and forwards them to the QGraphicsScene it displays. The scene
54 then forwards the events to the relevant items.
55
56 For example, when a QGraphicsView receives a QMouseEvent of type
57 MousePress as a response to a user click, the view sends a
58 QGraphicsSceneMouseEvent of type GraphicsSceneMousePress to the
59 underlying QGraphicsScene through its
60 \l{QGraphicsScene::}{mousePressEvent()} function. The default
61 QGraphicsScene::mousePressEvent() implementation determines which
62 item was clicked and forwards the event to
63 QGraphicsItem::mousePressEvent().
64
65 \omit ### Beskrive widget() \endomit
66
67 Subclasses such as QGraphicsSceneMouseEvent and
68 QGraphicsSceneContextMenuEvent provide the coordinates from the
69 original QEvent in screen, scene, and item coordinates (see
70 \l{QGraphicsSceneMouseEvent::}{screenPos()},
71 \l{QGraphicsSceneMouseEvent::}{scenePos()}, and
72 \l{QGraphicsSceneMouseEvent::}{pos()}). The item coordinates are
73 set by the QGraphicsScene before it forwards the event to the
74 event to a QGraphicsItem. The mouse events also add the
75 possibility to retrieve the coordinates from the last event
76 received by the view (see
77 \l{QGraphicsSceneMouseEvent::}{lastScreenPos()},
78 \l{QGraphicsSceneMouseEvent::}{lastScenePos()}, and
79 \l{QGraphicsSceneMouseEvent::}{lastPos()}).
80
81 \sa QEvent
82*/
83
84/*!
85 \class QGraphicsSceneMouseEvent
86 \brief The QGraphicsSceneMouseEvent class provides mouse events
87 in the graphics view framework.
88 \since 4.2
89 \ingroup multimedia
90 \ingroup graphicsview-api
91
92 When a QGraphicsView receives a QMouseEvent, it translates it to
93 a QGraphicsSceneMouseEvent. The event is then forwarded to the
94 QGraphicsScene associated with the view.
95
96 In addition to containing the item, scene, and screen coordinates
97 of the event (as pos(), scenePos(), and screenPos()), mouse
98 events also contain the coordinates of the previous mouse
99 event received by the view. These can be retrieved with
100 lastPos(), lastScreenPos(), and lastScenePos().
101
102 \sa QGraphicsSceneContextMenuEvent,
103 QGraphicsSceneHoverEvent, QGraphicsSceneWheelEvent,
104 QMouseEvent
105*/
106
107/*!
108 \class QGraphicsSceneWheelEvent
109 \brief The QGraphicsSceneWheelEvent class provides wheel events
110 in the graphics view framework.
111 \brief The QGraphicsSceneWheelEvent class provides wheel events in the
112 graphics view framework.
113 \since 4.2
114 \ingroup multimedia
115 \ingroup graphicsview-api
116
117 \l{QWheelEvent}{QWheelEvent}s received by a QGraphicsView are translated
118 into QGraphicsSceneWheelEvents; it translates the QWheelEvent::globalPos()
119 into item, scene, and screen coordinates (pos(), scenePos(), and
120 screenPos()).
121
122 \sa QGraphicsSceneMouseEvent, QGraphicsSceneContextMenuEvent,
123 QGraphicsSceneHoverEvent, QWheelEvent
124*/
125
126/*!
127 \class QGraphicsSceneContextMenuEvent
128 \brief The QGraphicsSceneContextMenuEvent class provides context
129 menu events in the graphics view framework.
130 \since 4.2
131 \ingroup multimedia
132 \ingroup graphicsview-api
133
134 A QContextMenuEvent received by a QGraphicsView is translated
135 into a QGraphicsSceneContextMenuEvent. The
136 QContextMenuEvent::globalPos() is translated into item, scene, and
137 screen coordinates (pos(), scenePos(), and screenPos()).
138
139 \sa QGraphicsSceneMouseEvent, QGraphicsSceneWheelEvent,
140 QContextMenuEvent
141*/
142
143/*!
144 \enum QGraphicsSceneContextMenuEvent::Reason
145
146 This enum describes the reason why the context event was sent.
147
148 \value Mouse The mouse caused the event to be sent. On most
149 platforms, this means the right mouse button was clicked.
150
151 \value Keyboard The keyboard caused this event to be sent. On
152 Windows and Mac OS X, this means the menu button was pressed.
153
154 \value Other The event was sent by some other means (i.e. not
155 by the mouse or keyboard).
156*/
157
158/*!
159 \class QGraphicsSceneHoverEvent
160 \brief The QGraphicsSceneHoverEvent class provides hover events
161 in the graphics view framework.
162 \since 4.2
163 \ingroup multimedia
164 \ingroup graphicsview-api
165
166 When a QGraphicsView receives a QHoverEvent event, it translates
167 it into QGraphicsSceneHoverEvent. The event is then forwarded to
168 the QGraphicsScene associated with the view.
169
170 \sa QGraphicsSceneMouseEvent, QGraphicsSceneContextMenuEvent,
171 QGraphicsSceneWheelEvent, QHoverEvent
172*/
173
174/*!
175 \class QGraphicsSceneHelpEvent
176 \brief The QGraphicsSceneHelpEvent class provides events when a
177 tooltip is requested.
178 \since 4.2
179 \ingroup multimedia
180 \ingroup graphicsview-api
181
182 When a QGraphicsView receives a QEvent of type
183 QEvent::ToolTip, it creates a QGraphicsSceneHelpEvent, which is
184 forwarded to the scene. You can set a tooltip on a QGraphicsItem
185 with \l{QGraphicsItem::}{setToolTip()}; by default QGraphicsScene
186 displays the tooltip of the QGraphicsItem with the highest
187 z-value (i.e, the top-most item) under the mouse position.
188
189 QGraphicsView does not forward events when
190 \l{QWhatsThis}{"What's This"} and \l{QStatusTipEvent}{status tip}
191 help is requested. If you need this, you can reimplement
192 QGraphicsView::viewportEvent() and forward QStatusTipEvent
193 events and \l{QEvent}{QEvents} of type QEvent::WhatsThis to the
194 scene.
195
196 \sa QEvent
197*/
198
199/*!
200 \class QGraphicsSceneDragDropEvent
201 \brief The QGraphicsSceneDragDropEvent class provides events for
202 drag and drop in the graphics view framework.
203 \since 4.2
204 \ingroup multimedia
205 \ingroup graphicsview-api
206
207 QGraphicsView inherits the drag and drop functionality provided
208 by QWidget. When it receives a drag and drop event, it translates
209 it to a QGraphicsSceneDragDropEvent.
210
211 QGraphicsSceneDragDropEvent stores events of type
212 GraphicsSceneDragEnter, GraphicsSceneDragLeave,
213 GraphicsSceneDragMove, or GraphicsSceneDrop.
214
215 QGraphicsSceneDragDropEvent contains the position of the mouse
216 cursor in both item, scene, and screen coordinates; this can be
217 retrieved with pos(), scenePos(), and screenPos().
218
219 The scene sends the event to the first QGraphicsItem under the
220 mouse cursor that accepts drops; a graphics item is set to accept
221 drops with \l{QGraphicsItem::}{setAcceptDrops()}.
222*/
223
224/*!
225 \class QGraphicsSceneResizeEvent
226 \brief The QGraphicsSceneResizeEvent class provides events for widget
227 resizing in the graphics view framework.
228 \since 4.4
229 \ingroup multimedia
230 \ingroup graphicsview-api
231
232 A QGraphicsWidget sends itself a QGraphicsSceneResizeEvent immediately
233 when its geometry changes.
234
235 It's similar to QResizeEvent, but its sizes, oldSize() and newSize(), use
236 QSizeF instead of QSize.
237
238 \sa QGraphicsWidget::setGeometry(), QGraphicsWidget::resize()
239*/
240
241/*!
242 \class QGraphicsSceneMoveEvent
243 \brief The QGraphicsSceneMoveEvent class provides events for widget
244 moving in the graphics view framework.
245 \since 4.4
246 \ingroup multimedia
247 \ingroup graphicsview-api
248
249 A QGraphicsWidget sends itself a QGraphicsSceneMoveEvent immediately when
250 its local position changes. The delivery is implemented as part of
251 QGraphicsItem::itemChange().
252
253 It's similar to QMoveEvent, but its positions, oldPos() and newPos(), use
254 QPointF instead of QPoint.
255
256 \sa QGraphicsItem::setPos(), QGraphicsItem::ItemPositionChange,
257 QGraphicsItem::ItemPositionHasChanged
258*/
259
260#include "qgraphicssceneevent.h"
261
262#ifndef QT_NO_GRAPHICSVIEW
263
264#ifndef QT_NO_DEBUG
265#include <QtCore/qdebug.h>
266#endif
267#include <QtCore/qmap.h>
268#include <QtCore/qpoint.h>
269#include <QtCore/qsize.h>
270#include <QtCore/qstring.h>
271
272QT_BEGIN_NAMESPACE
273
274class QGraphicsSceneEventPrivate
275{
276public:
277 inline QGraphicsSceneEventPrivate()
278 : widget(0),
279 q_ptr(0)
280 { }
281
282 inline virtual ~QGraphicsSceneEventPrivate()
283 { }
284
285 QWidget *widget;
286 QGraphicsSceneEvent *q_ptr;
287};
288
289/*!
290 \internal
291
292 Constructs a generic graphics scene event of the specified \a type.
293*/
294QGraphicsSceneEvent::QGraphicsSceneEvent(Type type)
295 : QEvent(type), d_ptr(new QGraphicsSceneEventPrivate)
296{
297 d_ptr->q_ptr = this;
298}
299
300/*!
301 \internal
302
303 Constructs a generic graphics scene event.
304*/
305QGraphicsSceneEvent::QGraphicsSceneEvent(QGraphicsSceneEventPrivate &dd, Type type)
306 : QEvent(type), d_ptr(&dd)
307{
308 d_ptr->q_ptr = this;
309}
310
311/*!
312 Destroys the event.
313*/
314QGraphicsSceneEvent::~QGraphicsSceneEvent()
315{
316 delete d_ptr;
317}
318
319/*!
320 Returns the widget where the event originated, or 0 if the event
321 originates from another application.
322*/
323QWidget *QGraphicsSceneEvent::widget() const
324{
325 return d_ptr->widget;
326}
327
328/*!
329 \internal
330
331 Sets the \a widget related to this event.
332
333 \sa widget()
334*/
335void QGraphicsSceneEvent::setWidget(QWidget *widget)
336{
337 d_ptr->widget = widget;
338}
339
340class QGraphicsSceneMouseEventPrivate : public QGraphicsSceneEventPrivate
341{
342 Q_DECLARE_PUBLIC(QGraphicsSceneMouseEvent)
343public:
344 inline QGraphicsSceneMouseEventPrivate()
345 : button(Qt::NoButton),
346 buttons(0), modifiers(0)
347 { }
348
349 QPointF pos;
350 QPointF scenePos;
351 QPoint screenPos;
352 QPointF lastPos;
353 QPointF lastScenePos;
354 QPoint lastScreenPos;
355 QMap<Qt::MouseButton, QPointF> buttonDownPos;
356 QMap<Qt::MouseButton, QPointF> buttonDownScenePos;
357 QMap<Qt::MouseButton, QPoint> buttonDownScreenPos;
358 Qt::MouseButton button;
359 Qt::MouseButtons buttons;
360 Qt::KeyboardModifiers modifiers;
361};
362
363/*!
364 \internal
365
366 Constructs a generic graphics scene mouse event of the specified \a type.
367*/
368QGraphicsSceneMouseEvent::QGraphicsSceneMouseEvent(Type type)
369 : QGraphicsSceneEvent(*new QGraphicsSceneMouseEventPrivate, type)
370{
371}
372
373/*!
374 Destroys the event.
375*/
376QGraphicsSceneMouseEvent::~QGraphicsSceneMouseEvent()
377{
378}
379
380/*!
381 Returns the mouse cursor position in item coordinates.
382
383 \sa scenePos(), screenPos(), lastPos()
384*/
385QPointF QGraphicsSceneMouseEvent::pos() const
386{
387 Q_D(const QGraphicsSceneMouseEvent);
388 return d->pos;
389}
390
391/*!
392 \internal
393*/
394void QGraphicsSceneMouseEvent::setPos(const QPointF &pos)
395{
396 Q_D(QGraphicsSceneMouseEvent);
397 d->pos = pos;
398}
399
400/*!
401 Returns the mouse cursor position in scene coordinates.
402
403 \sa pos(), screenPos(), lastScenePos()
404*/
405QPointF QGraphicsSceneMouseEvent::scenePos() const
406{
407 Q_D(const QGraphicsSceneMouseEvent);
408 return d->scenePos;
409}
410
411/*!
412 \internal
413*/
414void QGraphicsSceneMouseEvent::setScenePos(const QPointF &pos)
415{
416 Q_D(QGraphicsSceneMouseEvent);
417 d->scenePos = pos;
418}
419
420/*!
421 Returns the mouse cursor position in screen coordinates.
422
423 \sa pos(), scenePos(), lastScreenPos()
424*/
425QPoint QGraphicsSceneMouseEvent::screenPos() const
426{
427 Q_D(const QGraphicsSceneMouseEvent);
428 return d->screenPos;
429}
430
431/*!
432 \internal
433*/
434void QGraphicsSceneMouseEvent::setScreenPos(const QPoint &pos)
435{
436 Q_D(QGraphicsSceneMouseEvent);
437 d->screenPos = pos;
438}
439
440/*!
441 Returns the mouse cursor position in item coordinates where the specified
442 \a button was clicked.
443
444 \sa buttonDownScenePos(), buttonDownScreenPos(), pos()
445*/
446QPointF QGraphicsSceneMouseEvent::buttonDownPos(Qt::MouseButton button) const
447{
448 Q_D(const QGraphicsSceneMouseEvent);
449 return d->buttonDownPos.value(button);
450}
451
452/*!
453 \internal
454*/
455void QGraphicsSceneMouseEvent::setButtonDownPos(Qt::MouseButton button, const QPointF &pos)
456{
457 Q_D(QGraphicsSceneMouseEvent);
458 d->buttonDownPos.insert(button, pos);
459}
460
461/*!
462 Returns the mouse cursor position in scene coordinates where the
463 specified \a button was clicked.
464
465 \sa buttonDownPos(), buttonDownScreenPos(), scenePos()
466*/
467QPointF QGraphicsSceneMouseEvent::buttonDownScenePos(Qt::MouseButton button) const
468{
469 Q_D(const QGraphicsSceneMouseEvent);
470 return d->buttonDownScenePos.value(button);
471}
472
473/*!
474 \internal
475*/
476void QGraphicsSceneMouseEvent::setButtonDownScenePos(Qt::MouseButton button, const QPointF &pos)
477{
478 Q_D(QGraphicsSceneMouseEvent);
479 d->buttonDownScenePos.insert(button, pos);
480}
481
482/*!
483 Returns the mouse cursor position in screen coordinates where the
484 specified \a button was clicked.
485
486 \sa screenPos(), buttonDownPos(), buttonDownScenePos()
487*/
488QPoint QGraphicsSceneMouseEvent::buttonDownScreenPos(Qt::MouseButton button) const
489{
490 Q_D(const QGraphicsSceneMouseEvent);
491 return d->buttonDownScreenPos.value(button);
492}
493
494/*!
495 \internal
496*/
497void QGraphicsSceneMouseEvent::setButtonDownScreenPos(Qt::MouseButton button, const QPoint &pos)
498{
499 Q_D(QGraphicsSceneMouseEvent);
500 d->buttonDownScreenPos.insert(button, pos);
501}
502
503/*!
504 Returns the last recorded mouse cursor position in item