1 // Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "OCCViewer_ViewSketcher.h"
21 #include "OCCViewer_ViewWindow.h"
22 #include "OCCViewer_ViewPort3d.h"
24 #include "QtxRubberBand.h"
26 #include <QApplication>
29 #include <QMouseEvent>
32 /****************************************************************
33 ** Class: OCCViewer_ViewSketcher
35 *****************************************************************/
37 OCCViewer_ViewSketcher::OCCViewer_ViewSketcher( OCCViewer_ViewWindow* vw, int type )
39 mySketchButton( Qt::LeftButton ),
49 OCCViewer_ViewSketcher::~OCCViewer_ViewSketcher()
53 void OCCViewer_ViewSketcher::activate()
55 OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
57 mySavedCursor = avp->cursor();
58 avp->setCursor( Qt::PointingHandCursor );
59 avp->installEventFilter( this );
60 qApp->installEventFilter( this );
62 connect( avp, SIGNAL( vpDrawExternal( QPainter* ) ), this, SLOT( onDrawViewPort() ) );
70 void OCCViewer_ViewSketcher::deactivate()
72 OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
74 disconnect( avp, SIGNAL( vpDrawExternal( QPainter* ) ), this, SLOT( onDrawViewPort() ) );
76 qApp->removeEventFilter( this );
77 avp->removeEventFilter( this );
78 avp->setCursor( mySavedCursor );
83 int OCCViewer_ViewSketcher::type() const
88 void* OCCViewer_ViewSketcher::data() const
93 int OCCViewer_ViewSketcher::result() const
98 int OCCViewer_ViewSketcher::buttonState() const
100 return myButtonState;
103 bool OCCViewer_ViewSketcher::isHasShift() const
108 void OCCViewer_ViewSketcher::onActivate()
112 void OCCViewer_ViewSketcher::onDeactivate()
116 bool OCCViewer_ViewSketcher::isDefault() const
121 bool OCCViewer_ViewSketcher::eventFilter( QObject* o, QEvent* e )
123 OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
125 QMouseEvent* me = (QMouseEvent*)e;
126 SketchState state = EnTrain;
132 case QEvent::MouseMove:
133 case QEvent::MouseButtonPress:
134 case QEvent::MouseButtonRelease:
135 case QEvent::MouseButtonDblClick:
138 myButtonState = me->buttons();
139 if ( e->type() == QEvent::MouseButtonPress )
140 myButtonState |= me->button();
142 if ( myStart.isNull() && ( myButtonState & sketchButton() ) )
152 if ( myResult != Neutral )
156 myHasShift = ( me->modifiers() & Qt::ShiftModifier );
160 case QEvent::HideToParent:
168 if ( e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease )
170 ignore = onKey( (QKeyEvent*)e );
171 if ( myResult != Neutral )
180 return QObject::eventFilter( o, e );
183 void OCCViewer_ViewSketcher::onDrawViewPort()
188 bool OCCViewer_ViewSketcher::onKey( QKeyEvent* )
193 void OCCViewer_ViewSketcher::onMouse( QMouseEvent* )
197 int OCCViewer_ViewSketcher::sketchButton()
199 return mySketchButton;
202 void OCCViewer_ViewSketcher::setSketchButton( int b )
207 /****************************************************************
208 ** Class: OCCViewer_RectSketcher
210 *****************************************************************/
212 OCCViewer_RectSketcher::OCCViewer_RectSketcher( OCCViewer_ViewWindow* vw, int typ )
213 : OCCViewer_ViewSketcher( vw, typ )
217 OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
218 mypRectRB = new QtxRectRubberBand( avp );
222 OCCViewer_RectSketcher::~OCCViewer_RectSketcher()
224 delete (QRect*)mypData;
227 void OCCViewer_RectSketcher::onActivate()
229 mypData = new QRect();
232 void OCCViewer_RectSketcher::onDeactivate()
234 delete (QRect*)mypData;
236 mypRectRB->clearGeometry();
240 bool OCCViewer_RectSketcher::onKey( QKeyEvent* e )
242 if ( e->key() == Qt::Key_Escape )
244 else if ( e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return )
250 void OCCViewer_RectSketcher::onMouse( QMouseEvent* e )
252 OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
254 if ( avp->rect().contains( myCurr ) )
255 avp->setCursor( Qt::PointingHandCursor );
257 avp->setCursor( Qt::ForbiddenCursor );
259 if ( e->type() == QEvent::MouseButtonRelease && (int)e->button() == sketchButton() ) // todo Qt::MouseButton is unsigned int: comparison of int with uint
262 QApplication::postEvent( avp, new QMouseEvent( e->type(), e->pos(),
263 e->globalPos(), e->button(),
264 e->buttons(), e->modifiers() ) );
268 void OCCViewer_RectSketcher::onSketch( SketchState state )
270 //OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
274 QRect* sketchRect = (QRect*)data();
275 if ( myButtonState & sketchButton() )
277 QRect rect = QRect( myStart, myCurr ).normalized();
278 /*QRect rect( qMin( myStart.x(), myCurr.x() ), qMin( myStart.y(), myCurr.y() ),
279 qAbs( myStart.x() - myCurr.x() ), qAbs( myStart.y() - myCurr.y() ) );
281 p.setPen( Qt::white );
282 p.setCompositionMode( QPainter::CompositionMode_Xor );
285 //if ( state != Debut && !sketchRect->isEmpty() )
286 // p.drawRect( *sketchRect );
289 if ( !rect.isEmpty() && state != Fin )
291 //p.drawRect( *sketchRect );
292 mypRectRB->initGeometry( rect );
302 mypViewWindow->activateSketching( OCCViewer_ViewWindow::NoSketching );
306 /****************************************************************
307 ** Class: OCCViewer_PolygonSketcher
309 *****************************************************************/
311 OCCViewer_PolygonSketcher::OCCViewer_PolygonSketcher( OCCViewer_ViewWindow* vw, int typ )
312 : OCCViewer_ViewSketcher( vw, typ ),
320 mySketchButton = Qt::LeftButton;
323 OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
324 mypPolyRB = new QtxPolyRubberBand( avp );
325 mypCircleRB = new QtxCircleRubberBand( avp );
327 mypData = new QPolygon( 0 );
330 OCCViewer_PolygonSketcher::~OCCViewer_PolygonSketcher()
333 delete (QPolygon*)mypData;
336 void OCCViewer_PolygonSketcher::onActivate()
339 //mypPoints = new QPolygon( 0 );
341 switch ( sketchButton() )
344 myAddButton = Qt::RightButton;
345 myDelButton = Qt::MidButton;
348 myAddButton = Qt::LeftButton;
349 myDelButton = Qt::RightButton;
351 case Qt::RightButton:
353 myAddButton = Qt::LeftButton;
354 myDelButton = Qt::MidButton;
359 void OCCViewer_PolygonSketcher::onDeactivate()
362 mypPolyRB->clearGeometry();
366 mypCircleRB->clearGeometry();
369 ((QPolygon*)mypData)->clear();
372 bool OCCViewer_PolygonSketcher::onKey(QKeyEvent* e)
375 if (aKey == Qt::Key_Escape)
380 else if (aKey == Qt::Key_Enter || aKey == Qt::Key_Return)
382 QPolygon* points = (QPolygon*)data();
385 QPoint last = points->point(points->count() - 1);
388 points->resize(points->count() + 1);
389 points->setPoint(points->count() - 1, myCurr);
395 else if (aKey == Qt::Key_Backspace && e->type() == QEvent::KeyRelease)
397 QPolygon* points = (QPolygon*)data();
398 if (points->count() > 1)
399 points->resize(points->count() - 1);
403 else if (aKey == Qt::Key_Space && e->type() == QEvent::KeyRelease)
405 OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
407 QPolygon* points = (QPolygon*)data();
408 bool valid = avp->rect().contains(myCurr);
409 if (!myStart.isNull())
411 QRect aRect(myStart.x() - myToler.width(), myStart.y() - myToler.height(),
412 2 * myToler.width(), 2 * myToler.height());
413 closed = aRect.contains(myCurr);
415 valid = valid && isValid(points, myCurr);
416 if (closed && !valid)
422 if (myStart.isNull())
426 QPoint last = points->point(points->count() - 1);
427 if (last != myCurr && valid)
429 points->resize(points->count() + 1);
430 points->setPoint(points->count() - 1, myCurr);
441 void OCCViewer_PolygonSketcher::onMouse( QMouseEvent* e )
443 OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
445 QPolygon* points = (QPolygon*)data();
446 if ( !points->count() && !myStart.isNull() )
448 points->resize( points->count() + 1 );
449 points->setPoint( points->count() - 1, myStart );
453 bool valid = avp->rect().contains( myCurr );
454 if ( !myStart.isNull() )
456 QRect aRect( myStart.x() - myToler.width(), myStart.y() - myToler.height(),
457 2 * myToler.width(), 2 * myToler.height() );
458 closed = aRect.contains( myCurr );
460 valid = valid && isValid( points, myCurr );
461 if ( closed && !valid )
465 avp->setCursor( Qt::CrossCursor );
467 avp->setCursor( Qt::PointingHandCursor );
469 avp->setCursor( Qt::ForbiddenCursor );
474 if ( e->type() == QEvent::MouseButtonRelease && ( e->button() & sketchButton() ) )
476 myResult = (closed && (points->count() > 2)) ? Accept : Reject;
477 QApplication::postEvent( avp, new QMouseEvent( e->type(), e->pos(),
478 e->globalPos(), e->button(),
479 e->buttons(), e->modifiers() ) );
481 else if ( ( e->type() == QEvent::MouseButtonRelease && ( e->button() & myDelButton ) ) ||
482 ( e->type() == QEvent::MouseButtonDblClick && ( e->button() & myDelButton ) ) )
484 if ( points->count() > 1 )
485 points->resize( points->count() - 1 );
488 myDbl = e->type() == QEvent::MouseButtonDblClick && ( e->button() & myAddButton );
491 void OCCViewer_PolygonSketcher::onSketch( SketchState state )
493 QPolygon* points = (QPolygon*)data();
498 mypPolyRB->clearGeometry();
500 mypViewWindow->activateSketching(OCCViewer_ViewWindow::NoSketching);
503 mypPolyRB->setUpdatesEnabled(false);
504 if (!mypPolyRB->isVisible())
507 if (state != Fin && points->count())
508 mypPolyRB->initGeometry(QPolygon(*points) << myCurr);
509 mypPolyRB->setUpdatesEnabled(true);
516 mypCircleRB->getPoligon(points);
517 mypCircleRB->clearGeometry();
519 if (points->size() == CIRCLE_NB_POINTS)
521 mypViewWindow->activateSketching(OCCViewer_ViewWindow::NoSketching);
524 mypCircleRB->setUpdatesEnabled(false);
527 if (mypCircleRB->isCenterDefined()) {
528 mypCircleRB->setRadius(myCurr);
529 if ((mypCircleRB->radius() > MIN_RADIUS) && (!mypCircleRB->isVisible()))
533 mypCircleRB->initGeometry(myCurr);
536 mypCircleRB->setUpdatesEnabled(true);
543 bool OCCViewer_PolygonSketcher::isValid( const QPolygon* aPoints, const QPoint& aCur ) const
545 if ( !aPoints->count() )
548 if ( aPoints->count() == 1 && aPoints->point( 0 ) == aCur )
551 const QPoint& aLast = aPoints->point( aPoints->count() - 1 );
557 for ( int i = 0; i < aPoints->count() - 1 && res; i++ )
559 const QPoint& aStart = aPoints->point( i );
560 const QPoint& anEnd = aPoints->point( i + 1 );
561 res = !isIntersect( aStart, anEnd, aCur, aLast );
567 bool OCCViewer_PolygonSketcher::isIntersect( const QPoint& aStart1, const QPoint& anEnd1,
568 const QPoint& aStart2, const QPoint& anEnd2 ) const
570 if ( ( aStart1 == aStart2 && anEnd1 == anEnd2 ) ||
571 ( aStart1 == anEnd2 && anEnd1 == aStart2 ) )
574 if ( aStart1 == aStart2 || aStart2 == anEnd1 ||
575 aStart1 == anEnd2 || anEnd1 == anEnd2 )
578 double x11 = aStart1.x() * 1.0;
579 double x12 = anEnd1.x() * 1.0;
580 double y11 = aStart1.y() * 1.0;
581 double y12 = anEnd1.y() * 1.0;
583 double x21 = aStart2.x() * 1.0;
584 double x22 = anEnd2.x() * 1.0;
585 double y21 = aStart2.y() * 1.0;
586 double y22 = anEnd2.y() * 1.0;
588 double k1 = x12 == x11 ? 0 : ( y12 - y11 ) / ( x12 - x11 );
589 double k2 = x22 == x21 ? 0 : ( y22 - y21 ) / ( x22 - x21 );
591 double b1 = y11 - k1 * x11;
592 double b2 = y21 - k2 * x21;
599 return !( ( qMax( x11, x12 ) <= qMin( x21, x22 ) ||
600 qMin( x11, x12 ) >= qMax( x21, x22 ) ) &&
601 ( qMax( y11, y12 ) <= qMin( y21, y22 ) ||
602 qMin( y11, y12 ) >= qMax( y21, y22 ) ) );
606 double x0 = ( b2 - b1 ) / ( k1 - k2 );
607 double y0 = ( k1 * b2 - k2 * b1 ) / ( k1 - k2 );
609 if ( qMin( x11, x12 ) < x0 && x0 < qMax( x11, x12 ) &&
610 qMin( y11, y12 ) < y0 && y0 < qMax( y11, y12 ) &&
611 qMin( x21, x22 ) < x0 && x0 < qMax( x21, x22 ) &&
612 qMin( y21, y22 ) < y0 && y0 < qMax( y21, y22 ) )
619 void OCCViewer_PolygonSketcher::setSketcherMode(int theMode)
621 myMode = (SketchMode)theMode;