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>
31 #include "HYDRO_trace.hxx"
33 class HYDROGUI_OverviewBand : public QtxPolyRubberBand
36 HYDROGUI_OverviewBand( HYDROGUI_Overview* theOverview );
37 virtual ~HYDROGUI_OverviewBand();
39 void initGeometry( const QPolygon& );
40 void update( bool isFromMain );
41 QPoint center() const;
44 void drag( const QPoint&, bool isStart );
46 void dragging( const QPoint& );
47 bool contains( const QPoint& ) const;
48 QRect boundingRect() const;
51 virtual void paintEvent( QPaintEvent* );
54 HYDROGUI_Overview* myOverview;
56 QPolygon myStartPoints;
61 HYDROGUI_OverviewBand::HYDROGUI_OverviewBand( HYDROGUI_Overview* theOverview )
62 : QtxPolyRubberBand( theOverview->getViewPort( false ) ),
63 myOverview( theOverview ), myIsDrag( false )
67 HYDROGUI_OverviewBand::~HYDROGUI_OverviewBand()
71 bool isEmpty( const QPolygon& thePoly )
73 QSize s = thePoly.boundingRect().size();
74 if( s.width() < 2 || s.height() < 2 )
80 void HYDROGUI_OverviewBand::initGeometry( const QPolygon& thePoly )
82 int w2 = myOverview->width()*2;
83 int h2 = myOverview->height()*2;
86 for( int i=0, n=thePoly.size(); i<n; i++ )
88 QPoint p = thePoly.point( i );
99 QtxPolyRubberBand::initGeometry( thePoly );
100 if( isEmpty( thePoly ) )
106 QRect HYDROGUI_OverviewBand::boundingRect() const
108 return myPoints.boundingRect();
111 QPoint HYDROGUI_OverviewBand::center() const
114 int n = myPoints.size()-1;
118 for( int i=0; i<n; i++ )
119 c += myPoints.point( i );
125 void HYDROGUI_OverviewBand::drag( const QPoint& thePoint, bool isStart )
128 if( myIsDrag==isStart )
133 myStartPoints = myPoints;
134 /*if( contains( thePoint ) )
135 myStartPnt = thePoint;
137 myStartPnt = center();
139 dragging( thePoint );
143 dragging( thePoint );
148 bool HYDROGUI_OverviewBand::isDrag() const
153 void HYDROGUI_OverviewBand::dragging( const QPoint& thePoint )
155 DEBTRACE("dragging");
156 int n = myPoints.size();
157 QPoint delta = thePoint - myStartPnt;
158 for( int i=0; i<n; i++ )
159 myPoints.setPoint( i, myStartPoints.point( i ) + delta );
160 initGeometry( myPoints );
164 bool HYDROGUI_OverviewBand::contains( const QPoint& thePoint ) const
166 return myPoints.containsPoint( thePoint, Qt::OddEvenFill );
169 int HYDROGUI_OverviewBand::count() const
171 return myPoints.size();
174 void HYDROGUI_OverviewBand::update( bool isFromMain )
176 OCCViewer_ViewPort3d* main = myOverview->getViewPort( true );
179 int w = main->width();
180 int h = main->height();
183 QPoint p1 = myOverview->fromMain( 0, 0 );
184 QPoint p2 = myOverview->fromMain( w, 0 );
185 QPoint p3 = myOverview->fromMain( w, h );
186 QPoint p4 = myOverview->fromMain( 0, h );
192 initGeometry( poly );
196 OCCViewer_ViewPort3d* overview = myOverview->getViewPort( false );
198 double x1, y1, z1, x2, y2, z2;
199 main->getView()->Convert( main->width()/2, main->height()/2, x1, y1, z1 );
200 DEBTRACE("x1, y1, z1 " << x1 << " " << y1 << " " << z1);
201 // Patch for OCCT 6.9.1, on 7.0.0 the moving of point to plane XY is not necessary
202 // gp_Dir dm = main->getView()->Camera()->Direction();
203 // double t1 = -z1/dm.Z();
207 // DEBTRACE("x1, y1, z1 " << x1 << " " << y1 << " " << z1);
209 overview->getView()->Convert( c.x(), c.y(), x2, y2, z2 );
210 gp_Dir dov = overview->getView()->Camera()->Direction();
211 double t2 = -z2/dov.Z();
215 DEBTRACE("x2, y2, z2 " << x2 << " " << y2 << " " << z2);
218 aTrsf.SetTranslation( gp_Pnt( x1, y1, z1 ), gp_Pnt( x2, y2, z2 ) );
220 // Temporary patch for bug in OCCT 6.9.1
221 // Handle(Graphic3d_Camera) cam = main->getView()->Camera();
222 // gp_Dir u = cam->Up(), nu = u.Transformed (aTrsf);
223 // gp_Pnt e = cam->Eye(), ne = e.Transformed (aTrsf);
224 // gp_Pnt cen = cam->Center(), ncen = cen.Transformed (aTrsf);
226 // if (!nu.IsEqual (u, 0.0))
229 // DEBTRACE("nu " << nu.X() << " " << nu.Y() << " " << nu.Z());
232 // if (!ne.IsEqual (e, 0.0))
235 // DEBTRACE("ne " << ne.X() << " " << ne.Y() << " " << ne.Z())
238 // if (!ncen.IsEqual(cen, 0.0))
240 // cam->SetCenter (ncen);
241 // DEBTRACE("ncen " << ncen.X() << " " << ncen.Y() << " " << ncen.Z())
244 //version for new OCCT:
245 main->getView()->Camera()->Transform( aTrsf );
250 void HYDROGUI_OverviewBand::paintEvent( QPaintEvent* thePaintEvent )
252 QPainter painter( this );
253 painter.setRenderHint( QPainter::Antialiasing );
255 static QColor aColor = Qt::red;
256 static int aWidth = 2;
257 static QPen aPen( QBrush( aColor ), aWidth, Qt::DashDotLine );
259 painter.setPen( aPen );
260 painter.drawPolygon( myPoints );
265 HYDROGUI_Overview::HYDROGUI_Overview( const QString& theTitle, int theMargin, QWidget* theParent )
266 : QFrame( theParent ), myMargin( theMargin ),
267 myMainView( 0 ), myViewPort( 0 ), myBand( 0 )
269 setWindowTitle( theTitle );
270 myLayout = new QGridLayout( this );
271 myLayout->setMargin( 0 );
272 myLayout->setSpacing( 0 );
273 myLayout->setRowStretch( 0, 1 );
274 myLayout->setColumnStretch( 0, 1 );
277 HYDROGUI_Overview::~HYDROGUI_Overview()
282 QImage HYDROGUI_Overview::dump() const
287 Handle(V3d_View) view = myViewPort->getView();
291 int aWidth = myViewPort->width();
292 int aHeight = myViewPort->height();
296 #if OCC_VERSION_LARGE >= 0x07020000
297 view->ToPixMap( aPix,aWidth, aHeight,Graphic3d_BT_RGBA );
298 QImage anImage( aPix.Data(), aWidth, aHeight, QImage::Format_ARGB32 );
299 anImage = anImage.mirrored();
300 anImage = anImage.rgbSwapped();
302 view->ToPixMap(aPix, aWidth, aHeight, Graphic3d_BT_RGB);
303 QImage anImage( aWidth, aHeight, QImage::Format_ARGB32 );
304 for ( int i = 0; i < aWidth; i++ ) {
305 for ( int j = 0; j < aHeight; j++ ) {
306 Quantity_Color pixel = aPix.PixelColor( i, j ).GetRGB();
307 QColor color = QColor::fromRgbF( pixel.Red(), pixel.Green(), pixel.Blue() );
308 anImage.setPixelColor( i, j, color );
311 if ( aPix.IsTopDown() )
312 anImage = anImage.mirrored();
314 if( myBand && myBand->isVisible() )
316 QPixmap aPixmap = QPixmap::fromImage( anImage );
317 QPoint p = myBand->boundingRect().topLeft();
318 myBand->render( &aPixmap, p );
319 anImage = aPixmap.toImage();
324 OCCViewer_ViewPort3d* HYDROGUI_Overview::getViewPort( bool isMain ) const
328 if (myMainView!=NULL)
329 return myMainView->getViewPort();
337 void HYDROGUI_Overview::setMainView( OCCViewer_ViewFrame* theMainView )
339 myMainView = theMainView;
343 OCCViewer_ViewWindow* aMainView = myMainView->getView( OCCViewer_ViewFrame::MAIN_VIEW );
344 connect( aMainView, SIGNAL( vpTransformationFinished( OCCViewer_ViewWindow::OperationType ) ),
345 this, SLOT( OnTransformationAfterOp( OCCViewer_ViewWindow::OperationType ) ) );
346 connect( aMainView->getViewPort(), SIGNAL( vpResizeEvent( QResizeEvent* ) ),
347 this, SLOT( OnResizeEvent( QResizeEvent* ) ) );
348 connect( aMainView->getViewPort(), SIGNAL( vpTransformed( OCCViewer_ViewPort* ) ),
349 this, SLOT( OnTransformation() ) );
351 connect( myMainView, SIGNAL(destroyed()), this, SLOT( onMainViewDestr() ) );
355 myViewPort = new OCCViewer_ViewPort3d( this, myMainView->getViewPort()->getViewer(), V3d_ORTHOGRAPHIC );
359 myViewPort->setBackgroundColor( myMainView->getViewPort()->backgroundColor() );
361 connect( myViewPort, SIGNAL(destroyed()), this, SLOT( onViewPortDestr() ) );
362 connect( myViewPort, SIGNAL( vpMouseEvent( QMouseEvent* ) ),
363 this, SLOT( OnMouseEvent( QMouseEvent* ) ) );
364 connect( myViewPort, SIGNAL( vpResizeEvent( QResizeEvent* ) ),
365 this, SLOT( OnResizeEvent( QResizeEvent* ) ) );
367 myLayout->addWidget( myViewPort, 0, 0 );
371 #if defined(TEST_MODE) || defined(_DEBUG)
372 //qApp->installEventFilter( this );
375 qApp->processEvents();
379 qApp->processEvents();
382 myBand = new HYDROGUI_OverviewBand( this );
384 myBand->update( true );
387 void HYDROGUI_Overview::setTopView()
392 Handle(V3d_View) aView3d = myViewPort->getView();
393 if( !aView3d.IsNull() )
394 aView3d->SetProj( V3d_Zpos );
395 myViewPort->fitAll();
397 // Apply margins for internal area in the view port
400 QRect aRect( -myMargin, -myMargin, myViewPort->width()+2*myMargin, myViewPort->height()+2*myMargin );
401 myViewPort->fitRect( aRect );
405 myBand->update( true );
408 void HYDROGUI_Overview::OnTransformationAfterOp( OCCViewer_ViewWindow::OperationType theOp )
410 if( myViewPort && theOp>=OCCViewer_ViewWindow::WINDOWFIT )
412 myViewPort->fitAll();
414 if( theOp==OCCViewer_ViewWindow::FITSELECTION )
416 CustomFitSelection();
421 void HYDROGUI_Overview::OnTransformation()
424 myBand->update( true );
427 QPoint HYDROGUI_Overview::fromMain( int xp, int yp ) const
429 if( !myMainView || !myViewPort || !myViewPort->isVisible() )
432 const double EPS = 1E-2;
434 Handle(V3d_View) aMain = myMainView->getViewPort()->getView();
435 Handle(V3d_View) aXY = myViewPort->getView();
437 // View coordinates to 3d (world) coordinates
439 aMain->Convert( xp, yp, x, y, z );
441 // Moving to the XY plane
442 gp_Vec aDir = aMain->Camera()->Direction();
443 if( fabs( aDir.Z() )<EPS )
446 double t = -z/aDir.Z();
451 // 3d (world) coordinates to view coordinates in the overview
452 aXY->Convert( x, y, z, xp, yp );
454 return QPoint( xp, yp );
457 void HYDROGUI_Overview::OnMouseEvent( QMouseEvent* theEvent )
459 QPoint mp = theEvent->pos();
463 switch( theEvent->type() )
465 case QEvent::MouseButtonPress:
466 myBand->drag( mp, true );
469 case QEvent::MouseButtonRelease:
470 myBand->drag( mp, false );
473 case QEvent::MouseMove:
474 if( myBand->isDrag() )
475 myBand->dragging( mp );
480 bool HYDROGUI_Overview::eventFilter( QObject* theObject, QEvent* theEvent )
482 #if defined(TEST_MODE) || defined(_DEBUG)
483 /*switch( theEvent->type() )
485 case QEvent::MouseMove:
487 QPoint mp = ((QMouseEvent*)theEvent)->pos();
488 //mp = getViewPort(false)->mapFromGlobal(mp);
489 QString coords = QString( "(%0, %1)" ).arg( mp.x() ).arg( mp.y() );
490 std::string scoords = coords.toStdString();
491 qDebug( scoords.c_str() );
496 return QFrame::eventFilter( theObject, theEvent );
499 void HYDROGUI_Overview::OnResizeEvent( QResizeEvent* )
502 myBand->update( true );
506 void HYDROGUI_Overview::onMainViewDestr()
509 if (myViewPort == NULL)
511 Handle(V3d_View) ov = myViewPort->getView();
512 ov->View()->Deactivate();
513 delete myViewPort; //this will delete myBand
518 void HYDROGUI_Overview::onViewPortDestr()
524 void HYDROGUI_Overview::CustomFitSelection() const
526 DEBTRACE("CustomFitSelection");
527 OCCViewer_ViewPort3d* main = getViewPort( true );
531 int w = main->width();
532 int h = main->height();
534 Bnd_Box bounding = BoundingForSelection();
535 if( bounding.IsVoid() )
538 Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
539 bounding.Get( xmin, ymin, zmin, xmax, ymax, zmax );
541 QList<QPoint> points;
542 Standard_Integer xp, yp;
543 main->getView()->Convert( xmin, ymin, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
544 main->getView()->Convert( xmax, ymin, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
545 main->getView()->Convert( xmin, ymax, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
546 main->getView()->Convert( xmax, ymax, zmin, xp, yp ); points.append( QPoint( xp, yp ) );
547 main->getView()->Convert( xmin, ymin, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
548 main->getView()->Convert( xmax, ymin, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
549 main->getView()->Convert( xmin, ymax, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
550 main->getView()->Convert( xmax, ymax, zmax, xp, yp ); points.append( QPoint( xp, yp ) );
552 int xpmin, ypmin, xpmax, ypmax;
554 foreach( QPoint p, points )
556 int x = p.x(), y = p.y();
557 if( isFirst || x<xpmin )
559 if( isFirst || x>xpmax )
561 if( isFirst || y<ypmin )
563 if( isFirst || y>ypmax )
569 const int margin = 5;
570 QRect r( xpmin-margin, ypmin-margin, xpmax-xpmin+2*margin, ypmax-ypmin+2*margin );
574 Handle(AIS_InteractiveContext) HYDROGUI_Overview::context() const
578 SUIT_ViewModel* vm = myMainView->getViewManager()->getViewModel();
579 OCCViewer_Viewer* viewer = dynamic_cast<OCCViewer_Viewer*>( vm );
581 return viewer->getAISContext();
583 return Handle(AIS_InteractiveContext)();
586 typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)> AIS_MapOfObjectOwners1;
587 typedef NCollection_DataMap<Handle(SelectMgr_SelectableObject), Handle(SelectMgr_IndexedMapOfOwner)>::Iterator AIS_MapIteratorOfMapOfObjectOwners1;
588 Bnd_Box HYDROGUI_Overview::BoundingForSelection() const
590 DEBTRACE("BoundingForSelection");
591 Handle(AIS_InteractiveContext) c = context();
593 Bnd_Box aBndSelected;
595 AIS_MapOfObjectOwners1 anObjectOwnerMap;
596 for (c->InitSelected(); c->MoreSelected(); c->NextSelected())
598 const Handle(SelectMgr_EntityOwner)& anOwner = c->SelectedOwner();
599 Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
600 if (anObj->IsInfinite())
605 if (anOwner == anObj->GlobalSelOwner())
608 anObj->BoundingBox (aTmpBnd);
609 aBndSelected.Add (aTmpBnd);
613 Handle(SelectMgr_IndexedMapOfOwner) anOwnerMap;
614 if (!anObjectOwnerMap.Find (anOwner->Selectable(), anOwnerMap))
616 anOwnerMap = new SelectMgr_IndexedMapOfOwner();
617 anObjectOwnerMap.Bind (anOwner->Selectable(), anOwnerMap);
620 anOwnerMap->Add (anOwner);
624 for (AIS_MapIteratorOfMapOfObjectOwners1 anIter (anObjectOwnerMap); anIter.More(); anIter.Next())
626 const Handle(SelectMgr_SelectableObject) anObject = anIter.Key();
627 Bnd_Box aTmpBox = anObject->BndBoxOfSelected (anIter.ChangeValue());
628 aBndSelected.Add (aTmpBox);
631 anObjectOwnerMap.Clear();