Salome HOME
Copyright update 2022
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewSketcher.cxx
old mode 100755 (executable)
new mode 100644 (file)
index a6e8d8e..b7687b3
@@ -1,17 +1,17 @@
-// Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-// 
+// Copyright (C) 2007-2022  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,10 @@ void OCCViewer_RectSketcher::onActivate()
 
 void OCCViewer_RectSketcher::onDeactivate()
 {
-  delete mypData;
+  delete (QRect*)mypData;
   mypData = 0;
+  mypRectRB->clearGeometry();
+  mypRectRB->hide();
 }
 
 bool OCCViewer_RectSketcher::onKey( QKeyEvent* e )
@@ -238,36 +256,49 @@ void OCCViewer_RectSketcher::onMouse( QMouseEvent* e )
   else
     avp->setCursor( Qt::ForbiddenCursor );
 
-  if ( e->type() == QEvent::MouseButtonRelease && e->button() == sketchButton() )
+  if ( e->type() == QEvent::MouseButtonRelease && (int)e->button() == sketchButton() ) // todo Qt::MouseButton is unsigned int: comparison of int with uint
   {
     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 )
   {
-    QApplication::syncX();  /* force rectangle redrawing */
     mypViewWindow->activateSketching( OCCViewer_ViewWindow::NoSketching );
   }
 }
@@ -281,24 +312,31 @@ OCCViewer_PolygonSketcher::OCCViewer_PolygonSketcher( OCCViewer_ViewWindow* vw,
 : OCCViewer_ViewSketcher( vw, typ ),
   myDbl           ( false ),
   myToler         ( 5, 5 ),
-  mypPoints        ( 0L ),
+  //mypPoints        ( 0L ),
   myAddButton     ( 0 ),
-  myDelButton     ( 0 )
+  myDelButton     ( 0 ),
+  myMode          ( Poligone )
 {
-  mySketchButton = Qt::RightButton;
+  mySketchButton = Qt::LeftButton;
+  if ( vw )
+  {
+    OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
+    mypPolyRB = new QtxPolyRubberBand( avp );
+    mypCircleRB = new QtxCircleRubberBand( avp );
+  }
+  mypData = new QPolygon( 0 );
 }
 
 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 );
+  //mypPoints = new QPolygon( 0 );
 
   switch ( sketchButton() )
   {
@@ -320,42 +358,82 @@ void OCCViewer_PolygonSketcher::onActivate()
 
 void OCCViewer_PolygonSketcher::onDeactivate()
 {
-  delete mypPoints;
-  mypPoints = 0;
-  delete mypData;
-  mypData = 0;
+  if (mypPolyRB) {
+    mypPolyRB->clearGeometry();
+    mypPolyRB->hide();
+  }
+  if (mypCircleRB) {
+    mypCircleRB->clearGeometry();
+    mypCircleRB->hide();
+  }
+  ((QPolygon*)mypData)->clear();
 }
 
-bool OCCViewer_PolygonSketcher::onKey( QKeyEvent* e )
+bool OCCViewer_PolygonSketcher::onKey(QKeyEvent* e)
 {
-  if ( e->key() == Qt::Key_Escape )
+  int aKey = e->key();
+  if (aKey == Qt::Key_Escape)
   {
     myResult = Reject;
     return true;
   }
-  else if ( e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return )
+  else if (aKey == Qt::Key_Enter || aKey == Qt::Key_Return)
   {
-    QPointArray* points = (QPointArray*)data();
-    if ( points->count() )
+    QPolygon* points = (QPolygon*)data();
+    if (points->count())
     {
-      QPoint last = points->point( points->count() - 1 );
-      if ( last != myCurr )
+      QPoint last = points->point(points->count() - 1);
+      if (last != myCurr)
       {
-        points->resize( points->count() + 1 );
-        points->setPoint( points->count() - 1, myCurr );
+        points->resize(points->count() + 1);
+        points->setPoint(points->count() - 1, myCurr);
       }
     }
     myResult = Accept;
     return true;
   }
-  else if ( e->key() == Qt::Key_Backspace && e->type() == QEvent::KeyRelease )
+  else if (aKey == Qt::Key_Backspace && e->type() == QEvent::KeyRelease)
   {
-    QPointArray* points = (QPointArray*)data();
-    if ( points->count() > 1 )
-      points->resize( points->count() - 1 );
-    onMouse( 0 );
+    QPolygon* points = (QPolygon*)data();
+    if (points->count() > 1)
+      points->resize(points->count() - 1);
+    onMouse(0);
     return true;
   }
+  else if (aKey == Qt::Key_Space && e->type() == QEvent::KeyRelease) 
+  {
+    OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
+    bool closed = false;
+    QPolygon* points = (QPolygon*)data();
+    bool valid = avp->rect().contains(myCurr);
+    if (!myStart.isNull())
+    {
+      QRect aRect(myStart.x() - myToler.width(), myStart.y() - myToler.height(),
+        2 * myToler.width(), 2 * myToler.height());
+      closed = aRect.contains(myCurr);
+    }
+    valid = valid && isValid(points, myCurr);
+    if (closed && !valid)
+      closed = false;
+    if (closed)
+      myResult = Accept;
+    else
+    {
+      if (myStart.isNull())
+        myStart = myCurr;
+      else
+      {
+        QPoint last = points->point(points->count() - 1);
+        if (last != myCurr && valid)
+        {
+          points->resize(points->count() + 1);
+          points->setPoint(points->count() - 1, myCurr);
+        }
+        if (valid && myDbl)
+          myResult = Accept;
+      }
+    }
+  }
 
   return true;
 }
@@ -364,7 +442,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 );
@@ -395,30 +473,10 @@ void OCCViewer_PolygonSketcher::onMouse( QMouseEvent* e )
 
   if ( e->type() == QEvent::MouseButtonRelease && ( e->button() & sketchButton() ) )
   {
-    myResult = Reject;
+    myResult = (closed && (points->count() > 2)) ? Accept : Reject;
     QApplication::postEvent( avp, new QMouseEvent( e->type(), e->pos(),
-                                                   e->globalPos(), e->state(), e->button() ) );
-  }
-  else if ( e->type() == QEvent::MouseButtonRelease && ( e->button() & myAddButton ) )
-  {
-    if ( closed )
-      myResult = Accept;
-    else
-    {
-      if ( myStart.isNull() )
-        myStart = myCurr;
-      else
-      {
-        QPoint last = points->point( points->count() - 1 );
-        if ( last != myCurr && valid )
-        {
-          points->resize( points->count() + 1 );
-          points->setPoint( points->count() - 1, myCurr );
-        }
-        if ( valid && myDbl )
-          myResult = Accept;
-      }
-    }
+                                                   e->globalPos(), e->button(), 
+                                                   e->buttons(), e->modifiers() ) );
   }
   else if ( ( e->type() == QEvent::MouseButtonRelease && ( e->button() & myDelButton ) ) ||
             ( e->type() == QEvent::MouseButtonDblClick && ( e->button() & myDelButton ) ) )
