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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROGUI_Overview.h>
20 #include <OCCViewer_ViewPort3d.h>
21 #include <OCCViewer_ViewFrame.h>
22 #include <QtxRubberBand.h>
23 #include <QApplication>
25 #include <QMouseEvent>
28 class HYDROGUI_OverviewBand : public QtxPolyRubberBand
31 HYDROGUI_OverviewBand( HYDROGUI_Overview* theOverview );
32 virtual ~HYDROGUI_OverviewBand();
34 void initGeometry( const QPolygon& );
35 void update( bool isFromMain );
36 QPoint center() const;
39 void drag( const QPoint&, bool isStart );
41 void dragging( const QPoint& );
42 bool contains( const QPoint& ) const;
43 QRect boundingRect() const;
46 virtual void paintEvent( QPaintEvent* );
49 HYDROGUI_Overview* myOverview;
51 QPolygon myStartPoints;
56 HYDROGUI_OverviewBand::HYDROGUI_OverviewBand( HYDROGUI_Overview* theOverview )
57 : QtxPolyRubberBand( theOverview->getViewPort( false ) ),
58 myOverview( theOverview ), myIsDrag( false )
62 HYDROGUI_OverviewBand::~HYDROGUI_OverviewBand()
66 bool isEmpty( const QPolygon& thePoly )
68 QSize s = thePoly.boundingRect().size();
69 if( s.width() < 2 || s.height() < 2 )
75 void HYDROGUI_OverviewBand::initGeometry( const QPolygon& thePoly )
77 int w2 = myOverview->width()*2;
78 int h2 = myOverview->height()*2;
81 for( int i=0, n=thePoly.size(); i<n; i++ )
83 QPoint p = thePoly.point( i );
94 QtxPolyRubberBand::initGeometry( thePoly );
95 if( isEmpty( thePoly ) )
101 QRect HYDROGUI_OverviewBand::boundingRect() const
103 return myPoints.boundingRect();
106 QPoint HYDROGUI_OverviewBand::center() const
109 int n = myPoints.size()-1;
113 for( int i=0; i<n; i++ )
114 c += myPoints.point( i );
120 void HYDROGUI_OverviewBand::drag( const QPoint& thePoint, bool isStart )
122 if( myIsDrag==isStart )
127 myStartPoints = myPoints;
128 if( contains( thePoint ) )
129 myStartPnt = thePoint;
131 myStartPnt = center();
133 dragging( thePoint );
137 dragging( thePoint );
142 bool HYDROGUI_OverviewBand::isDrag() const
147 void HYDROGUI_OverviewBand::dragging( const QPoint& thePoint )
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 );
157 bool HYDROGUI_OverviewBand::contains( const QPoint& thePoint ) const
159 return myPoints.containsPoint( thePoint, Qt::OddEvenFill );
162 int HYDROGUI_OverviewBand::count() const
164 return myPoints.size();
167 void HYDROGUI_OverviewBand::update( bool isFromMain )
169 OCCViewer_ViewPort3d* main = myOverview->getViewPort( true );
172 int w = main->width();
173 int h = main->height();
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 );
185 initGeometry( poly );
189 OCCViewer_ViewPort3d* overview = myOverview->getViewPort( false );
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 );
196 aTrsf.SetTranslation( gp_Pnt( x1, y1, z1 ), gp_Pnt( x2, y2, z2 ) );
197 main->getView()->Camera()->Transform( aTrsf );
202 void HYDROGUI_OverviewBand::paintEvent( QPaintEvent* thePaintEvent )
204 QPainter painter( this );
205 painter.setRenderHint( QPainter::Antialiasing );
207 static QColor aColor = Qt::red;
208 static int aWidth = 2;
209 static QPen aPen( QBrush( aColor ), aWidth, Qt::SolidLine );
211 painter.setPen( aPen );
212 painter.drawPolygon( myPoints );
220 HYDROGUI_Overview::HYDROGUI_Overview( const QString& theTitle, int theMargin, QWidget* theParent )
221 : QFrame( theParent ), myMargin( theMargin ),
222 myMainView( 0 ), myViewPort( 0 ), myBand( 0 )
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 );
232 HYDROGUI_Overview::~HYDROGUI_Overview()
236 QImage HYDROGUI_Overview::dump() const
241 Handle(V3d_View) view = myViewPort->getView();
245 int aWidth = myViewPort->width();
246 int aHeight = myViewPort->height();
249 view->ToPixMap( aPix,aWidth, aHeight,Graphic3d_BT_RGBA );
251 QImage anImage( aPix.Data(), aWidth, aHeight, QImage::Format_ARGB32 );
252 anImage = anImage.mirrored();
253 anImage = anImage.rgbSwapped();
255 if( myBand && myBand->isVisible() )
257 QPixmap aPixmap = QPixmap::fromImage( anImage );
258 QPoint p = myBand->boundingRect().topLeft();
259 myBand->render( &aPixmap, p );
260 anImage = aPixmap.toImage();
266 OCCViewer_ViewPort3d* HYDROGUI_Overview::getViewPort( bool isMain ) const
268 return isMain ? myMainView->getViewPort() : myViewPort;
271 void HYDROGUI_Overview::setMainView( OCCViewer_ViewFrame* theMainView )
273 myMainView = theMainView;
277 OCCViewer_ViewWindow* aMainView = myMainView->getView( OCCViewer_ViewFrame::MAIN_VIEW );
278 connect( aMainView, SIGNAL( vpTransformationFinished( OCCViewer_ViewWindow::OperationType ) ),
279 this, SLOT( OnTransformationAfterOp( OCCViewer_ViewWindow::OperationType ) ) );
280 connect( aMainView->getViewPort(), SIGNAL( vpResizeEvent( QResizeEvent* ) ),
281 this, SLOT( OnResizeEvent( QResizeEvent* ) ) );
282 connect( aMainView->getViewPort(), SIGNAL( vpTransformed( OCCViewer_ViewPort* ) ),
283 this, SLOT( OnTransformation() ) );
287 myViewPort = new OCCViewer_ViewPort3d( this, myMainView->getViewPort()->getViewer(), V3d_ORTHOGRAPHIC );
288 //myViewPort->setBackgroundColor( Qt::white );
290 connect( myViewPort, SIGNAL( vpMouseEvent( QMouseEvent* ) ),
291 this, SLOT( OnMouseEvent( QMouseEvent* ) ) );
292 connect( myViewPort, SIGNAL( vpResizeEvent( QResizeEvent* ) ),
293 this, SLOT( OnResizeEvent( QResizeEvent* ) ) );
295 myLayout->addWidget( myViewPort, 0, 0 );
298 #if defined(TEST_MODE) || defined(_DEBUG)
299 //qApp->installEventFilter( this );
302 qApp->processEvents();
306 qApp->processEvents();
309 myBand = new HYDROGUI_OverviewBand( this );
311 myBand->update( true );
314 void HYDROGUI_Overview::setTopView()
316 Handle(V3d_View) aView3d = myViewPort->getView();
317 if( !aView3d.IsNull() )
318 aView3d->SetProj( V3d_Zpos );
319 myViewPort->fitAll();
321 // Apply margins for internal area in the view port
324 QRect aRect( -myMargin, -myMargin, myViewPort->width()+2*myMargin, myViewPort->height()+2*myMargin );
325 myViewPort->fitRect( aRect );
329 myBand->update( true );
332 void HYDROGUI_Overview::OnTransformationAfterOp( OCCViewer_ViewWindow::OperationType theOp )
334 if( theOp>=OCCViewer_ViewWindow::WINDOWFIT )
336 myViewPort->fitAll();
341 void HYDROGUI_Overview::OnTransformation()
344 myBand->update( true );
347 QPoint HYDROGUI_Overview::fromMain( int xp, int yp ) const
349 const double EPS = 1E-2;
351 Handle(V3d_View) aMain = myMainView->getViewPort()->getView();
352 Handle(V3d_View) aXY = myViewPort->getView();
354 // View coordinates to 3d (world) coordinates
356 aMain->Convert( xp, yp, x, y, z );
358 // Moving to the XY plane
359 gp_Vec aDir = aMain->Camera()->Direction();
360 if( fabs( aDir.Z() )<EPS )
363 double t = -z/aDir.Z();
368 // 3d (world) coordinates to view coordinates in the overview
369 aXY->Convert( x, y, z, xp, yp );
371 return QPoint( xp, yp );
374 void HYDROGUI_Overview::OnMouseEvent( QMouseEvent* theEvent )
376 QPoint mp = theEvent->pos();
380 switch( theEvent->type() )
382 case QEvent::MouseButtonPress:
383 myBand->drag( mp, true );
386 case QEvent::MouseButtonRelease:
387 myBand->drag( mp, false );
390 case QEvent::MouseMove:
391 if( myBand->isDrag() )
392 myBand->dragging( mp );
397 bool HYDROGUI_Overview::eventFilter( QObject* theObject, QEvent* theEvent )
399 #if defined(TEST_MODE) || defined(_DEBUG)
400 /*switch( theEvent->type() )
402 case QEvent::MouseMove:
404 QPoint mp = ((QMouseEvent*)theEvent)->pos();
405 //mp = getViewPort(false)->mapFromGlobal(mp);
406 QString coords = QString( "(%0, %1)" ).arg( mp.x() ).arg( mp.y() );
407 std::string scoords = coords.toStdString();
408 qDebug( scoords.c_str() );
413 return QFrame::eventFilter( theObject, theEvent );
416 void HYDROGUI_Overview::OnResizeEvent( QResizeEvent* )
419 myBand->update( true );