Salome HOME
Merge branch 'oscar/imps_2017'
[modules/gui.git] / src / GraphicsView / GraphicsView_ViewPort.cxx
index 40086a4f8c9754e8b4a0bed8a673c5db99d75643..4b3dafd5fc58e358995eb5757971ef61e8cc68c7 100644 (file)
@@ -34,6 +34,8 @@
 #include <QRectF>
 #include <QRubberBand>
 #include <QScrollBar>
+#include <QPrinter>
+#include <QPainter>
 
 #include <math.h>
 
@@ -132,6 +134,7 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent )
   myForegroundItem( 0 ),
   myGridItem( 0 ),
   myIsTransforming( false ),
+  myUnlimitedPanning( false ),
   myHighlightedObject( 0 ),
   myHighlightX( 0 ),
   myHighlightY( 0 ),
@@ -145,9 +148,11 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent )
   myIsSketchingByPath( false ),
   myIsDragging( false ),
   myIsDragPositionInitialized( false ),
+  myDraggingSelectedByLeftButton( false ),
   myIsPulling( false ),
   myPullingObject( 0 ),
-  myStoredCursor( Qt::ArrowCursor )
+  myStoredCursor( Qt::ArrowCursor ),
+  myZoomCoeff( 100 )
 {
   // scene
   myScene = new GraphicsView_Scene( this );
@@ -191,6 +196,9 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent )
                   QPainter::SmoothPixmapTransform |
                   QPainter::HighQualityAntialiasing );
 
+  myHBarPolicy = horizontalScrollBarPolicy();
+  myVBarPolicy = verticalScrollBarPolicy();
+
   connect( myScene, SIGNAL( gsKeyEvent( QKeyEvent* ) ),
            this, SLOT( onKeyEvent( QKeyEvent* ) ) );
   connect( myScene, SIGNAL( gsMouseEvent( QGraphicsSceneMouseEvent* ) ),
@@ -310,6 +318,15 @@ void GraphicsView_ViewPort::removeItem( QGraphicsItem* theItem )
   onBoundingRectChanged();
 }
 
+void GraphicsView_ViewPort::clearItems()
+{
+  myHighlightedObject = 0;
+  mySelectedObjects.clear();
+  myObjects.clear();
+  myScene->clear();
+  onBoundingRectChanged();
+}
+
 //================================================================
 // Function : getObjects
 // Purpose  : 
@@ -457,6 +474,38 @@ QImage GraphicsView_ViewPort::dumpView( bool theWholeScene,
   return anImage;
 }
 
+bool GraphicsView_ViewPort::dumpViewToFormat(const QString& fileName, const QString& format)
+{
+  if( format!="PS" && format!="EPS" )
+    return false;
+
+  QPrinter printer(QPrinter::ScreenResolution);
+#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
+  printer.setOutputFormat(QPrinter::PostScriptFormat);
+#else
+  printer.setOutputFormat(QPrinter::PdfFormat);
+#endif
+  printer.setOutputFileName(fileName);
+  QPainter painter;  
+  if (!painter.begin(&printer))
+    return false;
+
+  QRect view( 0, 0, printer.pageRect().width(), printer.paperRect().height() );
+  QRectF bounds = myScene->itemsBoundingRect();
+    
+  if( !view.isEmpty() && !bounds.isEmpty() )
+  {
+    float SCALE = 0.5;//qMin( view.width()/bounds.width(), view.height()/bounds.height() );
+    painter.setViewport( view );
+    painter.scale( SCALE, SCALE );
+  }
+  myScene->render( &painter, QRectF( view ), bounds );
+
+  if (!painter.end())
+    return false;
+  return true;
+}
+
 //================================================================
 // Function : setSceneGap
 // Purpose  : 
@@ -805,9 +854,29 @@ void GraphicsView_ViewPort::pan( double theDX, double theDY )
     myViewLabel->setAcceptMoveEvents( false );
 
   if( QScrollBar* aHBar = horizontalScrollBar() )
+  {
+    if( isUnlimitedPanning() )
+    {
+      int aNewValue = aHBar->value() - theDX;
+      if( aNewValue < aHBar->minimum() )
+        aHBar->setMinimum( aNewValue );
+      if( aNewValue > aHBar->maximum() )
+        aHBar->setMaximum( aNewValue );
+    }
     aHBar->setValue( aHBar->value() - theDX );
+  }
   if( QScrollBar* aVBar = verticalScrollBar() )
