]> Creatis software - cpPlugins.git/blobdiff - libs/cpPipelineEditor/Canvas.cxx
...
[cpPlugins.git] / libs / cpPipelineEditor / Canvas.cxx
index 0015421e15ed281a6c328aa050e61970e857dbc0..20f4b7aa0859f1f6aa27fcbcbab4c9065d5746c0 100644 (file)
@@ -2,13 +2,6 @@
 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
 // -------------------------------------------------------------------------
 
-
-#include <iostream>
-
-
-
-
-
 #include <cpPipelineEditor/Canvas.h>
 #include <cpPipelineEditor/FilterBlock.h>
 #include <cpPipelineEditor/FunctorBlock.h>
@@ -20,7 +13,8 @@
 // -------------------------------------------------------------------------
 cpPipelineEditor::Canvas::
 Canvas( QWidget* parent )
-  : Superclass( parent )
+  : Superclass( parent ),
+    m_DraggedBlock( NULL )
 {
   this->m_Scene = new QGraphicsScene( this );
   this->setScene( this->m_Scene );
@@ -48,6 +42,7 @@ createBlock( const std::string& name )
     new_block = new FunctorBlock( NULL, this->m_Scene );
   else if( block_type == "Widgets" )
     new_block = new WidgetBlock( NULL, this->m_Scene );
+  new_block->setPos( 0, 0 );
   return( new_block );
 }
 
@@ -57,52 +52,88 @@ dragEnterEvent( QDragEnterEvent* event )
 {
   const QMimeData* mime = event->mimeData( );
   if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
+  {
+    if( this->m_DraggedBlock == NULL )
+    {
+      QTreeWidget* tree = dynamic_cast< QTreeWidget* >( event->source( ) );
+      if( tree != NULL )
+      {
+        // Process dropped data
+        QList< QTreeWidgetItem* > items = tree->selectedItems( );
+        for( QTreeWidgetItem* item : items )
+        {
+          std::string name = "";
+          QTreeWidgetItem* p = item;
+          while( p != NULL )
+          {
+            name += p->text( 0 ).toStdString( ) + "@";
+            p = p->parent( );
+
+          } // elihw
+          name.resize( name.size( ) - 1 );
+
+          // Create block
+          this->m_DraggedBlock = this->createBlock( name );
+          QPoint drop_pos = event->pos( );
+          QPointF scene_pos = this->mapToScene( drop_pos );
+          this->m_DraggedBlock->setPos( scene_pos );
+
+        } // rof
+
+      } // fi
+
+    } // fi
     event->acceptProposedAction( );
+  }
+  else
+    event->ignore( );
 }
 
 // -------------------------------------------------------------------------
 void cpPipelineEditor::Canvas::
 dragLeaveEvent( QDragLeaveEvent* event )
 {
+  if( this->m_DraggedBlock != NULL )
+  {
+    delete this->m_DraggedBlock;
+    this->m_DraggedBlock = NULL;
+
+  } // fi
+  event->accept( );
 }
 
 // -------------------------------------------------------------------------
 void cpPipelineEditor::Canvas::
 dragMoveEvent( QDragMoveEvent* event )
 {
+  const QMimeData* mime = event->mimeData( );
+  if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
+  {
+    if( this->m_DraggedBlock != NULL )
+    {
+      QPoint drop_pos = event->pos( );
+      QPointF scene_pos = this->mapToScene( drop_pos );
+      this->m_DraggedBlock->setPos( scene_pos );
+
+    } // fi
+    event->acceptProposedAction( );
+  }
+  else
+    event->ignore( );
 }
 
 // -------------------------------------------------------------------------
 void cpPipelineEditor::Canvas::
 dropEvent( QDropEvent* event )
 {
-  // Get dropped data
   const QMimeData* mime = event->mimeData( );
-  if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
-    return;
-  event->acceptProposedAction( );
-  QTreeWidget* tree = dynamic_cast< QTreeWidget* >( event->source( ) );
-  if( tree == NULL )
-    return;
-
-  // Process dropped data
-  QList< QTreeWidgetItem* > items = tree->selectedItems( );
-  for( QTreeWidgetItem* item : items )
+  if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
   {
-    std::string name = "";
-    QTreeWidgetItem* p = item;
-    while( p != NULL )
-    {
-      name += p->text( 0 ).toStdString( ) + "@";
-      p = p->parent( );
-
-    } // elihw
-    name.resize( name.size( ) - 1 );
-
-    // Create block
-    Block* b = this->createBlock( name );
-
-  } // rof
+    this->m_DraggedBlock = NULL;
+    event->acceptProposedAction( );
+  }
+  else
+    event->ignore( );
 }
 
 // eof - $RCSfile$