summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Qt3DStudio/DragAndDrop/DropProxy.cpp
blob: a806310501d2efeffa038bc2496a084ad6f8e988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
271
272
273
274
275
276
277
278
279
280
281
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
312
313
314
315
316
317
318
319
320
321
322
323
324
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "Qt3DSCommonPrecompile.h"
#include "HotKeys.h"
#include "DropProxy.h"
#include "DropContainer.h"
#include "DropSource.h"
#include "Qt3DSFile.h"
#include "Qt3DSString.h"
#include "IDragable.h"

#include <QtGui/qevent.h>
#include <QtWidgets/qwidget.h>

//=============================================================================
/**
 * Constructor
 * @param inParent gesture listener that messages will be passed to
 */
CDropProxy::CDropProxy(CDropContainer *inParent)
{
    m_Parent = inParent;
}

//=============================================================================
/**
 * Destructor
 */
CDropProxy::~CDropProxy()
{
}

//=============================================================================
/**
 * Called when a drag begins over a window.
 */
void CDropProxy::dragEnterEvent(QDragEnterEvent *event)
{
    event->ignore();
    if (GetDragItemCount(event->mimeData())) {
        CDropContainer::TFlavorItr theItr = m_Parent->GetFlavorBegin();
        CDropContainer::TFlavorItr theEnd = m_Parent->GetFlavorEnd();

        for (; theItr != theEnd; ++theItr) {
            if (HasMainFlavor(event->mimeData(), *theItr)) {
                event->accept();
                event->setDropAction(Qt::CopyAction);
                break;
            }
        }
    }
}

//===============================================================================
/**
 * 	This will extract the Data from the OLEDropObject and convert it to a CDropSource.
 *	@param inDataObject the Win specific dropSource.
 *	@param inFlavor the Flavor we need.
 *	@param inItem the position of the data in the DataObject.
 *	@return Newly extracted or created DropSource.
 *	Note: if we created it we need to make sure we destroy this object.
 */
CDropSource *CDropProxy::GetDropSource(const QMimeData *inDataObject, long inFlavor, long inItem)
{
    const CDropSource *theDropSource = nullptr;
    switch (inFlavor) {
    case QT3DS_FLAVOR_FILE:
        // Check if we have a Drop File on our hands.
        if (inDataObject->hasUrls()) {
            // Get the number of files being dragged
            short theFileCount = inDataObject->urls().count();

            // Only allow single files to be dragged
            if (theFileCount > inItem && !inDataObject->urls().isEmpty()) {
                QString filePath = inDataObject->urls().at(inItem).toLocalFile();
                theDropSource = CDropSourceFactory::Create(QT3DS_FLAVOR_FILE, filePath);
            }
        }
        break;

    case QT3DS_FLAVOR_TEXT:
    case QT3DS_FLAVOR_BASIC_OBJECTS:
    case QT3DS_FLAVOR_ASSET_LIB:
    case QT3DS_FLAVOR_ASSET_TL:
    case QT3DS_FLAVOR_ASSET_UICFILE:
        // make an asset out of this
        // Get a pointer to the object
        theDropSource = dynamic_cast<const CDropSource *>(inDataObject);
        if (theDropSource && theDropSource->GetFlavor() != inFlavor)
            theDropSource = nullptr;
        break;
    }
    return const_cast<CDropSource *>(theDropSource);
}

//===============================================================================
/**
 * 	This is to count the number of objects in the DataObject.
 *	@param inDataObject the WinSpecific dropobject
 *	@return  The number of items found.
 */
long CDropProxy::GetDragItemCount(const QMimeData *inDataObject)
{
    long theCount = 0;

    // Check if we have a Drop File on our hands.
    if (inDataObject->hasUrls()) {
        // Get the number of files being dragged
        theCount = inDataObject->urls().count();
    } else {
        auto source = dynamic_cast<const CDropSource *>(inDataObject);
        theCount = source != nullptr ? 1 : 0;
    }

    return theCount;
}
//===============================================================================
/**
 * 	This will check the DataObject for the Flavor.
 *	@param inDataObject the Win specific data object.
 *	@param inFlavor the requested Flavor.
 *	@param true if the DataObject contains the flavor.
 */
bool CDropProxy::HasMainFlavor(const QMimeData *inDataObject, long inFlavor)
{
    if (inFlavor == QT3DS_FLAVOR_FILE)
        return inDataObject->hasUrls();
    auto source = dynamic_cast<const CDropSource *>(inDataObject);
    return source != nullptr && source->GetFlavor() == inFlavor;
}

//=============================================================================
/**
 * Called when a drag event leaves the window.  May cause the drag to end if
 * the mouse button is no longer down.
 */
void CDropProxy::dragLeaveEvent(QDragLeaveEvent *event)
{
    if (m_Parent) {
        m_Parent->OnDragLeave();
        event->accept();
    }
}

//=============================================================================
/**
 * Called when an item is being dragged over this window.  Passes the message
 * on to the gesture listener.
 */
