Salome HOME
bc809703f433f68c82e3be656123c6c455761bbd
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Overview.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROGUI_Overview.h>
20 #include <OCCViewer_ViewPort3d.h>
21 #include <OCCViewer_ViewFrame.h>
22 #include <OCCViewer_ViewModel.h>
23 #include <OCCViewer_ViewManager.h>
24 #include <QtxRubberBand.h>
25 #include <QApplication>
26 #include <QPainter>
27 #include <QMouseEvent>
28 #include <QLayout>
29
30 class HYDROGUI_OverviewBand : public QtxPolyRubberBand
31 {
32 public:
33   HYDROGUI_OverviewBand( HYDROGUI_Overview* theOverview );
34   virtual ~HYDROGUI_OverviewBand();
35
36   void initGeometry( const QPolygon& );
37   void update( bool isFromMain );
38   QPoint center() const;
39   int    count() const;
40
41   void   drag( const QPoint&, bool isStart );
42   bool   isDrag() const;
43   void   dragging( const QPoint& );
44   bool   contains( const QPoint& ) const;
45   QRect  boundingRect() const;
46
47 protected:
48   virtual void paintEvent( QPaintEvent* );
49
50 private:
51   HYDROGUI_Overview* myOverview;
52   bool               myIsDrag;
53   QPolygon           myStartPoints;
54   QPoint             myStartPnt;
55 };
56
57
58 HYDROGUI_OverviewBand::HYDROGUI_OverviewBand( HYDROGUI_Overview* theOverview )
59   : QtxPolyRubberBand( theOverview->getViewPort( false ) ),
60     myOverview( theOverview ), myIsDrag( false )
61 {
62 }
63
64 HYDROGUI_OverviewBand::~HYDROGUI_OverviewBand()
65 {
66 }
67
68 bool isEmpty( const QPolygon& thePoly )
69 {
70   QSize s = thePoly.boundingRect().size();
71   if( s.width() < 2 || s.height() < 2 )
72     return true;
73   else
74     return false;
75 }
76
77 void HYDROGUI_OverviewBand::initGeometry( const QPolygon& thePoly )
78 {
79   int w2 = myOverview->width()*2;
80   int h2 = myOverview->height()*2;
81
82   QPolygon aPoly;
83   for( int i=0, n=thePoly.size(); i<n; i++ )
84   {
85     QPoint p = thePoly.point( i );
86     if( p.x() < -w2 )
87       p.setX( -w2 );
88     if( p.x() > w2 )
89       p.setX( w2 );
90     if( p.y() < -h2 )
91       p.setY( -h2 );
92     if( p.y() > h2 )
93       p.setY( h2 );
94   }
95
96   QtxPolyRubberBand::initGeometry( thePoly );
97   if( isEmpty( thePoly ) )
98     hide();
99   else
100     show();
101 }
102
103 QRect HYDROGUI_OverviewBand::boundingRect() const
104 {
105   return myPoints.boundingRect();
106 }
107
108 QPoint HYDROGUI_OverviewBand::center() const
109 {
110   QPoint c;
111   int n = myPoints.size()-1;
112   if( n==0 )
113     return QPoint();
114
115   for( int i=0; i<n; i++ )
116     c += myPoints.point( i );
117
118   c = c/n;
119   return c;
120 }
121
122 void HYDROGUI_OverviewBand::drag( const QPoint& thePoint, bool isStart )
123 {
124   if( myIsDrag==isStart )
125     return;
126
127   if( isStart )
128   {
129     myStartPoints = myPoints;
130     /*if( contains( thePoint ) )
131       myStartPnt = thePoint;
132     else*/
133       myStartPnt = center();
134     myIsDrag = true;
135     dragging( thePoint );
136   }
137   else
138   {
139     dragging( thePoint );
140     myIsDrag = false;
141   }
142 }
143
144 bool HYDROGUI_OverviewBand::isDrag() const
145 {
146   return myIsDrag;
147 }
148
149 void HYDROGUI_OverviewBand::dragging( const QPoint& thePoint )
150 {
151   int n = myPoints.size();
152   QPoint delta = thePoint - myStartPnt;
153   for( int i=0; i<n; i++ )
154     myPoints.setPoint( i, myStartPoints.point( i ) + delta );
155   initGeometry( myPoints );
156   update( false );
157 }
158
159 bool HYDROGUI_OverviewBand::contains( const QPoint& thePoint ) const
160 {
161   return myPoints.containsPoint( thePoint, Qt::OddEvenFill );
162 }
163
164 int HYDROGUI_OverviewBand::count() const
165 {
166   return myPoints.size();
167 }
168
169 void HYDROGUI_OverviewBand::update( bool isFromMain )
170 {
171   OCCViewer_ViewPort3d* main = myOverview->getViewPort( true );
172   if( isFromMain )
173   {
174     int w = main->width();
175     int h = main->height();
176
177     QPolygon poly;
178     QPoint p1 = myOverview->fromMain( 0, 0 );
179     QPoint p2 = myOverview->fromMain( w, 0 );
180     QPoint p3 = myOverview->fromMain( w, h );
181     QPoint p4 = myOverview->fromMain( 0, h );
182     poly.append( p1 );
183     poly.append( p2 );
184     poly.append( p3 );
185     poly.append( p4 );
186     poly.append( p1 );
187     initGeometry( poly );
188   }
189   else
190   {
191     OCCViewer_ViewPort3d* overview = myOverview->getViewPort( false );
192     QPoint c = center();
193     double x1, y1, z1, x2, y2, z2;
194     main->getView()->Convert( main->width()/2, main->height()/2, x1, y1, z1 );
195
196     // Patch for OCCT 6.9.1, on 7.0.0 the moving of point to plane XY is not necessary
197     gp_Dir dm = main->getView()->Camera()->Direction();
198     double t1 = -z1/dm.Z();
199     x1 += dm.X()*t1;
200     y1 += dm.Y()*t1;
201     z1 += dm.Z()*t1;
202
203     overview->getView()->Convert( c.x(), c.y(), x2, y2, z2 );
204     gp_Dir dov = overview->getView()->Camera()->Direction();
205     double t2 = -z2/dov.Z();
206     x2 += dov.X()*t2;
207     y2 += dov.Y()*t2;
208     z2 += dov.Z()*t2;
209
210     gp_Trsf aTrsf;
211     aTrsf.SetTranslation( gp_Pnt( x1, y1, z1 ), gp_Pnt( x2, y2, z2 ) );
212
213     // Temporary patch for bug in OCCT 6.9.1
214     Handle(Graphic3d_Camera) cam = main->getView()->Camera();
215     gp_Dir u = cam->Up(), nu = u.Transformed (aTrsf);
216     gp_Pnt e = cam->Eye(), ne = e.Transformed (aTrsf);
217     gp_Pnt cen = cam->Center(), ncen = cen.Transformed (aTrsf);
218
219     if (!nu.IsEqual (u, 0.0))
220       cam->SetUp(nu);
221
222     if (!ne.IsEqual (e, 0.0))
223       cam->SetEye(ne);
224
225     if (!ncen.IsEqual(cen, 0.0))
226       cam->SetCenter (ncen);
227
228     //version for new OCCT:
229     //main->getView()->Camera()->Transform( aTrsf );
230     main->repaint();
231   }
232 }
233
234 void HYDROGUI_OverviewBand::paintEvent( QPaintEvent* thePaintEvent )
235 {
236   QPainter painter( this );
237   painter.setRenderHint( QPainter::Antialiasing );
238
239   static QColor aColor = Qt::red;
240   static int aWidth = 2;
241   static QPen aPen( QBrush( aColor ), aWidth, Qt::DashDotLine );
242
243   painter.setPen( aPen );
244   painter.drawPolygon( myPoints );
245 }
246
247
248
249
250
251
252 HYDROGUI_Overview::HYDROGUI_Overview( const QString& theTitle, int theMargin, QWidget* theParent )
253   : QFrame( theParent ), myMargin( theMargin ),
254     myMainView( 0 ), myViewPort( 0 ), myBand( 0 )
255 {
256   setWindowTitle( theTitle );
257   myLayout = new QGridLayout( this );
258   myLayout->setMargin( 0 );
259   myLayout->setSpacing( 0 );
260   myLayout->setRowStretch( 0, 1 );
261   myLayout->setColumnStretch( 0, 1 );
262 }
263
264 HYDROGUI_Overview::~HYDROGUI_Overview()
265 {
266 }
267
268 QImage HYDROGUI_Overview::dump() const
269 {
270   if( !myViewPort )
271     return QImage();
272
273   Handle(V3d_View) view = myViewPort->getView();
274   if( view.IsNull() )
275     return QImage();
276
277   int aWidth = myViewPort->width();
278   int aHeight = myViewPort->height();
279
280   Image_PixMap aPix;
281   view->ToPixMap( aPix,aWidth, aHeight,Graphic3d_BT_RGBA );
282
283   QImage anImage( aPix.Data(), aWidth, aHeight, QImage::Format_ARGB32 );
284   anImage = anImage.mirrored();
285   anImage = anImage.rgbSwapped();
286
287   if( myBand && myBand->isVisible() )
288   {
289     QPixmap aPixmap = QPixmap::fromImage( anImage );
290     QPoint p = myBand->boundingRect().topLeft();
291     myBand->render( &aPixmap, p );
292     anImage = aPixmap.toImage();
293   }
294
295   return anImage;
296 }
297
298 OCCViewer_ViewPort3d* HYDROGUI_Overview::getViewPort( bool isMain ) const
299 {
300   return isMain ? myMainView->getViewPort() : myViewPort;
301 }
302
303 void HYDROGUI_Overview::setMainView( OCCViewer_ViewFrame* theMainView )
304 {
305   myMainView = theMainView;
306   if( !myMainView )
307     return;
308
309   OCCViewer_ViewWindow* aMainView = myMainView->getView( OCCViewer_ViewFrame::MAIN_VIEW );
310   connect( aMainView, SIGNAL( vpTransformationFinished( OCCViewer_ViewWindow::OperationType ) ),
311            this,      SLOT( OnTransformationAfterOp( OCCViewer_ViewWindow::OperationType ) ) );
312   connect( aMainView->getViewPort(), SIGNAL( vpResizeEvent( QResizeEvent* ) ),
313            this,       SLOT( OnResizeEvent( QResizeEvent* ) ) );
314   connect( aMainView->getViewPort(), SIGNAL( vpTransformed( OCCViewer_ViewPort* ) ),
315            this,       SLOT( OnTransformation() ) );
316
317   if( !myViewPort )
318   {
319     myViewPort = new OCCViewer_ViewPort3d( this, myMainView->getViewPort()->getViewer(), V3d_ORTHOGRAPHIC );
320
321     if( myViewPort )
322     {
323       myViewPort->setBackgroundColor( myMainView->getViewPort()->backgroundColor() );
324
325       connect( myViewPort, SIGNAL( vpMouseEvent( QMouseEvent* ) ), 
326               this,       SLOT( OnMouseEvent( QMouseEvent* ) ) );
327       connect( myViewPort, SIGNAL( vpResizeEvent( QResizeEvent* ) ),
328               this,       SLOT( OnResizeEvent( QResizeEvent* ) ) );
329
330       myLayout->addWidget( myViewPort, 0, 0 );
331     }
332   }
333
334 #if defined(TEST_MODE) || defined(_DEBUG)
335   //qApp->installEventFilter( this );
336 #endif
337
338   qApp->processEvents();
339
340   setTopView();
341
342   qApp->processEvents();
343
344   if( !myBand )
345     myBand = new HYDROGUI_OverviewBand( this );
346
347   myBand->update( true );
348 }
349
350 void HYDROGUI_Overview::setTopView()
351 {
352   if( !myViewPort )
353     return;
354
355   Handle(V3d_View) aView3d = myViewPort->getView();
356   if( !aView3d.IsNull() )
357     aView3d->SetProj( V3d_Zpos );
358   myViewPort->fitAll();
359
360   // Apply margins for internal area in the view port
361   if( myMargin>0 )
362   {
363     QRect aRect( -myMargin, -myMargin, myViewPort->width()+2*myMargin, myViewPort->height()+2*myMargin );
364     myViewPort->fitRect( aRect );
365   }
366
367   if( myBand )
368     myBand->update( true );
369 }
370
371 void HYDROGUI_Overview::OnTransformationAfterOp( OCCViewer_ViewWindow::OperationType theOp )
372 {
373   if( myViewPort && theOp>=OCCViewer_ViewWindow::WINDOWFIT )
374   {
375     myViewPort->fitAll();
376   }
377   if( theOp==OCCViewer_ViewWindow::FITSELECTION )
378   {
379     CustomFitSelection();
380   }
381   OnTransformation();
382 }
383
384 void HYDROGUI_Overview::OnTransformation()
385 {
386   if( myBand )
387     myBand->update( true );
388 }
389
390 QPoint HYDROGUI_Overview::fromMain( int xp, int yp ) const
391 {
392   if( !myMainView || !myViewPort )
393     return QPoint();
394
395   const double EPS = 1E-2;
396
397   Handle(V3d_View) aMain = myMainView->getViewPort()->getView();
398   Handle(V3d_View) aXY = myViewPort->getView();
399
400   // View coordinates to 3d (world) coordinates
401   double x, y, z;
402   aMain->Convert( xp, yp, x, y, z );
403
404   // Moving to the XY plane
405   gp_Vec aDir = aMain->Camera()->Direction();
406   if( fabs( aDir.Z() )<EPS )
407     return QPoint();
408
409   double t = -z/aDir.Z();
410   x += t * aDir.X();
411   y += t * aDir.Y();
412   z += t * aDir.Z();
413
414   // 3d (world) coordinates to view coordinates in the overview
415   aXY->Convert( x, y, z, xp, yp );
416
417   return QPoint( xp, yp );
418 }
419
420 void HYDROGUI_Overview::OnMouseEvent( QMouseEvent* theEvent )
421 {
422   QPoint mp = theEvent->pos();
423   if( !myBand )
424     return;
425
426   switch( theEvent->type() )
427   {
428   case QEvent::MouseButtonPress:
429     myBand->drag( mp, true );
430     break;
431
432   case QEvent::MouseButtonRelease:
433     myBand->drag( mp, false );
434     break;
435
436   case QEvent::MouseMove:
437     if( myBand->isDrag() )
438       myBand->dragging( mp );
439     break;
440   }
441 }
442
443 bool HYDROGUI_Overview::eventFilter( QObject* theObject, QEvent* theEvent )
444 {
445 #if defined(TEST_MODE) || defined(_DEBUG)
446   /*switch( theEvent->type() )
447   {
448   case QEvent::MouseMove:
449     {
450       QPoint mp = ((QMouseEvent*)theEvent)->pos();
451       //mp = getViewPort(false)->mapFromGlobal(mp);
452       QString coords = QString( "(%0, %1)" ).arg( mp.x() ).arg( mp.y() );
453       std::string scoords = coords.toStdString();
454       qDebug( scoords.c_str() );
455     }
456     break;
457   }*/
458 #endif
459   return QFrame::eventFilter( theObject, theEvent );
460 }
461
462 void HYDROGUI_Overview::OnResizeEvent( QResizeEvent* )
463 {
464   if( myBand )
465     myBand->update( true );
466 }
467
468 void HYDROGUI_Overview::CustomFitSelection() const
469 {
470   OCCViewer_ViewPort3d* main = getViewPort( true );
471   if( !main )
472     return;
473
474   int w = main->width();
475   int h = main->height();
476
477   Bnd_Box bounding = BoundingForSelection();
478   if( bounding.IsVoid() )
479     return;
480
481   Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
482   bounding.Get( xmin, ymin, zmin, xmax, ymax, zmax );
483
484   QList<QPoint> points;
485   Standard_Integer xp, yp;
486   main->getView()->Convert( xmin, ymin, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
487   main->getView()->Convert( xmax, ymin, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
488   main->getView()->Convert( xmin, ymax, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
489   main->getView()->Convert( xmax, ymax, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
490   main->getView()->Convert( xmin, ymin, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
491   main->getView()->Convert( xmax, ymin, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
492   main->getView()->Convert( xmin, ymax, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
493   main->getView()->Convert( xmax, ymax, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
494
495   int xpmin, ypmin, xpmax, ypmax;
496   bool isFirst = true;
497   foreach( QPoint p, points )
498   {
499     int x = p.x(), y = p.y();
500     if( isFirst || x<xpmin )
501       xpmin = x;
502     if( isFirst || x>xpmax )
503       xpmax = x;
504     if( isFirst || y<ypmin )
505       ypmin = y;
506     if( isFirst || y>ypmax )
507       ypmax = y;
508
509     isFirst = false;
510   }
511
512   const int margin = 5;
513   QRect r( xpmin-margin, ypmin-margin, xpmax-xpmin+2*margin, ypmax-ypmin+2*margin );
514   main->fitRect( r );
515 }
516
517 Handle(AIS_InteractiveContext) HYDROGUI_Overview::context() const
518 {
519   if( myMainView )
520   {
521     SUIT_ViewModel* vm = myMainView->getViewManager()->getViewModel();
522     OCCViewer_Viewer* viewer = dynamic_cast<OCCViewer_Viewer*>( vm );
523     if( viewer )
524       return viewer->getAISContext();
525   }
526   return Handle(AIS_InteractiveContext)();
527 }
528
529 typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)> AIS_MapOfObjectOwners1;
530   typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)>::Iterator AIS_MapIteratorOfMapOfObjectOwners1;
531 Bnd_Box HYDROGUI_Overview::BoundingForSelection() const
532 {
533   Handle(AIS_InteractiveContext) c = context();
534
535   Bnd_Box aBndSelected;
536
537   AIS_MapOfObjectOwners1 anObjectOwnerMap;
538   for (c->InitSelected(); c->MoreSelected(); c->NextSelected())
539   {
540     const Handle(SelectMgr_EntityOwner)& anOwner = c->SelectedOwner();
541     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
542     if (anObj->IsInfinite())
543     {
544       continue;
545     }
546
547     if (anOwner == anObj->GlobalSelOwner())
548     {
549       Bnd_Box aTmpBnd;
550       anObj->BoundingBox (aTmpBnd);
551       aBndSelected.Add (aTmpBnd);
552     }
553     else
554     {
555       Handle(SelectMgr_IndexedMapOfOwner) anOwnerMap;
556       if (!anObjectOwnerMap.Find (anOwner->Selectable(), anOwnerMap))
557       {
558         anOwnerMap = new SelectMgr_IndexedMapOfOwner();
559         anObjectOwnerMap.Bind (anOwner->Selectable(), anOwnerMap);
560       }
561
562       anOwnerMap->Add (anOwner);
563     }
564   }
565
566   for (AIS_MapIteratorOfMapOfObjectOwners1 anIter (anObjectOwnerMap); anIter.More(); anIter.Next())
567   {
568     const Handle(SelectMgr_SelectableObject) anObject = anIter.Key();
569     Bnd_Box aTmpBox = anObject->BndBoxOfSelected (anIter.ChangeValue());
570     aBndSelected.Add (aTmpBox);
571   }
572
573   anObjectOwnerMap.Clear();
574
575   return aBndSelected;
576 }