Salome HOME
Merge V9_dev branch into master
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewSketcher.cxx
index a6e8d8e30d49d5c44371013391f2c98834efd07b..16d028f1d787bfbdf1ae8cb0b3eb4e5ef95218c2 100755 (executable)
@@ -1,17 +1,17 @@
-// Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-// 
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either 
-// version 2.1 of the License.
-// 
-// This library is distributed in the hope that it will be useful 
-// but WITHOUT ANY WARRANTY; without even the implied warranty of 
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 //
-// You should have received a copy of the GNU Lesser General Public  
-// License along with this library; if not, write to the Free Software 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #include "OCCViewer_ViewWindow.h"
 #include "OCCViewer_ViewPort3d.h"
 
-#include <qapplication.h>
-#include <qpainter.h>
-#include <qpointarray.h>
+#include "QtxRubberBand.h"
+
+#include <QApplication>
+#include <QPainter>
+#include <QPolygon>
+#include <QMouseEvent>
+#include <QKeyEvent>
 
 /****************************************************************
 **  Class: OCCViewer_ViewSketcher
@@ -37,7 +41,8 @@ mypViewWindow( vw ),
 myType( type ),
 mypData( 0 ),
 myResult( Neutral ),
-myButtonState( 0 )
+myButtonState( 0 ),
+myHasShift( false )
 {
 }
 
@@ -95,6 +100,11 @@ int OCCViewer_ViewSketcher::buttonState() const
   return myButtonState;
 }
 
+bool OCCViewer_ViewSketcher::isHasShift() const
+{
+  return myHasShift;
+}
+
 void OCCViewer_ViewSketcher::onActivate()
 {
 }
@@ -112,6 +122,7 @@ bool OCCViewer_ViewSketcher::eventFilter( QObject* o, QEvent* e )
 {
   OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
 
+  QMouseEvent* me = (QMouseEvent*)e;
   SketchState state = EnTrain;
   bool ignore = false;
   if ( o == avp )
@@ -123,9 +134,8 @@ bool OCCViewer_ViewSketcher::eventFilter( QObject* o, QEvent* e )
       case QEvent::MouseButtonRelease:
       case QEvent::MouseButtonDblClick:
       {
-        QMouseEvent* me = (QMouseEvent*)e;
 
-        myButtonState = me->state();
+        myButtonState = me->buttons();
         if ( e->type() == QEvent::MouseButtonPress )
           myButtonState |= me->button();
 
@@ -143,6 +153,7 @@ bool OCCViewer_ViewSketcher::eventFilter( QObject* o, QEvent* e )
           state = Fin;
 
         ignore = true;
+        myHasShift = ( me->modifiers() & Qt::ShiftModifier );
         break;
       }
       case QEvent::Hide:
@@ -199,13 +210,18 @@ void OCCViewer_ViewSketcher::setSketchButton( int b )
 *****************************************************************/
 
 OCCViewer_RectSketcher::OCCViewer_RectSketcher( OCCViewer_ViewWindow* vw, int typ )
-: OCCViewer_ViewSketcher( vw, typ )
+  : OCCViewer_ViewSketcher( vw, typ )
 {
+  if ( vw )
+    {
+      OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
+      mypRectRB = new QtxRectRubberBand( avp );
+    }
 }
 
 OCCViewer_RectSketcher::~OCCViewer_RectSketcher()
 {
-  delete mypData;
+  delete (QRect*)mypData;
 }
 
 void OCCViewer_RectSketcher::onActivate()
@@ -215,8 +231,9 @@ void OCCViewer_RectSketcher::onActivate()
 
 void OCCViewer_RectSketcher::onDeactivate()
 {
-  delete mypData;
+  delete (QRect*)mypData;
   mypData = 0;
+  mypRectRB->clearGeometry();
 }
 
 bool OCCViewer_RectSketcher::onKey( QKeyEvent* e )
