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( )
96 // -------------------------------------------------------------------------
97 void cpBaseQtApplication::Canvas::
98 wheelEvent( QWheelEvent* event )
101 std::pow( double( 2 ), event->delta( ) / double( 240 ) )
105 // -------------------------------------------------------------------------
106 void cpBaseQtApplication::Canvas::
107 dragEnterEvent( QDragEnterEvent* event )
109 const QMimeData* mime = event->mimeData( );
110 if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
111 event->acceptProposedAction( );
114 // -------------------------------------------------------------------------
115 void cpBaseQtApplication::Canvas::
116 dragLeaveEvent( QDragLeaveEvent* event )
120 // -------------------------------------------------------------------------
121 void cpBaseQtApplication::Canvas::
122 dragMoveEvent( QDragMoveEvent* event )
126 // -------------------------------------------------------------------------
127 void cpBaseQtApplication::Canvas::
128 dropEvent( QDropEvent* event )
130 const QMimeData* mime = event->mimeData( );
131 if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
133 event->acceptProposedAction( );
134 auto tree = dynamic_cast< QTreeWidget* >( event->source( ) );
138 QPointF p = this->mapToScene( event->pos( ) );
139 QList< QTreeWidgetItem* > items = tree->selectedItems( );
140 for( auto iIt = items.begin( ); iIt != items.end( ); ++iIt )
142 auto parent = ( *iIt )->parent( );
145 std::string category = parent->text( 0 ).toStdString( );
146 std::string filter = ( *iIt )->text( 0 ).toStdString( );
147 this->m_Editor->createFilter( category, filter, p );
154 // -------------------------------------------------------------------------
155 void cpBaseQtApplication::Canvas::
156 _scaleView( qreal scaleFactor )
158 qreal factor = this->transform( ).
159 scale( scaleFactor, scaleFactor ).
160 mapRect( QRectF( 0, 0, 1, 1 ) ).
162 if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
164 this->scale( scaleFactor, scaleFactor );