]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/Canvas.cxx
85e7bfb1a9bf7bc592bdb0dd947b9a494836fc3e
[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 }
95
96 // -------------------------------------------------------------------------
97 void cpBaseQtApplication::Canvas::
98 wheelEvent( QWheelEvent* event )
99 {
100   this->_scaleView(
101     std::pow( double( 2 ), event->delta( ) / double( 240 ) )
102     );
103 }
104
105 // -------------------------------------------------------------------------
106 void cpBaseQtApplication::Canvas::
107 dragEnterEvent( QDragEnterEvent* event )
108 {
109   const QMimeData* mime = event->mimeData( );
110   if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
111     event->acceptProposedAction( );
112 }
113
114 // -------------------------------------------------------------------------
115 void cpBaseQtApplication::Canvas::
116 dragLeaveEvent( QDragLeaveEvent* event )
117 {
118 }
119
120 // -------------------------------------------------------------------------
121 void cpBaseQtApplication::Canvas::
122 dragMoveEvent( QDragMoveEvent* event )
123 {
124 }
125
126 // -------------------------------------------------------------------------
127 void cpBaseQtApplication::Canvas::
128 dropEvent( QDropEvent* event )
129 {
130   const QMimeData* mime = event->mimeData( );
131   if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
132     return;
133   event->acceptProposedAction( );
134   auto tree = dynamic_cast< QTreeWidget* >( event->source( ) );
135   if( tree == NULL )
136     return;
137
138   QPointF p = this->mapToScene( event->pos( ) );
139   QList< QTreeWidgetItem* > items = tree->selectedItems( );
140   for( auto iIt = items.begin( ); iIt != items.end( ); ++iIt )
141   {
142     auto parent = ( *iIt )->parent( );
143     if( parent != NULL )
144     {
145       std::string category = parent->text( 0 ).toStdString( );
146       std::string filter = ( *iIt )->text( 0 ).toStdString( );
147       this->m_Editor->createFilter( category, filter, p );
148
149     } // fi
150
151   } // rof
152 }
153
154 // -------------------------------------------------------------------------
155 void cpBaseQtApplication::Canvas::
156 _scaleView( qreal scaleFactor )
157 {
158   qreal factor = this->transform( ).
159     scale( scaleFactor, scaleFactor ).
160     mapRect( QRectF( 0, 0, 1, 1 ) ).
161     width( );
162   if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
163     return;
164   this->scale( scaleFactor, scaleFactor );
165 }
166
167 // eof - $RCSfile$