@@ -242,32 +259,48 @@ void OCCViewer_RectSketcher::onMouse( QMouseEvent* e )
   {
     myResult = Accept;
     QApplication::postEvent( avp, new QMouseEvent( e->type(), e->pos(),
-                                                   e->globalPos(), e->state(), e->button() ) );
+                                                   e->globalPos(), e->button(), 
+                                                   e->buttons(), e->modifiers() ) );
   }
 }
 
 void OCCViewer_RectSketcher::onSketch( SketchState state )
 {
-  OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
-
-  QRect* sketchRect = (QRect*)data();
-  if ( myButtonState & sketchButton() )
-  {
-    QRect rect( QMIN( myStart.x(), myCurr.x() ), QMIN( myStart.y(), myCurr.y() ),
-                QABS( myStart.x() - myCurr.x() ), QABS( myStart.y() - myCurr.y() ) );
-    QPainter p( avp );
-    p.setPen( Qt::white );
-    p.setRasterOp( Qt::XorROP );
-    if ( state != Debut && !sketchRect->isEmpty() )
-      p.drawRect( *sketchRect );
-    *sketchRect = rect;
-    if ( !rect.isEmpty() && state != Fin )
-      p.drawRect( *sketchRect );
-  }
+  //OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
+
+  if ( mypRectRB )
+    {      
+      QRect* sketchRect = (QRect*)data();
+      if ( myButtonState & sketchButton() )
+        {   
+          QRect rect = QRect( myStart, myCurr ).normalized();
+          /*QRect rect( qMin( myStart.x(), myCurr.x() ), qMin( myStart.y(), myCurr.y() ),
+                      qAbs( myStart.x() - myCurr.x() ), qAbs( myStart.y() - myCurr.y() ) );
+          QPainter p( avp );
+          p.setPen( Qt::white );
+          p.setCompositionMode( QPainter::CompositionMode_Xor );
+          */
+          
+          //if ( state != Debut && !sketchRect->isEmpty() )
+          //  p.drawRect( *sketchRect );
+
+          *sketchRect = rect;
+          if ( !rect.isEmpty() && state != Fin )
+            {
+              //p.drawRect( *sketchRect );            
+              mypRectRB->initGeometry( rect );
+              mypRectRB->show();
+            }          
+          else
+            mypRectRB->hide();
+        }
+    }
 
   if ( state == Fin )
   {
+#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
     QApplication::syncX();  /* force rectangle redrawing */
+#endif
     mypViewWindow->activateSketching( OCCViewer_ViewWindow::NoSketching );
   }
 }
