| 1 |
/**************************************************************************\ |
| 2 |
* Copyright (c) Kongsberg Oil & Gas Technologies AS |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* Redistributions of source code must retain the above copyright notice, |
| 10 |
* this list of conditions and the following disclaimer. |
| 11 |
* |
| 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. |
| 15 |
* |
| 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. |
| 19 |
* |
| 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 |
\**************************************************************************/ |
| 32 |
|
| 33 |
/*! |
| 34 |
\class SIM::Coin3D::Quarter::DragDropHandler DragDropHandler.h Quarter/devices/DragDropHandler.h |
| 35 |
|
| 36 |
\brief The DragDropHandler event filter provides drag and drop |
| 37 |
functionality to the QuarterWidget. |
| 38 |
*/ |
| 39 |
|
| 40 |
#include <QDragEnterEvent> |
| 41 |
#include <QDropEvent> |
| 42 |
#include <QFileInfo> |
| 43 |
#include <QMimeData> |
| 44 |
#include <QStringList> |
| 45 |
#include <QUrl> |
| 46 |
#include <Inventor/SoInput.h> |
| 47 |
#include <Inventor/nodes/SoSeparator.h> |
| 48 |
|
| 49 |
#include "QuarterWidget.h" |
| 50 |
#include "eventhandlers/DragDropHandler.h" |
| 51 |
|
| 52 |
|
| 53 |
namespace SIM { namespace Coin3D { namespace Quarter { |
| 54 |
|
| 55 |
class DragDropHandlerP { |
| 56 |
public: |
| 57 |
explicit DragDropHandlerP(DragDropHandler * master) { |
| 58 |
this->master = master; |
| 59 |
} |
| 60 |
void dragEnterEvent(QDragEnterEvent * event); |
| 61 |
void dropEvent(QDropEvent * event); |
| 62 |
|
| 63 |
QStringList suffixes; |
| 64 |
DragDropHandler * master; |
| 65 |
QuarterWidget * quarterwidget; |
| 66 |
}; |
| 67 |
|
| 68 |
}}} // namespace |
| 69 |
|
| 70 |
#define PRIVATE(obj) obj->pimpl |
| 71 |
#define PUBLIC(obj) obj->master |
| 72 |
|
| 73 |
using namespace SIM::Coin3D::Quarter; |
| 74 |
|
| 75 |
/*! |
| 76 |
Constructor |
| 77 |
|
| 78 |
\sa QObject::QObject(QObject *) |
| 79 |
*/ |
| 80 |
DragDropHandler::DragDropHandler(QuarterWidget * parent) |
| 81 |
: QObject(parent) |
| 82 |
{ |
| 83 |
PRIVATE(this) = new DragDropHandlerP(this); |
| 84 |
PRIVATE(this)->quarterwidget = parent; |
| 85 |
assert(PRIVATE(this)->quarterwidget); |
| 86 |
PRIVATE(this)->suffixes << "iv" << "wrl"; |
| 87 |
} |
| 88 |
|
| 89 |
DragDropHandler::~DragDropHandler() |
| 90 |
{ |
| 91 |
delete PRIVATE(this); |
| 92 |
} |
| 93 |
|
| 94 |
/*! |
| 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 |
| 98 |
*/ |
| 99 |
bool |
| 100 |
DragDropHandler::eventFilter(QObject *, QEvent * event) |
| 101 |
{ |
| 102 |
switch (event->type()) { |
| 103 |
case QEvent::DragEnter: |
| 104 |
PRIVATE(this)->dragEnterEvent(dynamic_cast<QDragEnterEvent *>(event)); |
| 105 |
return true; |
| 106 |
case QEvent::Drop: |
| 107 |
PRIVATE(this)->dropEvent(dynamic_cast<QDropEvent *>(event)); |
| 108 |
return true; |
| 109 |
default: |
| 110 |
return false; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void |
| 115 |
DragDropHandlerP::dragEnterEvent(QDragEnterEvent * event) |
| 116 |
{ |
| 117 |
const QMimeData * mimedata = event->mimeData(); |
| 118 |
if (!mimedata->hasUrls() && !mimedata->hasText()) |
| 119 |
return; |
| 120 |
|
| 121 |
if (mimedata->hasUrls()) { |
| 122 |
QFileInfo fileinfo(mimedata->urls().constFirst().path()); |
| 123 |
QString suffix = fileinfo.suffix().toLower(); |
| 124 |
if (!this->suffixes.contains(suffix)) { |
| 125 |
return; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
event->acceptProposedAction(); |
| 130 |
} |
| 131 |
|
| 132 |
void |
| 133 |
DragDropHandlerP::dropEvent(QDropEvent * event) |
| 134 |
{ |
| 135 |
const QMimeData * mimedata = event->mimeData(); |
| 136 |
|
| 137 |
SoSeparator * root; |
| 138 |
SoInput in; |
| 139 |
QByteArray bytes; |
| 140 |
|
| 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())) |
| 146 |
return; |
| 147 |
} |
| 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()) |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
// attempt to import it |
| 157 |
root = SoDB::readAll(&in); |
| 158 |
if (!root) |
| 159 |
return; |
| 160 |
|
| 161 |
// set new scene graph |
| 162 |
this->quarterwidget->setSceneGraph(root); |
| 163 |
this->quarterwidget->viewport()->update(); |
| 164 |
} |
| 165 |
|
| 166 |
#undef PRIVATE |
| 167 |
#undef PUBLIC |