1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, 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();
239 bool OCCViewer_RectSketcher::onKey( QKeyEvent* e )
241 if ( e->key() == Qt::Key_Escape )
243 else if ( e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return )
249 void OCCViewer_RectSketcher::onMouse( QMouseEvent* e )
251 OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
253 if ( avp->rect().contains( myCurr ) )
254 avp->setCursor( Qt::PointingHandCursor );
256 avp->setCursor( Qt::ForbiddenCursor );
258 if ( e->type() == QEvent::MouseButtonRelease && e->button() == sketchButton() )
261 QApplication::postEvent( avp, new QMouseEvent( e->type(), e->pos(),
262 e->globalPos(), e->button(),
263 e->buttons(), e->modifiers() ) );
267 void OCCViewer_RectSketcher::onSketch( SketchState state )
269 //OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
273 QRect* sketchRect = (QRect*)data();
274 if ( myButtonState & sketchButton() )
276 QRect rect = QRect( myStart, myCurr ).normalized();
277 /*QRect rect( qMin( myStart.x(), myCurr.x() ), qMin( myStart.y(), myCurr.y() ),
278 qAbs( myStart.x() - myCurr.x() ), qAbs( myStart.y() - myCurr.y() ) );
280 p.setPen( Qt::white );
281 p.setCompositionMode( QPainter::CompositionMode_Xor );
284 //if ( state != Debut && !sketchRect->isEmpty() )
285 // p.drawRect( *sketchRect );
288 if ( !rect.isEmpty() && state != Fin )
290 //p.drawRect( *sketchRect );
291 mypRectRB->initGeometry( rect );
301 mypViewWindow->activateSketching( OCCViewer_ViewWindow::NoSketching );
305 /****************************************************************
306 ** Class: OCCViewer_PolygonSketcher
308 *****************************************************************/
310 OCCViewer_PolygonSketcher::OCCViewer_PolygonSketcher( OCCViewer_ViewWindow* vw, int typ )
311 : OCCViewer_ViewSketcher( vw, typ ),
319 mySketchButton = Qt::RightButton;
322 OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
323 mypPolyRB = new QtxPolyRubberBand( avp );
324 mypCircleRB = new QtxCircleRubberBand( avp );
326 mypData = new QPolygon( 0 );
329 OCCViewer_PolygonSketcher::~OCCViewer_PolygonSketcher()
332 delete (QPolygon*)mypData;
335 void OCCViewer_PolygonSketcher::onActivate()
338 //mypPoints = new QPolygon( 0 );
340 switch ( sketchButton() )
343 myAddButton = Qt::RightButton;
344 myDelButton = Qt::MidButton;
347 myAddButton = Qt::LeftButton;
348 myDelButton = Qt::RightButton;
350 case Qt::RightButton:
352 myAddButton = Qt::LeftButton;
353 myDelButton = Qt::MidButton;
358 void OCCViewer_PolygonSketcher::onDeactivate()
361 mypPolyRB->clearGeometry();
363 mypCircleRB->clearGeometry();
364 ((QPolygon*)mypData)->clear();
367 bool OCCViewer_PolygonSketcher::onKey( QKeyEvent* e )
369 if ( e->key() == Qt::Key_Escape )
374 else if ( e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return )
376 QPolygon* points = (QPolygon*)data();
377 if ( points->count() )
379 QPoint last = points->point( points->count() - 1 );
380 if ( last != myCurr )
382 points->resize( points->count() + 1 );
383 points->setPoint( points->count() - 1, myCurr );
389 else if ( e->key() == Qt::Key_Backspace && e->type() == QEvent::KeyRelease )
391 QPolygon* points = (QPolygon*)data();
392 if ( points->count() > 1 )
393 points->resize( points->count() - 1 );
401 void OCCViewer_PolygonSketcher::onMouse( QMouseEvent* e )
403 OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
405 QPolygon* points = (QPolygon*)data();
406 if ( !points->count() && !myStart.isNull() )
408 points->resize( points->count() + 1 );
409 points->setPoint( points->count() - 1, myStart );
413 bool valid = avp->rect().contains( myCurr );
414 if ( !myStart.isNull() )
416 QRect aRect( myStart.x() - myToler.width(), myStart.y() - myToler.height(),
417 2 * myToler.width(), 2 * myToler.height() );
418 closed = aRect.contains( myCurr );
420 valid = valid && isValid( points, myCurr );
421 if ( closed && !valid )
425 avp->setCursor( Qt::CrossCursor );
427 avp->setCursor( Qt::PointingHandCursor );
429 avp->setCursor( Qt::ForbiddenCursor );
434 if ( e->type() == QEvent::MouseButtonRelease && ( e->button() & sketchButton() ) )
437 QApplication::postEvent( avp, new QMouseEvent( e->type(), e->pos(),
438 e->globalPos(), e->button(),
439 e->buttons(), e->modifiers() ) );
441 else if ( e->type() == QEvent::MouseButtonRelease && ( e->button() & myAddButton ) )
447 if ( myStart.isNull() )
451 QPoint last = points->point( points->count() - 1 );
452 if ( last != myCurr && valid )
454 points->resize( points->count() + 1 );
455 points->setPoint( points->count() - 1, myCurr );
457 if ( valid && myDbl )
462 else if ( ( e->type() == QEvent::MouseButtonRelease && ( e->button() & myDelButton ) ) ||
463 ( e->type() == QEvent::MouseButtonDblClick && ( e->button() & myDelButton ) ) )
465 if ( points->count() > 1 )
466 points->resize( points->count() - 1 );
469 myDbl = e->type() == QEvent::MouseButtonDblClick && ( e->button() & myAddButton );
472 void OCCViewer_PolygonSketcher::onSketch( SketchState state )
474 QPolygon* points = (QPolygon*)data();
479 mypPolyRB->clearGeometry();
481 mypViewWindow->activateSketching(OCCViewer_ViewWindow::NoSketching);
484 mypPolyRB->setUpdatesEnabled(false);
485 if (!mypPolyRB->isVisible())
488 if (state != Fin && points->count())
489 mypPolyRB->initGeometry(QPolygon(*points) << myCurr);
490 mypPolyRB->setUpdatesEnabled(true);
497 mypCircleRB->getPoligon(points);
498 mypCircleRB->clearGeometry();
500 if (points->size() == CIRCLE_NB_POINTS)
502 mypViewWindow->activateSketching(OCCViewer_ViewWindow::NoSketching);
505 mypCircleRB->setUpdatesEnabled(false);
508 if (mypCircleRB->isCenterDefined()) {
509 mypCircleRB->setRadius(myCurr);
510 if ((mypCircleRB->radius() > MIN_RADIUS) && (!mypCircleRB->isVisible()))
514 mypCircleRB->initGeometry(myCurr);
517 mypCircleRB->setUpdatesEnabled(true);
524 bool OCCViewer_PolygonSketcher::isValid( const QPolygon* aPoints, const QPoint& aCur ) const
526 if ( !aPoints->count() )
529 if ( aPoints->count() == 1 && aPoints->point( 0 ) == aCur )
532 const QPoint& aLast = aPoints->point( aPoints->count() - 1 );
538 for ( uint i = 0; i < aPoints->count() - 1 && res; i++ )
540 const QPoint& aStart = aPoints->point( i );
541 const QPoint& anEnd = aPoints->point( i + 1 );
542 res = !isIntersect( aStart, anEnd, aCur, aLast );
548 bool OCCViewer_PolygonSketcher::isIntersect( const QPoint& aStart1, const QPoint& anEnd1,
549 const QPoint& aStart2, const QPoint& anEnd2 ) const
551 if ( ( aStart1 == aStart2 && anEnd1 == anEnd2 ) ||
552 ( aStart1 == anEnd2 && anEnd1 == aStart2 ) )
555 if ( aStart1 == aStart2 || aStart2 == anEnd1 ||
556 aStart1 == anEnd2 || anEnd1 == anEnd2 )
559 double x11 = aStart1.x() * 1.0;
560 double x12 = anEnd1.x() * 1.0;
561 double y11 = aStart1.y() * 1.0;
562 double y12 = anEnd1.y() * 1.0;
564 double x21 = aStart2.x() * 1.0;
565 double x22 = anEnd2.x() * 1.0;
566 double y21 = aStart2.y() * 1.0;
567 double y22 = anEnd2.y() * 1.0;
569 double k1 = x12 == x11 ? 0 : ( y12 - y11 ) / ( x12 - x11 );
570 double k2 = x22 == x21 ? 0 : ( y22 - y21 ) / ( x22 - x21 );
572 double b1 = y11 - k1 * x11;
573 double b2 = y21 - k2 * x21;
580 return !( ( qMax( x11, x12 ) <= qMin( x21, x22 ) ||
581 qMin( x11, x12 ) >= qMax( x21, x22 ) ) &&
582 ( qMax( y11, y12 ) <= qMin( y21, y22 ) ||
583 qMin( y11, y12 ) >= qMax( y21, y22 ) ) );
587 double x0 = ( b2 - b1 ) / ( k1 - k2 );
588 double y0 = ( k1 * b2 - k2 * b1 ) / ( k1 - k2 );
590 if ( qMin( x11, x12 ) < x0 && x0 < qMax( x11, x12 ) &&
591 qMin( y11, y12 ) < y0 && y0 < qMax( y11, y12 ) &&
592 qMin( x21, x22 ) < x0 && x0 < qMax( x21, x22 ) &&
593 qMin( y21, y22 ) < y0 && y0 < qMax( y21, y22 ) )
600 void OCCViewer_PolygonSketcher::setSketcherMode(int theMode)
602 myMode = (SketchMode)theMode;