customdraganddrop.cpp 10.8 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
8
9
10
11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
**
**************************************************************************/

#include "customdraganddrop.h"

#include <QtCore/QMimeData>
#include <QtCore/QPoint>
#include <QLabel>

#include <QMouseEvent>
#include <QApplication>
#include <QDebug>
#include <QPainter>

44
45
46
47
#ifdef Q_OS_WIN
#include <private/qwidget_p.h>
#endif

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
namespace QmlDesigner {

namespace QmlDesignerItemLibraryDragAndDrop {

void CustomDragAndDropIcon::startDrag()
{
    m_size = m_icon.size();
    m_iconAlpha = 1;
    m_previewAlpha = 0;

    QPoint pos = QCursor::pos();
    QWidget *widget = QApplication::topLevelAt(pos);
    setParent(widget);
    raise();
    show();

64
65
66
67
68
69
70
71
72
73
74
75
    grabMouseSafely();
}

void CustomDragAndDropIcon::grabMouseSafely()
{
#ifdef Q_OS_WIN
    // grabMouse calls SetWindowsHookEx() - this function causes a system-wide
    // freeze if any other app on the system installs a hook and fails to
    // process events.
    QWidgetPrivate *p = qt_widget_private(this);
    p->grabMouseWhileInWindow();
#else
76
    grabMouse();
77
#endif
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
}

void CustomDragAndDropIcon::mouseReleaseEvent(QMouseEvent *event)
{
     QPoint globalPos = event->globalPos();
     releaseMouse();
     move(-1000, -1000); //-1000, -1000 is used to hide because hiding causes propblems with the mouse grabber
     QWidget* target = QApplication::widgetAt(globalPos - QPoint(2,2)); //-(2, 2) because:
                                                                        // otherwise we just get this widget

     if (CustomDragAndDrop::isAccepted()) // if the target widget accepted the enter event,
                                          // then create a drop event
         CustomDragAndDrop::drop(target, globalPos);
     CustomDragAndDrop::endCustomDrag();
}

void CustomDragAndDropIcon::mouseMoveEvent(QMouseEvent *event)
{
    QPoint globalPos = event->globalPos();
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
97
    QWidget * widget = QApplication::topLevelAt(globalPos); // grap the "current" top level widget
98
    if (widget) {
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
99
        setParent(widget); // set the current top level widget as parent
100
        QPoint pos = parentWidget()->mapFromGlobal(globalPos);
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
101
        if ((pos.y() > 30 && CustomDragAndDrop::isVisible())) // do not allow dragging over the menubar
102
103
            move(pos);
        else
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
104
            move(-1000, -1000); // no hiding because of mouse grabbing
105
106
107
108
109
        resize(m_size);
        show();
        update();
    }
    else {
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
110
       move(-1000, -1000); // if no top level widget is found we are out of the main window
111
112
113
114
115
116
117
118
119
    }
    QWidget* target = QApplication::widgetAt(globalPos - QPoint(2,2)); //-(2, 2) because:
                                                                       // otherwise we just get this widget
    if (target != m_oldTarget) {
      if (CustomDragAndDrop::isAccepted())
        CustomDragAndDrop::leave(m_oldTarget, globalPos);  // create DragLeave event if drag enter
                                                           // event was accepted
      bool wasAccepted = CustomDragAndDrop::isAccepted();
      CustomDragAndDrop::enter(target, globalPos);         // create and handle the create enter event
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
120
121
      releaseMouse(); // to set the cursor we have to disable the mouse grabber
      if (CustomDragAndDrop::isAccepted()) { // setting the right cursor and trigger animation
122
123
          setCursor(Qt::CrossCursor);
          if (!wasAccepted)
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
124
              enter();                      // trigger animation if enter event was accepted
125
126
127
128
129
130
      } else {
          setCursor(Qt::ForbiddenCursor);
          if (wasAccepted)
              leave();                     // trigger animation if we leave a widget that accepted
                                           // the drag enter event
      }
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
131
      // enable the mouse grabber again - after the curser is set
132
      grabMouseSafely();
133
134
135
136
137
138
139
140
    } else {
        if (CustomDragAndDrop::isAccepted()) // create DragMoveEvents if the current widget
                                             // accepted the DragEnter event
            CustomDragAndDrop::move(target, globalPos);
    }
    m_oldTarget = target;
}

141
void CustomDragAndDropIcon::paintEvent(QPaintEvent *event)
142
{
143
144
    QWidget::paintEvent(event);
    QPainter p(this);
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    if  (CustomDragAndDrop::isAccepted()) {
        p.setOpacity(m_previewAlpha);
        p.drawPixmap(0 ,0 , m_size.width(), m_size.height(), m_preview);
        p.setOpacity(m_iconAlpha);
        p.drawPixmap(0, 0, m_size.width(), m_size.height(), m_icon);
    } else {
        p.setOpacity(m_iconAlpha);
        p.drawPixmap(0, 0, m_size.width(), m_size.height(), m_icon);
        p.setOpacity(m_previewAlpha);
        p.drawPixmap(0 ,0 , m_size.width(), m_size.height(), m_preview);
    }
}

void CustomDragAndDropIcon::enter()
{
    connect(&m_timeLine, SIGNAL( frameChanged (int)), this, SLOT(animateDrag(int)));
    m_timeLine.setFrameRange(0, 10);
162
    m_timeLine.setDuration(10);
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    m_timeLine.setLoopCount(1);
    m_timeLine.setCurveShape(QTimeLine::EaseInCurve);
    m_timeLine.start();
    m_size  = m_icon.size();
    m_iconAlpha = 1;
    m_previewAlpha = 0;
}

void CustomDragAndDropIcon::leave()
{
    connect(&m_timeLine, SIGNAL( frameChanged (int)), this, SLOT(animateDrag(int)));
    m_timeLine.setFrameRange(0, 10);
    m_timeLine.setDuration(250);
    m_timeLine.setLoopCount(1);
    m_timeLine.setCurveShape(QTimeLine::EaseInCurve);
    m_timeLine.start();
    m_size  = m_preview.size();
    m_iconAlpha = 0;
    m_previewAlpha = 1;
}

void CustomDragAndDropIcon::animateDrag(int frame)
{
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
186
    // interpolation of m_size and alpha values
187
    if (CustomDragAndDrop::isAccepted()) {
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
188
189
190
191
192
193
        // small -> big
        m_iconAlpha = 1.0 - qreal(frame) / 10.0;
        m_previewAlpha = qreal(frame) / 10.0;
        int width = qreal(m_preview.width()) * (qreal(frame) / 10.0) + qreal(m_icon.width()) * (1.0 - qreal(frame) / 10.0);
        int height = qreal(m_preview.height()) * (qreal(frame) / 10.0) + qreal(m_icon.height()) * (1 - qreal(frame) / 10.0);
        m_size = QSize(width, height);
194
    } else {
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
195
196
197
198
199
200
        // big -> small
        m_previewAlpha = 1.0 - qreal(frame) / 10.0;
        m_iconAlpha = qreal(frame) / 10.0;
        int width = qreal(m_icon.width()) * (qreal(frame) / 10.0) + qreal(m_preview.width()) * (1.0 - qreal(frame) / 10.0);
        int height = qreal(m_icon.height()) * (qreal(frame) / 10.0) + qreal(m_preview.height()) * (1 - qreal(frame) / 10.0);
        m_size = QSize(width, height);
201
202
203
204
    }
    QPoint p = pos();
    resize(m_size);
    move(p);
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
205
    update(); // redrawing needed
206
207
208
}


Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
209
210
211
212
213
214
class CustomDragAndDropGuard { // This guard destroys the singleton in its destructor
public:                   // This should avoid that a memory leak is reported
    ~CustomDragAndDropGuard() {
        if (CustomDragAndDrop::m_instance)
            delete CustomDragAndDrop::m_instance;
    }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
};


CustomDragAndDrop* CustomDragAndDrop::m_instance = 0;

CustomDragAndDrop::CustomDragAndDrop() : m_customDragActive(0), m_mimeData(0), m_accepted(false)
{
    m_widget = new CustomDragAndDropIcon(QApplication::topLevelWidgets().first());
    m_widget->move(-1000, 1000);
    m_widget->resize(32, 32);
    m_widget->show();
}


CustomDragAndDrop* CustomDragAndDrop::instance()
{
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
231
    static CustomDragAndDropGuard guard; // The destructor destroys the singleton. See above
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    if (m_instance == 0)
        m_instance = new CustomDragAndDrop();
    return m_instance;
}

void CustomDragAndDrop::endCustomDrag()
{
    instance()->m_customDragActive = false;
}

void CustomDragAndDrop::startCustomDrag(const QPixmap icon, const QPixmap preview, QMimeData * mimeData)
{
    if (instance()->m_customDragActive) {
        qWarning("CustomDragAndDrop::startCustomDrag drag is active");
        return ;
    }
    instance()->m_customDragActive = true;
    instance()-> m_accepted = false;
    show();
    instance()->m_mimeData = mimeData;
    instance()->m_widget->setIcon(icon);
    instance()->m_widget->setPreview(preview);
    instance()->m_widget->startDrag();
}

bool CustomDragAndDrop::customDragActive()
{
    return instance()->m_customDragActive;
}

bool CustomDragAndDrop::isAccepted()
{
    return instance()->m_accepted;
}

void CustomDragAndDrop::enter(QWidget *target, QPoint globalPos)
{
    if (target) {
        QPoint pos = target->mapFromGlobal(globalPos);
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
271
272
273
        QDragEnterEvent event(pos, Qt::MoveAction, instance()->m_mimeData ,Qt::RightButton, Qt::NoModifier);
        QApplication::sendEvent(target, &event);
        instance()-> m_accepted = event.isAccepted(); // if the event is accepted we enter the "accepted" state
274
275
276
277
278
279
280
    } else {
        instance()-> m_accepted = false;
    }
}

void CustomDragAndDrop::leave(QWidget *target, QPoint globalPos)
{
281
    Q_UNUSED(globalPos)
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    if (target) {
        QDragLeaveEvent event;
        QApplication::sendEvent(target, &event);
    } else {
       qWarning() <<  "CustomDragAndDrop::leave leaving invalid target";
    }
}

void CustomDragAndDrop::move(QWidget *target, QPoint globalPos)
{
    if (target) {
        QPoint pos = target->mapFromGlobal(globalPos);
        QDragMoveEvent event(pos, Qt::MoveAction, instance()->m_mimeData ,Qt::RightButton, Qt::NoModifier);
        QApplication::sendEvent(target, &event);
    } else {
        qWarning() <<  "CustomDragAndDrop::move move in invalid target";
    }
}

void CustomDragAndDrop::drop(QWidget *target, QPoint globalPos)
{
    if (target) {
        QPoint pos = target->mapFromGlobal(globalPos);
        QDropEvent event(pos, Qt::MoveAction, instance()->m_mimeData ,Qt::RightButton, Qt::NoModifier);
        QApplication::sendEvent(target, &event);
    } else {
        qWarning() <<  "CustomDragAndDrop::drop dropping in invalid target";
    }
}

312
313
314
315
316
317
318
bool CustomDragAndDrop::isAnimated()
{
    if (instance()->m_widget)
        return instance()->m_widget->isAnimated();
    return false;
}

Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
319
320
} // namespace QmlDesignerItemLibraryDragAndDrop
} // namespave QmlDesigner