void CSVRender::WorldspaceWidget::keyPressEvent (QKeyEvent *event)
{
    if(event->key() == Qt::Key_Escape)
    {
        abortDrag();
    }
    else
        SceneWidget::keyPressEvent(event);
}
CSVRender::WorldspaceWidget::WorldspaceWidget (CSMDoc::Document& document, QWidget* parent)
    : SceneWidget (document.getData().getResourceSystem(), parent, 0, false)
    , mSceneElements(0)
    , mRun(0)
    , mDocument(document)
    , mInteractionMask (0)
    , mEditMode (0)
    , mLocked (false)
    , mDragMode(InteractionType_None)
    , mDragging (false)
    , mDragX(0)
    , mDragY(0)
    , mSpeedMode(false)
    , mDragFactor(0)
    , mDragWheelFactor(0)
    , mDragShiftFactor(0)
    , mToolTipPos (-1, -1)
    , mShowToolTips(false)
    , mToolTipDelay(0)
{
    setAcceptDrops(true);

    QAbstractItemModel *referenceables =
        document.getData().getTableModel (CSMWorld::UniversalId::Type_Referenceables);

    connect (referenceables, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)),
        this, SLOT (referenceableDataChanged (const QModelIndex&, const QModelIndex&)));
    connect (referenceables, SIGNAL (rowsAboutToBeRemoved (const QModelIndex&, int, int)),
        this, SLOT (referenceableAboutToBeRemoved (const QModelIndex&, int, int)));
    connect (referenceables, SIGNAL (rowsInserted (const QModelIndex&, int, int)),
        this, SLOT (referenceableAdded (const QModelIndex&, int, int)));

    QAbstractItemModel *references =
        document.getData().getTableModel (CSMWorld::UniversalId::Type_References);

    connect (references, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)),
        this, SLOT (referenceDataChanged (const QModelIndex&, const QModelIndex&)));
    connect (references, SIGNAL (rowsAboutToBeRemoved (const QModelIndex&, int, int)),
        this, SLOT (referenceAboutToBeRemoved (const QModelIndex&, int, int)));
    connect (references, SIGNAL (rowsInserted (const QModelIndex&, int, int)),
        this, SLOT (referenceAdded (const QModelIndex&, int, int)));

    QAbstractItemModel *pathgrids = document.getData().getTableModel (CSMWorld::UniversalId::Type_Pathgrids);

    connect (pathgrids, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)),
        this, SLOT (pathgridDataChanged (const QModelIndex&, const QModelIndex&)));
    connect (pathgrids, SIGNAL (rowsAboutToBeRemoved (const QModelIndex&, int, int)),
        this, SLOT (pathgridAboutToBeRemoved (const QModelIndex&, int, int)));
    connect (pathgrids, SIGNAL (rowsInserted (const QModelIndex&, int, int)),
        this, SLOT (pathgridAdded (const QModelIndex&, int, int)));

    QAbstractItemModel *debugProfiles =
        document.getData().getTableModel (CSMWorld::UniversalId::Type_DebugProfiles);

    connect (debugProfiles, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)),
        this, SLOT (debugProfileDataChanged (const QModelIndex&, const QModelIndex&)));
    connect (debugProfiles, SIGNAL (rowsAboutToBeRemoved (const QModelIndex&, int, int)),
        this, SLOT (debugProfileAboutToBeRemoved (const QModelIndex&, int, int)));

    mToolTipDelayTimer.setSingleShot (true);
    connect (&mToolTipDelayTimer, SIGNAL (timeout()), this, SLOT (showToolTip()));

    CSMPrefs::get()["3D Scene Input"].update();
    CSMPrefs::get()["Tooltips"].update();

    // Shortcuts
    CSMPrefs::Shortcut* primaryEditShortcut = new CSMPrefs::Shortcut("scene-edit-primary", "scene-speed-modifier",
            CSMPrefs::Shortcut::SM_Detach, this);
    connect(primaryEditShortcut, SIGNAL(activated(bool)), this, SLOT(primaryEdit(bool)));
    connect(primaryEditShortcut, SIGNAL(secondary(bool)), this, SLOT(speedMode(bool)));

    CSMPrefs::Shortcut* secondaryEditShortcut = new CSMPrefs::Shortcut("scene-edit-secondary", this);
    connect(secondaryEditShortcut, SIGNAL(activated(bool)), this, SLOT(secondaryEdit(bool)));

    CSMPrefs::Shortcut* primarySelectShortcut = new CSMPrefs::Shortcut("scene-select-primary", this);
    connect(primarySelectShortcut, SIGNAL(activated(bool)), this, SLOT(primarySelect(bool)));

    CSMPrefs::Shortcut* secondarySelectShortcut = new CSMPrefs::Shortcut("scene-select-secondary", this);
    connect(secondarySelectShortcut, SIGNAL(activated(bool)), this, SLOT(secondarySelect(bool)));

    CSMPrefs::Shortcut* abortShortcut = new CSMPrefs::Shortcut("scene-edit-abort", this);
    connect(abortShortcut, SIGNAL(activated()), this, SLOT(abortDrag()));
}
void FV_UnixFrameEdit::mouseDrag(UT_sint32 x, UT_sint32 y)
{
    UT_DEBUGMSG(("Mouse drag x=%d y=%d \n",x,y));
    bool bYOK = ( (y > 0) && (y < getView()->getWindowHeight()));
    if(!bYOK || (x> 0 && x < getView()->getWindowWidth()))
     {
         m_bDragOut = false;
	 _mouseDrag(x,y);
	 return;
     }
     if(FV_DragWhole != getDragWhat())
     {
         m_bDragOut = false;
	 _mouseDrag(x,y);
	 return;
     }
     if(FV_FrameEdit_DRAG_EXISTING != getFrameEditMode())
     {
         m_bDragOut = false;
	 _mouseDrag(x,y);
	 return;
     }
     if(!isImageWrapper())
     {
         m_bDragOut = false;
	 _mouseDrag(x,y);
	 return;
     }
     if(!m_bDragOut)
     {
	 const UT_ByteBuf * pBuf = NULL;
	 const char * pszData = getPNGImage(&pBuf);
	 UT_DEBUGMSG(("Got image buffer %p pszData %p \n",pBuf,pszData));
	 if(pBuf)
	 {
       //
       // write the image to a temperary file
       //
	     XAP_UnixApp * pXApp = static_cast<XAP_UnixApp *>(XAP_App::getApp());
	     pXApp->removeTmpFile();
	     char ** pszTmpName = pXApp->getTmpFile();
	     UT_UTF8String sTmpF = g_get_tmp_dir();
	     sTmpF += "/";
	     sTmpF += pszData;
	     sTmpF += ".png";
	     //
	     // Now write the contents of the buffer to a temp file.
	     //
	     FILE * fd = fopen(sTmpF.utf8_str(),"w");
	     fwrite(pBuf->getPointer(0),sizeof(UT_Byte),pBuf->getLength(),fd);
	     fclose(fd);

       //
       // OK set up the gtk drag and drop code to andle this
       //
	     XAP_Frame * pFrame = static_cast<XAP_Frame*>(getView()->getParentData());
	     XAP_UnixFrameImpl * pFrameImpl =static_cast<XAP_UnixFrameImpl *>( pFrame->getFrameImpl());
	     GtkWidget * pWindow = pFrameImpl->getTopLevelWindow();
	     GtkTargetList *target_list = gtk_target_list_new(targets, G_N_ELEMENTS(targets));
	     GdkDragContext *context = gtk_drag_begin(pWindow, target_list,
						(GdkDragAction)(GDK_ACTION_COPY ), 1, NULL);

	     gdk_drag_status(context, GDK_ACTION_COPY, 0);
	     gtk_target_list_unref(target_list);
	     *pszTmpName = g_strdup(sTmpF.utf8_str());  
	     UT_DEBUGMSG(("Created Tmp File %s XApp %s \n",sTmpF.utf8_str(),*pXApp->getTmpFile()));

	 }
	 //
	 // OK quit dragging the image and return to the previous state
	 //
	 m_bDragOut = true;
	 abortDrag();
     }
     m_bDragOut = true;
}