@@ -281,24 +314,29 @@ OCCViewer_PolygonSketcher::OCCViewer_PolygonSketcher( OCCViewer_ViewWindow* vw,
 : OCCViewer_ViewSketcher( vw, typ ),
   myDbl           ( false ),
   myToler         ( 5, 5 ),
-  mypPoints        ( 0L ),
+  //mypPoints        ( 0L ),
   myAddButton     ( 0 ),
   myDelButton     ( 0 )
 {
   mySketchButton = Qt::RightButton;
+  if ( vw )
+    {
+      OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
+      mypPolyRB = new QtxPolyRubberBand( avp );
+    }
 }
 
 OCCViewer_PolygonSketcher::~OCCViewer_PolygonSketcher()
 {
-  delete mypPoints;
-  delete mypData;
+  //delete mypPoints;
+  delete (QPolygon*)mypData;
 }
 
 void OCCViewer_PolygonSketcher::onActivate()
 {
   myDbl = false;
-  mypData = new QPointArray( 0 );
-  mypPoints = new QPointArray( 0 );
+  mypData = new QPolygon( 0 );
+  //mypPoints = new QPolygon( 0 );
 
   switch ( sketchButton() )
   {
@@ -320,10 +358,13 @@ void OCCViewer_PolygonSketcher::onActivate()
 
 void OCCViewer_PolygonSketcher::onDeactivate()
 {
-  delete mypPoints;
-  mypPoints = 0;
-  delete mypData;
+  //delete mypPoints;
+  //mypPoints = 0;
+  delete (QPolygon*)mypData;
   mypData = 0;
+
+  if ( mypPolyRB )
+    mypPolyRB->clearGeometry();  
 }
 
 bool OCCViewer_PolygonSketcher::onKey( QKeyEvent* e )
@@ -335,7 +376,7 @@ bool OCCViewer_PolygonSketcher::onKey( QKeyEvent* e )
   }
   else if ( e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return )
   {
-    QPointArray* points = (QPointArray*)data();
+    QPolygon* points = (QPolygon*)data();
     if ( points->count() )
     {
       QPoint last = points->point( points->count() - 1 );
@@ -350,7 +391,7 @@ bool OCCViewer_PolygonSketcher::onKey( QKeyEvent* e )
   }
   else if ( e->key() == Qt::Key_Backspace && e->type() == QEvent::KeyRelease )
   {
-    QPointArray* points = (QPointArray*)data();
+    QPolygon* points = (QPolygon*)data();
     if ( points->count() > 1 )
       points->resize( points->count() - 1 );
     onMouse( 0 );
@@ -364,7 +405,7 @@ void OCCViewer_PolygonSketcher::onMouse( QMouseEvent* e )
 {
   OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
 
-  QPointArray* points = (QPointArray*)data();
+  QPolygon* points = (QPolygon*)data();
   if ( !points->count() && !myStart.isNull() )
   {
     points->resize( points->count() + 1 );
@@ -397,7 +438,8 @@ void OCCViewer_PolygonSketcher::onMouse( QMouseEvent* e )
   {
     myResult = Reject;
     QApplication::postEvent( avp, new QMouseEvent( e->type(), e->pos(),
-                                                   e->globalPos(), e->state(), e->button() ) );
+                                                   e->globalPos(), e->button(), 
+                                                   e->buttons(), e->modifiers() ) );
   }
   else if ( e->type() == QEvent::MouseButtonRelease && ( e->button() & myAddButton ) )
   {
@@ -432,12 +474,12 @@ void OCCViewer_PolygonSketcher::onMouse( QMouseEvent* e )
 
 void OCCViewer_PolygonSketcher::onSketch( SketchState state )
 {
-  OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
+  //OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
 
-  QPointArray* points = (QPointArray*)data();
-  QPainter p( avp );
+  QPolygon* points = (QPolygon*)data();
+  /*QPainter p( avp );
   p.setPen( Qt::white );
-  p.setRasterOp( Qt::XorROP );
+  p.setCompositionMode( QPainter::CompositionMode_Xor );
   if ( state != Debut )
     p.drawPolyline( *mypPoints );
 
@@ -449,16 +491,35 @@ void OCCViewer_PolygonSketcher::onSketch( SketchState state )
     mypPoints->setPoint( points->count(), myCurr );
     if ( state != Fin )
       p.drawPolyline( *mypPoints );
-  }
-
-  if ( state == Fin )
-  {
-    QApplication::syncX();
-    mypViewWindow->activateSketching( OCCViewer_ViewWindow::NoSketching );
+      }*/
+  if ( mypPolyRB ) {
+    if ( state == Fin ) {
+      mypPolyRB->clearGeometry();
+      mypPolyRB->hide();
+#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
+      QApplication::syncX();
+#endif
+      mypViewWindow->activateSketching( OCCViewer_ViewWindow::NoSketching );
+    } else {
+      mypPolyRB->setUpdatesEnabled ( false );
+      if ( !mypPolyRB->isVisible() )
+       mypPolyRB->show();
+      //if ( state != Debut )
+      //  mypPolyRB->repaint();
+      
+      if ( state != Fin && points->count() )
+       mypPolyRB->initGeometry( QPolygon(*points) << myCurr );
+      //mypPolyRB->addNode( myCurr );
+      
+      //if ( state != Fin )
+      //  mypPolyRB->repaint();
+      mypPolyRB->setUpdatesEnabled ( true );
+      //mypPolyRB->repaint();
+    }
   }
 }
 
-bool OCCViewer_PolygonSketcher::isValid( const QPointArray* aPoints, const QPoint& aCur ) const
+bool OCCViewer_PolygonSketcher::isValid( const QPolygon* aPoints, const QPoint& aCur ) const
 {
   if ( !aPoints->count() )
     return true;
@@ -483,7 +544,7 @@ bool OCCViewer_PolygonSketcher::isValid( const QPointArray* aPoints, const QPoin
 }
 
 bool OCCViewer_PolygonSketcher::isIntersect( const QPoint& aStart1, const QPoint& anEnd1,
-                                            const QPoint& aStart2, const QPoint& anEnd2 ) const
+                                             const QPoint& aStart2, const QPoint& anEnd2 ) const
 {
   if ( ( aStart1 == aStart2 && anEnd1 == anEnd2 ) ||
        ( aStart1 == anEnd2 && anEnd1 == aStart2 ) )
@@ -514,20 +575,20 @@ bool OCCViewer_PolygonSketcher::isIntersect( const QPoint& aStart1, const QPoint
     if ( b1 != b2 )
       return false;
     else
-      return !( ( QMAX( x11, x12 ) <= QMIN( x21, x22 ) ||
-                  QMIN( x11, x12 ) >= QMAX( x21, x22 ) ) &&
-                ( QMAX( y11, y12 ) <= QMIN( y21, y22 ) ||
-                  QMIN( y11, y12 ) >= QMAX( y21, y22 ) ) );
+      return !( ( qMax( x11, x12 ) <= qMin( x21, x22 ) ||
+                  qMin( x11, x12 ) >= qMax( x21, x22 ) ) &&
+                ( qMax( y11, y12 ) <= qMin( y21, y22 ) ||
+                  qMin( y11, y12 ) >= qMax( y21, y22 ) ) );
   }
   else
   {
     double x0 = ( b2 - b1 ) / ( k1 - k2 );
     double y0 = ( k1 * b2 - k2 * b1 ) / ( k1 - k2 );
 
-    if ( QMIN( x11, x12 ) < x0 && x0 < QMAX( x11, x12 ) &&
-         QMIN( y11, y12 ) < y0 && y0 < QMAX( y11, y12 ) &&
-         QMIN( x21, x22 ) < x0 && x0 < QMAX( x21, x22 ) &&
-         QMIN( y21, y22 ) < y0 && y0 < QMAX( y21, y22 ) )
+    if ( qMin( x11, x12 ) < x0 && x0 < qMax( x11, x12 ) &&
+         qMin( y11, y12 ) < y0 && y0 < qMax( y11, y12 ) &&
+         qMin( x21, x22 ) < x0 && x0 < qMax( x21, x22 ) &&
+         qMin( y21, y22 ) < y0 && y0 < qMax( y21, y22 ) )
       return true;
   }
   return false;