Qt 4.8
qdnd_qws.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qapplication.h"
43 
44 #ifndef QT_NO_DRAGANDDROP
45 
46 #include "qwidget.h"
47 #include "qdatetime.h"
48 #include "qbitmap.h"
49 #include "qcursor.h"
50 #include "qevent.h"
51 #include "qpainter.h"
52 #include "qdnd_p.h"
53 
55 
57 
58 static QPixmap *defaultPm = 0;
59 static const int default_pm_hotx = -2;
60 static const int default_pm_hoty = -16;
61 static const char *const default_pm[] = {
62 "13 9 3 1",
63 ". c None",
64 " c #000000",
65 "X c #FFFFFF",
66 "X X X X X X X",
67 " X X X X X X ",
68 "X ......... X",
69 " X.........X ",
70 "X ......... X",
71 " X.........X ",
72 "X ......... X",
73 " X X X X X X ",
74 "X X X X X X X",
75 };
76 
77 // Shift/Ctrl handling, and final drop status
79 static Qt::DropActions possible_actions = Qt::IgnoreAction;
80 
81 
82 // static variables in place of a proper cross-process solution
84 static bool qt_qws_dnd_dragging = false;
85 
86 
87 static Qt::KeyboardModifiers oldstate;
88 
89 class QShapedPixmapWidget : public QWidget {
91 public:
94  {
95  // ### Temporary workaround for 4.2-rc1!!! To prevent flickering when
96  // using drag'n drop in a client application. (task 126956)
97  // setAttribute() should be done unconditionally!
100  }
101 
103  {
104  pixmap = pm;
105  if (!pixmap.mask().isNull()) {
106  setMask(pixmap.mask());
107  } else {
108  clearMask();
109  }
110  resize(pm.width(),pm.height());
111  }
112 
114  {
115  QPainter p(this);
116  p.drawPixmap(0,0,pixmap);
117  }
118 };
119 
120 
122 
123 
125 {
126  if (qt_qws_dnd_deco) {
127  QPixmap pm;
129  if (drag_object) {
130  pm = drag_object->pixmap();
131  if (!pm.isNull())
132  pm_hot = drag_object->hotSpot();
133  }
134  if (pm.isNull()) {
135  if (!defaultPm)
136  defaultPm = new QPixmap(default_pm);
137  pm = *defaultPm;
138  }
139  qt_qws_dnd_deco->setPixmap(pm);
140  qt_qws_dnd_deco->move(QCursor::pos()-pm_hot);
141  if (willDrop) {
142  qt_qws_dnd_deco->show();
143  } else {
144  qt_qws_dnd_deco->hide();
145  }
146  }
147 }
148 
150 
151 void QDragManager::move(const QPoint &) { }
152 
154 {
155 #ifndef QT_NO_CURSOR
156  if (willDrop) {
157  if (qt_qws_dnd_deco)
158  qt_qws_dnd_deco->show();
159  if (currentActionForOverrideCursor != global_accepted_action) {
161  currentActionForOverrideCursor = global_accepted_action;
162  }
163  } else {
164  QCursor *overrideCursor = QApplication::overrideCursor();
165  if (!overrideCursor || overrideCursor->shape() != Qt::ForbiddenCursor) {
167  currentActionForOverrideCursor = Qt::IgnoreAction;
168  }
169  if (qt_qws_dnd_deco)
170  qt_qws_dnd_deco->hide();
171  }
172 #endif
173 }
174 
175 
177 {
178  if (beingCancelled) {
179  if (e->type() == QEvent::KeyRelease && static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) {
180  qApp->removeEventFilter(this);
181  Q_ASSERT(object == 0);
182  beingCancelled = false;
183  eventLoop->exit();
184  return true; // block the key release
185  }
186  return false;
187  }
188 
189 
190 
191  if (!o->isWidgetType())
192  return false;
193 
194  switch(e->type()) {
196  // prevent accelerators from firing while dragging
197  e->accept();
198  return true;
199 
200  case QEvent::KeyPress:
201  case QEvent::KeyRelease:
202  {
203  QKeyEvent *ke = ((QKeyEvent*)e);
204  if (ke->key() == Qt::Key_Escape && e->type() == QEvent::KeyPress) {
205  cancel();
206  qApp->removeEventFilter(this);
207  beingCancelled = false;
208  eventLoop->exit();
209  } else {
210  updateCursor();
211  }
212  return true; // Eat all key events
213  }
214 
216  case QEvent::MouseMove:
217  {
218  if (!object) { //#### this should not happen
219  qWarning("QDragManager::eventFilter: No object");
220  return true;
221  }
222 
223  QDragManager *manager = QDragManager::self();
224  QMimeData *dropData = manager->object ? manager->dragPrivate()->data : manager->dropData;
225  if (manager->object)
227  else
229 
230  QMouseEvent *me = (QMouseEvent *)e;
231  if (me->buttons()) {
234 
235  // Fix for when we move mouse on to the deco widget
236  if (qt_qws_dnd_deco && cw == qt_qws_dnd_deco)
237  cw = object->target();
238 
239  while (cw && !cw->acceptDrops() && !cw->isWindow())
240  cw = cw->parentWidget();
241 
242  if (object->target() != cw) {
243  if (object->target()) {
244  QDragLeaveEvent dle;
245  QApplication::sendEvent(object->target(), &dle);
246  willDrop = false;
248  updateCursor();
249  restoreCursor = true;
250  object->d_func()->target = 0;
251  }
252  if (cw && cw->acceptDrops()) {
253  object->d_func()->target = cw;
254  QDragEnterEvent dee(cw->mapFromGlobal(me->globalPos()), possible_actions, dropData,
255  me->buttons(), me->modifiers());
256  QApplication::sendEvent(object->target(), &dee);
257  willDrop = dee.isAccepted() && dee.dropAction() != Qt::IgnoreAction;
258  global_accepted_action = willDrop ? dee.dropAction() : Qt::IgnoreAction;
259  updateCursor();
260  restoreCursor = true;
261  }
262  } else if (cw) {
263  QDragMoveEvent dme(cw->mapFromGlobal(me->globalPos()), possible_actions, dropData,
264  me->buttons(), me->modifiers());
267  dme.accept();
268  }
269  QApplication::sendEvent(cw, &dme);
270  willDrop = dme.isAccepted();
271  global_accepted_action = willDrop ? dme.dropAction() : Qt::IgnoreAction;
272  updatePixmap();
273  updateCursor();
274  }
275  if (global_accepted_action != prevAction)
276  emitActionChanged(global_accepted_action);
277  }
278  return true; // Eat all mouse events
279  }
280 
282  {
283  qApp->removeEventFilter(this);
284  if (restoreCursor) {
285  willDrop = false;
286 #ifndef QT_NO_CURSOR
288 #endif
289  restoreCursor = false;
290  }
291  if (object && object->target()) {
292  QMouseEvent *me = (QMouseEvent *)e;
293 
294  QDragManager *manager = QDragManager::self();
295  QMimeData *dropData = manager->object ? manager->dragPrivate()->data : manager->dropData;
296 
297  QDropEvent de(object->target()->mapFromGlobal(me->globalPos()), possible_actions, dropData,
298  me->buttons(), me->modifiers());
299  QApplication::sendEvent(object->target(), &de);
300  if (de.isAccepted())
301  global_accepted_action = de.dropAction();
302  else
304 
305  if (object)
306  object->deleteLater();
307  drag_object = object = 0;
308  }
309  eventLoop->exit();
310  return true; // Eat all mouse events
311  }
312 
313  default:
314  break;
315  }
316 
317  return false;
318 }
319 
321 {
322  if (object == o || !o || !o->source())
323  return Qt::IgnoreAction;
324 
325  if (object) {
326  cancel();
327  qApp->removeEventFilter(this);
328  beingCancelled = false;
329  }
330 
331  object = drag_object = o;
332  qt_qws_dnd_deco = new QShapedPixmapWidget();
333  oldstate = Qt::NoModifier; // #### Should use state that caused the drag
334 // drag_mode = mode;
335 
336  willDrop = false;
337  updatePixmap();
338  updateCursor();
339  restoreCursor = true;
340  object->d_func()->target = 0;
341  qApp->installEventFilter(this);
342 
344 #ifndef QT_NO_CURSOR
345  qApp->setOverrideCursor(Qt::ArrowCursor);
346  restoreCursor = true;
347  updateCursor();
348 #endif
349 
350  qt_qws_dnd_dragging = true;
351 
352  eventLoop = new QEventLoop;
353  (void) eventLoop->exec();
354  delete eventLoop;
355  eventLoop = 0;
356 
357  delete qt_qws_dnd_deco;
358  qt_qws_dnd_deco = 0;
359  qt_qws_dnd_dragging = false;
360 
361 
362  return global_accepted_action;
363 }
364 
365 
366 void QDragManager::cancel(bool deleteSource)
367 {
368 // qDebug("QDragManager::cancel");
369  beingCancelled = true;
370 
371  if (object->target()) {
372  QDragLeaveEvent dle;
373  QApplication::sendEvent(object->target(), &dle);
374  }
375 
376 #ifndef QT_NO_CURSOR
377  if (restoreCursor) {
379  restoreCursor = false;
380  }
381 #endif
382 
383  if (drag_object) {
384  if (deleteSource)
385  object->deleteLater();
386  drag_object = object = 0;
387  }
388 
389  delete qt_qws_dnd_deco;
390  qt_qws_dnd_deco = 0;
391 
393 }
394 
395 
396 void QDragManager::drop()
397 {
398 }
399 
401 {
402  if (!drag_object)
403  return QVariant();
404  QByteArray data = drag_object->mimeData()->data(mimetype);
405  if (type == QVariant::String)
406  return QString::fromUtf8(data);
407  return data;
408 }
409 
410 bool QDropData::hasFormat_sys(const QString &format) const
411 {
412  return formats().contains(format);
413 }
414 
416 {
417  if (drag_object)
418  return drag_object->mimeData()->formats();
419  return QStringList();
420 }
421 
422 
423 #endif // QT_NO_DRAGANDDROP
424 
425 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
void updateCursor()
Definition: qdnd_mac.mm:268
QWidget * parentWidget() const
Returns the parent of this widget, or 0 if it does not have any parent widget.
Definition: qwidget.h:1035
Qt::DropActions possible_actions
Definition: qdnd_p.h:182
The QKeyEvent class describes a key event.
Definition: qevent.h:224
bool hasFormat_sys(const QString &mimeType) const
Definition: qdnd_mac.mm:214
int type
Definition: qmetatype.cpp:239
QStringList formats_sys() const
Definition: qdnd_mac.mm:245
The QCursor class provides a mouse cursor with an arbitrary shape.
Definition: qcursor.h:89
QWidget * source() const
Returns the source of the drag object.
Definition: qdrag.cpp:209
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
virtual QStringList formats() const
Returns a list of formats supported by the object.
Definition: qmimedata.cpp:579
The QDrag class provides support for MIME-based drag and drop data transfer.
Definition: qdrag.h:61
int width() const
Returns the width of the pixmap.
Definition: qpixmap.cpp:630
bool isWindow() const
Returns true if the widget is an independent window, otherwise returns false.
Definition: qwidget.h:945
QDrag * object
Definition: qdnd_p.h:238
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static const int default_pm_hoty
Definition: qdnd_qws.cpp:60
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void setDropAction(Qt::DropAction action)
Sets the action to be performed on the data by the target.
Definition: qevent.cpp:2746
static bool qt_qws_dnd_dragging
Definition: qdnd_qws.cpp:84
QByteArray data(const QString &mimetype) const
Returns the data stored in the object in the format described by the MIME type specified by mimeType...
Definition: qmimedata.cpp:524
void cancel(bool deleteSource=true)
Definition: qdnd_mac.mm:272
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QDragMoveEvent class provides an event which is sent while a drag and drop action is in progress...
Definition: qevent.h:530
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
Qt::DropAction drag(QDrag *)
Definition: qdnd_mac.mm:530
static void changeOverrideCursor(const QCursor &)
Changes the currently active application override cursor to cursor.
static QWidget * widgetAt(const QPoint &p)
Returns the widget at global screen position point, or 0 if there is no Qt widget there...
int key() const
Returns the code of the key that was pressed or released.
Definition: qevent.h:231
void clearMask()
Removes any mask set by setMask().
Definition: qwidget.cpp:13324
static QCursor * overrideCursor()
Strips out vertical alignment flags and transforms an alignment align of Qt::AlignLeft into Qt::Align...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QEventLoop class provides a means of entering and leaving an event loop.
Definition: qeventloop.h:55
void setPixmap(QPixmap pm)
Definition: qdnd_qws.cpp:102
friend class QPixmap
Definition: qwidget.h:748
#define qApp
static QShapedPixmapWidget * qt_qws_dnd_deco
Definition: qdnd_qws.cpp:121
void updatePixmap()
Definition: qdnd_mac.mm:696
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QWidgetData * data
Definition: qwidget.h:815
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
static Qt::DropActions possible_actions
Definition: qdnd_qws.cpp:79
Q_CORE_EXPORT void qWarning(const char *,...)
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
The QDragLeaveEvent class provides an event that is sent to a widget when a drag and drop action leav...
Definition: qevent.h:577
DropAction
Definition: qnamespace.h:1597
void show()
Shows the widget and its child widgets.
QPixmap pixmap() const
Returns the pixmap used to represent the data in a drag and drop operation.
Definition: qdrag.cpp:175
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
void paintEvent(QPaintEvent *)
This event handler can be reimplemented in a subclass to receive paint events passed in event...
Definition: qdnd_qws.cpp:113
static const int default_pm_hotx
Definition: qdnd_qws.cpp:59
void setMask(const QBitmap &)
Causes only the pixels of the widget for which bitmap has a corresponding 1 bit to be visible...
Definition: qwidget.cpp:13309
bool acceptDrops
whether drop events are enabled for this widget
Definition: qwidget.h:197
void hide()
Hides the widget.
Definition: qwidget.h:501
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
static QDragManager * self()
Definition: qdnd.cpp:163
QVariant retrieveData_sys(const QString &mimeType, QVariant::Type type) const
Definition: qdnd_mac.mm:229
bool isWidgetType() const
Returns true if the object is a widget; otherwise returns false.
Definition: qobject.h:146
The QDropEvent class provides an event which is sent when a drag and drop action is completed...
Definition: qevent.h:476
Qt::MouseButtons buttons() const
Returns the button state when the event was generated.
Definition: qevent.h:102
void resize(int w, int h)
This corresponds to resize(QSize(w, h)).
Definition: qwidget.h:1014
QBitmap mask() const
Extracts a bitmap mask from the pixmap&#39;s alpha channel.
Definition: qpixmap.cpp:2077
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
static const char *const default_pm[]
Definition: qdnd_qws.cpp:61
QMimeData * data
Definition: qdnd_p.h:179
QMimeData * mimeData() const
Returns the MIME data that is encapsulated by the drag object.
Definition: qdrag.cpp:155
QDropData * dropData
Definition: qdnd_p.h:251
The QDragEnterEvent class provides an event which is sent to a widget when a drag and drop action ent...
Definition: qevent.h:555
int key
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
Definition: qnamespace.h:54
static QDrag * drag_object
Definition: qdnd_qws.cpp:83
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
#define QT_USE_NAMESPACE
This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined and nothing otherwise.
Definition: qglobal.h:88
QPoint mapFromGlobal(const QPoint &) const
Translates the global screen coordinate pos to widget coordinates.
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device...
Definition: qpainter.cpp:5619
int height() const
Returns the height of the pixmap.
Definition: qpixmap.cpp:645
void accept()
Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
Definition: qcoreevent.h:309
void move(const QPoint &)
Definition: qdnd_mac.mm:283
static void restoreOverrideCursor()
Undoes the last setOverrideCursor().
static QPixmap * defaultPm
Definition: qdnd_qws.cpp:58
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615
Qt::KeyboardModifiers modifiers() const
Returns the keyboard modifier flags that existed immediately before the event occurred.
Definition: qevent.h:79
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
void drop()
Definition: qdnd_mac.mm:287
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
const QPoint & globalPos() const
Returns the global position of the mouse cursor at the time of the event.
Definition: qevent.h:96
static Type type()
Returns the type of application (Tty , GuiClient, or GuiServer).
bool eventFilter(QObject *, QEvent *)
Filters events if this object has been installed as an event filter for the watched object...
Definition: qdnd_mac.mm:263
QDragPrivate * dragPrivate() const
Definition: qdnd_p.h:231
int exec(ProcessEventsFlags flags=AllEvents)
Enters the main event loop and waits until exit() is called.
Definition: qeventloop.cpp:181
void move(int x, int y)
This corresponds to move(QPoint(x, y)).
Definition: qwidget.h:1011
static QPoint pos()
Returns the position of the cursor (hot spot) in global screen coordinates.
Definition: qcursor_mac.mm:310
static Qt::DropAction global_accepted_action
Definition: qdnd_qws.cpp:78
QPoint hotSpot() const
Returns the position of the hot spot relative to the top-left corner of the cursor.
Definition: qdrag.cpp:199
static Qt::KeyboardModifiers oldstate
Definition: qdnd_qws.cpp:87
void timerEvent(QTimerEvent *)
This event handler can be reimplemented in a subclass to receive timer events for the object...
Definition: qdnd_mac.mm:259
Qt::CursorShape shape() const
Returns the cursor shape identifier.
Definition: qcursor.cpp:469