Salome HOME
Copyright update 2022
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewSketcher.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 103880d..b7687b3
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// 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
@@ -234,6 +234,7 @@ void OCCViewer_RectSketcher::onDeactivate()
   delete (QRect*)mypData;
   mypData = 0;
   mypRectRB->clearGeometry();
+  mypRectRB->hide();
 }
 
 bool OCCViewer_RectSketcher::onKey( QKeyEvent* e )
@@ -255,7 +256,7 @@ 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(),
@@ -298,9 +299,6 @@ void OCCViewer_RectSketcher::onSketch( SketchState state )
 
   if ( state == Fin )
   {
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-    QApplication::syncX();  /* force rectangle redrawing */
-#endif
     mypViewWindow->activateSketching( OCCViewer_ViewWindow::NoSketching );
   }
 }
@@ -316,14 +314,17 @@ OCCViewer_PolygonSketcher::OCCViewer_PolygonSketcher( OCCViewer_ViewWindow* vw,
   myToler         ( 5, 5 ),
   //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 );
-    }
+  {
+    OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
+    mypPolyRB = new QtxPolyRubberBand( avp );
+    mypCircleRB = new QtxCircleRubberBand( avp );
+  }
+  mypData = new QPolygon( 0 );
 }
 
 OCCViewer_PolygonSketcher::~OCCViewer_PolygonSketcher()
@@ -335,7 +336,6 @@ OCCViewer_PolygonSketcher::~OCCViewer_PolygonSketcher()
 void OCCViewer_PolygonSketcher::onActivate()
 {
   myDbl = false;
-  mypData = new QPolygon( 0 );
   //mypPoints = new QPolygon( 0 );
 
   switch ( sketchButton() )
@@ -358,45 +358,82 @@ void OCCViewer_PolygonSketcher::onActivate()
 
 void OCCViewer_PolygonSketcher::onDeactivate()
 {
-  //delete mypPoints;
-  //mypPoints = 0;
-  delete (QPolygon*)mypData;
-  mypData = 0;
-
-  if ( mypPolyRB )
-    mypPolyRB->clearGeometry();  
+  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)
   {
     QPolygon* points = (QPolygon*)data();
-    if ( points->count() )
+    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)
   {
     QPolygon* points = (QPolygon*)data();
-    if ( points->count() > 1 )
-      points->resize( points->count() - 1 );
-    onMouse( 0 );
+    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;
 }
@@ -436,32 +473,11 @@ 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->button(), 
                                                    e->buttons(), e->modifiers() ) );
   }
-  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;
-      }
-    }
-  }
   else if ( ( e->type() == QEvent::MouseButtonRelease && ( e->button() & myDelButton ) ) ||
             ( e->type() == QEvent::MouseButtonDblClick && ( e->button() & myDelButton ) ) )
   {
@@ -474,53 +490,53 @@ void OCCViewer_PolygonSketcher::onMouse( QMouseEvent* e )
 
 void OCCViewer_PolygonSketcher::onSketch( SketchState state )
 {
-  //OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
-
   QPolygon* points = (QPolygon*)data();
-  /*QPainter p( avp );
-  p.setPen( Qt::white );
-  p.setCompositionMode( QPainter::CompositionMode_Xor );
-  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 ( mypPolyRB )
-    {
-      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();
-    }
-      
-  if ( state == Fin )
-  {
-    if ( mypPolyRB )
-      {
+  switch (myMode) {
+  case Poligone:
+    if (mypPolyRB) {
+      if (state == Fin) {
         mypPolyRB->clearGeometry();
         mypPolyRB->hide();
+        mypViewWindow->activateSketching(OCCViewer_ViewWindow::NoSketching);
       }
-#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 != 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;
   }
 }
 
@@ -538,7 +554,7 @@ bool OCCViewer_PolygonSketcher::isValid( const QPolygon* aPoints, const QPoint&
     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 );
@@ -600,3 +616,7 @@ bool OCCViewer_PolygonSketcher::isIntersect( const QPoint& aStart1, const QPoint
 }
 
 
+void OCCViewer_PolygonSketcher::setSketcherMode(int theMode)
+{
+  myMode = (SketchMode)theMode;
+}