dragAndDrop.cc 6.91 KB
Newer Older
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
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
34

35 36 37 38 39 40 41
/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57



//=============================================================================
//
//  CLASS CoreWidget - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================

// -------------------- mview
#include "CoreWidget.hh"

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
58
#include <OpenFlipper/common/GlobalOptions.hh>
Jan Möbius's avatar
 
Jan Möbius committed
59 60 61 62 63 64 65 66 67 68 69 70



//== IMPLEMENTATION ==========================================================

// Drag evencts view Magic ( header for drag and drop of views )
static const char VIEW_MAGIC[] = "ACG::QtWidgets::QGLViewerWidget encoded view";

//=============================================================================

void CoreWidget::startDrag ( QMouseEvent* _event )
{
71 72 73 74 75 76 77 78 79 80
  QObject* senderPointer = sender();
  int examinerId = -1;

  if ( senderPointer != 0 ) {
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets(); ++i ) {
      if ( senderPointer == examiner_widgets_[i] ) {
        examinerId = i;
        break;
      }
    }
Jan Möbius's avatar
 
Jan Möbius committed
81

82
  }
Jan Möbius's avatar
 
Jan Möbius committed
83

84
  if ( examinerId == -1 ) {
85
    emit log(LOGERR , tr("startDrag in Core called by non examiner, stopping here"));
86 87
    return;
  }
Jan Möbius's avatar
 
Jan Möbius committed
88

89
  PluginFunctions::setActiveExaminer(examinerId);
Jan Möbius's avatar
 
Jan Möbius committed
90

91 92
  // Get the correct position in the widget
  QPoint position = _event->pos();
Jan Möbius's avatar
 
Jan Möbius committed
93

94 95 96
  // -1 if no object id found for the current picking position
  // otherwise the id of the object
  int objectId = -1;
Jan Möbius's avatar
 
Jan Möbius committed
97

98 99 100 101
  // Do picking in the gl area to find an object
  unsigned int    node_idx, target_idx;
  ACG::Vec3d      hit_point;
  BaseObjectData* object;
Jan Möbius's avatar
 
Jan Möbius committed
102
  if ( PluginFunctions::scenegraphPick ( ACG::SceneGraph::PICK_ANYTHING,
103 104 105 106
                                          position,
                                          node_idx,
                                          target_idx,
                                          &hit_point ) ) {
Jan Möbius's avatar
 
Jan Möbius committed
107
    if ( PluginFunctions::getPickedObject ( node_idx, object ) )
108 109
        objectId = object->id();
  }
Jan Möbius's avatar
 
Jan Möbius committed
110

111
  if ( objectId != -1 ) {
112
    emit log(LOGERR , tr("dragEvent Picked Object"));
113
  }
Jan Möbius's avatar
 
Jan Möbius committed
114 115 116 117




118 119
  QString view;
  examiner_widgets_[PluginFunctions::activeExaminer()]->encodeView ( view );
Jan Möbius's avatar
 
Jan Möbius committed
120

Jan Möbius's avatar
 
Jan Möbius committed
121
  QDrag     * drag = new QDrag ( this );
122 123 124 125 126
  QMimeData * mime_data = new QMimeData;

  mime_data->setText ( view );
  drag->setMimeData ( mime_data );
  drag->start();
Jan Möbius's avatar
 
Jan Möbius committed
127

128
}
Jan Möbius's avatar
 
Jan Möbius committed
129

130 131 132 133 134 135 136 137 138
void CoreWidget::dragEnterEvent ( QDragEnterEvent* _event ) {

  if ( _event->mimeData()->hasFormat ( "text/plain" ) ) {
      QString view ( _event->mimeData()->text() );

      // view information entering via drag
      if ( view.left ( sizeof ( VIEW_MAGIC ) - 1 ) == QString ( VIEW_MAGIC ) ) {
        _event->acceptProposedAction();
      }
Jan Möbius's avatar
 
Jan Möbius committed
139

140
  }
Jan Möbius's avatar
 
Jan Möbius committed
141

142
}
Jan Möbius's avatar
 
Jan Möbius committed
143

144 145 146
void CoreWidget::dropEvent ( QDropEvent* _event ) {
  QObject* senderPointer = sender();
  int examinerId = -1;
Jan Möbius's avatar
 
Jan Möbius committed
147

148 149 150 151 152
  if ( senderPointer != 0 ) {
    for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets(); ++i ) {
      if ( senderPointer == examiner_widgets_[i] ) {
        examinerId = i;
        break;
Jan Möbius's avatar
 
Jan Möbius committed
153
      }
154 155 156 157
    }
  }

  if ( examinerId == -1 ) {
158
    emit log(LOGERR , tr("startDrag in Core called by non examiner, stopping here"));
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    return;
  }

  PluginFunctions::setActiveExaminer(examinerId);

  if ( _event->mimeData()->hasFormat ( "text/plain" ) ) {

    QString view ( _event->mimeData()->text() );

    // Dropped view information
    if ( view.left ( sizeof ( VIEW_MAGIC ) - 1 ) == QString ( VIEW_MAGIC ) ) {
      examiner_widgets_[PluginFunctions::activeExaminer()]->decodeView ( view );
      _event->acceptProposedAction();
      return;
    }
174 175 176 177 178 179 180 181 182
    
    // Dropped file information
    if ( view.left ( 4 ) == QString("file://") ) {
      _event->acceptProposedAction();
      emit dragOpenFile(view.remove(0,7));
      return;
    } 
    
    std::cerr << "Skipped unknown drop event : " << view.toStdString() << std::endl;
183 184

  }
Jan Möbius's avatar
 
Jan Möbius committed
185 186


187
}
Jan Möbius's avatar
 
Jan Möbius committed
188 189

//=============================================================================