]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/Canvas.cxx
...
[cpPlugins.git] / lib / cpBaseQtApplication / Canvas.cxx
1 #include <cpBaseQtApplication/Canvas.h>
2 #include <cpBaseQtApplication/Editor.h>
3 #include <cpBaseQtApplication/Block.h>
4 #include <cpBaseQtApplication/Connection.h>
5 #include <cpBaseQtApplication/Port.h>
6
7 #include <QDragEnterEvent>
8 #include <QWheelEvent>
9 #include <QTreeWidget>
10
11 // -------------------------------------------------------------------------
12 cpBaseQtApplication::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 cpBaseQtApplication::Canvas::
27 ~Canvas( )
28 {
29 }
30
31 // -------------------------------------------------------------------------
32 cpBaseQtApplication::
33 Editor* cpBaseQtApplication::Canvas::
34 editor( )
35 {
36   return( this->m_Editor );
37 }
38
39 // -------------------------------------------------------------------------
40 const cpBaseQtApplication::
41 Editor* cpBaseQtApplication::Canvas::
42 editor( ) const
43 {
44   return( this->m_Editor );
45 }
46
47 // -------------------------------------------------------------------------
48 void cpBaseQtApplication::Canvas::
49 keyPressEvent( QKeyEvent* event )
50 {
51   static const int del_key = 16777223;
52   if( event->key( ) == del_key )
53   {
54     auto _items = this->items( );
55     auto i = _items.begin( );
56     while( i != _items.end( ) )
57     {
58       if( ( *i )->isSelected( ) )
59       {
60         Block* b = dynamic_cast< Block* >( *i );
61         Connection* c = dynamic_cast< Connection* >( *i );
62         if( b != NULL )
63         {
64           if( this->m_Editor->deleteFilter( b->namePort( ).toStdString( ) ) )
65             delete b;
66         }
67         else if( c != NULL )
68         {
69           if(
70             this->m_Editor->deleteConnection(
71               c->port1( )->block( )->namePort( ).toStdString( ),
72               c->port2( )->block( )->namePort( ).toStdString( ),
73               c->port1( )->name( ).toStdString( ),
74               c->port2( )->name( ).toStdString( )
75               )
76             )
77             delete c;
78
79         } // fi
80         i = _items.end( );
81       }
82       else
83         i++;
84
85     } // elihw
86
87   } // fi
88 }
89
90 // -------------------------------------------------------------------------
91 void cpBaseQtApplication::Canvas::
92 wheelEvent( QWheelEvent* event )
93 {
94   this->_scaleView(
95     std::pow( double( 2 ), event->delta( ) / double( 240 ) )
96     );
97 }
98
99 // -------------------------------------------------------------------------
100 void cpBaseQtApplication::Canvas::
101 dragEnterEvent( QDragEnterEvent* event )
102 {
103   const QMimeData* mime = event->mimeData( );
104   if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
105     event->acceptProposedAction( );
106 }
107
108 // -------------------------------------------------------------------------
109 void cpBaseQtApplication::Canvas::
110 dragLeaveEvent( QDragLeaveEvent* event )
111 {
112 }
113
114 // -------------------------------------------------------------------------
115 void cpBaseQtApplication::Canvas::
116 dragMoveEvent( QDragMoveEvent* event )
117 {
118 }
119
120 // -------------------------------------------------------------------------
121 void cpBaseQtApplication::Canvas::
122 dropEvent( QDropEvent* event )
123 {
124   const QMimeData* mime = event->mimeData( );
125   if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
126     return;
127
128   event->acceptProposedAction( );
129   auto tree = dynamic_cast< QTreeWidget* >( event->source( ) );
130   if( tree == NULL )
131     return;
132
133   QList< QTreeWidgetItem* > items = tree->selectedItems( );
134   for( auto iIt = items.begin( ); iIt != items.end( ); ++iIt )
135   {
136     auto parent = ( *iIt )->parent( );
137     if( parent != NULL )
138       this->m_Editor->createFilter(
139         parent->text( 0 ).toStdString( ),
140         ( *iIt )->text( 0 ).toStdString( ),
141         this->mapToScene( event->pos( ) )
142         );
143
144   } // rof
145 }
146
147 // -------------------------------------------------------------------------
148 void cpBaseQtApplication::Canvas::
149 _scaleView( qreal scaleFactor )
150 {
151   qreal factor = this->transform( ).
152     scale( scaleFactor, scaleFactor ).
153     mapRect( QRectF( 0, 0, 1, 1 ) ).
154     width( );
155   if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
156     return;
157   this->scale( scaleFactor, scaleFactor );
158 }
159
160 // eof - $RCSfile$