void CDropProxy::dragMoveEvent(QDragMoveEvent *event)
{
    bool theAcceptFlag = false;

    if (m_Parent) {
        long theCount = GetDragItemCount(event->mimeData());
        for (long theIndex = 0; theIndex < theCount; ++theIndex) {
            Qt::KeyboardModifiers theModifyerFlags = ReflectMouse(event->pos().x(), event->pos().y());

            CDropContainer::TFlavorItr theItr = m_Parent->GetFlavorBegin();
            CDropContainer::TFlavorItr theEnd = m_Parent->GetFlavorEnd();

            for (; theItr != theEnd; ++theItr) {
                // Find the Flavor of this Item.
                if (HasMainFlavor(event->mimeData(), *theItr)) {
                    CDropSource *theDropSource = GetDropSource(event->mimeData(), *theItr, theIndex);
                    if (theDropSource) {
                        CPt thePoint(event->pos());
                        theModifyerFlags = CHotKeys::GetCurrentKeyModifiers();
                        theDropSource->SetCurrentPoint(thePoint);
                        theDropSource->SetCurrentFlags(theModifyerFlags);

                        theDropSource->InterpretKeyFlags(theModifyerFlags);
                        // This will be implemented in the cross platform code.
                        theAcceptFlag = m_Parent->OnDragWithin(*theDropSource);
                        event->setAccepted(theAcceptFlag);
                        if (theAcceptFlag) {
                            if (theDropSource->CanCopy())
                                event->setDropAction(Qt::CopyAction);
                            else
                                event->setDropAction(Qt::MoveAction);
                            // Breakout of the outer loop.
                            theIndex = theCount;
                        }

                        // delete the drop source if it was a file
                        if (QT3DS_FLAVOR_FILE == theDropSource->GetFlavor()) {
                            delete theDropSource;
                        }
                        break;
                    }
                }
            }
        }
    }
}

//=============================================================================
/**
 * Called when an OLE item is dropped on this window.
 * @return TRUE if the drop was valid, otherwise FALSE
 */
void CDropProxy::dropEvent(QDropEvent *event)
{
    if (m_Parent) {
        long theCount = GetDragItemCount(event->mimeData());
        for (long theIndex = 0; theIndex < theCount; ++theIndex) {
            Qt::KeyboardModifiers theModifyerFlags = ReflectMouse(event->pos().x(), event->pos().y());

            theModifyerFlags = CHotKeys::GetCurrentKeyModifiers();

            CDropContainer::TFlavorItr theItr = m_Parent->GetFlavorBegin();
            CDropContainer::TFlavorItr theEnd = m_Parent->GetFlavorEnd();

            for (; theItr != theEnd; ++theItr) {
                // Find the Flavor of this Item.
                if (HasMainFlavor(event->mimeData(), *theItr)) {
                    // This will convert all stuff into a DropSource
                    CDropSource *theDropSource = GetDropSource(event->mimeData(), *theItr, theIndex);

                    // This will be implemented in the cross platform code.
                    if (theDropSource) {
                        CPt thePoint(event->pos());
                        theDropSource->SetCurrentPoint(thePoint);
                        theDropSource->SetCurrentFlags(theModifyerFlags);

                        // Don't call the recieve if we did not have a valid droptarget to begin
                        // with.
                        if (theDropSource->GetHasValidTarget()) {
                            theDropSource->InterpretKeyFlags(theModifyerFlags);
                            // This will be implemented in the cross platform code.
                            m_Parent->OnDragReceive(*theDropSource);
                        }
                    }

                    // Don't delete the drop source here, the creator will destroy it
                    // delete theDropSource;
                }
            }
        }
    }

    event->accept();
}
//===============================================================================
/**
 *	This function is used to report back to the container where the mouse is during a drag.
 *	The container can send this to children as MouseOvers so the children can do mouseover
 *things.
 *	@param inX the X position
 * 	@param inY the Y position.
 * @return the Key modifyers.
 */
Qt::KeyboardModifiers CDropProxy::ReflectMouse(long inX, long inY)
{
    Qt::KeyboardModifiers theModifierFlags = Qt::NoModifier;

    if (m_Parent) {
        // Get the mouse stuff
        CPt theMouseLoc(inX, inY);

        // Determine which modifier keys are down so that we can pass them along

        theModifierFlags = CHotKeys::GetCurrentKeyModifiers();

        // Pass the gesture into the studio control
        m_Parent->OnReflectMouse(theMouseLoc, theModifierFlags);
    }
    return theModifierFlags;
}

void CDropProxy::Register(QWidget *widget)
{
    widget->installEventFilter(this);
    widget->setAcceptDrops(true);
}

bool CDropProxy::eventFilter(QObject *watched, QEvent *event)
{
    Q_UNUSED(watched);

    switch (event->type()) {
    case QEvent::DragEnter:
        dragEnterEvent(static_cast<QDragEnterEvent *>(event));
        return event->isAccepted();
    case QEvent::DragLeave:
        dragLeaveEvent(static_cast<QDragLeaveEvent *>(event));
        return event->isAccepted();
    case QEvent::DragMove:
        dragMoveEvent(static_cast<QDragMoveEvent *>(event));
        return event->isAccepted();
    case QEvent::Drop:
        dropEvent(static_cast<QDropEvent *>(event));
        return event->isAccepted();
    default:
        return false;
    }
}