]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/Canvas.cxx
c55141f17b1b35c40a545553c73e6b109096b371
[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
53   switch( event->key( ) )
54   {
55   case del_key:
56   {
57     auto _items = this->items( );
58     auto i = _items.begin( );
59     while( i != _items.end( ) )
60     {
61       if( ( *i )->isSelected( ) )
62       {
63         Block* b = dynamic_cast< Block* >( *i );
64         Connection* c = dynamic_cast< Connection* >( *i );
65         if( b != NULL )
66         {
67           if( this->m_Editor->deleteFilter( b->namePort( ).toStdString( ) ) )
68             delete b;
69         }
70         else if( c != NULL )
71         {
72           if(
73             this->m_Editor->deleteConnection(
74               c->port1( )->block( )->namePort( ).toStdString( ),
75               c->port2( )->block( )->namePort( ).toStdString( ),
76               c->port1( )->name( ).toStdString( ),
77               c->port2( )->name( ).toStdString( )
78               )
79             )
80             delete c;
81
82         } // fi
83         i = _items.end( );
84       }
85       else
86         i++;
87
88     } // elihw
89   }
90   break;
91   default:
92     break;
93   } // hctiws
94   /* TODO
95      if( event->key( ) == del_key )
96      {
97      auto _items = this->items( );
98      auto i = _items.begin( );
99      while( i != _items.end( ) )
100      {
101      if( ( *i )->isSelected( ) )
102      {
103      Block* b = dynamic_cast< Block* >( *i );
104      Connection* c = dynamic_cast< Connection* >( *i );
105      if( b != NULL )
106      {
107      if( this->m_Editor->deleteFilter( b->namePort( ).toStdString( ) ) )
108      delete b;
109      }
110      else if( c != NULL )
111      {
112      if(
113      this->m_Editor->deleteConnection(
114      c->port1( )->block( )->namePort( ).toStdString( ),
115      c->port2( )->block( )->namePort( ).toStdString( ),
116      c->port1( )->name( ).toStdString( ),
117      c->port2( )->name( ).toStdString( )
118      )
119      )
120      delete c;
121
122      } // fi
123      i = _items.end( );
124      }
125      else
126      i++;
127
128      } // elihw
129
130      } // fi
131   */
132 }
133
134 // -------------------------------------------------------------------------
135 void cpBaseQtApplication::Canvas::
136 wheelEvent( QWheelEvent* event )
137 {
138   this->_scaleView(
139     std::pow( double( 2 ), event->delta( ) / double( 240 ) )
140     );
141 }
142
143 // -------------------------------------------------------------------------
144 void cpBaseQtApplication::Canvas::
145 dragEnterEvent( QDragEnterEvent* event )
146 {
147   const QMimeData* mime = event->mimeData( );
148   if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
149     event->acceptProposedAction( );
150 }
151
152 // -------------------------------------------------------------------------
153 void cpBaseQtApplication::Canvas::
154 dragLeaveEvent( QDragLeaveEvent* event )
155 {
156 }
157
158 // -------------------------------------------------------------------------
159 void cpBaseQtApplication::Canvas::
160 dragMoveEvent( QDragMoveEvent* event )
161 {
162 }
163
164 // -------------------------------------------------------------------------
165 void cpBaseQtApplication::Canvas::
166 dropEvent( QDropEvent* event )
167 {
168   const QMimeData* mime = event->mimeData( );
169   if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
170     return;
171   event->acceptProposedAction( );
172   auto tree = dynamic_cast< QTreeWidget* >( event->source( ) );
173   if( tree == NULL )
174     return;
175
176   QPointF p = this->mapToScene( event->pos( ) );
177   QList< QTreeWidgetItem* > items = tree->selectedItems( );
178   for( auto iIt = items.begin( ); iIt != items.end( ); ++iIt )
179   {
180     auto parent = ( *iIt )->parent( );
181     if( parent != NULL )
182     {
183       std::string category = parent->text( 0 ).toStdString( );
184       std::string filter = ( *iIt )->text( 0 ).toStdString( );
185       this->m_Editor->createFilter( category, filter, p );
186
187     } // fi
188
189   } // rof
190 }
191
192 // -------------------------------------------------------------------------
193 void cpBaseQtApplication::Canvas::
194 _scaleView( qreal scaleFactor )
195 {
196   qreal factor = this->transform( ).
197     scale( scaleFactor, scaleFactor ).
198     mapRect( QRectF( 0, 0, 1, 1 ) ).
199     width( );
200   if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
201     return;
202   this->scale( scaleFactor, scaleFactor );
203 }
204
205 // eof - $RCSfile$