Salome HOME
Merge branch 'BR_1330' into BR_DEMO
[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 <QtxRubberBand.h>
23 #include <QApplication>
24 #include <QPainter>
25 #include <QMouseEvent>
26 #include <QLayout>
27
28 class HYDROGUI_OverviewBand : public QtxPolyRubberBand
29 {
30 public:
31   HYDROGUI_OverviewBand( HYDROGUI_Overview* theOverview );
32   virtual ~HYDROGUI_OverviewBand();
33
34   void initGeometry( const QPolygon& );
35   void update( bool isFromMain );
36   QPoint center() const;
37   int    count() const;
38
39   void   drag( const QPoint&, bool isStart );
40   bool   isDrag() const;
41   void   dragging( const QPoint& );
42   bool   contains( const QPoint& ) const;
43   QRect  boundingRect() const;
44
45 protected:
46   virtual void paintEvent( QPaintEvent* );
47
48 private:
49   HYDROGUI_Overview* myOverview;
50   bool               myIsDrag;
51   QPolygon           myStartPoints;
52   QPoint             myStartPnt;
53 };
54
55
56 HYDROGUI_OverviewBand::HYDROGUI_OverviewBand( HYDROGUI_Overview* theOverview )
57   : QtxPolyRubberBand( theOverview->getViewPort( false ) ),
58     myOverview( theOverview ), myIsDrag( false )
59 {
60 }
61
62 HYDROGUI_OverviewBand::~HYDROGUI_OverviewBand()
63 {
64 }
65
66 bool isEmpty( const QPolygon& thePoly )
67 {
68   QSize s = thePoly.boundingRect().size();
69   if( s.width() < 2 || s.height() < 2 )
70     return true;
71   else
72     return false;
73 }
74
75 void HYDROGUI_OverviewBand::initGeometry( const QPolygon& thePoly )
76 {
77   int w2 = myOverview->width()*2;
78   int h2 = myOverview->height()*2;
79
80   QPolygon aPoly;
81   for( int i=0, n=thePoly.size(); i<n; i++ )
82   {
83     QPoint p = thePoly.point( i );
84     if( p.x() < -w2 )
85       p.setX( -w2 );
86     if( p.x() > w2 )
87       p.setX( w2 );
88     if( p.y() < -h2 )
89       p.setY( -h2 );
90     if( p.y() > h2 )
91       p.setY( h2 );
92   }
93
94   QtxPolyRubberBand::initGeometry( thePoly );
95   if( isEmpty( thePoly ) )
96     hide();
97   else
98     show();
99 }
100
101 QRect HYDROGUI_OverviewBand::boundingRect() const
102 {
103   return myPoints.boundingRect();
104 }
105
106 QPoint HYDROGUI_OverviewBand::center() const
107 {
108   QPoint c;
109   int n = myPoints.size()-1;
110   if( n==0 )
111     return QPoint();
112
113   for( int i=0; i<n; i++ )
114     c += myPoints.point( i );
115
116   c = c/n;
117   return c;
118 }
119
120 void HYDROGUI_OverviewBand::drag( const QPoint& thePoint, bool isStart )
121 {
122   if( myIsDrag==isStart )
123     return;
124
125   if( isStart )
126   {
127     myStartPoints = myPoints;
128     if( contains( thePoint ) )
129       myStartPnt = thePoint;
130     else
131       myStartPnt = center();
132     myIsDrag = true;
133     dragging( thePoint );
134   }
135   else
136   {
137     dragging( thePoint );
138     myIsDrag = false;
139   }
140 }
141
142 bool HYDROGUI_OverviewBand::isDrag() const
143 {
144   return myIsDrag;
145 }
146
147 void HYDROGUI_OverviewBand::dragging( const QPoint& thePoint )
148 {
149   int n = myPoints.size();
150   QPoint delta = thePoint - myStartPnt;
151   for( int i=0; i<n; i++ )
152     myPoints.setPoint( i, myStartPoints.point( i ) + delta );
153   initGeometry( myPoints );
154   update( false );
155 }
156
157 bool HYDROGUI_OverviewBand::contains( const QPoint& thePoint ) const
158 {
159   return myPoints.containsPoint( thePoint, Qt::OddEvenFill );
160 }
161
162 int HYDROGUI_OverviewBand::count() const
163 {
164   return myPoints.size();
165 }
166
167 void HYDROGUI_OverviewBand::update( bool isFromMain )
168 {
169   OCCViewer_ViewPort3d* main = myOverview->getViewPort( true );
170   if( isFromMain )
171   {
172     int w = main->width();
173     int h = main->height();
174
175     QPolygon poly;
176     QPoint p1 = myOverview->fromMain( 0, 0 );
177     QPoint p2 = myOverview->fromMain( w, 0 );
178     QPoint p3 = myOverview->fromMain( w, h );
179     QPoint p4 = myOverview->fromMain( 0, h );
180     poly.append( p1 );
181     poly.append( p2 );
182     poly.append( p3 );
183     poly.append( p4 );
184     poly.append( p1 );
185     initGeometry( poly );
186   }
187   else
188   {
189     OCCViewer_ViewPort3d* overview = myOverview->getViewPort( false );
190     QPoint c = center();
191     double x1, y1, z1, x2, y2, z2;
192     main->getView()->Convert( main->width()/2, main->height()/2, x1, y1, z1 );
193     overview->getView()->Convert( c.x(), c.y(), x2, y2, z2 );
194
195     gp_Trsf aTrsf;
196     aTrsf.SetTranslation( gp_Pnt( x1, y1, z1 ), gp_Pnt( x2, y2, z2 ) );
197     main->getView()->Camera()->Transform( aTrsf );
198     main->repaint();
199   }
200 }
201
202 void HYDROGUI_OverviewBand::paintEvent( QPaintEvent* thePaintEvent )
203 {
204   QPainter painter( this );
205   painter.setRenderHint( QPainter::Antialiasing );
206
207   static QColor aColor = Qt::red;
208   static int aWidth = 2;
209   static QPen aPen( QBrush( aColor ), aWidth, Qt::SolidLine );
210
211   painter.setPen( aPen );
212   painter.drawPolygon( myPoints );
213 }
214
215
216
217
218
219
220 HYDROGUI_Overview::HYDROGUI_Overview( const QString& theTitle, int theMargin, QWidget* theParent )
221   : QFrame( theParent ), myMargin( theMargin ),
222     myMainView( 0 ), myViewPort( 0 ), myBand( 0 )
223 {
224   setWindowTitle( theTitle );
225   myLayout = new QGridLayout( this );
226   myLayout->setMargin( 0 );
227   myLayout->setSpacing( 0 );
228   myLayout->setRowStretch( 0, 1 );
229   myLayout->setColumnStretch( 0, 1 );
230 }
231
232 HYDROGUI_Overview::~HYDROGUI_Overview()
233 {
234 }
235
236 QImage HYDROGUI_Overview::dump() const
237 {
238   if( !myViewPort )
239     return QImage();
240
241   Handle(V3d_View) view = myViewPort->getView();
242   if( view.IsNull() )
243     return QImage();
244
245   int aWidth = myViewPort->width();
246   int aHeight = myViewPort->height();
247
248   Image_PixMap aPix;
249   view->ToPixMap( aPix,aWidth, aHeight,Graphic3d_BT_RGBA );
250
251   QImage anImage( aPix.Data(), aWidth, aHeight, QImage::Format_ARGB32 );
252   anImage = anImage.mirrored();
253   anImage = anImage.rgbSwapped();
254
255   if( myBand && myBand->isVisible() )
256   {
257     QPixmap aPixmap = QPixmap::fromImage( anImage );
258     QPoint p = myBand->boundingRect().topLeft();
259     myBand->render( &aPixmap, p );
260     anImage = aPixmap.toImage();
261   }
262
263   return anImage;
264 }
265
266 OCCViewer_ViewPort3d* HYDROGUI_Overview::getViewPort( bool isMain ) const
267 {
268   return isMain ? myMainView->getViewPort() : myViewPort;
269 }
270
271 void HYDROGUI_Overview::setMainView( OCCViewer_ViewFrame* theMainView )
272 {
273   myMainView = theMainView;
274   if( !myMainView )
275     return;
276
277   OCCViewer_ViewWindow* aMainView = myMainView->getView( OCCViewer_ViewFrame::MAIN_VIEW );
278   connect( aMainView, SIGNAL( vpTransformationFinished( OCCViewer_ViewWindow::OperationType ) ),
279            this,      SLOT( OnTransformation() ) );
280   connect( aMainView->getViewPort(), SIGNAL( vpResizeEvent( QResizeEvent* ) ),
281            this,       SLOT( OnResizeEvent( QResizeEvent* ) ) );
282   connect( aMainView->getViewPort(), SIGNAL( vpTransformed( OCCViewer_ViewPort* ) ),
283            this,       SLOT( OnTransformation() ) );
284
285   if( !myViewPort )
286   {
287     myViewPort = new OCCViewer_ViewPort3d( this, myMainView->getViewPort()->getViewer(), V3d_ORTHOGRAPHIC );
288     //myViewPort->setBackgroundColor( Qt::white );
289
290     connect( myViewPort, SIGNAL( vpMouseEvent( QMouseEvent* ) ), 
291             this,       SLOT( OnMouseEvent( QMouseEvent* ) ) );
292     connect( myViewPort, SIGNAL( vpResizeEvent( QResizeEvent* ) ),
293             this,       SLOT( OnResizeEvent( QResizeEvent* ) ) );
294
295     myLayout->addWidget( myViewPort, 0, 0 );
296   }
297
298 #if defined(TEST_MODE) || defined(_DEBUG)
299   //qApp->installEventFilter( this );
300 #endif
301
302   qApp->processEvents();
303
304   setTopView();
305
306   qApp->processEvents();
307
308   if( !myBand )
309     myBand = new HYDROGUI_OverviewBand( this );
310
311   myBand->update( true );
312 }
313
314 void HYDROGUI_Overview::setTopView()
315 {
316   Handle(V3d_View) aView3d = myViewPort->getView();
317   if( !aView3d.IsNull() )
318     aView3d->SetProj( V3d_Zpos );
319   myViewPort->fitAll();
320
321   // Apply margins for internal area in the view port
322   if( myMargin>0 )
323   {
324     QRect aRect( -myMargin, -myMargin, myViewPort->width()+2*myMargin, myViewPort->height()+2*myMargin );
325     myViewPort->fitRect( aRect );
326   }
327
328   if( myBand )
329     myBand->update( true );
330 }
331
332 void HYDROGUI_Overview::OnTransformation()
333 {
334   if( myBand )
335     myBand->update( true );
336 }
337
338 QPoint HYDROGUI_Overview::fromMain( int xp, int yp ) const
339 {
340   const double EPS = 1E-2;
341
342   Handle(V3d_View) aMain = myMainView->getViewPort()->getView();
343   Handle(V3d_View) aXY = myViewPort->getView();
344
345   // View coordinates to 3d (world) coordinates
346   double x, y, z;
347   aMain->Convert( xp, yp, x, y, z );
348
349   // Moving to the XY plane
350   gp_Vec aDir = aMain->Camera()->Direction();
351   if( fabs( aDir.Z() )<EPS )
352     return QPoint();
353
354   double t = -z/aDir.Z();
355   x += t * aDir.X();
356   y += t * aDir.Y();
357   z += t * aDir.Z();
358
359   // 3d (world) coordinates to view coordinates in the overview
360   aXY->Convert( x, y, z, xp, yp );
361
362   return QPoint( xp, yp );
363 }
364
365 void HYDROGUI_Overview::OnMouseEvent( QMouseEvent* theEvent )
366 {
367   QPoint mp = theEvent->pos();
368   if( !myBand )
369     return;
370
371   switch( theEvent->type() )
372   {
373   case QEvent::MouseButtonPress:
374     myBand->drag( mp, true );
375     break;
376
377   case QEvent::MouseButtonRelease:
378     myBand->drag( mp, false );
379     break;
380
381   case QEvent::MouseMove:
382     if( myBand->isDrag() )
383       myBand->dragging( mp );
384     break;
385   }
386 }
387
388 bool HYDROGUI_Overview::eventFilter( QObject* theObject, QEvent* theEvent )
389 {
390 #if defined(TEST_MODE) || defined(_DEBUG)
391   /*switch( theEvent->type() )
392   {
393   case QEvent::MouseMove:
394     {
395       QPoint mp = ((QMouseEvent*)theEvent)->pos();
396       //mp = getViewPort(false)->mapFromGlobal(mp);
397       QString coords = QString( "(%0, %1)" ).arg( mp.x() ).arg( mp.y() );
398       std::string scoords = coords.toStdString();
399       qDebug( scoords.c_str() );
400     }
401     break;
402   }*/
403 #endif
404   return QFrame::eventFilter( theObject, theEvent );
405 }
406
407 void HYDROGUI_Overview::OnResizeEvent( QResizeEvent* )
408 {
409   if( myBand )
410     myBand->update( true );
411 }