]> Creatis software - cpPlugins.git/blob - lib/cpPipelineEditor/Canvas.cxx
...
[cpPlugins.git] / lib / cpPipelineEditor / Canvas.cxx
1 #include "Canvas.h"
2 #include "Editor.h"
3 #include "Block.h"
4 #include "Connection.h"
5 #include "Port.h"
6
7 #include <QDragEnterEvent>
8 #include <QWheelEvent>
9 #include <QTreeWidget>
10
11 // -------------------------------------------------------------------------
12 cpPipelineEditor::Canvas::
13 Canvas( QWidget* parent )
14   : QGraphicsView( parent )
15 {
16   QGraphicsScene* scene = new QGraphicsScene( this );
17   this->setScene( scene );
18   this->setRenderHint( QPainter::Antialiasing );
19   this->setAcceptDrops( true );
20
21   this->m_Editor = new Editor( this );
22   this->m_Editor->install( scene );
23 }
24
25 // -------------------------------------------------------------------------
26 cpPipelineEditor::Canvas::
27 ~Canvas( )
28 {
29 }
30
31 // -------------------------------------------------------------------------
32 cpPipelineEditor::
33 Editor* cpPipelineEditor::Canvas::
34 editor( )
35 {
36   return( this->m_Editor );
37 }
38
39 // -------------------------------------------------------------------------
40 const cpPipelineEditor::
41 Editor* cpPipelineEditor::Canvas::
42 editor( ) const
43 {
44   return( this->m_Editor );
45 }
46
47 // -------------------------------------------------------------------------
48 void cpPipelineEditor::Canvas::
49 wheelEvent( QWheelEvent* event )
50 {
51   this->_scaleView(
52     std::pow( double( 2 ), event->delta( ) / double( 240 ) )
53     );
54 }
55
56 // -------------------------------------------------------------------------
57 void cpPipelineEditor::Canvas::
58 dragEnterEvent( QDragEnterEvent* event )
59 {
60   const QMimeData* mime = event->mimeData( );
61   if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
62     event->acceptProposedAction( );
63 }
64
65 // -------------------------------------------------------------------------
66 void cpPipelineEditor::Canvas::
67 dragLeaveEvent( QDragLeaveEvent* event )
68 {
69 }
70
71 // -------------------------------------------------------------------------
72 void cpPipelineEditor::Canvas::
73 dragMoveEvent( QDragMoveEvent* event )
74 {
75 }
76
77 // -------------------------------------------------------------------------
78 void cpPipelineEditor::Canvas::
79 dropEvent( QDropEvent* event )
80 {
81   const QMimeData* mime = event->mimeData( );
82   if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
83     return;
84
85   event->acceptProposedAction( );
86   auto tree = dynamic_cast< QTreeWidget* >( event->source( ) );
87   if( tree == NULL )
88     return;
89
90   QList< QTreeWidgetItem* > items = tree->selectedItems( );
91   for( auto iIt = items.begin( ); iIt != items.end( ); ++iIt )
92     this->m_Editor->createFilter(
93       ( *iIt )->text( 0 ).toStdString( ), this->mapToScene( event->pos( ) )
94       );
95 }
96
97 // -------------------------------------------------------------------------
98 void cpPipelineEditor::Canvas::
99 _scaleView( qreal scaleFactor )
100 {
101   qreal factor = this->transform( ).
102     scale( scaleFactor, scaleFactor ).
103     mapRect( QRectF( 0, 0, 1, 1 ) ).
104     width( );
105   if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
106     return;
107   this->scale( scaleFactor, scaleFactor );
108 }
109
110 // eof - $RCSfile$