@@ -432,33 +490,57 @@ void OCCViewer_PolygonSketcher::onMouse( QMouseEvent* e )
 
 void OCCViewer_PolygonSketcher::onSketch( SketchState state )
 {
-  OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
-
-  QPointArray* points = (QPointArray*)data();
-  QPainter p( avp );
-  p.setPen( Qt::white );
-  p.setRasterOp( Qt::XorROP );
-  if ( state != Debut )
-    p.drawPolyline( *mypPoints );
-
-  if ( points->count() )
-  {
-    mypPoints->resize( points->count() + 1 );
-    for ( uint i = 0; i < points->count(); i++ )
-      mypPoints->setPoint( i, points->point( i ) );
-    mypPoints->setPoint( points->count(), myCurr );
-    if ( state != Fin )
-      p.drawPolyline( *mypPoints );
-  }
-
-  if ( state == Fin )
-  {
-    QApplication::syncX();
-    mypViewWindow->activateSketching( OCCViewer_ViewWindow::NoSketching );
+  QPolygon* points = (QPolygon*)data();
+  switch (myMode) {
+  case Poligone:
+    if (mypPolyRB) {
+      if (state == Fin) {
+        mypPolyRB->clearGeometry();
+        mypPolyRB->hide();
+        mypViewWindow->activateSketching(OCCViewer_ViewWindow::NoSketching);
+      }
+      else {
+        mypPolyRB->setUpdatesEnabled(false);
+        if (!mypPolyRB->isVisible())
+          mypPolyRB->show();
+
+        if (state != Fin && points->count())
+          mypPolyRB->initGeometry(QPolygon(*points) << myCurr);
+        mypPolyRB->setUpdatesEnabled(true);
+      }
+    }
+    break;
+  case Circle:
+    if (mypCircleRB) {
+      if (state == Fin) {
+        mypCircleRB->getPoligon(points);
+        mypCircleRB->clearGeometry();
+        mypCircleRB->hide();
+        if (points->size() == CIRCLE_NB_POINTS)
+          myResult = Accept;
+        mypViewWindow->activateSketching(OCCViewer_ViewWindow::NoSketching);
+      }
+      else {
+        mypCircleRB->setUpdatesEnabled(false);
+
+        if (state != Fin) {
+          if (mypCircleRB->isCenterDefined()) {
+            mypCircleRB->setRadius(myCurr);
+            if ((mypCircleRB->radius() > MIN_RADIUS) && (!mypCircleRB->isVisible()))
+              mypCircleRB->show();
+          }
+          else {
+            mypCircleRB->initGeometry(myCurr);
+          }
+        }
+        mypCircleRB->setUpdatesEnabled(true);
+      }
+    }
+    break;
   }
 }
 
-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;
@@ -472,7 +554,7 @@ bool OCCViewer_PolygonSketcher::isValid( const QPointArray* aPoints, const QPoin
     return true;
 
   bool res = true;
-  for ( uint i = 0; i < aPoints->count() - 1 && res; i++ )
+  for ( int i = 0; i < aPoints->count() - 1 && res; i++ )
   {
     const QPoint& aStart = aPoints->point( i );
     const QPoint& anEnd  = aPoints->point( i + 1 );
@@ -483,7 +565,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,23 +596,27 @@ 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;
 }
 
 
+void OCCViewer_PolygonSketcher::setSketcherMode(int theMode)
+{
+  myMode = (SketchMode)theMode;
+}