1 // File: GLViewer_ViewPort2d.cxx
2 // Created: November, 2004
4 // Copyright (C) CEA 2004
6 /* GLViewer_ViewPort2d Source File */
8 #include "GLViewer_ViewPort2d.h"
9 #include "GLViewer_Viewer2d.h"
10 #include "GLViewer_ViewFrame.h"
11 #include "GLViewer_Context.h"
12 #include "GLViewer_Object.h"
13 #include "QtxToolTip.h"
15 //#include "QAD_Desktop.h"
17 #include <Precision.hxx>
23 //#include <iostream.h>
28 #include <qpopupmenu.h>
31 #include <qapplication.h>
32 #include <qclipboard.h>
36 /***************************************************************************
37 ** Class: GLViewer_RectangularGrid
38 ** Descr: OpenGL Grid for GLViewer_ViewPort2d
40 ** Created: UI team, 16.09.02
41 ****************************************************************************/
44 #define STEP 2*PI/SEGMENTS
49 GLViewer_RectangularGrid::GLViewer_RectangularGrid() :
50 myGridList( -1 ), myGridHeight( (GLfloat)0.0 ), myGridWidth( (GLfloat)0.0 ),
51 myWinW( (GLfloat)0.0 ), myWinH( (GLfloat)0.0 ), myXSize( (GLfloat)0.0 ), myYSize( (GLfloat)0.0 ),
52 myXPan( (GLfloat)0.0 ), myYPan( (GLfloat)0.0 ), myXScale( (GLfloat)1.0 ), myYScale( (GLfloat)1.0 ),
53 myLineWidth( (GLfloat)0.05 ), myCenterWidth( (GLfloat)1.5 ), myCenterRadius( (GLint)5.0 ),
54 myScaleFactor( 10 ), myIsUpdate( GL_FALSE )
59 myAxisColor[0] = 0.75;
60 myAxisColor[1] = 0.75;
61 myAxisColor[2] = 0.75;
64 GLViewer_RectangularGrid::GLViewer_RectangularGrid( GLfloat width, GLfloat height,
65 GLfloat winW, GLfloat winH,
66 GLfloat xSize, GLfloat ySize,
67 GLfloat xPan, GLfloat yPan,
68 GLfloat xScale, GLfloat yScale ) :
69 myGridList( -1 ), myGridHeight( (GLfloat)0.0 ), myGridWidth( (GLfloat)0.0 ),
70 myWinW( (GLfloat)0.0 ), myWinH( (GLfloat)0.0 ), myXSize( (GLfloat)0.0 ), myYSize( (GLfloat)0.0 ),
71 myXPan( (GLfloat)0.0 ), myYPan( (GLfloat)0.0 ), myXScale( (GLfloat)1.0 ), myYScale( (GLfloat)1.0 ),
72 myLineWidth( (GLfloat)0.05 ), myCenterWidth( (GLfloat)1.5 ), myCenterRadius( (GLint)5.0 ),
73 myScaleFactor( 10 ), myIsUpdate( GL_FALSE )
78 myAxisColor[0] = 0.75;
79 myAxisColor[1] = 0.75;
80 myAxisColor[2] = 0.75;
83 GLViewer_RectangularGrid::~GLViewer_RectangularGrid()
87 void GLViewer_RectangularGrid::draw()
89 if ( myGridList == -1 || myIsUpdate )
92 glCallList( myGridList );
95 void GLViewer_RectangularGrid::setGridColor( GLfloat r, GLfloat g, GLfloat b )
97 if( myGridColor[0] == r && myGridColor[1] == g && myGridColor[2] == b )
103 myIsUpdate = GL_TRUE;
106 void GLViewer_RectangularGrid::setAxisColor( GLfloat r, GLfloat g, GLfloat b )
108 if( myAxisColor[0] == r && myAxisColor[1] == g && myAxisColor[2] == b )
114 myIsUpdate = GL_TRUE;
117 void GLViewer_RectangularGrid::setGridWidth( float w )
119 if( myGridWidth == w )
123 myIsUpdate = GL_TRUE;
126 void GLViewer_RectangularGrid::setCenterRadius( int r )
128 if( myCenterRadius == r )
132 myIsUpdate = GL_TRUE;
135 void GLViewer_RectangularGrid::setSize( float xSize, float ySize )
137 if( myXSize == xSize && myYSize == ySize )
142 myIsUpdate = GL_TRUE;
145 void GLViewer_RectangularGrid::setPan( float xPan, float yPan )
147 if( myXPan == xPan && myYPan == yPan )
152 myIsUpdate = GL_TRUE;
155 bool GLViewer_RectangularGrid::setZoom( float zoom )
161 float bXScale = myXScale;
162 float bYScale = myYScale;
167 if( fabs(myXScale) < Precision::Confusion() || fabs(myYScale) < Precision::Confusion() )
175 myGridHeight /= zoom;
176 myIsUpdate = GL_TRUE;
180 void GLViewer_RectangularGrid::setResize( float WinW, float WinH, float zoom )
182 if( myWinW == WinW && myWinH == WinH && zoom == 1.0 )
185 myGridWidth = myGridWidth + ( WinW - myWinW ) * myXScale;
186 myGridHeight = myGridHeight + ( WinH - myWinH ) * myYScale;
190 myIsUpdate = GL_TRUE;
193 void GLViewer_RectangularGrid::getSize( float& xSize, float& ySize ) const
199 void GLViewer_RectangularGrid::getPan( float& xPan, float& yPan ) const
205 void GLViewer_RectangularGrid::getScale( float& xScale, float& yScale ) const
211 bool GLViewer_RectangularGrid::initList()
213 myIsUpdate = GL_FALSE;
218 if( myXSize == (GLfloat)0.0 )
219 myXSize = (GLfloat)0.1;
220 if( myYSize == (GLfloat)0.0 )
221 myYSize = (GLfloat)0.1;
224 if( ( myXSize >= myGridWidth/5 ) && ( myYSize >= myGridHeight/5 ) )
226 myXSize /= myScaleFactor;
227 myYSize /= myScaleFactor;
230 else if( ( myXSize * myScaleFactor < myGridWidth/5 )
231 || ( myYSize * myScaleFactor < myGridHeight/5 ) )
233 myXSize *= myScaleFactor;
234 myYSize *= myScaleFactor;
238 n = ( int )( myGridWidth / myXSize );
239 m = ( int )( myGridHeight / myYSize );
241 if( ( n != 0 ) || ( m != 0 ) )
243 if ( myGridList != -1 )
245 glDeleteLists( myGridList, 1 );
246 if ( glGetError() != GL_NO_ERROR )
250 xLoc1 = ( int )( myXPan / myXSize );
251 yLoc1 = ( int )( myYPan / myYSize );
253 xLoc = xLoc1 * myXSize;
254 yLoc = yLoc1 * myYSize;
256 myGridList = glGenLists( 1 );
257 glNewList( myGridList, GL_COMPILE );
259 glColor3f( myGridColor[0], myGridColor[1], myGridColor[2] );
260 glLineWidth( myLineWidth );
263 for( int j = 0; ( j-1 ) * myXSize <= myGridWidth / 2 ; j++ )
265 glVertex2d( -myXSize * j - xLoc, -myGridHeight / 2 - myYSize - yLoc );
266 glVertex2d( -myXSize * j - xLoc, myGridHeight / 2 + myYSize - yLoc );
267 glVertex2d( myXSize * j - xLoc, -myGridHeight / 2 - myYSize - yLoc );
268 glVertex2d( myXSize * j - xLoc, myGridHeight / 2 + myYSize - yLoc );
270 for( int i = 0; ( i-1 ) * myYSize <= myGridHeight / 2 ; i++)
272 glVertex2d( -myGridWidth / 2 - myXSize - xLoc, -myYSize * i - yLoc );
273 glVertex2d( myGridWidth / 2 + myXSize - xLoc, -myYSize * i - yLoc );
274 glVertex2d( -myGridWidth / 2 - myXSize - xLoc, myYSize * i - yLoc );
275 glVertex2d( myGridWidth / 2 + myXSize - xLoc, myYSize * i - yLoc );
279 glColor3f( myAxisColor[0], myAxisColor[1], myAxisColor[2] );
280 glLineWidth( myCenterWidth );
283 glVertex2d( myGridWidth / 2 + myXSize - xLoc, 0);
284 glVertex2d( -myGridWidth / 2 - myXSize - xLoc, 0);
285 glVertex2d( 0, myGridHeight / 2 + myYSize - yLoc );
286 glVertex2d( 0, -myGridHeight / 2 - myYSize - yLoc );
289 glBegin( GL_LINE_LOOP );
291 for ( int k = 0; k < SEGMENTS; k++ )
293 glVertex2f( cos(angle) * myCenterRadius * myXScale,
294 sin(angle) * myCenterRadius * myYScale );
303 /***************************************************************************
304 ** Class: GLViewer_Compass
305 ** Descr: OpenGL Compass for ViewPort 2D
307 ** Created: UI team, 29.03.04
308 ****************************************************************************/
309 GLViewer_Compass::GLViewer_Compass ( const QColor& color, const int size, const Position pos,
310 const int WidthTop, const int WidthBottom, const int HeightTop,
311 const int HeightBottom ){
315 myArrowWidthTop = WidthTop;
316 myArrowWidthBottom = WidthBottom;
317 myArrowHeightTop = HeightTop;
318 myArrowHeightBottom = HeightBottom;
320 QFont* aFont = new QFont("Times",16);
321 myFont = new GLViewer_TexFont( aFont );
323 //myFont->generateTexture();
326 GLViewer_TexFont* GLViewer_Compass::getFont()
330 myFont->generateTexture();
337 /***************************************************************************
338 ** Class: GLViewer_ViewPort2d
339 ** Descr: OpenGL ViewPort 2D
341 ** Created: UI team, 02.09.02
342 ****************************************************************************/
348 int static aLastViewPostId = 0;
350 void rotate_point( float& theX, float& theY, float theAngle )
352 float aTempX = theX * cos(theAngle) - theY * sin(theAngle);
353 float aTempY = theX * sin(theAngle) + theY * cos(theAngle);
358 GLViewer_ViewPort2d::GLViewer_ViewPort2d( QWidget* parent, GLViewer_ViewFrame* theViewFrame ) :
359 GLViewer_ViewPort( parent ),
360 myMargin( MARGIN ), myWidth( WIDTH ), myHeight( HEIGHT ),
361 myXScale( 1.0 ), myYScale( 1.0 ), myXOldScale( 1.0 ), myYOldScale( 1.0 ),
362 myXPan( 0.0 ), myYPan( 0.0 )
364 if( theViewFrame == NULL )
365 myViewFrame = ( GLViewer_ViewFrame* )parent;
367 myViewFrame = theViewFrame;
369 myBorder = new QRect(0,0,0,0);
371 QBoxLayout* qbl = new QHBoxLayout( this );
372 myGLWidget = new GLViewer_Widget( this, 0 ) ;
373 qbl->addWidget( myGLWidget );
374 myGLWidget->setFocusProxy( this );
375 setMouseTracking( TRUE );
377 myIsDragProcess = noDrag;
378 //myCurDragMousePos = QPoint();
379 myCurDragPosX = NULL;
380 myCurDragPosY = NULL;
383 //myCompass = new GLViewer_Compass();
384 //myCompass = new GLViewer_Compass( Qt::green, 30, GLViewer_Compass::TopRight, 10, 5, 12, 3 );
388 myViewPortId = aLastViewPostId;
391 mypFirstPoint = NULL;
394 myObjectTip = new QtxToolTip( myGLWidget );///GLViewer_ObjectTip( this );
395 connect( myObjectTip, SIGNAL( maybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ),
396 this, SLOT( onMaybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ) );
397 // myGLWidget->installEventFilter( myObjectTip );
400 GLViewer_ViewPort2d::~GLViewer_ViewPort2d()
412 void GLViewer_ViewPort2d::onCreatePopup()
414 //cout << "GLViewer_ViewPort2d::onCreatePopup" << endl;
416 //QAD_Desktop* desktop = (QAD_Desktop*) QAD_Application::getDesktop();
419 QString theParent("Viewer");
422 //desktop->definePopup( theContext, theParent, theObject );
423 //desktop->createPopup( myPopup, theContext, theParent, theObject);
424 //desktop->customPopup( myPopup, theContext, theParent, theObject );
427 if ( myPopup->count() > 0 )
428 myIDs.append ( myPopup->insertSeparator() );
430 myIDs.append ( id = myPopup->insertItem (tr ("MEN_VP3D_CHANGEBGR")) );
431 QAD_ASSERT ( myPopup->connectItem ( id, this, SLOT(onChangeBackgroundColor())) );
437 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
439 QAD_ASSERT( myPopupActions.isEmpty() );
441 QAction* a = new QAction( "", tr( "MEN_VP_CHANGEBGR" ), 0, this );
442 a->setStatusTip( tr( "PRP_VP_CHANGEBGR" ) );
443 connect( a, SIGNAL( activated() ), SLOT( onChangeBgColor() ) );
444 myPopupActions.append( a );
447 myPopup->insertSeparator();
449 a = new QAction( "", tr( "MEN_VP_CREATE_GLMARKERS" ), 0, this );
450 a->setStatusTip( tr( "PRP_VP_CREATE_GLMARKERS" ) );
451 connect( a, SIGNAL( activated() ), aViewer, SLOT( onCreateGLMarkers() ) );
452 myPopupActions.append( a );
455 a = new QAction( "", tr( "MEN_VP_CREATE_GLPOLYLINE" ), 0, this );
456 a->setStatusTip( tr( "PRP_VP_CREATE_GLPOLYLINE" ) );
457 connect( a, SIGNAL( activated() ), aViewer, SLOT( onCreateGLPolyline() ) );
458 myPopupActions.append( a );
461 a = new QAction( "", tr( "MEN_VP_CREATE_GLTEXT" ), 0, this );
462 a->setStatusTip( tr( "PRP_VP_CREATE_GLTEXT" ) );
463 connect( a, SIGNAL( activated() ), aViewer, SLOT( onCreateGLText() ) );
464 myPopupActions.append( a );
467 myPopup->insertSeparator();
469 //GLViewer_Object* aMovingObject = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext()->getCurrentObject();
470 int aSelObjects = aViewer->getGLContext()->NbSelected();
471 if( aSelObjects > 0 )
473 a = new QAction( "", tr( "MEN_VP_CUTOBJ" ), 0, this );
474 a->setStatusTip( tr( "PRP_VP_CUTOBJ" ) );
475 connect( a, SIGNAL( activated() ), SLOT( onCutObject() ) );
476 myPopupActions.append( a );
479 a = new QAction( "", tr( "MEN_VP_COPYOBJ" ), 0, this );
480 a->setStatusTip( tr( "PRP_VP_COPYOBJ" ) );
481 connect( a, SIGNAL( activated() ), SLOT( onCopyObject() ) );
482 myPopupActions.append( a );
486 a = new QAction( "", tr( "MEN_VP_PASTEOBJ" ), 0, this );
487 a->setStatusTip( tr( "PRP_VP_PASTEOBJ" ) );
488 connect( a, SIGNAL( activated() ), SLOT( onPasteObject() ) );
489 myPopupActions.append( a );
492 QClipboard *aClipboard = QApplication::clipboard();
493 QMimeSource* aMimeSource = aClipboard->data();
494 if( !aMimeSource->provides( "GLViewer_Objects" ) )
495 a->setEnabled( false );
498 if( aSelObjects > 0 )
500 myPopup->insertSeparator();
501 a = new QAction( "", tr( "MEN_VP_DRAGOBJ" ), 0, this );
502 a->setStatusTip( tr( "PRP_VP_DRAGOBJ" ) );
503 connect( a, SIGNAL( activated() ), SLOT( onStartDragObject() ) );
504 myPopupActions.append( a );
506 myCurDragPosX = new float((float)QCursor::pos().x());
507 myCurDragPosY = new float((float)QCursor::pos().y());
508 //myCurDragMousePos = QCursor::pos();
514 void GLViewer_ViewPort2d::onCreatePopup( QPopupMenu* popup )
519 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
520 if( !aViewer->isSketchingActive() )
522 if( !myPopupActions.isEmpty() )
524 QAction* a = new QAction( "", tr( "MEN_VP_CHANGEBGR" ), 0, this );
525 a->setStatusTip( tr( "PRP_VP_CHANGEBGR" ) );
526 connect( a, SIGNAL( activated() ), SLOT( onChangeBgColor() ) );
527 myPopupActions.append( a );
530 popup->insertSeparator();
532 //GLViewer_Object* aMovingObject = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext()->getCurrentObject();
533 int aSelObjects = aViewer->getGLContext()->NbSelected();
534 if( aSelObjects > 0 )
536 a = new QAction( "", tr( "MEN_VP_CUTOBJ" ), 0, this );
537 a->setStatusTip( tr( "PRP_VP_CUTOBJ" ) );
538 connect( a, SIGNAL( activated() ), SLOT( onCutObject() ) );
539 myPopupActions.append( a );
542 a = new QAction( "", tr( "MEN_VP_COPYOBJ" ), 0, this );
543 a->setStatusTip( tr( "PRP_VP_COPYOBJ" ) );
544 connect( a, SIGNAL( activated() ), SLOT( onCopyObject() ) );
545 myPopupActions.append( a );
549 a = new QAction( "", tr( "MEN_VP_PASTEOBJ" ), 0, this );
550 a->setStatusTip( tr( "PRP_VP_PASTEOBJ" ) );
551 connect( a, SIGNAL( activated() ), SLOT( onPasteObject() ) );
552 myPopupActions.append( a );
555 QClipboard *aClipboard = QApplication::clipboard();
556 QMimeSource* aMimeSource = aClipboard->data();
557 if( !aMimeSource->provides( "GLViewer_Objects" ) )
558 a->setEnabled( false );
561 if( aSelObjects > 0 )
563 popup->insertSeparator();
564 a = new QAction( "", tr( "MEN_VP_DRAGOBJ" ), 0, this );
565 a->setStatusTip( tr( "PRP_VP_DRAGOBJ" ) );
566 connect( a, SIGNAL( activated() ), SLOT( onStartDragObject() ) );
567 myPopupActions.append( a );
569 myCurDragPosX = new float((float)QCursor::pos().x());
570 myCurDragPosY = new float((float)QCursor::pos().y());
571 //myCurDragMousePos = QCursor::pos();
576 QAD_ASSERT( myPopupActions.isEmpty() );
577 QAction* a = new QAction( "", tr( "MEN_VP_SKETCH_DEL_OBJECT" ), 0, this );
578 a->setStatusTip( tr( "PRP_VP_SKETCH_DEL_OBJECT" ) );
579 connect( a, SIGNAL( activated() ), aViewer, SLOT( onSketchDelObject() ) );
580 myPopupActions.append( a );
583 popup->insertSeparator();
585 a = new QAction( "", tr( "MEN_VP_SKETCH_UNDO_LAST" ), 0, this );
586 a->setStatusTip( tr( "PRP_VP_SKETCH_UNDO_LAST" ) );
587 connect( a, SIGNAL( activated() ), aViewer, SLOT( onSketchUndoLast() ) );
588 myPopupActions.append( a );
591 int aSkType = aViewer->getSketchingType();
592 if( ( aSkType == GLViewer_Viewer2d::Polyline ) ||
593 ( aSkType == GLViewer_Viewer2d::Curve) ||
594 ( aSkType == GLViewer_Viewer2d::Scribble ) )
596 a = new QAction( "", tr( "MEN_VP_SKETCH_FINISH" ), 0, this );
597 a->setStatusTip( tr( "PRP_VP_SKETCH_FINISH" ) );
598 connect( a, SIGNAL( activated() ), aViewer, SLOT( onSketchFinish() ) );
599 myPopupActions.append( a );
607 void GLViewer_ViewPort2d::onDestroyPopup( QPopupMenu* popup )
612 for ( QAction* a = myPopupActions.first(); a; a = myPopupActions.next() )
613 a->removeFrom( popup );
614 myPopupActions.clear();
621 void GLViewer_ViewPort2d::onStartDragObject( )
623 if( myIsDragProcess == noDrag )
625 myIsDragProcess = initDrag;
626 QCursor::setPos( ( int )( *myCurDragPosX ), ( int )( *myCurDragPosY ) );
627 //myCurDragMousePos = QPoint( 0, 0 );
628 delete myCurDragPosX;
629 delete myCurDragPosY;
630 myCurDragPosX = NULL;
631 myCurDragPosY = NULL;
636 void GLViewer_ViewPort2d::onCutObject()
638 /*GLViewer_Object* aMovingObject = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext()->getCurrentObject();
641 GLViewer_MimeSource* aMimeSource = new GLViewer_MimeSource();
642 aMimeSource->setObject( aMovingObject );
644 QClipboard *aClipboard = QApplication::clipboard();
646 aClipboard->setData( aMimeSource );
648 ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext()->deleteObject( aMovingObject );
650 GLViewer_Context* aContext = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext();
651 int aObjNum = aContext->NbSelected();
654 QValueList<GLViewer_Object*> aObjects;
655 GLViewer_MimeSource* aMimeSource = new GLViewer_MimeSource();
656 aContext->InitSelected();
657 for( ; aContext->MoreSelected(); aContext->NextSelected() )
658 aObjects.append( aContext->SelectedObject() );
660 //aMimeSource->setObjects( aObjects ); ouv 6.05.04
662 QClipboard *aClipboard = QApplication::clipboard();
664 aClipboard->setData( aMimeSource );
666 for( int i = 0; i < aObjNum; i++ )
667 aContext->deleteObject( aObjects[i] );
671 void GLViewer_ViewPort2d::onCopyObject()
673 /*GLViewer_Object* aMovingObject = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext()->getCurrentObject();
676 GLViewer_MimeSource* aMimeSource = new GLViewer_MimeSource();
677 aMimeSource->setObject( aMovingObject );
679 QClipboard *aClipboard = QApplication::clipboard();
681 aClipboard->setData( aMimeSource );
684 GLViewer_Context* aContext = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext();
685 int aObjNum = aContext->NbSelected();
688 QValueList<GLViewer_Object*> aObjects;
689 GLViewer_MimeSource* aMimeSource = new GLViewer_MimeSource();
690 aContext->InitSelected();
691 for( ; aContext->MoreSelected(); aContext->NextSelected() )
692 aObjects.append( aContext->SelectedObject() );
694 //aMimeSource->setObjects( aObjects ); ouv 6.05.04
696 QClipboard *aClipboard = QApplication::clipboard();
698 aClipboard->setData( aMimeSource );
702 void GLViewer_ViewPort2d::onPasteObject()
704 /*QClipboard *aClipboard = QApplication::clipboard();
705 QMimeSource* aMimeSource = aClipboard->data();
706 if( aMimeSource->provides( "GLViewer_Object" ) )
713 aType = aMimeSource->format( i );
714 anArray = aMimeSource->encodedData( aType );
715 if( anArray.size() != 0 )
720 if( anArray.size() == 0 )
723 GLViewer_Object* aObject = GLViewer_MimeSource::getObject( anArray, aType );
727 ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext()->insertObject( aObject, true );
730 //QClipboard *aClipboard = QApplication::clipboard();
731 //QMimeSource* aMimeSource = aClipboard->data();
734 if( aMimeSource->provides( "GLViewer_Objects" ) )
736 QByteArray anArray = aMimeSource->encodedData( "GLViewer_Objects" );
737 QValueList<GLViewer_Object*> aObjects = GLViewer_MimeSource::getObjects( anArray, "GLViewer_Objects" );
738 if( aObjects.empty() )
740 GLViewer_Context* aContext = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext();
741 for( int i = 0; i < aObjects.count(); i++ )
742 aContext->insertObject( aObjects[i], true );
747 void GLViewer_ViewPort2d::onDragObject( QMouseEvent* e )
749 //cout << "---GLViewer_ViewPort2d::onDragObject()---" << endl;
750 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
751 GLViewer_Context* aContext = aViewer->getGLContext();
752 GLViewer_Object* anObject = aContext->getCurrentObject();
757 float aX = e->pos().x();
758 float anY = e->pos().y();
759 aViewer->transPoint( aX, anY );
761 if( myCurDragPosX == NULL && myCurDragPosY == NULL )
763 myCurDragPosX = new float(aX);
764 myCurDragPosY = new float(anY);
768 //QPoint aNewPos = e->pos();
769 //GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
771 if( anObject && (e->state() & LeftButton ) )
773 if( aContext->isSelected( anObject ) )
775 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
777 GLViewer_Object* aMovingObject = aContext->SelectedObject();
778 //= aViewer->getGLContext()->getCurrentObject();
781 aMovingObject->moveObject( aX - *myCurDragPosX ,
782 anY - *myCurDragPosY);
783 //QRect* rect = aMovingObject->getRect()->toQRect();
784 //aViewer->updateBorders( *rect );
785 aViewer->updateBorders();
790 anObject->moveObject( aX - *myCurDragPosX, anY - *myCurDragPosY);
792 else if( aContext->NbSelected() && (e->state() & MidButton ) )
793 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
794 (aContext->SelectedObject())->moveObject( aX - *myCurDragPosX, anY - *myCurDragPosY);
796 aViewer->updateBorders();
798 delete myCurDragPosX;
799 delete myCurDragPosY;
800 myCurDragPosX = new float(aX);
801 myCurDragPosY = new float(anY);
803 myGLWidget->updateGL();
807 Emits 'mouseEvent' signal. [ virtual protected ]
809 void GLViewer_ViewPort2d::mousePressEvent( QMouseEvent *e )
813 /*GLViewer_Object* aMovingObject = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext()->getCurrentObject();
818 emit vpMouseEvent( e );
820 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
821 GLViewer_Context* aContext = aViewer->getGLContext();
823 GLViewer_Object* anObject = NULL;
825 anObject = aContext->getCurrentObject();
827 bool accel = e->state() & GLViewer_ViewTransformer::accelKey();
828 if( ( anObject && !( accel || e->button() == Qt::RightButton ) )
830 ( aContext->NbSelected() && !accel && e->button() == Qt::MidButton ) )
832 myIsDragProcess = inDrag;
837 Emits 'mouseEvent' signal. [ virtual protected ]
839 void GLViewer_ViewPort2d::mouseMoveEvent( QMouseEvent* e )
841 //GLViewer_Context* context = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext();
842 //if( context->getCurrentObject() ) cout << "LASTPICKED" << endl;
843 emit vpMouseEvent( e );
845 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
846 GLViewer_Context* aContext = aViewer->getGLContext();
848 if( myIsDragProcess == inDrag )
851 GLViewer_Object* anObj = aContext->getCurrentObject();
852 if( anObj && aContext->currentObjectIsChanged() )
854 //cout << "GLViewer_ViewPort2d::mouseMoveEvent{QToolTip::add}" << endl;
855 //QToolTip::remove( myGLWidget );
856 QRect* aRect = (aViewer->getWinObjectRect(anObj));
857 //QToolTip::add( myGLWidget, *aRect, anObj->getToolTipText() );
858 myGLWidget->addToolTip( anObj->getToolTipText(), *aRect );
862 //cout << "GLViewer_ViewPort2d::mouseMoveEvent{QToolTip::remove}" << endl;
863 //QRect* aRect = (aViewer->getWinObjectRect(anObj));
864 //QToolTip::remove( myGLWidget, *aRect );
865 myGLWidget->removeToolTip();
870 Emits 'mouseEvent' signal. [ virtual protected ]
872 void GLViewer_ViewPort2d::mouseReleaseEvent( QMouseEvent *e )
874 /*if( myIsDragProcess == inDrag )
876 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
877 GLViewer_Context* aContext = aViewer->getGLContext();
878 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
880 GLViewer_Object* aMovingObject = aContext->SelectedObject();
882 aMovingObject->finishMove();
885 myIsDragProcess = noDrag;
886 //yCurDragMousePos.setX( 0 );
887 //myCurDragMousePos.setY( 0 );
888 delete myCurDragPosX;
889 delete myCurDragPosY;
890 myCurDragPosX = NULL;
891 myCurDragPosY = NULL;
895 /* show popup menu */
896 if ( e->button() == Qt::RightButton )
898 //QPopupMenu* popup = createPopup();
899 //if ( popup && popup->count() )
900 // popup->exec( QCursor::pos() );
901 //destroyPopup( /*popup*/ );
903 emit vpMouseEvent( e );
905 if( myIsDragProcess == inDrag )
907 bool isAnyMoved = false;
908 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
909 GLViewer_Context* aContext = aViewer->getGLContext();
910 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
912 GLViewer_Object* aMovingObject = aContext->SelectedObject();
915 aMovingObject->finishMove();
920 GLViewer_Object* anObject = aContext->getCurrentObject();
922 anObject->finishMove();
924 myIsDragProcess = noDrag;
925 //yCurDragMousePos.setX( 0 );
926 //myCurDragMousePos.setY( 0 );
927 delete myCurDragPosX;
928 delete myCurDragPosY;
929 myCurDragPosX = NULL;
930 myCurDragPosY = NULL;
935 void GLViewer_ViewPort2d::turnCompass( GLboolean on )
938 myCompass = new GLViewer_Compass( Qt::green, 30, GLViewer_Compass::TopRight, 10, 5, 12, 3 );
943 void GLViewer_ViewPort2d::turnGrid( GLboolean on )
946 myGrid = new GLViewer_RectangularGrid( 2*WIDTH, 2*HEIGHT,
950 myXScale, myYScale );
955 void GLViewer_ViewPort2d::setGridColor( const QColor gridColor, const QColor axisColor )
959 myGrid->setGridColor( ( GLfloat )gridColor.red() / 255,
960 ( GLfloat )gridColor.green() / 255,
961 ( GLfloat )gridColor.blue() / 255 );
962 myGrid->setAxisColor( ( GLfloat )axisColor.red() / 255,
963 ( GLfloat )axisColor.green() / 255,
964 ( GLfloat )axisColor.blue() / 255 );
968 void GLViewer_ViewPort2d::setBackgroundColor( const QColor& color )
970 GLViewer_ViewPort::setBackgroundColor( color );
971 myGLWidget->makeCurrent();
972 glClearColor( ( GLfloat )color.red() / 255,
973 ( GLfloat )color.green() / 255,
974 ( GLfloat )color.blue() / 255, 1.0 );
975 myGLWidget->repaint();
978 QColor GLViewer_ViewPort2d::backgroundColor() const
980 return GLViewer_ViewPort::backgroundColor();
983 void GLViewer_ViewPort2d::initResize( int x, int y )
985 float xa, xb, ya, yb;
986 xa = myBorder->left() - myMargin;
987 xb = myBorder->right() + myMargin;
988 ya = myBorder->top() - myMargin;
989 yb = myBorder->bottom() + myMargin;
991 GLfloat zoom, xzoom, yzoom;
996 xzoom = (GLfloat)x / myWidth;
997 yzoom = (GLfloat)y / myHeight;
999 if ( ( xzoom < yzoom ) && ( xzoom < 1 ) )
1001 else if ( ( yzoom < xzoom ) && ( yzoom < 1 ) )
1006 zoom = xzoom > yzoom ? xzoom : yzoom;
1009 if ( !max && ( ! ( ( ( myXPan + w/2 ) < xb * myXScale * zoom ) ||
1010 ( ( myXPan - w/2 ) > xa * myXScale * zoom ) ||
1011 ( ( myYPan + h/2 ) < yb * myYScale * zoom ) ||
1012 ( ( myYPan - h/2 ) > ya * myYScale * zoom ) ) ) )
1015 if ( max && ( ( ( myXPan + w/2 ) < xb * myXScale * zoom ) ||
1016 ( ( myXPan - w/2 ) > xa * myXScale * zoom ) ||
1017 ( ( myYPan + h/2 ) < yb * myYScale * zoom ) ||
1018 ( ( myYPan - h/2 ) > ya * myYScale * zoom ) ) )
1025 myYScale = myXScale;
1028 myGrid->setResize( 2*x, 2*y, zoom );
1030 myGLWidget->setScale( myXScale, myYScale, 1.0 );
1033 void GLViewer_ViewPort2d::paintEvent( QPaintEvent* e )
1035 //cout << "GLViewer_ViewPort2d::paintEvent" << endl;
1036 myGLWidget->updateGL();
1037 GLViewer_ViewPort::paintEvent( e );
1040 void GLViewer_ViewPort2d::resizeEvent( QResizeEvent* e )
1042 //cout << "GLViewer_ViewPort2d::resizeEvent" << endl;
1043 GLViewer_ViewPort::resizeEvent( e );
1046 void GLViewer_ViewPort2d::reset()
1048 //cout << "GLViewer_ViewPort2d::reset" << endl;
1051 GLint vpWidth, vpHeight;
1053 myGLWidget->makeCurrent();
1054 glGetIntegerv( GL_VIEWPORT, val );
1058 GLint w = myGLWidget->getWidth();
1059 GLint h = myGLWidget->getHeight();
1060 GLfloat zoom = vpWidth / ( GLfloat )w < vpHeight / ( GLfloat )h ?
1061 vpWidth / ( GLfloat )w : vpHeight / ( GLfloat )h;
1065 myGrid->setPan( 0.0, 0.0 );
1066 myGrid->setZoom( zoom / myXScale );
1074 myGLWidget->setPan( myXPan, myYPan, 0.0 );
1075 myGLWidget->setScale( myXScale, myYScale, 1.0 );
1076 myGLWidget->setRotationAngle( 0.0 );
1077 myGLWidget->setRotation( 0.0, 0.0, 0.0, 1.0 );
1078 myGLWidget->updateGL();
1081 void GLViewer_ViewPort2d::pan( int dx, int dy )
1083 //cout << "GLViewer_ViewPort2d::pan " << dx << " " << dy << endl;
1085 /*myXPan += dx / myXScale;
1086 myYPan += dy / myYScale;
1088 float ra, rx, ry, rz;
1089 myGLWidget->getRotation( ra, rx, ry, rz );
1090 GLfloat angle = ra * PI / 180.;
1093 myGrid->setPan( myXPan*cos(angle) + myYPan*sin(angle),
1094 -myXPan*sin(angle) + myYPan*cos(angle) );
1097 float ra, rx, ry, rz;
1098 myGLWidget->getRotation( ra, rx, ry, rz );
1099 GLfloat angle = ra * PI / 180.;
1101 myXPan += (dx*cos(angle) + dy*sin(angle)) / myXScale;
1102 myYPan += (-dx*sin(angle) + dy*cos(angle)) / myXScale;
1105 myGrid->setPan( myXPan, myYPan );
1107 myGLWidget->setPan( myXPan, myYPan, 0.0 );
1108 myGLWidget->setScale( myXScale, myYScale, 1.0 );
1109 myGLWidget->updateGL();
1112 void GLViewer_ViewPort2d::setCenter( int x, int y )
1114 //cout << "GLViewer_ViewPort2d::setCenter" << endl;
1117 GLint vpWidth, vpHeight;
1119 myGLWidget->makeCurrent();
1120 glGetIntegerv( GL_VIEWPORT, val );
1124 myXPan -= ( x - vpWidth/2 ) / myXScale;
1125 myYPan += ( y - vpHeight/2 ) / myYScale;
1129 myGrid->setPan( myXPan, myYPan );
1130 myGrid->setZoom( myXOldScale / myXScale );
1133 myXScale = myXOldScale;
1134 myYScale = myYOldScale;
1136 myGLWidget->setPan( myXPan, myYPan, 0.0 );
1137 myGLWidget->setScale( myXScale, myYScale, 1.0 );
1138 myGLWidget->updateGL();
1141 void GLViewer_ViewPort2d::zoom( int x0, int y0, int x, int y )
1143 //cout << "GLViewer_ViewPort2d::zoom" << endl;
1149 if ( dx == 0. && dy == 0. )
1152 zm = sqrt(dx * dx + dy * dy) / 100. + 1;
1153 zm = (dx > 0.) ? zm : 1. / zm;
1156 float bX = myXScale;
1157 float bY = myYScale;
1163 if( myGrid->setZoom( zm ) )
1165 myGLWidget->setPan( myXPan, myYPan, 0.0 );
1166 myGLWidget->setScale( myXScale, myYScale, 1.0 );
1167 myGLWidget->updateGL();
1177 myGLWidget->setPan( myXPan, myYPan, 0.0 );
1178 myGLWidget->setScale( myXScale, myYScale, 1.0 );
1179 myGLWidget->updateGL();
1183 void GLViewer_ViewPort2d::fitRect( const QRect& rect )
1185 //cout << "GLViewer_ViewPort2d::fitRect" << endl;
1187 float x0, x1, y0, y1;
1188 float dx, dy, zm, centerX, centerY;
1191 GLint vpWidth, vpHeight;
1193 myGLWidget->makeCurrent();
1194 glGetIntegerv( GL_VIEWPORT, val );
1203 dx = fabs( x1 - x0 );
1204 dy = fabs( y1 - y0 );
1205 centerX = ( x0 + x1 ) / 2.;
1206 centerY = ( y0 + y1 ) / 2.;
1208 if ( dx == 0. || dy == 0. )
1211 zm = vpWidth / dx < vpHeight / dy ? vpWidth / dx : vpHeight / dy;
1213 //myXPan += ( vpWidth / 2. - centerX ) / myXScale;
1214 //myYPan -= ( vpHeight / 2. - centerY ) / myYScale;
1216 float aDX = ( vpWidth / 2. - centerX ) / myXScale;
1217 float aDY = ( vpHeight / 2. - centerY ) / myYScale;
1219 float ra, rx, ry, rz;
1220 myGLWidget->getRotation( ra, rx, ry, rz );
1221 GLfloat angle = ra * PI / 180.;
1223 myXPan += (aDX*cos(angle) - aDY*sin(angle));
1224 myYPan -= (aDX*sin(angle) + aDY*cos(angle));
1227 myGrid->setPan( myXPan, myYPan );
1230 myYScale = myXScale;
1233 myGrid->setZoom( zm );
1235 myGLWidget->setPan( myXPan, myYPan, 0.0 );
1236 myGLWidget->setScale( myXScale, myYScale, 1.0 );
1237 myGLWidget->updateGL();
1240 void GLViewer_ViewPort2d::fitSelect()
1242 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
1243 GLViewer_Context* aContext = aViewer->getGLContext();
1248 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
1249 aSelRect |= *(aViewer->getWinObjectRect( aContext->SelectedObject() ));
1251 if( aSelRect.isValid() )
1253 aSelRect.setTop( aSelRect.top() - SELECTION_RECT_GAP );
1254 aSelRect.setBottom( aSelRect.bottom() + SELECTION_RECT_GAP );
1255 aSelRect.setLeft( aSelRect.left() - SELECTION_RECT_GAP );
1256 aSelRect.setRight( aSelRect.right() + SELECTION_RECT_GAP );
1257 fitRect( aSelRect );
1261 void GLViewer_ViewPort2d::fitAll( bool keepScale, bool withZ )
1263 //cout << "GLViewer_ViewPort2d::fitAll" << endl;
1265 float xa, xb, ya, yb;
1267 float xScale, yScale;
1269 myMargin = QMAX( myBorder->width(), myBorder->height() ) / 10;
1271 xa = myBorder->left() - myMargin;
1272 xb = myBorder->right() + myMargin;
1273 ya = myBorder->top() - myMargin;
1274 yb = myBorder->bottom() + myMargin;
1276 float aPoints[8] = { xa, ya, xb, ya, xa, yb, xb, yb };
1278 float ra, rx, ry, rz;
1279 myGLWidget->getRotation( ra, rx, ry, rz );
1280 float angle = ra * PI / 180.;
1283 for( i = 0; i < 7; i = i + 2 )
1284 rotate_point( aPoints[i], aPoints[i+1], angle );
1286 float aBorders[4] = { aPoints[0], aPoints[0], aPoints[1], aPoints[1] };
1288 for( i = 2; i < 7; i = i + 2 )
1290 if( aBorders[0] < aPoints[i] )
1291 aBorders[0] = aPoints[i];
1292 if( aBorders[1] > aPoints[i] )
1293 aBorders[1] = aPoints[i];
1295 if( aBorders[2] < aPoints[i+1] )
1296 aBorders[2] = aPoints[i+1];
1297 if( aBorders[3] > aPoints[i+1] )
1298 aBorders[3] = aPoints[i+1];
1302 GLint vpWidth, vpHeight;
1304 myGLWidget->makeCurrent();
1305 glGetIntegerv( GL_VIEWPORT, val );
1309 dx = fabs( aBorders[1] - aBorders[0] );
1310 dy = fabs( aBorders[3] - aBorders[2] );
1312 myXPan = -( aBorders[0] + aBorders[1] ) / 2;
1313 myYPan = -( aBorders[2] + aBorders[3] ) / 2;
1318 myXOldScale = myXScale;
1319 myYOldScale = myYScale;
1324 zm = vpWidth / dx < vpHeight / dy ? vpWidth / dx : vpHeight / dy;
1331 myGrid->setPan( myXPan, myYPan );
1333 myGrid->setZoom( zm / xScale );
1335 myGrid->setZoom( zm / yScale );
1338 myGLWidget->setPan( myXPan, myYPan, 0.0 );
1339 myGLWidget->setScale( myXScale, myYScale, 1.0 );
1340 myGLWidget->updateGL();
1343 emit vpUpdateValues();
1346 void GLViewer_ViewPort2d::startRotation( int x, int y )
1348 //cout << "GLViewer_ViewPort2d::startRotation" << endl;
1350 myGLWidget->setRotationStart( x, y, 1.0 );
1353 void GLViewer_ViewPort2d::rotate( int x, int y )
1355 //cout << "GLViewer_ViewPort2d::rotate " << x << " " << y << endl;
1358 GLint vpWidth, vpHeight;
1360 myGLWidget->makeCurrent();
1361 glGetIntegerv( GL_VIEWPORT, val );
1365 float x0 = vpWidth/2;
1366 float y0 = vpHeight/2;
1368 float xs, ys, zs, dx, dy;
1369 myGLWidget->getRotationStart( xs, ys, zs );
1372 x = ( int )( x - x0 );
1375 y = ( int )( y0 - y );
1378 float l1 = pow( double( xs*xs + ys*ys ), 0.5 );
1379 float l2 = pow( double( x*x + y*y ), 0.5 );
1380 float l = pow( double( dx*dx + dy*dy ), 0.5 );
1382 double mult = xs * y - x * ys;
1384 if( mult > 0 ) sign = 1;
1385 else if( mult < 0 ) sign = -1;
1388 float anglePrev = myGLWidget->getRotationAngle();
1389 float angleNew = sign * acos( ( l1*l1 + l2*l2 - l*l ) / ( 2 * l1 * l2 )) * 180. / PI;
1390 float angle = anglePrev + angleNew;
1392 //GLfloat anAngle = angle * PI / 180.;
1395 // myGrid->setPan( myXPan*cos(anAngle) + myYPan*sin(anAngle),
1396 // -myXPan*sin(anAngle) + myYPan*cos(anAngle) );
1398 //cout << l1 << endl;
1399 //cout << l2 << endl;
1400 //cout << l << endl;
1402 //cout << xs << " " << ys << " " << x << " " << y << endl;
1403 //cout << "MULTIPLICATION : " << mult << endl;
1405 //cout << "ANGLE_PREV = " << anglePrev << endl;
1406 //cout << "ANGLE_NEW = " << angleNew << endl;
1407 //cout << "ANGLE = " << angle << endl;
1409 float ra, rx, ry, rz;
1410 myGLWidget->getRotation( ra, rx, ry, rz );
1411 myGLWidget->setRotation( angle, rx, ry, rz );
1412 myGLWidget->updateGL();
1415 void GLViewer_ViewPort2d::endRotation()
1417 //cout << "GLViewer_ViewPort2d::endRotation" << endl;
1419 float ra, rx, ry, rz;
1420 myGLWidget->getRotation( ra, rx, ry, rz );
1421 myGLWidget->setRotationAngle( ra );
1424 void GLViewer_ViewPort2d::drawCompass(){
1425 if( !myCompass->getVisible() ) return;
1427 GLfloat xScale, yScale, xPan, yPan;
1429 int xPos = getWidth();
1430 int yPos = getHeight();
1432 int cPos = myCompass->getPos();
1433 int cSize = myCompass->getSize();
1434 QColor* cCol = &(myCompass->getColor());
1435 int cWidthTop = myCompass->getArrowWidthTop();
1436 int cWidthBot = myCompass->getArrowWidthBottom();
1437 int cHeightTop = myCompass->getArrowHeightTop();
1438 int cHeightBot = myCompass->getArrowHeightBottom();
1440 GLfloat colorR = (cCol->red())/255;
1441 GLfloat colorG = (cCol->green())/255;
1442 GLfloat colorB = (cCol->blue())/255;
1444 float delX = cSize * 0.5;
1445 float delY = cSize * 0.5;
1447 getScale( xScale, yScale );
1448 getPan( xPan, yPan);
1450 float centerX = (xPos/2 - delX - cSize)/xScale;
1451 float centerY = (yPos/2 - delY - cSize)/yScale;
1455 case GLViewer_Compass::TopLeft:
1458 case GLViewer_Compass::BottomLeft:
1462 case GLViewer_Compass::BottomRight:
1468 float ra, rx, ry, rz;
1469 myGLWidget->getRotation( ra, rx, ry, rz );
1470 GLfloat angle = ra * PI / 180.;
1471 GLfloat /*r = 0.0,*/ x = 0.0 , y = 0.0;
1473 rotate_point( centerX, centerY, -angle );
1478 glColor3f( colorR, colorG, colorB );
1479 glBegin( GL_POLYGON );
1481 x = centerX; y = centerY + cSize / yScale;
1484 x = centerX + cWidthTop / xScale; y = centerY + ( cSize - cHeightTop ) / yScale ;
1487 x = centerX + cWidthBot / xScale; y = centerY + ( cSize - cHeightTop ) / yScale ;
1490 x = centerX + cWidthBot / xScale; y = centerY - cSize/yScale;
1493 x = centerX; y = centerY - (cSize - cHeightBot) / yScale ;
1496 x = centerX - cWidthBot / xScale; y = centerY - cSize/yScale;
1499 x = centerX - cWidthBot / xScale; y = centerY + ( cSize - cHeightTop ) / yScale ;
1502 x = centerX - cWidthTop / xScale; y = centerY + ( cSize - cHeightTop ) / yScale ;
1506 glEnable( GL_LINE_SMOOTH );
1507 glBegin(GL_LINE_LOOP);
1509 float aCircAngle = 0;
1510 for ( int i = 0; i < 20 * SEGMENTS + 1; i++ )
1512 x = centerX + cos(aCircAngle) * cSize / xScale;
1513 y = centerY + sin(aCircAngle) * cSize / yScale;
1515 aCircAngle += float( STEP ) / 2;
1519 GLdouble modelMatrix[16], projMatrix[16];
1521 GLdouble winxN, winyN, winz;
1522 GLdouble winxE, winyE;
1523 GLdouble winxS, winyS;
1524 GLdouble winxW, winyW;
1527 GLViewer_TexFont* aFont = myCompass->getFont();
1528 float widN = (float)aFont->getStringWidth( "N" );
1529 float widW = (float)aFont->getStringWidth( "W" );
1530 float widS = (float)aFont->getStringWidth( "S" );
1531 float widE = (float)aFont->getStringWidth( "E" );
1532 float heightL = (float)aFont->getStringHeight();
1534 float xGapN = - widN/2 *( 1.0 + sin(angle) );
1535 float xGapS = - widS/2 *( 1.0 - sin(angle) );
1536 float xGapW = - widW/2 *( 1.0 + cos(angle) );
1537 float xGapE = - widE/2 *( 1.0 - cos(angle) );
1539 float yGapN = - heightL/2 *( 1.0 - cos(angle) ) * 0.75;
1540 float yGapS = - heightL/2 *( 1.0 + cos(angle) ) * 0.75;
1541 float yGapW = - heightL/2 *( 1.0 + sin(angle) ) * 0.75;
1542 float yGapE = - heightL/2 *( 1.0 - sin(angle) ) * 0.75;
1544 glGetIntegerv (GL_VIEWPORT, viewport);
1545 glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
1546 glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
1548 gluProject (centerX, centerY + cSize / yScale, 0, modelMatrix, projMatrix, viewport, &winxN, &winyN, &winz);
1549 gluProject (centerX + cSize / xScale, centerY, 0, modelMatrix, projMatrix, viewport, &winxE, &winyE, &winz);
1550 gluProject (centerX, centerY - cSize / yScale, 0, modelMatrix, projMatrix, viewport, &winxS, &winyS, &winz);
1551 gluProject (centerX - cSize / xScale, centerY, 0, modelMatrix, projMatrix, viewport, &winxW, &winyW, &winz);
1553 glColor3f( 1.0, 1.0, 1.0 );
1555 aTextList = glGenLists( 1 );
1556 glNewList( aTextList, GL_COMPILE );
1558 glMatrixMode(GL_PROJECTION);
1561 glOrtho(0,viewport[2],0,viewport[3],-100,100);
1562 glMatrixMode(GL_MODELVIEW);
1566 aFont->drawString( "N", winxN + xGapN, winyN + yGapN );
1567 aFont->drawString( "E", winxE + xGapE, winyE + yGapE );
1568 aFont->drawString( "S", winxS + xGapS, winyS + yGapS );
1569 aFont->drawString( "W", winxW + xGapW, winyW + yGapW );
1571 glMatrixMode(GL_PROJECTION);
1573 glMatrixMode(GL_MODELVIEW);
1578 if ( aTextList != -1 )
1579 glCallList( aTextList );
1582 BlockStatus GLViewer_ViewPort2d::currentBlock()
1584 if( myIsDragProcess == inDrag && myCurDragPosX != NULL && myCurDragPosY != NULL)
1585 return BlockStatus(BS_Highlighting | BS_Selection);
1587 if( mypFirstPoint && mypLastPoint )
1588 return BlockStatus(BS_Highlighting | BS_Selection);
1593 void GLViewer_ViewPort2d::startSelectByRect( int x, int y )
1595 if( !mypFirstPoint && !mypLastPoint )
1597 mypFirstPoint = new QPoint( x, y );
1598 mypLastPoint = new QPoint( x, y );
1601 void GLViewer_ViewPort2d::drawSelectByRect( int x, int y )
1603 if( mypFirstPoint && mypLastPoint )
1606 QPainter p( getPaintDevice() );
1607 p.setPen( Qt::white );
1608 p.setRasterOp( Qt::XorROP );
1610 p.drawRect( selectionRect() ); /* erase */
1612 mypLastPoint->setX( x );
1613 mypLastPoint->setY( y );
1615 p.drawRect( selectionRect() ); /* draw */
1619 void GLViewer_ViewPort2d::finishSelectByRect()
1621 if( mypFirstPoint && mypLastPoint )
1624 QPainter p( getPaintDevice() );
1625 p.setPen( Qt::white );
1626 p.setRasterOp( Qt::XorROP );
1628 p.drawRect( selectionRect() ); /* erase */
1630 delete mypFirstPoint;
1631 delete mypLastPoint;
1633 mypFirstPoint = NULL;
1634 mypLastPoint = NULL;
1638 QRect GLViewer_ViewPort2d::selectionRect()
1641 if( mypFirstPoint && mypLastPoint )
1643 aRect.setLeft( QMIN( mypFirstPoint->x(), mypLastPoint->x() ) );
1644 aRect.setTop( QMIN( mypFirstPoint->y(), mypLastPoint->y() ) );
1645 aRect.setRight( QMAX( mypFirstPoint->x(), mypLastPoint->x() ) );
1646 aRect.setBottom( QMAX( mypFirstPoint->y(), mypLastPoint->y() ) );
1652 bool GLViewer_ViewPort2d::startPulling( GLViewer_Pnt point )
1654 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
1655 GLViewer_Context* aContext = aViewer->getGLContext();
1656 ObjectMap anObjects = aContext->getObjects();
1658 for( ObjectMap::Iterator it = anObjects.begin(); it != anObjects.end(); ++it )
1660 GLViewer_Rect* aRect = it.key()->getRect();
1662 if( aRect->contains( point ) && it.key()->startPulling( point ) )
1665 myPullingObject = it.key();
1666 setCursor( *getHandCursor() );
1674 void GLViewer_ViewPort2d::drawPulling( GLViewer_Pnt point )
1676 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
1677 GLViewer_Context* aContext = aViewer->getGLContext();
1678 ObjectMap anObjects = aContext->getObjects();
1680 GLViewer_Object* aLockedObject = 0;
1681 for( ObjectMap::Iterator it = anObjects.begin(); it != anObjects.end(); ++it )
1683 GLViewer_Rect* aRect = it.key()->getRect();
1685 if( aRect->contains( point ) && it.key()->portContains( point ) )
1687 aLockedObject = it.key();
1692 myPullingObject->pull( point, aLockedObject );
1695 void GLViewer_ViewPort2d::finishPulling()
1697 myIsPulling = false;
1698 myPullingObject->finishPulling();
1699 setCursor( *getDefaultCursor() );
1702 GLViewer_Rect GLViewer_ViewPort2d::win2GLV( const QRect& theRect ) const
1704 GLViewer_Rect aRect;
1706 GLdouble modelMatrix[16], projMatrix[16];
1709 GLdouble objx1, objy1;
1710 GLdouble objx2, objy2;
1713 glGetIntegerv (GL_VIEWPORT, viewport);
1714 glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
1715 glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
1717 gluUnProject( theRect.left(), viewport[3] - theRect.top(), 0, modelMatrix, projMatrix, viewport, &objx1, &objy1, &objz );
1718 gluUnProject( theRect.right(), viewport[3] - theRect.bottom(), 0, modelMatrix, projMatrix, viewport, &objx2, &objy2, &objz );
1720 aRect.setLeft( objx1 );
1721 aRect.setTop( objy1 );
1722 aRect.setRight( objx2 );
1723 aRect.setBottom( objy2 );
1728 QRect GLViewer_ViewPort2d::GLV2win( const GLViewer_Rect& theRect ) const
1732 GLdouble modelMatrix[16], projMatrix[16];
1735 GLdouble winx1, winy1;
1736 GLdouble winx2, winy2;
1739 glGetIntegerv (GL_VIEWPORT, viewport);
1740 glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
1741 glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
1743 gluProject( theRect.left(), theRect.top(), 0, modelMatrix, projMatrix, viewport, &winx1, &winy1, &winz );
1744 gluProject( theRect.right(), theRect.bottom(), 0, modelMatrix, projMatrix, viewport, &winx2, &winy2, &winz );
1746 aRect.setLeft( (int)(winx1) );
1747 aRect.setTop( (int)(viewport[3] - winy1) );
1748 aRect.setRight( (int)(winx2) );
1749 aRect.setBottom( (int)(viewport[3] - winy2) );
1754 void GLViewer_ViewPort2d::onMaybeTip( QPoint thePoint, QString& theText, QFont& theFont, QRect& theTextReg, QRect& theRegion )
1756 GLViewer_Context* aContext = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext();
1758 GLViewer_Object* anObj = aContext->getCurrentObject();
1761 theText = anObj->getToolTipText();
1762 if( theText.isEmpty() )
1763 theText = anObj->getName();
1766 if( anObj->isTooTipHTML() )
1767 aList = QStringList::split( "<br>", theText );
1769 aList = QStringList::split( "\n", theText );
1771 if( !aList.isEmpty() )
1774 int str_size = aList.first().length();
1775 for( int i = 1, size = aList.count(); i < size; i++ )
1777 if( str_size < aList[i].length() )
1780 str_size = aList[i].length();
1784 int cur_height = 24;
1785 QCursor* aCursor = QApplication::overrideCursor();
1788 const QBitmap* aBitmap = aCursor->bitmap();
1790 cur_height = aBitmap->height();
1792 QFontMetrics fm( theFont );
1794 theTextReg = QRect( thePoint.x(), thePoint.y() + cur_height, fm.width( /*theText*/aList[index] ), fm.height()*aList.count() + 2 );
1795 theRegion = QRect( thePoint.x(), thePoint.y(), 1, 1 );