]> Creatis software - cpPlugins.git/blobdiff - lib/cpPipelineEditor/Canvas.cxx
First dump for version 0.1.0
[cpPlugins.git] / lib / cpPipelineEditor / Canvas.cxx
diff --git a/lib/cpPipelineEditor/Canvas.cxx b/lib/cpPipelineEditor/Canvas.cxx
new file mode 100644 (file)
index 0000000..90f1285
--- /dev/null
@@ -0,0 +1,160 @@
+#include "Canvas.h"
+#include "Editor.h"
+#include "Block.h"
+#include "Connection.h"
+#include "Port.h"
+
+#include <QDragEnterEvent>
+#include <QWheelEvent>
+#include <QTreeWidget>
+
+// -------------------------------------------------------------------------
+cpPipelineEditor::Canvas::
+Canvas( QWidget* parent )
+  : QGraphicsView( parent )
+{
+  QGraphicsScene* scene = new QGraphicsScene( this );
+  this->setScene( scene );
+  this->setRenderHint( QPainter::Antialiasing );
+  this->setAcceptDrops( true );
+
+  this->m_Editor = new Editor( this );
+  this->m_Editor->install( scene );
+}
+
+// -------------------------------------------------------------------------
+cpPipelineEditor::Canvas::
+~Canvas( )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPipelineEditor::
+Editor* cpPipelineEditor::Canvas::
+editor( )
+{
+  return( this->m_Editor );
+}
+
+// -------------------------------------------------------------------------
+const cpPipelineEditor::
+Editor* cpPipelineEditor::Canvas::
+editor( ) const
+{
+  return( this->m_Editor );
+}
+
+// -------------------------------------------------------------------------
+void cpPipelineEditor::Canvas::
+keyPressEvent( QKeyEvent* event )
+{
+  static const int del_key = 16777223;
+  if( event->key( ) == del_key )
+  {
+    auto _items = this->items( );
+    auto i = _items.begin( );
+    while( i != _items.end( ) )
+    {
+      if( ( *i )->isSelected( ) )
+      {
+        Block* b = dynamic_cast< Block* >( *i );
+        Connection* c = dynamic_cast< Connection* >( *i );
+        if( b != NULL )
+        {
+          if( this->m_Editor->deleteFilter( b->namePort( ).toStdString( ) ) )
+            delete b;
+        }
+        else if( c != NULL )
+        {
+          if(
+            this->m_Editor->deleteConnection(
+              c->port1( )->block( )->namePort( ).toStdString( ),
+              c->port2( )->block( )->namePort( ).toStdString( ),
+              c->port1( )->name( ).toStdString( ),
+              c->port2( )->name( ).toStdString( )
+              )
+            )
+            delete c;
+
+        } // fi
+        i = _items.end( );
+      }
+      else
+        i++;
+
+    } // elihw
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+void cpPipelineEditor::Canvas::
+wheelEvent( QWheelEvent* event )
+{
+  this->_scaleView(
+    std::pow( double( 2 ), event->delta( ) / double( 240 ) )
+    );
+}
+
+// -------------------------------------------------------------------------
+void cpPipelineEditor::Canvas::
+dragEnterEvent( QDragEnterEvent* event )
+{
+  const QMimeData* mime = event->mimeData( );
+  if( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) )
+    event->acceptProposedAction( );
+}
+
+// -------------------------------------------------------------------------
+void cpPipelineEditor::Canvas::
+dragLeaveEvent( QDragLeaveEvent* event )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPipelineEditor::Canvas::
+dragMoveEvent( QDragMoveEvent* event )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPipelineEditor::Canvas::
+dropEvent( QDropEvent* event )
+{
+  const QMimeData* mime = event->mimeData( );
+  if( !( mime->hasFormat( "application/x-qabstractitemmodeldatalist" ) ) )
+    return;
+
+  event->acceptProposedAction( );
+  auto tree = dynamic_cast< QTreeWidget* >( event->source( ) );
+  if( tree == NULL )
+    return;
+
+  QList< QTreeWidgetItem* > items = tree->selectedItems( );
+  for( auto iIt = items.begin( ); iIt != items.end( ); ++iIt )
+  {
+    auto parent = ( *iIt )->parent( );
+    if( parent != NULL )
+      this->m_Editor->createFilter(
+        parent->text( 0 ).toStdString( ),
+        ( *iIt )->text( 0 ).toStdString( ),
+        this->mapToScene( event->pos( ) )
+        );
+
+  } // rof
+}
+
+// -------------------------------------------------------------------------
+void cpPipelineEditor::Canvas::
+_scaleView( qreal scaleFactor )
+{
+  qreal factor = this->transform( ).
+    scale( scaleFactor, scaleFactor ).
+    mapRect( QRectF( 0, 0, 1, 1 ) ).
+    width( );
+  if( factor < qreal( 0.07 ) || factor > qreal( 100 ) )
+    return;
+  this->scale( scaleFactor, scaleFactor );
+}
+
+// eof - $RCSfile$