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>
7 #include <QDragEnterEvent>
11 // -------------------------------------------------------------------------
12 cpBaseQtApplication::Canvas::
13 Canvas( QWidget* parent )
14 : QGraphicsView( parent )
16 QGraphicsScene* scene = new QGraphicsScene( this );
17 this->setScene( scene );
18 this->setRenderHint( QPainter::Antialiasing );
19 this->setAcceptDrops( true );
21 this->m_Editor = new Editor( this );
22 this->m_Editor->install( scene );
25 // -------------------------------------------------------------------------
26 cpBaseQtApplication::Canvas::
31 // -------------------------------------------------------------------------
33 Editor* cpBaseQtApplication::Canvas::
36 return( this->m_Editor );
39 // -------------------------------------------------------------------------
40 const cpBaseQtApplication::
41 Editor* cpBaseQtApplication::Canvas::
44 return( this->m_Editor );
47 // -------------------------------------------------------------------------
48 void cpBaseQtApplication::Canvas::
49 keyPressEvent( QKeyEvent* event )
51 static const int del_key = 16777223;
53 switch( event->key( ) )
57 auto _items = this->items( );
58 auto i = _items.begin( );
59 while( i != _items.end( ) )
61 if( ( *i )->isSelected( ) )
63 Block* b = dynamic_cast< Block* >( *i );
64 Connection* c = dynamic_cast< Connection* >( *i );
67 if( this->m_Editor->deleteFilter( b->namePort( ).toStdString( ) ) )
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( )
95 if( event->key( ) == del_key )
97 auto _items = this->items( );
98 auto i = _items.begin( );
99 while( i != _items.end( ) )
101 if( ( *i )->isSelected( ) )
103 Block* b = dynamic_cast< Block* >( *i );
104 Connection* c = dynamic_cast< Connection* >( *i );
107 if( this->m_Editor->deleteFilter( b->namePort( ).toStdString( ) ) )
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( )
134 // -------------------------------------------------------------------------
135 void cpBaseQtApplication::Canvas::
136 wheelEvent( QWheelEvent* event )
139 std::pow( double( 2 ), event->delta( ) / double( 240 ) )
143 // -------------------------------------------------------------------------
144 void cpBaseQtApplication::Canvas::
145 dragEnterEvent( QDragEnterEvent* event )
147 const QMimeData* mime = event->mimeData( );
148 if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
149 event->acceptProposedAction( );
152 // -------------------------------------------------------------------------
153 void cpBaseQtApplication::Canvas::
154 dragLeaveEvent( QDragLeaveEvent* event )
158 // -------------------------------------------------------------------------
159 void cpBaseQtApplication::Canvas::
160 dragMoveEvent( QDragMoveEvent* event )
164 // -------------------------------------------------------------------------
165 void cpBaseQtApplication::Canvas::
166 dropEvent( QDropEvent* event )
168 const QMimeData* mime = event->mimeData( );
169 if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
171 event->acceptProposedAction( );
172 auto tree = dynamic_cast< QTreeWidget* >( event->source( ) );
176 QPointF p = this->mapToScene( event->pos( ) );
177 QList< QTreeWidgetItem* > items = tree->selectedItems( );
178 for( auto iIt = items.begin( ); iIt != items.end( ); ++iIt )
180 auto parent = ( *iIt )->parent( );
183 std::string category = parent->text( 0 ).toStdString( );
184 std::string filter = ( *iIt )->text( 0 ).toStdString( );
185 this->m_Editor->createFilter( category, filter, p );
192 // -------------------------------------------------------------------------
193 void cpBaseQtApplication::Canvas::
194 _scaleView( qreal scaleFactor )
196 qreal factor = this->transform( ).
197 scale( scaleFactor, scaleFactor ).
198 mapRect( QRectF( 0, 0, 1, 1 ) ).
200 if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
202 this->scale( scaleFactor, scaleFactor );