Salome HOME
e70aa8c2bd519fb0e6e8f681445739e7a509187f
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewSketcher.cxx
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "OCCViewer_ViewSketcher.h"
21 #include "OCCViewer_ViewWindow.h"
22 #include "OCCViewer_ViewPort3d.h"
23
24 #include "QtxRubberBand.h"
25
26 #include <QApplication>
27 #include <QPainter>
28 #include <QPolygon>
29 #include <QMouseEvent>
30 #include <QKeyEvent>
31
32 /****************************************************************
33 **  Class: OCCViewer_ViewSketcher
34 **  Level: Public
35 *****************************************************************/
36
37 OCCViewer_ViewSketcher::OCCViewer_ViewSketcher( OCCViewer_ViewWindow* vw, int type )
38 : QObject( vw ),
39 mySketchButton( Qt::LeftButton ),
40 mypViewWindow( vw ),
41 myType( type ),
42 mypData( 0 ),
43 myResult( Neutral ),
44 myButtonState( 0 ),
45 myHasShift( false )
46 {
47 }
48
49 OCCViewer_ViewSketcher::~OCCViewer_ViewSketcher()
50 {
51 }
52
53 void OCCViewer_ViewSketcher::activate()
54 {
55   OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
56
57   mySavedCursor = avp->cursor();
58   avp->setCursor( Qt::PointingHandCursor );
59   avp->installEventFilter( this );
60   qApp->installEventFilter( this );
61
62   connect( avp, SIGNAL( vpDrawExternal( QPainter* ) ), this, SLOT( onDrawViewPort() ) );
63
64   myStart = QPoint();
65   myResult = Neutral;
66
67   onActivate();
68 }
69
70 void OCCViewer_ViewSketcher::deactivate()
71 {
72   OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
73
74   disconnect( avp, SIGNAL( vpDrawExternal( QPainter* ) ), this, SLOT( onDrawViewPort() ) );
75
76   qApp->removeEventFilter( this );
77   avp->removeEventFilter( this );
78   avp->setCursor( mySavedCursor );
79
80   onDeactivate();
81 }
82
83 int OCCViewer_ViewSketcher::type() const
84 {
85   return myType;
86 }
87
88 void* OCCViewer_ViewSketcher::data() const
89 {
90   return mypData;
91 }
92
93 int OCCViewer_ViewSketcher::result() const
94 {
95   return myResult;
96 }
97
98 int OCCViewer_ViewSketcher::buttonState() const
99 {
100   return myButtonState;
101 }
102
103 bool OCCViewer_ViewSketcher::isHasShift() const
104 {
105   return myHasShift;
106 }
107
108 void OCCViewer_ViewSketcher::onActivate()
109 {
110 }
111
112 void OCCViewer_ViewSketcher::onDeactivate()
113 {
114 }
115
116 bool OCCViewer_ViewSketcher::isDefault() const
117 {
118   return true;
119 }
120
121 bool OCCViewer_ViewSketcher::eventFilter( QObject* o, QEvent* e )
122 {
123   OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
124
125   QMouseEvent* me = (QMouseEvent*)e;
126   SketchState state = EnTrain;
127   bool ignore = false;
128   if ( o == avp )
129   {
130     switch ( e->type() )
131     {
132       case QEvent::MouseMove:
133       case QEvent::MouseButtonPress:
134       case QEvent::MouseButtonRelease:
135       case QEvent::MouseButtonDblClick:
136       {
137
138         myButtonState = me->buttons();
139         if ( e->type() == QEvent::MouseButtonPress )
140           myButtonState |= me->button();
141
142         if ( myStart.isNull() && ( myButtonState & sketchButton() ) )
143         {
144           state = Debut;
145           myStart = me->pos();
146         }
147
148         myCurr = me->pos();
149
150         onMouse( me );
151
152         if ( myResult != Neutral )
153           state = Fin;
154
155         ignore = true;
156         myHasShift = ( me->modifiers() & Qt::ShiftModifier );
157         break;
158       }
159       case QEvent::Hide:
160       case QEvent::HideToParent:
161         myResult = Reject;
162         onSketch( Fin );
163         break;
164       default:
165         break;
166     }
167   }
168   if ( e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease )
169   {
170     ignore = onKey( (QKeyEvent*)e );
171     if ( myResult != Neutral )
172       state = Fin;
173   }
174
175   if ( ignore )
176   {
177     onSketch( state );
178     return true;
179   }
180   return QObject::eventFilter( o, e );
181 }
182
183 void OCCViewer_ViewSketcher::onDrawViewPort()
184 {
185   onSketch( Debut );
186 }
187
188 bool OCCViewer_ViewSketcher::onKey( QKeyEvent* )
189 {
190   return false;
191 }
192
193 void OCCViewer_ViewSketcher::onMouse( QMouseEvent* )
194 {
195 }
196
197 int OCCViewer_ViewSketcher::sketchButton()
198 {
199   return mySketchButton;
200 }
201
202 void OCCViewer_ViewSketcher::setSketchButton( int b )
203 {
204   mySketchButton = b;
205 }
206
207 /****************************************************************
208 **  Class: OCCViewer_RectSketcher
209 **  Level: Public
210 *****************************************************************/
211
212 OCCViewer_RectSketcher::OCCViewer_RectSketcher( OCCViewer_ViewWindow* vw, int typ )
213   : OCCViewer_ViewSketcher( vw, typ )
214 {
215   if ( vw )
216     {
217       OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
218       mypRectRB = new QtxRectRubberBand( avp );
219     }
220 }
221
222 OCCViewer_RectSketcher::~OCCViewer_RectSketcher()
223 {
224   delete (QRect*)mypData;
225 }
226
227 void OCCViewer_RectSketcher::onActivate()
228 {
229   mypData = new QRect();
230 }
231
232 void OCCViewer_RectSketcher::onDeactivate()
233 {
234   delete (QRect*)mypData;
235   mypData = 0;
236   mypRectRB->clearGeometry();
237 }
238
239 bool OCCViewer_RectSketcher::onKey( QKeyEvent* e )
240 {
241   if ( e->key() == Qt::Key_Escape )
242     myResult = Reject;
243   else if ( e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return )
244     myResult = Accept;
245
246   return true;
247 }
248
249 void OCCViewer_RectSketcher::onMouse( QMouseEvent* e )
250 {
251   OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
252
253   if ( avp->rect().contains( myCurr ) )
254     avp->setCursor( Qt::PointingHandCursor );
255   else
256     avp->setCursor( Qt::ForbiddenCursor );
257
258   if ( e->type() == QEvent::MouseButtonRelease && e->button() == sketchButton() )
259   {
260     myResult = Accept;
261     QApplication::postEvent( avp, new QMouseEvent( e->type(), e->pos(),
262                                                    e->globalPos(), e->button(), 
263                                                    e->buttons(), e->modifiers() ) );
264   }
265 }
266
267 void OCCViewer_RectSketcher::onSketch( SketchState state )
268 {
269   //OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
270
271   if ( mypRectRB )
272     {      
273       QRect* sketchRect = (QRect*)data();
274       if ( myButtonState & sketchButton() )
275         {   
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() ) );
279           QPainter p( avp );
280           p.setPen( Qt::white );
281           p.setCompositionMode( QPainter::CompositionMode_Xor );
282           */
283           
284           //if ( state != Debut && !sketchRect->isEmpty() )
285           //  p.drawRect( *sketchRect );
286
287           *sketchRect = rect;
288           if ( !rect.isEmpty() && state != Fin )
289             {
290               //p.drawRect( *sketchRect );            
291               mypRectRB->initGeometry( rect );
292               mypRectRB->show();
293             }          
294           else
295             mypRectRB->hide();
296         }
297     }
298
299   if ( state == Fin )
300   {
301     mypViewWindow->activateSketching( OCCViewer_ViewWindow::NoSketching );
302   }
303 }
304
305 /****************************************************************
306 **  Class: OCCViewer_PolygonSketcher
307 **  Level: Public
308 *****************************************************************/
309
310 OCCViewer_PolygonSketcher::OCCViewer_PolygonSketcher( OCCViewer_ViewWindow* vw, int typ )
311 : OCCViewer_ViewSketcher( vw, typ ),
312   myDbl           ( false ),
313   myToler         ( 5, 5 ),
314   //mypPoints        ( 0L ),
315   myAddButton     ( 0 ),
316   myDelButton     ( 0 )
317 {
318   mySketchButton = Qt::RightButton;
319   if ( vw )
320     {
321       OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
322       mypPolyRB = new QtxPolyRubberBand( avp );
323     }
324 }
325
326 OCCViewer_PolygonSketcher::~OCCViewer_PolygonSketcher()
327 {
328   //delete mypPoints;
329   delete (QPolygon*)mypData;
330 }
331
332 void OCCViewer_PolygonSketcher::onActivate()
333 {
334   myDbl = false;
335   mypData = new QPolygon( 0 );
336   //mypPoints = new QPolygon( 0 );
337
338   switch ( sketchButton() )
339   {
340   case Qt::LeftButton:
341     myAddButton = Qt::RightButton;
342     myDelButton = Qt::MidButton;
343     break;
344   case Qt::MidButton:
345     myAddButton = Qt::LeftButton;
346     myDelButton = Qt::RightButton;
347     break;
348   case Qt::RightButton:
349   default:
350     myAddButton = Qt::LeftButton;
351     myDelButton = Qt::MidButton;
352     break;
353   };
354 }
355
356 void OCCViewer_PolygonSketcher::onDeactivate()
357 {
358   //delete mypPoints;
359   //mypPoints = 0;
360   delete (QPolygon*)mypData;
361   mypData = 0;
362
363   if ( mypPolyRB )
364     mypPolyRB->clearGeometry();  
365 }
366
367 bool OCCViewer_PolygonSketcher::onKey( QKeyEvent* e )
368 {
369   if ( e->key() == Qt::Key_Escape )
370   {
371     myResult = Reject;
372     return true;
373   }
374   else if ( e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return )
375   {
376     QPolygon* points = (QPolygon*)data();
377     if ( points->count() )
378     {
379       QPoint last = points->point( points->count() - 1 );
380       if ( last != myCurr )
381       {
382         points->resize( points->count() + 1 );
383         points->setPoint( points->count() - 1, myCurr );
384       }
385     }
386     myResult = Accept;
387     return true;
388   }
389   else if ( e->key() == Qt::Key_Backspace && e->type() == QEvent::KeyRelease )
390   {
391     QPolygon* points = (QPolygon*)data();
392     if ( points->count() > 1 )
393       points->resize( points->count() - 1 );
394     onMouse( 0 );
395     return true;
396   }
397
398   return true;
399 }
400
401 void OCCViewer_PolygonSketcher::onMouse( QMouseEvent* e )
402 {
403   OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
404
405   QPolygon* points = (QPolygon*)data();
406   if ( !points->count() && !myStart.isNull() )
407   {
408     points->resize( points->count() + 1 );
409     points->setPoint( points->count() - 1, myStart );
410   }
411
412   bool closed = false;
413   bool valid = avp->rect().contains( myCurr );
414   if ( !myStart.isNull() )
415   {
416     QRect aRect( myStart.x() - myToler.width(), myStart.y() - myToler.height(),
417                  2 * myToler.width(), 2 * myToler.height() );
418     closed = aRect.contains( myCurr );
419   }
420   valid = valid && isValid( points, myCurr );
421   if ( closed && !valid )
422     closed = false;
423
424   if ( closed )
425     avp->setCursor( Qt::CrossCursor );
426   else if ( valid )
427     avp->setCursor( Qt::PointingHandCursor );
428   else
429     avp->setCursor( Qt::ForbiddenCursor );
430
431   if ( !e )
432     return;
433
434   if ( e->type() == QEvent::MouseButtonRelease && ( e->button() & sketchButton() ) )
435   {
436     myResult = Reject;
437     QApplication::postEvent( avp, new QMouseEvent( e->type(), e->pos(),
438                                                    e->globalPos(), e->button(), 
439                                                    e->buttons(), e->modifiers() ) );
440   }
441   else if ( e->type() == QEvent::MouseButtonRelease && ( e->button() & myAddButton ) )
442   {
443     if ( closed )
444       myResult = Accept;
445     else
446     {
447       if ( myStart.isNull() )
448         myStart = myCurr;
449       else
450       {
451         QPoint last = points->point( points->count() - 1 );
452         if ( last != myCurr && valid )
453         {
454           points->resize( points->count() + 1 );
455           points->setPoint( points->count() - 1, myCurr );
456         }
457         if ( valid && myDbl )
458           myResult = Accept;
459       }
460     }
461   }
462   else if ( ( e->type() == QEvent::MouseButtonRelease && ( e->button() & myDelButton ) ) ||
463             ( e->type() == QEvent::MouseButtonDblClick && ( e->button() & myDelButton ) ) )
464   {
465     if ( points->count() > 1 )
466       points->resize( points->count() - 1 );
467     onMouse( 0 );
468   }
469   myDbl = e->type() == QEvent::MouseButtonDblClick && ( e->button() & myAddButton );
470 }
471
472 void OCCViewer_PolygonSketcher::onSketch( SketchState state )
473 {
474   //OCCViewer_ViewPort3d* avp = mypViewWindow->getViewPort();
475
476   QPolygon* points = (QPolygon*)data();
477   /*QPainter p( avp );
478   p.setPen( Qt::white );
479   p.setCompositionMode( QPainter::CompositionMode_Xor );
480   if ( state != Debut )
481     p.drawPolyline( *mypPoints );
482
483   if ( points->count() )
484   {
485     mypPoints->resize( points->count() + 1 );
486     for ( uint i = 0; i < points->count(); i++ )
487       mypPoints->setPoint( i, points->point( i ) );
488     mypPoints->setPoint( points->count(), myCurr );
489     if ( state != Fin )
490       p.drawPolyline( *mypPoints );
491       }*/
492   if ( mypPolyRB ) {
493     if ( state == Fin ) {
494       mypPolyRB->clearGeometry();
495       mypPolyRB->hide();
496       mypViewWindow->activateSketching( OCCViewer_ViewWindow::NoSketching );
497     } else {
498       mypPolyRB->setUpdatesEnabled ( false );
499       if ( !mypPolyRB->isVisible() )
500         mypPolyRB->show();
501       //if ( state != Debut )
502       //  mypPolyRB->repaint();
503       
504       if ( state != Fin && points->count() )
505         mypPolyRB->initGeometry( QPolygon(*points) << myCurr );
506       //mypPolyRB->addNode( myCurr );
507       
508       //if ( state != Fin )
509       //  mypPolyRB->repaint();
510       mypPolyRB->setUpdatesEnabled ( true );
511       //mypPolyRB->repaint();
512     }
513   }
514 }
515
516 bool OCCViewer_PolygonSketcher::isValid( const QPolygon* aPoints, const QPoint& aCur ) const
517 {
518   if ( !aPoints->count() )
519     return true;
520
521   if ( aPoints->count() == 1 && aPoints->point( 0 ) == aCur )
522     return false;
523
524   const QPoint& aLast = aPoints->point( aPoints->count() - 1 );
525
526   if ( aLast == aCur )
527     return true;
528
529   bool res = true;
530   for ( uint i = 0; i < aPoints->count() - 1 && res; i++ )
531   {
532     const QPoint& aStart = aPoints->point( i );
533     const QPoint& anEnd  = aPoints->point( i + 1 );
534     res = !isIntersect( aStart, anEnd, aCur, aLast );
535   }
536
537   return res;
538 }
539
540 bool OCCViewer_PolygonSketcher::isIntersect( const QPoint& aStart1, const QPoint& anEnd1,
541                                              const QPoint& aStart2, const QPoint& anEnd2 ) const
542 {
543   if ( ( aStart1 == aStart2 && anEnd1 == anEnd2 ) ||
544        ( aStart1 == anEnd2 && anEnd1 == aStart2 ) )
545     return true;
546
547   if ( aStart1 == aStart2 || aStart2 == anEnd1 ||
548        aStart1 == anEnd2 || anEnd1 == anEnd2 )
549     return false;
550
551   double x11 = aStart1.x() * 1.0;
552   double x12 = anEnd1.x() * 1.0;
553   double y11 = aStart1.y() * 1.0;
554   double y12 = anEnd1.y() * 1.0;
555
556   double x21 = aStart2.x() * 1.0;
557   double x22 = anEnd2.x() * 1.0;
558   double y21 = aStart2.y() * 1.0;
559   double y22 = anEnd2.y() * 1.0;
560
561   double k1 = x12 == x11 ? 0 : ( y12 - y11 ) / ( x12 - x11 );
562   double k2 = x22 == x21 ? 0 : ( y22 - y21 ) / ( x22 - x21 );
563
564   double b1 = y11 - k1 * x11;
565   double b2 = y21 - k2 * x21;
566
567   if ( k1 == k2 )
568   {
569     if ( b1 != b2 )
570       return false;
571     else
572       return !( ( qMax( x11, x12 ) <= qMin( x21, x22 ) ||
573                   qMin( x11, x12 ) >= qMax( x21, x22 ) ) &&
574                 ( qMax( y11, y12 ) <= qMin( y21, y22 ) ||
575                   qMin( y11, y12 ) >= qMax( y21, y22 ) ) );
576   }
577   else
578   {
579     double x0 = ( b2 - b1 ) / ( k1 - k2 );
580     double y0 = ( k1 * b2 - k2 * b1 ) / ( k1 - k2 );
581
582     if ( qMin( x11, x12 ) < x0 && x0 < qMax( x11, x12 ) &&
583          qMin( y11, y12 ) < y0 && y0 < qMax( y11, y12 ) &&
584          qMin( x21, x22 ) < x0 && x0 < qMax( x21, x22 ) &&
585          qMin( y21, y22 ) < y0 && y0 < qMax( y21, y22 ) )
586       return true;
587   }
588   return false;
589 }
590
591