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 <OCCViewer_ViewModel.h>
23 #include <OCCViewer_ViewManager.h>
24 #include <QtxRubberBand.h>
25 #include <QApplication>
27 #include <QMouseEvent>
30 class HYDROGUI_OverviewBand : public QtxPolyRubberBand
33 HYDROGUI_OverviewBand( HYDROGUI_Overview* theOverview );
34 virtual ~HYDROGUI_OverviewBand();
36 void initGeometry( const QPolygon& );
37 void update( bool isFromMain );
38 QPoint center() const;
41 void drag( const QPoint&, bool isStart );
43 void dragging( const QPoint& );
44 bool contains( const QPoint& ) const;
45 QRect boundingRect() const;
48 virtual void paintEvent( QPaintEvent* );
51 HYDROGUI_Overview* myOverview;
53 QPolygon myStartPoints;
58 HYDROGUI_OverviewBand::HYDROGUI_OverviewBand( HYDROGUI_Overview* theOverview )
59 : QtxPolyRubberBand( theOverview->getViewPort( false ) ),
60 myOverview( theOverview ), myIsDrag( false )
64 HYDROGUI_OverviewBand::~HYDROGUI_OverviewBand()
68 bool isEmpty( const QPolygon& thePoly )
70 QSize s = thePoly.boundingRect().size();
71 if( s.width() < 2 || s.height() < 2 )
77 void HYDROGUI_OverviewBand::initGeometry( const QPolygon& thePoly )
79 int w2 = myOverview->width()*2;
80 int h2 = myOverview->height()*2;
83 for( int i=0, n=thePoly.size(); i<n; i++ )
85 QPoint p = thePoly.point( i );
96 QtxPolyRubberBand::initGeometry( thePoly );
97 if( isEmpty( thePoly ) )
103 QRect HYDROGUI_OverviewBand::boundingRect() const
105 return myPoints.boundingRect();
108 QPoint HYDROGUI_OverviewBand::center() const
111 int n = myPoints.size()-1;
115 for( int i=0; i<n; i++ )
116 c += myPoints.point( i );
122 void HYDROGUI_OverviewBand::drag( const QPoint& thePoint, bool isStart )
124 if( myIsDrag==isStart )
129 myStartPoints = myPoints;
130 /*if( contains( thePoint ) )
131 myStartPnt = thePoint;
133 myStartPnt = center();
135 dragging( thePoint );
139 dragging( thePoint );
144 bool HYDROGUI_OverviewBand::isDrag() const
149 void HYDROGUI_OverviewBand::dragging( const QPoint& thePoint )
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 );
159 bool HYDROGUI_OverviewBand::contains( const QPoint& thePoint ) const
161 return myPoints.containsPoint( thePoint, Qt::OddEvenFill );
164 int HYDROGUI_OverviewBand::count() const
166 return myPoints.size();
169 void HYDROGUI_OverviewBand::update( bool isFromMain )
171 OCCViewer_ViewPort3d* main = myOverview->getViewPort( true );
174 int w = main->width();
175 int h = main->height();
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 );
187 initGeometry( poly );
191 OCCViewer_ViewPort3d* overview = myOverview->getViewPort( false );
193 double x1, y1, z1, x2, y2, z2;
194 main->getView()->Convert( main->width()/2, main->height()/2, x1, y1, z1 );
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();
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();
211 aTrsf.SetTranslation( gp_Pnt( x1, y1, z1 ), gp_Pnt( x2, y2, z2 ) );
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);
219 if (!nu.IsEqual (u, 0.0))
222 if (!ne.IsEqual (e, 0.0))
225 if (!ncen.IsEqual(cen, 0.0))
226 cam->SetCenter (ncen);
228 //version for new OCCT:
229 //main->getView()->Camera()->Transform( aTrsf );
234 void HYDROGUI_OverviewBand::paintEvent( QPaintEvent* thePaintEvent )
236 QPainter painter( this );
237 painter.setRenderHint( QPainter::Antialiasing );
239 static QColor aColor = Qt::red;
240 static int aWidth = 2;
241 static QPen aPen( QBrush( aColor ), aWidth, Qt::DashDotLine );
243 painter.setPen( aPen );
244 painter.drawPolygon( myPoints );
252 HYDROGUI_Overview::HYDROGUI_Overview( const QString& theTitle, int theMargin, QWidget* theParent )
253 : QFrame( theParent ), myMargin( theMargin ),
254 myMainView( 0 ), myViewPort( 0 ), myBand( 0 )
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 );
264 HYDROGUI_Overview::~HYDROGUI_Overview()
268 QImage HYDROGUI_Overview::dump() const
273 Handle(V3d_View) view = myViewPort->getView();
277 int aWidth = myViewPort->width();
278 int aHeight = myViewPort->height();
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();
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 );
297 if ( aPix.IsTopDown() )
298 anImage = anImage.mirrored();
300 if( myBand && myBand->isVisible() )
302 QPixmap aPixmap = QPixmap::fromImage( anImage );
303 QPoint p = myBand->boundingRect().topLeft();
304 myBand->render( &aPixmap, p );
305 anImage = aPixmap.toImage();
310 OCCViewer_ViewPort3d* HYDROGUI_Overview::getViewPort( bool isMain ) const
312 return isMain ? myMainView->getViewPort() : myViewPort;
315 void HYDROGUI_Overview::setMainView( OCCViewer_ViewFrame* theMainView )
317 myMainView = theMainView;
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() ) );
331 myViewPort = new OCCViewer_ViewPort3d( this, myMainView->getViewPort()->getViewer(), V3d_ORTHOGRAPHIC );
335 myViewPort->setBackgroundColor( myMainView->getViewPort()->backgroundColor() );
337 connect( myViewPort, SIGNAL( vpMouseEvent( QMouseEvent* ) ),
338 this, SLOT( OnMouseEvent( QMouseEvent* ) ) );
339 connect( myViewPort, SIGNAL( vpResizeEvent( QResizeEvent* ) ),
340 this, SLOT( OnResizeEvent( QResizeEvent* ) ) );
342 myLayout->addWidget( myViewPort, 0, 0 );
346 #if defined(TEST_MODE) || defined(_DEBUG)
347 //qApp->installEventFilter( this );
350 qApp->processEvents();
354 qApp->processEvents();
357 myBand = new HYDROGUI_OverviewBand( this );
359 myBand->update( true );
362 void HYDROGUI_Overview::setTopView()
367 Handle(V3d_View) aView3d = myViewPort->getView();
368 if( !aView3d.IsNull() )
369 aView3d->SetProj( V3d_Zpos );
370 myViewPort->fitAll();
372 // Apply margins for internal area in the view port
375 QRect aRect( -myMargin, -myMargin, myViewPort->width()+2*myMargin, myViewPort->height()+2*myMargin );
376 myViewPort->fitRect( aRect );
380 myBand->update( true );
383 void HYDROGUI_Overview::OnTransformationAfterOp( OCCViewer_ViewWindow::OperationType theOp )
385 if( myViewPort && theOp>=OCCViewer_ViewWindow::WINDOWFIT )
387 myViewPort->fitAll();
389 if( theOp==OCCViewer_ViewWindow::FITSELECTION )
391 CustomFitSelection();
396 void HYDROGUI_Overview::OnTransformation()
399 myBand->update( true );
402 QPoint HYDROGUI_Overview::fromMain( int xp, int yp ) const
404 if( !myMainView || !myViewPort || !myViewPort->isVisible() )
407 const double EPS = 1E-2;
409 Handle(V3d_View) aMain = myMainView->getViewPort()->getView();
410 Handle(V3d_View) aXY = myViewPort->getView();
412 // View coordinates to 3d (world) coordinates
414 aMain->Convert( xp, yp, x, y, z );
416 // Moving to the XY plane
417 gp_Vec aDir = aMain->Camera()->Direction();
418 if( fabs( aDir.Z() )<EPS )
421 double t = -z/aDir.Z();
426 // 3d (world) coordinates to view coordinates in the overview
427 aXY->Convert( x, y, z, xp, yp );
429 return QPoint( xp, yp );
432 void HYDROGUI_Overview::OnMouseEvent( QMouseEvent* theEvent )
434 QPoint mp = theEvent->pos();
438 switch( theEvent->type() )
440 case QEvent::MouseButtonPress:
441 myBand->drag( mp, true );
444 case QEvent::MouseButtonRelease:
445 myBand->drag( mp, false );
448 case QEvent::MouseMove:
449 if( myBand->isDrag() )
450 myBand->dragging( mp );
455 bool HYDROGUI_Overview::eventFilter( QObject* theObject, QEvent* theEvent )
457 #if defined(TEST_MODE) || defined(_DEBUG)
458 /*switch( theEvent->type() )
460 case QEvent::MouseMove:
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() );
471 return QFrame::eventFilter( theObject, theEvent );
474 void HYDROGUI_Overview::OnResizeEvent( QResizeEvent* )
477 myBand->update( true );
480 void HYDROGUI_Overview::CustomFitSelection() const
482 OCCViewer_ViewPort3d* main = getViewPort( true );
486 int w = main->width();
487 int h = main->height();
489 Bnd_Box bounding = BoundingForSelection();
490 if( bounding.IsVoid() )
493 Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
494 bounding.Get( xmin, ymin, zmin, xmax, ymax, zmax );
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 ) );
507 int xpmin, ypmin, xpmax, ypmax;
509 foreach( QPoint p, points )
511 int x = p.x(), y = p.y();
512 if( isFirst || x<xpmin )
514 if( isFirst || x>xpmax )
516 if( isFirst || y<ypmin )
518 if( isFirst || y>ypmax )
524 const int margin = 5;
525 QRect r( xpmin-margin, ypmin-margin, xpmax-xpmin+2*margin, ypmax-ypmin+2*margin );
529 Handle(AIS_InteractiveContext) HYDROGUI_Overview::context() const
533 SUIT_ViewModel* vm = myMainView->getViewManager()->getViewModel();
534 OCCViewer_Viewer* viewer = dynamic_cast<OCCViewer_Viewer*>( vm );
536 return viewer->getAISContext();
538 return Handle(AIS_InteractiveContext)();
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
545 Handle(AIS_InteractiveContext) c = context();
547 Bnd_Box aBndSelected;
549 AIS_MapOfObjectOwners1 anObjectOwnerMap;
550 for (c->InitSelected(); c->MoreSelected(); c->NextSelected())
552 const Handle(SelectMgr_EntityOwner)& anOwner = c->SelectedOwner();
553 Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
554 if (anObj->IsInfinite())
559 if (anOwner == anObj->GlobalSelOwner())
562 anObj->BoundingBox (aTmpBnd);
563 aBndSelected.Add (aTmpBnd);
567 Handle(SelectMgr_IndexedMapOfOwner) anOwnerMap;
568 if (!anObjectOwnerMap.Find (anOwner->Selectable(), anOwnerMap))
570 anOwnerMap = new SelectMgr_IndexedMapOfOwner();
571 anObjectOwnerMap.Bind (anOwner->Selectable(), anOwnerMap);
574 anOwnerMap->Add (anOwner);
578 for (AIS_MapIteratorOfMapOfObjectOwners1 anIter (anObjectOwnerMap); anIter.More(); anIter.Next())
580 const Handle(SelectMgr_SelectableObject) anObject = anIter.Key();
581 Bnd_Box aTmpBox = anObject->BndBoxOfSelected (anIter.ChangeValue());
582 aBndSelected.Add (aTmpBox);
585 anObjectOwnerMap.Clear();