1
/**************************************************************************\
2
* Copyright (c) Kongsberg Oil & Gas Technologies AS
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are
9
* Redistributions of source code must retain the above copyright notice,
10
* this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
16
* Neither the name of the copyright holder nor the names of its
17
* contributors may be used to endorse or promote products derived from
18
* this software without specific prior written permission.
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
\**************************************************************************/
34
\class SIM::Coin3D::Quarter::DragDropHandler DragDropHandler.h Quarter/devices/DragDropHandler.h
36
\brief The DragDropHandler event filter provides drag and drop
37
functionality to the QuarterWidget.
40
#include <QDragEnterEvent>
46
#include <Inventor/SoInput.h>
47
#include <Inventor/nodes/SoSeparator.h>
49
#include "QuarterWidget.h"
50
#include "eventhandlers/DragDropHandler.h"
53
namespace SIM { namespace Coin3D { namespace Quarter {
55
class DragDropHandlerP {
57
explicit DragDropHandlerP(DragDropHandler * master) {
58
this->master = master;
60
void dragEnterEvent(QDragEnterEvent * event);
61
void dropEvent(QDropEvent * event);
64
DragDropHandler * master;
65
QuarterWidget * quarterwidget;
70
#define PRIVATE(obj) obj->pimpl
71
#define PUBLIC(obj) obj->master
73
using namespace SIM::Coin3D::Quarter;
78
\sa QObject::QObject(QObject *)
80
DragDropHandler::DragDropHandler(QuarterWidget * parent)
83
PRIVATE(this) = new DragDropHandlerP(this);
84
PRIVATE(this)->quarterwidget = parent;
85
assert(PRIVATE(this)->quarterwidget);
86
PRIVATE(this)->suffixes << "iv" << "wrl";
89
DragDropHandler::~DragDropHandler()
95
Detects a QDragEnterEvent and if the event is the dropping of a
96
valid Inventor or VRML file it opens the file, reads in the scene graph
97
and calls setSceneGraph on the QuarterWidget
100
DragDropHandler::eventFilter(QObject *, QEvent * event)
102
switch (event->type()) {
103
case QEvent::DragEnter:
104
PRIVATE(this)->dragEnterEvent(dynamic_cast<QDragEnterEvent *>(event));
107
PRIVATE(this)->dropEvent(dynamic_cast<QDropEvent *>(event));
115
DragDropHandlerP::dragEnterEvent(QDragEnterEvent * event)
117
const QMimeData * mimedata = event->mimeData();
118
if (!mimedata->hasUrls() && !mimedata->hasText())
121
if (mimedata->hasUrls()) {
122
QFileInfo fileinfo(mimedata->urls().constFirst().path());
123
QString suffix = fileinfo.suffix().toLower();
124
if (!this->suffixes.contains(suffix)) {
129
event->acceptProposedAction();
133
DragDropHandlerP::dropEvent(QDropEvent * event)
135
const QMimeData * mimedata = event->mimeData();
141
if (mimedata->hasUrls()) {
142
QUrl url = mimedata->urls().constFirst();
143
if (url.scheme().isEmpty() || url.scheme().toLower() == QString("file") ) {
144
// attempt to open file
145
if (!in.openFile(url.toLocalFile().toLatin1().constData()))
148
} else if (mimedata->hasText()) {
149
/* FIXME 2007-11-09 preng: dropping text buffer does not work on Windows Vista. */
150
bytes = mimedata->text().toUtf8();
151
in.setBuffer((void *) bytes.constData(), bytes.size());
152
if (!in.isValidBuffer())
156
// attempt to import it
157
root = SoDB::readAll(&in);
161
// set new scene graph
162
this->quarterwidget->setSceneGraph(root);
163
this->quarterwidget->viewport()->update();