Salome HOME
Merge branch 'BR_2017_PORTING' into BR_2018_V8_5
[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
282 #if OCC_VERSION_LARGE >= 0x07020000
283   view->ToPixMap( aPix,aWidth, aHeight,Graphic3d_BT_RGBA );
284   QImage anImage( aPix.Data(), aWidth, aHeight, QImage::Format_ARGB32 );
285   anImage = anImage.mirrored();
286   anImage = anImage.rgbSwapped();
287 #else
288   view->ToPixMap(aPix, aWidth, aHeight, Graphic3d_BT_RGB);
289   QImage anImage( aWidth, aHeight, QImage::Format_ARGB32 );
290   for ( int i = 0; i < aWidth; i++ ) {
291     for ( int j = 0; j < aHeight; j++ ) {
292       Quantity_Color pixel = aPix.PixelColor( i, j ).GetRGB();
293       QColor color = QColor::fromRgbF( pixel.Red(), pixel.Green(), pixel.Blue() );
294       anImage.setPixelColor( i, j, color );
295     }
296   }
297   if ( aPix.IsTopDown() )
298     anImage = anImage.mirrored();
299 #endif
300   if( myBand && myBand->isVisible() )
301   {
302     QPixmap aPixmap = QPixmap::fromImage( anImage );
303     QPoint p = myBand->boundingRect().topLeft();
304     myBand->render( &aPixmap, p );
305     anImage = aPixmap.toImage();
306   }
307   return anImage;
308 }
309
310 OCCViewer_ViewPort3d* HYDROGUI_Overview::getViewPort( bool isMain ) const
311 {
312   return isMain ? myMainView->getViewPort() : myViewPort;
313 }
314
315 void HYDROGUI_Overview::setMainView( OCCViewer_ViewFrame* theMainView )
316 {
317   myMainView = theMainView;
318   if( !myMainView )
319     return;
320
321   OCCViewer_ViewWindow* aMainView = myMainView->getView( OCCViewer_ViewFrame::MAIN_VIEW );
322   connect( aMainView, SIGNAL( vpTransformationFinished( OCCViewer_ViewWindow::OperationType ) ),
323            this,      SLOT( OnTransformationAfterOp( OCCViewer_ViewWindow::OperationType ) ) );
324   connect( aMainView->getViewPort(), SIGNAL( vpResizeEvent( QResizeEvent* ) ),
325            this,       SLOT( OnResizeEvent( QResizeEvent* ) ) );
326   connect( aMainView->getViewPort(), SIGNAL( vpTransformed( OCCViewer_ViewPort* ) ),
327            this,       SLOT( OnTransformation() ) );
328
329   if( !myViewPort )
330   {
331     myViewPort = new OCCViewer_ViewPort3d( this, myMainView->getViewPort()->getViewer(), V3d_ORTHOGRAPHIC );
332
333     if( myViewPort )
334     {
335       myViewPort->setBackgroundColor( myMainView->getViewPort()->backgroundColor() );
336
337       connect( myViewPort, SIGNAL( vpMouseEvent( QMouseEvent* ) ), 
338               this,       SLOT( OnMouseEvent( QMouseEvent* ) ) );
339       connect( myViewPort, SIGNAL( vpResizeEvent( QResizeEvent* ) ),
340               this,       SLOT( OnResizeEvent( QResizeEvent* ) ) );
341
342       myLayout->addWidget( myViewPort, 0, 0 );
343     }
344   }
345
346 #if defined(TEST_MODE) || defined(_DEBUG)
347   //qApp->installEventFilter( this );
348 #endif
349
350   qApp->processEvents();
351
352   setTopView();
353
354   qApp->processEvents();
355
356   if( !myBand )
357     myBand = new HYDROGUI_OverviewBand( this );
358
359   myBand->update( true );
360 }
361
362 void HYDROGUI_Overview::setTopView()
363 {
364   if( !myViewPort )
365     return;
366
367   Handle(V3d_View) aView3d = myViewPort->getView();
368   if( !aView3d.IsNull() )
369     aView3d->SetProj( V3d_Zpos );
370   myViewPort->fitAll();
371
372   // Apply margins for internal area in the view port
373   if( myMargin>0 )
374   {
375     QRect aRect( -myMargin, -myMargin, myViewPort->width()+2*myMargin, myViewPort->height()+2*myMargin );
376     myViewPort->fitRect( aRect );
377   }
378
379   if( myBand )
380     myBand->update( true );
381 }
382
383 void HYDROGUI_Overview::OnTransformationAfterOp( OCCViewer_ViewWindow::OperationType theOp )
384 {
385   if( myViewPort && theOp>=OCCViewer_ViewWindow::WINDOWFIT )
386   {
387     myViewPort->fitAll();
388   }
389   if( theOp==OCCViewer_ViewWindow::FITSELECTION )
390   {
391     CustomFitSelection();
392   }
393   OnTransformation();
394 }
395
396 void HYDROGUI_Overview::OnTransformation()
397 {
398   if( myBand )
399     myBand->update( true );
400 }
401
402 QPoint HYDROGUI_Overview::fromMain( int xp, int yp ) const
403 {
404   if( !myMainView || !myViewPort || !myViewPort->isVisible() )
405     return QPoint();
406
407   const double EPS = 1E-2;
408
409   Handle(V3d_View) aMain = myMainView->getViewPort()->getView();
410   Handle(V3d_View) aXY = myViewPort->getView();
411
412   // View coordinates to 3d (world) coordinates
413   double x, y, z;
414   aMain->Convert( xp, yp, x, y, z );
415
416   // Moving to the XY plane
417   gp_Vec aDir = aMain->Camera()->Direction();
418   if( fabs( aDir.Z() )<EPS )
419     return QPoint();
420
421   double t = -z/aDir.Z();
422   x += t * aDir.X();
423   y += t * aDir.Y();
424   z += t * aDir.Z();
425
426   // 3d (world) coordinates to view coordinates in the overview
427   aXY->Convert( x, y, z, xp, yp );
428
429   return QPoint( xp, yp );
430 }
431
432 void HYDROGUI_Overview::OnMouseEvent( QMouseEvent* theEvent )
433 {
434   QPoint mp = theEvent->pos();
435   if( !myBand )
436     return;
437
438   switch( theEvent->type() )
439   {
440   case QEvent::MouseButtonPress:
441     myBand->drag( mp, true );
442     break;
443
444   case QEvent::MouseButtonRelease:
445     myBand->drag( mp, false );
446     break;
447
448   case QEvent::MouseMove:
449     if( myBand->isDrag() )
450       myBand->dragging( mp );
451     break;
452   }
453 }
454
455 bool HYDROGUI_Overview::eventFilter( QObject* theObject, QEvent* theEvent )
456 {
457 #if defined(TEST_MODE) || defined(_DEBUG)
458   /*switch( theEvent->type() )
459   {
460   case QEvent::MouseMove:
461     {
462       QPoint mp = ((QMouseEvent*)theEvent)->pos();
463       //mp = getViewPort(false)->mapFromGlobal(mp);
464       QString coords = QString( "(%0, %1)" ).arg( mp.x() ).arg( mp.y() );
465       std::string scoords = coords.toStdString();
466       qDebug( scoords.c_str() );
467     }
468     break;
469   }*/
470 #endif
471   return QFrame::eventFilter( theObject, theEvent );
472 }
473
474 void HYDROGUI_Overview::OnResizeEvent( QResizeEvent* )
475 {
476   if( myBand )
477     myBand->update( true );
478 }
479
480 void HYDROGUI_Overview::CustomFitSelection() const
481 {
482   OCCViewer_ViewPort3d* main = getViewPort( true );
483   if( !main )
484     return;
485
486   int w = main->width();
487   int h = main->height();
488
489   Bnd_Box bounding = BoundingForSelection();
490   if( bounding.IsVoid() )
491     return;
492
493   Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
494   bounding.Get( xmin, ymin, zmin, xmax, ymax, zmax );
495
496   QList<QPoint> points;
497   Standard_Integer xp, yp;
498   main->getView()->Convert( xmin, ymin, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
499   main->getView()->Convert( xmax, ymin, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
500   main->getView()->Convert( xmin, ymax, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
501   main->getView()->Convert( xmax, ymax, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
502   main->getView()->Convert( xmin, ymin, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
503   main->getView()->Convert( xmax, ymin, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
504   main->getView()->Convert( xmin, ymax, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
505   main->getView()->Convert( xmax, ymax, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
506
507   int xpmin, ypmin, xpmax, ypmax;
508   bool isFirst = true;
509   foreach( QPoint p, points )
510   {
511     int x = p.x(), y = p.y();
512     if( isFirst || x<xpmin )
513       xpmin = x;
514     if( isFirst || x>xpmax )
515       xpmax = x;
516     if( isFirst || y<ypmin )
517       ypmin = y;
518     if( isFirst || y>ypmax )
519       ypmax = y;
520
521     isFirst = false;
522   }
523
524   const int margin = 5;
525   QRect r( xpmin-margin, ypmin-margin, xpmax-xpmin+2*margin, ypmax-ypmin+2*margin );
526   main->fitRect( r );
527 }
528
529 Handle(AIS_InteractiveContext) HYDROGUI_Overview::context() const
530 {
531   if( myMainView )
532   {
533     SUIT_ViewModel* vm = myMainView->getViewManager()->getViewModel();
534     OCCViewer_Viewer* viewer = dynamic_cast<OCCViewer_Viewer*>( vm );
535     if( viewer )
536       return viewer->getAISContext();
537   }
538   return Handle(AIS_InteractiveContext)();
539 }
540
541 typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)> AIS_MapOfObjectOwners1;
542   typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)>::Iterator AIS_MapIteratorOfMapOfObjectOwners1;
543 Bnd_Box HYDROGUI_Overview::BoundingForSelection() const
544 {
545   Handle(AIS_InteractiveContext) c = context();
546
547   Bnd_Box aBndSelected;
548
549   AIS_MapOfObjectOwners1 anObjectOwnerMap;
550   for (c->InitSelected(); c->MoreSelected(); c->NextSelected())
551   {
552     const Handle(SelectMgr_EntityOwner)& anOwner = c->SelectedOwner();
553     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
554     if (anObj->IsInfinite())
555     {
556       continue;
557     }
558
559     if (anOwner == anObj->GlobalSelOwner())
560     {
561       Bnd_Box aTmpBnd;
562       anObj->BoundingBox (aTmpBnd);
563       aBndSelected.Add (aTmpBnd);
564     }
565     else
566     {
567       Handle(SelectMgr_IndexedMapOfOwner) anOwnerMap;
568       if (!anObjectOwnerMap.Find (anOwner->Selectable(), anOwnerMap))
569       {
570         anOwnerMap = new SelectMgr_IndexedMapOfOwner();
571         anObjectOwnerMap.Bind (anOwner->Selectable(), anOwnerMap);
572       }
573
574       anOwnerMap->Add (anOwner);
575     }
576   }
577
578   for (AIS_MapIteratorOfMapOfObjectOwners1 anIter (anObjectOwnerMap); anIter.More(); anIter.Next())
579   {
580     const Handle(SelectMgr_SelectableObject) anObject = anIter.Key();
581     Bnd_Box aTmpBox = anObject->BndBoxOfSelected (anIter.ChangeValue());
582     aBndSelected.Add (aTmpBox);
583   }
584
585   anObjectOwnerMap.Clear();
586
587   return aBndSelected;
588 }