]> Creatis software - cpPlugins.git/blob - libs/cpPipelineEditor/Canvas.cxx
...
[cpPlugins.git] / libs / cpPipelineEditor / Canvas.cxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5
6 #include <iostream>
7
8
9
10
11
12 #include <cpPipelineEditor/Canvas.h>
13 #include <cpPipelineEditor/FilterBlock.h>
14 #include <cpPipelineEditor/FunctorBlock.h>
15 #include <cpPipelineEditor/WidgetBlock.h>
16
17 #include <QDragEnterEvent>
18 #include <QTreeWidget>
19
20 // -------------------------------------------------------------------------
21 cpPipelineEditor::Canvas::
22 Canvas( QWidget* parent )
23   : Superclass( parent )
24 {
25   this->m_Scene = new QGraphicsScene( this );
26   this->setScene( this->m_Scene );
27   this->setRenderHint( QPainter::Antialiasing );
28   this->setAcceptDrops( true );
29 }
30
31 // -------------------------------------------------------------------------
32 cpPipelineEditor::Canvas::
33 ~Canvas( )
34 {
35 }
36
37 // -------------------------------------------------------------------------
38 cpPipelineEditor::Block* cpPipelineEditor::Canvas::
39 createBlock( const std::string& name )
40 {
41   QString str = name.c_str( );
42   QStringList tokens = str.split( "@" );
43   std::string block_type = tokens.back( ).toStdString( );
44   Block* new_block = NULL;
45   if( block_type == "Filters" )
46     new_block = new FilterBlock( NULL, this->m_Scene );
47   else if( block_type == "Functors" )
48     new_block = new FunctorBlock( NULL, this->m_Scene );
49   else if( block_type == "Widgets" )
50     new_block = new WidgetBlock( NULL, this->m_Scene );
51   return( new_block );
52 }
53
54 // -------------------------------------------------------------------------
55 void cpPipelineEditor::Canvas::
56 dragEnterEvent( QDragEnterEvent* event )
57 {
58   const QMimeData* mime = event->mimeData( );
59   if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
60     event->acceptProposedAction( );
61 }
62
63 // -------------------------------------------------------------------------
64 void cpPipelineEditor::Canvas::
65 dragLeaveEvent( QDragLeaveEvent* event )
66 {
67 }
68
69 // -------------------------------------------------------------------------
70 void cpPipelineEditor::Canvas::
71 dragMoveEvent( QDragMoveEvent* event )
72 {
73 }
74
75 // -------------------------------------------------------------------------
76 void cpPipelineEditor::Canvas::
77 dropEvent( QDropEvent* event )
78 {
79   // Get dropped data
80   const QMimeData* mime = event->mimeData( );
81   if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
82     return;
83   event->acceptProposedAction( );
84   QTreeWidget* tree = dynamic_cast< QTreeWidget* >( event->source( ) );
85   if( tree == NULL )
86     return;
87
88   // Process dropped data
89   QList< QTreeWidgetItem* > items = tree->selectedItems( );
90   for( QTreeWidgetItem* item : items )
91   {
92     std::string name = "";
93     QTreeWidgetItem* p = item;
94     while( p != NULL )
95     {
96       name += p->text( 0 ).toStdString( ) + "@";
97       p = p->parent( );
98
99     } // elihw
100     name.resize( name.size( ) - 1 );
101
102     // Create block
103     Block* b = this->createBlock( name );
104
105   } // rof
106 }
107
108 // eof - $RCSfile$