+  {
+    if( isUnlimitedPanning() )
+    {
+      int aNewValue = aVBar->value() + theDY;
+      if( aNewValue < aVBar->minimum() )
+        aVBar->setMinimum( aNewValue );
+      if( aNewValue > aVBar->maximum() )
+        aVBar->setMaximum( aNewValue );
+    }
     aVBar->setValue( aVBar->value() + theDY );
+  }
 
   if( myViewLabel )
     myViewLabel->setAcceptMoveEvents( true );
@@ -857,7 +926,7 @@ void GraphicsView_ViewPort::zoom( double theX1, double theY1, double theX2, doub
 
   // increasing of diagonal coefficients (>300) leads to a crash sometimes
   // at the values of 100 some primitives are drawn incorrectly
-  if( qMax( aM11, aM22 ) < 100 )
+  if( myZoomCoeff < 0 || qMax( aM11, aM22 ) < myZoomCoeff )
     setTransform( aTransform );
 
   myIsTransforming = false;
@@ -973,6 +1042,41 @@ void GraphicsView_ViewPort::applyTransform()
       anObject->setViewTransform( transform() );
 }
 
+//================================================================
+// Function : setZoomCoeff
+// Purpose  : 
+//================================================================
+void GraphicsView_ViewPort::setZoomCoeff( const int& theZoomCoeff )
+{
+  myZoomCoeff = theZoomCoeff;
+}
+
+//================================================================
+// Function : setUnlimitedPanning
+// Purpose  : 
+//================================================================
+void GraphicsView_ViewPort::setUnlimitedPanning( const bool& theValue )
+{
+  if ( myUnlimitedPanning == theValue )
+    return;
+
+  myUnlimitedPanning = theValue;
+
+  if( myUnlimitedPanning )
+  {
+    myHBarPolicy = horizontalScrollBarPolicy();
+    myVBarPolicy = verticalScrollBarPolicy();
+
+    setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
+    setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
+  }
+  else
+  {
+    setHorizontalScrollBarPolicy( myHBarPolicy );
+    setVerticalScrollBarPolicy( myVBarPolicy );
+  }
+}
+
 //================================================================
 // Function : currentBlock
 // Purpose  : 
@@ -1469,6 +1573,15 @@ bool GraphicsView_ViewPort::isSketching( bool* theIsPath ) const
   return myIsSketching;
 }
 
+//================================================================
+// Function : setDraggingSelectedByLeftButton
+// Purpose  : 
+//================================================================
+void GraphicsView_ViewPort::setDraggingSelectedByLeftButton( const bool& theValue )
+{
+  myDraggingSelectedByLeftButton = theValue;
+}
+
 //================================================================
 // Function : dragObjects
 // Purpose  : 
@@ -1496,8 +1609,9 @@ void GraphicsView_ViewPort::dragObjects( QGraphicsSceneMouseEvent* e )
     else
       anObjectsToMove.append( anObject );
   }
-  else if( hasInteractionFlag( DraggingByMiddleButton ) &&
-           nbSelected() && ( e->buttons() & Qt::MidButton ) )
+  else if( ( hasInteractionFlag( DraggingByMiddleButton ) && ( e->buttons() & Qt::MidButton ) ||
+             isDraggingSelectedByLeftButton() && ( e->buttons() & Qt::LeftButton ) ) &&
+           nbSelected() )
   {
     for( initSelected(); moreSelected(); nextSelected() )
       if( GraphicsView_Object* aMovingObject = selectedObject() )
@@ -1692,8 +1806,9 @@ void GraphicsView_ViewPort::onMouseEvent( QGraphicsSceneMouseEvent* e )
         if( ( getHighlightedObject() &&
               getHighlightedObject()->isMovable() &&
               !( anAccel || e->button() != Qt::LeftButton ) ) ||
-            ( hasInteractionFlag( DraggingByMiddleButton ) &&
-              nbSelected() && !anAccel && e->button() == Qt::MidButton ) )
+            ( ( hasInteractionFlag( DraggingByMiddleButton ) && e->button() == Qt::MidButton ||
+                isDraggingSelectedByLeftButton() && e->button() == Qt::LeftButton ) &&
+              nbSelected() && !anAccel ) )
         {
           myIsDragging = true;
           myStoredCursor = cursor();