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