1 // File: GLViewer_ViewPort2d.cxx
2 // Created: November, 2004
4 // Copyright (C) CEA 2004
6 /* GLViewer_ViewPort2d Source File */
8 //#include <GLViewerAfx.h>
9 #include "GLViewer_ViewPort2d.h"
10 #include "GLViewer_Viewer2d.h"
11 #include "GLViewer_ViewFrame.h"
12 #include "GLViewer_MimeSource.h"
13 #include "GLViewer_Context.h"
14 #include "GLViewer_Compass.h"
15 #include "GLViewer_Grid.h"
17 #include <QtxToolTip.h>
22 #include <qpopupmenu.h>
24 #include <qapplication.h>
25 #include <qclipboard.h>
34 #define GRID_XSIZE 100
35 #define GRID_YSIZE 100
37 int static aLastViewPostId = 0;
39 void rotate_point( float& theX, float& theY, float theAngle )
41 float aTempX = theX * cos(theAngle) - theY * sin(theAngle);
42 float aTempY = theX * sin(theAngle) + theY * cos(theAngle);
47 GLViewer_ViewPort2d::GLViewer_ViewPort2d( QWidget* parent, GLViewer_ViewFrame* theViewFrame ) :
48 GLViewer_ViewPort( parent ),
49 myMargin( MARGIN ), myWidth( WIDTH ), myHeight( HEIGHT ),
50 myXScale( 1.0 ), myYScale( 1.0 ), myXOldScale( 1.0 ), myYOldScale( 1.0 ),
51 myXPan( 0.0 ), myYPan( 0.0 )
53 if( theViewFrame == NULL )
54 myViewFrame = ( GLViewer_ViewFrame* )parent;
56 myViewFrame = theViewFrame;
60 myBorder = new GLViewer_Rect();
62 QBoxLayout* qbl = new QHBoxLayout( this );
63 myGLWidget = new GLViewer_Widget( this, 0 ) ;
64 qbl->addWidget( myGLWidget );
65 myGLWidget->setFocusProxy( this );
66 setMouseTracking( TRUE );
68 myIsDragProcess = noDrag;
69 //myCurDragMousePos = QPoint();
75 myViewPortId = aLastViewPostId;
81 myObjectTip = new QtxToolTip( myGLWidget );///GLViewer_ObjectTip( this );
82 myObjectTip->setShowDelayTime( 60000 );
83 connect( myObjectTip, SIGNAL( maybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ),
84 this, SLOT( onMaybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ) );
85 // myGLWidget->installEventFilter( myObjectTip );
88 GLViewer_ViewPort2d::~GLViewer_ViewPort2d()
100 void GLViewer_ViewPort2d::onStartDragObject( )
102 if( myIsDragProcess == noDrag )
104 myIsDragProcess = initDrag;
105 QCursor::setPos( (int)(*myCurDragPosX), (int)(*myCurDragPosY) );
106 //myCurDragMousePos = QPoint( 0, 0 );
107 delete myCurDragPosX;
108 delete myCurDragPosY;
109 myCurDragPosX = NULL;
110 myCurDragPosY = NULL;
115 void GLViewer_ViewPort2d::onCutObject()
117 /*GLViewer_Object* aMovingObject = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext()->getCurrentObject();
120 GLViewer_MimeSource* aMimeSource = new GLViewer_MimeSource();
121 aMimeSource->setObject( aMovingObject );
123 QClipboard *aClipboard = QApplication::clipboard();
125 aClipboard->setData( aMimeSource );
127 ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext()->deleteObject( aMovingObject );
129 GLViewer_Context* aContext = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext();
130 int aObjNum = aContext->NbSelected();
133 QValueList<GLViewer_Object*> aObjects;
134 GLViewer_MimeSource* aMimeSource = new GLViewer_MimeSource();
135 aContext->InitSelected();
136 for( ; aContext->MoreSelected(); aContext->NextSelected() )
137 aObjects.append( aContext->SelectedObject() );
139 //aMimeSource->setObjects( aObjects ); ouv 6.05.04
141 QClipboard *aClipboard = QApplication::clipboard();
143 aClipboard->setData( aMimeSource );
145 for( int i = 0; i < aObjNum; i++ )
146 aContext->deleteObject( aObjects[i] );
150 void GLViewer_ViewPort2d::onCopyObject()
152 /*GLViewer_Object* aMovingObject = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext()->getCurrentObject();
155 GLViewer_MimeSource* aMimeSource = new GLViewer_MimeSource();
156 aMimeSource->setObject( aMovingObject );
158 QClipboard *aClipboard = QApplication::clipboard();
160 aClipboard->setData( aMimeSource );
163 GLViewer_Context* aContext = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext();
164 int aObjNum = aContext->NbSelected();
167 QValueList<GLViewer_Object*> aObjects;
168 GLViewer_MimeSource* aMimeSource = new GLViewer_MimeSource();
169 aContext->InitSelected();
170 for( ; aContext->MoreSelected(); aContext->NextSelected() )
171 aObjects.append( aContext->SelectedObject() );
173 //aMimeSource->setObjects( aObjects ); ouv 6.05.04
175 QClipboard *aClipboard = QApplication::clipboard();
177 aClipboard->setData( aMimeSource );
181 void GLViewer_ViewPort2d::onPasteObject()
183 /*QClipboard *aClipboard = QApplication::clipboard();
184 QMimeSource* aMimeSource = aClipboard->data();
185 if( aMimeSource->provides( "GLViewer_Object" ) )
192 aType = aMimeSource->format( i );
193 anArray = aMimeSource->encodedData( aType );
194 if( anArray.size() != 0 )
199 if( anArray.size() == 0 )
202 GLViewer_Object* aObject = GLViewer_MimeSource::getObject( anArray, aType );
206 ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext()->insertObject( aObject, true );
210 QClipboard *aClipboard = QApplication::clipboard();
212 QMimeSource* aMimeSource = aClipboard->data();
213 if( aMimeSource->provides( "GLViewer_Objects" ) )
215 QByteArray anArray = aMimeSource->encodedData( "GLViewer_Objects" );
216 QValueList<GLViewer_Object*> aObjects = GLViewer_MimeSource::getObjects( anArray, "GLViewer_Objects" );
217 if( aObjects.empty() )
219 GLViewer_Context* aContext = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext();
220 for( int i = 0; i < aObjects.count(); i++ )
221 aContext->insertObject( aObjects[i], true );
226 void GLViewer_ViewPort2d::onDragObject( QMouseEvent* e )
228 //cout << "---GLViewer_ViewPort2d::onDragObject()---" << endl;
229 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
230 GLViewer_Context* aContext = aViewer->getGLContext();
231 GLViewer_Object* anObject = aContext->getCurrentObject();
236 float aX = e->pos().x();
237 float anY = e->pos().y();
238 aViewer->transPoint( aX, anY );
240 if( myCurDragPosX == NULL && myCurDragPosY == NULL )
242 myCurDragPosX = new float(aX);
243 myCurDragPosY = new float(anY);
247 //QPoint aNewPos = e->pos();
248 //GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
250 if( anObject && (e->state() & LeftButton ) )
252 if( aContext->isSelected( anObject ) )
254 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
256 GLViewer_Object* aMovingObject = aContext->SelectedObject();
258 aMovingObject->moveObject( aX - *myCurDragPosX, anY - *myCurDragPosY);
262 anObject->moveObject( aX - *myCurDragPosX, anY - *myCurDragPosY);
264 else if( aContext->NbSelected() && (e->state() & MidButton ) )
265 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
266 (aContext->SelectedObject())->moveObject( aX - *myCurDragPosX, anY - *myCurDragPosY);
268 delete myCurDragPosX;
269 delete myCurDragPosY;
270 myCurDragPosX = new float(aX);
271 myCurDragPosY = new float(anY);
273 myGLWidget->updateGL();
277 Emits 'mouseEvent' signal. [ virtual protected ]
279 void GLViewer_ViewPort2d::mousePressEvent( QMouseEvent* e )
281 emit vpMouseEvent( e );
283 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
284 GLViewer_Context* aContext = aViewer->getGLContext();
286 GLViewer_Object* anObject = NULL;
288 anObject = aContext->getCurrentObject();
290 bool accel = e->state() & GLViewer_ViewTransformer::accelKey();
291 if( ( anObject && !( accel || e->button() == Qt::RightButton ) ) ||
292 ( aContext->NbSelected() && !accel && e->button() == Qt::MidButton ) )
294 myIsDragProcess = inDrag;
299 Emits 'mouseEvent' signal. [ virtual protected ]
301 void GLViewer_ViewPort2d::mouseMoveEvent( QMouseEvent* e )
303 emit vpMouseEvent( e );
305 if( myIsDragProcess == inDrag )
308 /*GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
309 GLViewer_Context* aContext = aViewer->getGLContext();
311 GLViewer_Object* anObj = aContext->getCurrentObject();
312 if( anObj && aContext->currentObjectIsChanged() )
314 //cout << "GLViewer_ViewPort2d::mouseMoveEvent{QToolTip::add}" << endl;
315 //QToolTip::remove( myGLWidget );
316 QRect* aRect = (aViewer->getWinObjectRect(anObj));
317 //QToolTip::add( myGLWidget, *aRect, anObj->getToolTipText() );
318 myGLWidget->addToolTip( anObj->getToolTipText(), *aRect );
322 //cout << "GLViewer_ViewPort2d::mouseMoveEvent{QToolTip::remove}" << endl;
323 //QRect* aRect = (aViewer->getWinObjectRect(anObj));
324 //QToolTip::remove( myGLWidget, *aRect );
325 myGLWidget->removeToolTip();
330 Emits 'mouseEvent' signal. [ virtual protected ]
332 void GLViewer_ViewPort2d::mouseReleaseEvent( QMouseEvent* e )
334 /* show popup menu */
335 if ( e->button() == Qt::RightButton )
337 //QPopupMenu* popup = createPopup();
338 //if ( popup && popup->count() )
339 // popup->exec( QCursor::pos() );
340 //destroyPopup( /*popup*/ );
342 emit vpMouseEvent( e );
344 if( myIsDragProcess == inDrag )
346 bool isAnyMoved = false;
347 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
348 GLViewer_Context* aContext = aViewer->getGLContext();
349 GLViewer_Object* aMovingObject;
350 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
352 aMovingObject = aContext->SelectedObject();
354 isAnyMoved = aMovingObject->finishMove() || isAnyMoved;
357 aMovingObject = aContext->getCurrentObject();
359 isAnyMoved = aMovingObject->finishMove() || isAnyMoved;
361 myIsDragProcess = noDrag;
362 //myCurDragMousePos.setX( 0 );
363 //myCurDragMousePos.setY( 0 );
364 delete myCurDragPosX;
365 delete myCurDragPosY;
366 myCurDragPosX = NULL;
367 myCurDragPosY = NULL;
372 aViewer->updateBorders();
377 void GLViewer_ViewPort2d::turnCompass( GLboolean on )
380 myCompass = new GLViewer_Compass( Qt::green, 30, GLViewer_Compass::TopRight, 10, 5, 12, 3 );
385 void GLViewer_ViewPort2d::turnGrid( GLboolean on )
389 myGrid = new GLViewer_Grid( 2*WIDTH, 2*HEIGHT,
391 GRID_XSIZE, GRID_YSIZE,
393 myXScale, myYScale );
399 void GLViewer_ViewPort2d::setGridColor( const QColor gridColor, const QColor axisColor )
403 myGrid->setGridColor( ( GLfloat )gridColor.red() / 255,
404 ( GLfloat )gridColor.green() / 255,
405 ( GLfloat )gridColor.blue() / 255 );
406 myGrid->setAxisColor( ( GLfloat )axisColor.red() / 255,
407 ( GLfloat )axisColor.green() / 255,
408 ( GLfloat )axisColor.blue() / 255 );
412 void GLViewer_ViewPort2d::setBackgroundColor( const QColor& color )
414 GLViewer_ViewPort::setBackgroundColor( color );
415 myGLWidget->makeCurrent();
416 glClearColor( ( GLfloat )color.red() / 255,
417 ( GLfloat )color.green() / 255,
418 ( GLfloat )color.blue() / 255, 1.0 );
419 myGLWidget->repaint();
422 QColor GLViewer_ViewPort2d::backgroundColor() const
424 return GLViewer_ViewPort::backgroundColor();
427 void GLViewer_ViewPort2d::initResize( int x, int y )
429 float xa, xb, ya, yb;
430 xa = myBorder->left() - myMargin;
431 xb = myBorder->right() + myMargin;
432 ya = myBorder->top() - myMargin;
433 yb = myBorder->bottom() + myMargin;
435 GLfloat zoom, xzoom, yzoom;
440 xzoom = (GLfloat)x / myWidth;
441 yzoom = (GLfloat)y / myHeight;
443 if ( ( xzoom < yzoom ) && ( xzoom < 1 ) )
445 else if ( ( yzoom < xzoom ) && ( yzoom < 1 ) )
450 zoom = xzoom > yzoom ? xzoom : yzoom;
453 if ( !max && ( ! ( ( ( myXPan + w/2 ) < xb * myXScale * zoom ) ||
454 ( ( myXPan - w/2 ) > xa * myXScale * zoom ) ||
455 ( ( myYPan + h/2 ) < yb * myYScale * zoom ) ||
456 ( ( myYPan - h/2 ) > ya * myYScale * zoom ) ) ) )
459 if ( max && ( ( ( myXPan + w/2 ) < xb * myXScale * zoom ) ||
460 ( ( myXPan - w/2 ) > xa * myXScale * zoom ) ||
461 ( ( myYPan + h/2 ) < yb * myYScale * zoom ) ||
462 ( ( myYPan - h/2 ) > ya * myYScale * zoom ) ) )
472 myGrid->setResize( 2*x, 2*y, zoom );
474 myGLWidget->setScale( myXScale, myYScale, 1.0 );
477 void GLViewer_ViewPort2d::paintEvent( QPaintEvent* e )
479 //cout << "GLViewer_ViewPort2d::paintEvent" << endl;
480 myGLWidget->updateGL();
481 GLViewer_ViewPort::paintEvent( e );
484 void GLViewer_ViewPort2d::resizeEvent( QResizeEvent* e )
486 //cout << "GLViewer_ViewPort2d::resizeEvent" << endl;
487 GLViewer_ViewPort::resizeEvent( e );
490 void GLViewer_ViewPort2d::reset()
492 //cout << "GLViewer_ViewPort2d::reset" << endl;
495 GLint vpWidth, vpHeight;
497 myGLWidget->makeCurrent();
498 glGetIntegerv( GL_VIEWPORT, val );
502 GLint w = myGLWidget->getWidth();
503 GLint h = myGLWidget->getHeight();
504 GLfloat zoom = vpWidth / ( GLfloat )w < vpHeight / ( GLfloat )h ?
505 vpWidth / ( GLfloat )w : vpHeight / ( GLfloat )h;
509 myGrid->setPan( 0.0, 0.0 );
510 myGrid->setZoom( zoom / myXScale );
518 myGLWidget->setPan( myXPan, myYPan, 0.0 );
519 myGLWidget->setScale( myXScale, myYScale, 1.0 );
520 myGLWidget->setRotationAngle( 0.0 );
521 myGLWidget->setRotation( 0.0, 0.0, 0.0, 1.0 );
522 myGLWidget->updateGL();
525 void GLViewer_ViewPort2d::pan( int dx, int dy )
527 //cout << "GLViewer_ViewPort2d::pan " << dx << " " << dy << endl;
529 /*myXPan += dx / myXScale;
530 myYPan += dy / myYScale;
532 float ra, rx, ry, rz;
533 myGLWidget->getRotation( ra, rx, ry, rz );
534 GLfloat angle = ra * PI / 180.;
537 myGrid->setPan( myXPan*cos(angle) + myYPan*sin(angle),
538 -myXPan*sin(angle) + myYPan*cos(angle) );
541 float ra, rx, ry, rz;
542 myGLWidget->getRotation( ra, rx, ry, rz );
543 GLfloat angle = ra * PI / 180.;
545 myXPan += (dx*cos(angle) + dy*sin(angle)) / myXScale;
546 myYPan += (-dx*sin(angle) + dy*cos(angle)) / myXScale;
549 myGrid->setPan( myXPan, myYPan );
551 myGLWidget->setPan( myXPan, myYPan, 0.0 );
552 myGLWidget->setScale( myXScale, myYScale, 1.0 );
553 myGLWidget->updateGL();
556 void GLViewer_ViewPort2d::setCenter( int x, int y )
558 //cout << "GLViewer_ViewPort2d::setCenter" << endl;
561 GLint vpWidth, vpHeight;
563 myGLWidget->makeCurrent();
564 glGetIntegerv( GL_VIEWPORT, val );
568 myXPan -= ( x - vpWidth/2 ) / myXScale;
569 myYPan += ( y - vpHeight/2 ) / myYScale;
573 myGrid->setPan( myXPan, myYPan );
574 myGrid->setZoom( myXOldScale / myXScale );
577 myXScale = myXOldScale;
578 myYScale = myYOldScale;
580 myGLWidget->setPan( myXPan, myYPan, 0.0 );
581 myGLWidget->setScale( myXScale, myYScale, 1.0 );
582 myGLWidget->updateGL();
585 void GLViewer_ViewPort2d::zoom( int x0, int y0, int x, int y )
587 //cout << "GLViewer_ViewPort2d::zoom" << endl;
593 if ( dx == 0. && dy == 0. )
596 zm = sqrt(dx * dx + dy * dy) / 100. + 1;
597 zm = (dx > 0.) ? zm : 1. / zm;
607 if( myGrid->setZoom( zm ) )
609 myGLWidget->setPan( myXPan, myYPan, 0.0 );
610 myGLWidget->setScale( myXScale, myYScale, 1.0 );
611 myGLWidget->updateGL();
621 myGLWidget->setPan( myXPan, myYPan, 0.0 );
622 myGLWidget->setScale( myXScale, myYScale, 1.0 );
623 myGLWidget->updateGL();
627 void GLViewer_ViewPort2d::fitRect( const QRect& rect )
629 float x0, x1, y0, y1;
630 float dx, dy, zm, centerX, centerY;
633 GLint vpWidth, vpHeight;
635 myGLWidget->makeCurrent();
636 glGetIntegerv( GL_VIEWPORT, val );
645 dx = fabs( x1 - x0 );
646 dy = fabs( y1 - y0 );
647 centerX = ( x0 + x1 ) / 2.;
648 centerY = ( y0 + y1 ) / 2.;
650 if ( dx == 0. || dy == 0. )
653 zm = vpWidth / dx < vpHeight / dy ? vpWidth / dx : vpHeight / dy;
655 float aDX = ( vpWidth / 2. - centerX ) / myXScale;
656 float aDY = ( vpHeight / 2. - centerY ) / myYScale;
658 float ra, rx, ry, rz;
659 myGLWidget->getRotation( ra, rx, ry, rz );
660 GLfloat angle = ra * PI / 180.;
662 myXPan += (aDX*cos(angle) - aDY*sin(angle));
663 myYPan -= (aDX*sin(angle) + aDY*cos(angle));
666 myGrid->setPan( myXPan, myYPan );
672 myGrid->setZoom( zm );
674 myGLWidget->setPan( myXPan, myYPan, 0.0 );
675 myGLWidget->setScale( myXScale, myYScale, 1.0 );
676 myGLWidget->updateGL();
679 void GLViewer_ViewPort2d::fitSelect()
681 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
682 GLViewer_Context* aContext = aViewer->getGLContext();
687 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
688 aSelRect |= *(aViewer->getWinObjectRect( aContext->SelectedObject() ));
690 if( aSelRect.isValid() )
692 aSelRect.setTop( aSelRect.top() - SELECTION_RECT_GAP );
693 aSelRect.setBottom( aSelRect.bottom() + SELECTION_RECT_GAP );
694 aSelRect.setLeft( aSelRect.left() - SELECTION_RECT_GAP );
695 aSelRect.setRight( aSelRect.right() + SELECTION_RECT_GAP );
700 void GLViewer_ViewPort2d::fitAll( bool keepScale, bool withZ )
702 //cout << "GLViewer_ViewPort2d::fitAll" << endl;
704 float xa, xb, ya, yb;
706 float xScale, yScale;
708 myMargin = QMAX( myBorder->width(), myBorder->height() ) / 5;
710 xa = myBorder->left() - myMargin;
711 xb = myBorder->right() + myMargin;
712 ya = myBorder->bottom() - myMargin;
713 yb = myBorder->top() + myMargin;
715 float aPoints[8] = { xa, ya, xb, ya, xa, yb, xb, yb };
717 float ra, rx, ry, rz;
718 myGLWidget->getRotation( ra, rx, ry, rz );
719 float angle = ra * PI / 180.;
722 for( i = 0; i < 7; i = i + 2 )
723 rotate_point( aPoints[i], aPoints[i+1], angle );
725 float aBorders[4] = { aPoints[0], aPoints[0], aPoints[1], aPoints[1] };
727 for( i = 2; i < 7; i = i + 2 )
729 if( aBorders[0] < aPoints[i] )
730 aBorders[0] = aPoints[i];
731 if( aBorders[1] > aPoints[i] )
732 aBorders[1] = aPoints[i];
734 if( aBorders[2] < aPoints[i+1] )
735 aBorders[2] = aPoints[i+1];
736 if( aBorders[3] > aPoints[i+1] )
737 aBorders[3] = aPoints[i+1];
741 GLint vpWidth, vpHeight;
743 myGLWidget->makeCurrent();
744 glGetIntegerv( GL_VIEWPORT, val );
748 dx = fabs( aBorders[1] - aBorders[0] );
749 dy = fabs( aBorders[3] - aBorders[2] );
751 myXPan = -( aBorders[0] + aBorders[1] ) / 2;
752 myYPan = -( aBorders[2] + aBorders[3] ) / 2;
757 myXOldScale = myXScale;
758 myYOldScale = myYScale;
764 zm = vpWidth / dx < vpHeight / dy ? vpWidth / dx : vpHeight / dy;
773 myGrid->setPan( myXPan, myYPan );
775 myGrid->setZoom( zm / xScale );
777 myGrid->setZoom( zm / yScale );
780 myGLWidget->setPan( myXPan, myYPan, 0.0 );
781 myGLWidget->setScale( myXScale, myYScale, 1.0 );
782 myGLWidget->updateGL();
785 emit vpUpdateValues();
788 void GLViewer_ViewPort2d::startRotation( int x, int y )
790 myGLWidget->setRotationStart( x, y, 1.0 );
793 void GLViewer_ViewPort2d::rotate( int intX, int intY )
796 GLint vpWidth, vpHeight;
798 myGLWidget->makeCurrent();
799 glGetIntegerv( GL_VIEWPORT, val );
803 float x = intX, y = intY;
804 float x0 = vpWidth/2;
805 float y0 = vpHeight/2;
807 float xs, ys, zs, dx, dy;
808 myGLWidget->getRotationStart( xs, ys, zs );
817 float l1 = pow( double( xs*xs + ys*ys ), 0.5 );
818 float l2 = pow( double( x*x + y*y ), 0.5 );
819 float l = pow( double( dx*dx + dy*dy ), 0.5 );
821 double mult = xs * y - x * ys;
823 if( mult > 0 ) sign = 1;
824 else if( mult < 0 ) sign = -1;
827 float anglePrev = myGLWidget->getRotationAngle();
828 float angleNew = sign * acos( ( l1*l1 + l2*l2 - l*l ) / ( 2 * l1 * l2 )) * 180. / PI;
829 float angle = anglePrev + angleNew;
831 // GLfloat anAngle = angle * PI / 180.; unused
833 float ra, rx, ry, rz;
834 myGLWidget->getRotation( ra, rx, ry, rz );
835 myGLWidget->setRotation( angle, rx, ry, rz );
836 myGLWidget->updateGL();
839 void GLViewer_ViewPort2d::endRotation()
841 float ra, rx, ry, rz;
842 myGLWidget->getRotation( ra, rx, ry, rz );
843 myGLWidget->setRotationAngle( ra );
846 void GLViewer_ViewPort2d::drawCompass()
848 if( !myCompass->getVisible() )
851 GLfloat xScale, yScale, xPan, yPan;
853 int xPos = getWidth();
854 int yPos = getHeight();
856 int cPos = myCompass->getPos();
857 int cSize = myCompass->getSize();
858 QColor cCol = myCompass->getColor();
859 int cWidthTop = myCompass->getArrowWidthTop();
860 int cWidthBot = myCompass->getArrowWidthBottom();
861 int cHeightTop = myCompass->getArrowHeightTop();
862 int cHeightBot = myCompass->getArrowHeightBottom();
864 GLfloat colorR = (cCol.red())/255;
865 GLfloat colorG = (cCol.green())/255;
866 GLfloat colorB = (cCol.blue())/255;
868 float delX = cSize * 0.5;
869 float delY = cSize * 0.5;
871 getScale( xScale, yScale );
874 float centerX = (xPos/2 - delX - cSize)/xScale;
875 float centerY = (yPos/2 - delY - cSize)/yScale;
879 case GLViewer_Compass::TopLeft:
882 case GLViewer_Compass::BottomLeft:
886 case GLViewer_Compass::BottomRight:
892 float ra, rx, ry, rz;
893 myGLWidget->getRotation( ra, rx, ry, rz );
894 GLfloat angle = ra * PI / 180.;
895 GLfloat /*r = 0.0,*/ x = 0.0 , y = 0.0;
897 rotate_point( centerX, centerY, -angle );
902 glColor3f( colorR, colorG, colorB );
903 glBegin( GL_POLYGON );
905 x = centerX; y = centerY + cSize / yScale;
908 x = centerX + cWidthTop / xScale; y = centerY + ( cSize - cHeightTop ) / yScale ;
911 x = centerX + cWidthBot / xScale; y = centerY + ( cSize - cHeightTop ) / yScale ;
914 x = centerX + cWidthBot / xScale; y = centerY - cSize/yScale;
917 x = centerX; y = centerY - (cSize - cHeightBot) / yScale ;
920 x = centerX - cWidthBot / xScale; y = centerY - cSize/yScale;
923 x = centerX - cWidthBot / xScale; y = centerY + ( cSize - cHeightTop ) / yScale ;
926 x = centerX - cWidthTop / xScale; y = centerY + ( cSize - cHeightTop ) / yScale ;
930 glEnable( GL_LINE_SMOOTH );
931 glBegin(GL_LINE_LOOP);
933 float aCircAngle = 0;
934 for ( int i = 0; i < 20 * SEGMENTS + 1; i++ )
936 x = centerX + cos(aCircAngle) * cSize / xScale;
937 y = centerY + sin(aCircAngle) * cSize / yScale;
939 aCircAngle += float( STEP ) / 2;
943 GLdouble modelMatrix[16], projMatrix[16];
945 GLdouble winxN, winyN, winz;
946 GLdouble winxE, winyE;
947 GLdouble winxS, winyS;
948 GLdouble winxW, winyW;
951 GLViewer_TexFont* aFont = myCompass->getFont();
952 float widN = (float)aFont->getStringWidth( "N" );
953 float widW = (float)aFont->getStringWidth( "W" );
954 float widS = (float)aFont->getStringWidth( "S" );
955 float widE = (float)aFont->getStringWidth( "E" );
956 float heightL = (float)aFont->getStringHeight();
958 float xGapN = - widN/2 *( 1.0 + sin(angle) );
959 float xGapS = - widS/2 *( 1.0 - sin(angle) );
960 float xGapW = - widW/2 *( 1.0 + cos(angle) );
961 float xGapE = - widE/2 *( 1.0 - cos(angle) );
963 float yGapN = - heightL/2 *( 1.0 - cos(angle) ) * 0.75;
964 float yGapS = - heightL/2 *( 1.0 + cos(angle) ) * 0.75;
965 float yGapW = - heightL/2 *( 1.0 + sin(angle) ) * 0.75;
966 float yGapE = - heightL/2 *( 1.0 - sin(angle) ) * 0.75;
968 glGetIntegerv (GL_VIEWPORT, viewport);
969 glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
970 glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
972 gluProject (centerX, centerY + cSize / yScale, 0, modelMatrix, projMatrix, viewport, &winxN, &winyN, &winz);
973 gluProject (centerX + cSize / xScale, centerY, 0, modelMatrix, projMatrix, viewport, &winxE, &winyE, &winz);
974 gluProject (centerX, centerY - cSize / yScale, 0, modelMatrix, projMatrix, viewport, &winxS, &winyS, &winz);
975 gluProject (centerX - cSize / xScale, centerY, 0, modelMatrix, projMatrix, viewport, &winxW, &winyW, &winz);
977 glColor3f( 1.0, 1.0, 1.0 );
979 aTextList = glGenLists( 1 );
980 glNewList( aTextList, GL_COMPILE );
982 glMatrixMode(GL_PROJECTION);
985 glOrtho(0,viewport[2],0,viewport[3],-100,100);
986 glMatrixMode(GL_MODELVIEW);
990 aFont->drawString( "N", winxN + xGapN, winyN + yGapN );
991 aFont->drawString( "E", winxE + xGapE, winyE + yGapE );
992 aFont->drawString( "S", winxS + xGapS, winyS + yGapS );
993 aFont->drawString( "W", winxW + xGapW, winyW + yGapW );
995 glMatrixMode(GL_PROJECTION);
997 glMatrixMode(GL_MODELVIEW);
1002 if ( aTextList != -1 )
1003 glCallList( aTextList );
1006 BlockStatus GLViewer_ViewPort2d::currentBlock()
1008 if( myIsDragProcess == inDrag && myCurDragPosX != NULL && myCurDragPosY != NULL)
1009 return BlockStatus(BS_Highlighting | BS_Selection);
1011 if( mypFirstPoint && mypLastPoint )
1012 return BlockStatus(BS_Highlighting | BS_Selection);
1017 void GLViewer_ViewPort2d::startSelectByRect( int x, int y )
1019 if( !mypFirstPoint && !mypLastPoint )
1021 mypFirstPoint = new QPoint( x, y );
1022 mypLastPoint = new QPoint( x, y );
1025 void GLViewer_ViewPort2d::drawSelectByRect( int x, int y )
1027 if( mypFirstPoint && mypLastPoint )
1030 QPainter p( getPaintDevice() );
1031 p.setPen( Qt::white );
1032 p.setRasterOp( Qt::XorROP );
1034 p.drawRect( selectionRect() ); /* erase */
1036 mypLastPoint->setX( x );
1037 mypLastPoint->setY( y );
1039 p.drawRect( selectionRect() ); /* draw */
1043 void GLViewer_ViewPort2d::finishSelectByRect()
1045 if( mypFirstPoint && mypLastPoint )
1048 QPainter p( getPaintDevice() );
1049 p.setPen( Qt::white );
1050 p.setRasterOp( Qt::XorROP );
1052 p.drawRect( selectionRect() ); /* erase */
1054 delete mypFirstPoint;
1055 delete mypLastPoint;
1057 mypFirstPoint = NULL;
1058 mypLastPoint = NULL;
1062 QRect GLViewer_ViewPort2d::selectionRect()
1065 if( mypFirstPoint && mypLastPoint )
1067 aRect.setLeft( QMIN( mypFirstPoint->x(), mypLastPoint->x() ) );
1068 aRect.setTop( QMIN( mypFirstPoint->y(), mypLastPoint->y() ) );
1069 aRect.setRight( QMAX( mypFirstPoint->x(), mypLastPoint->x() ) );
1070 aRect.setBottom( QMAX( mypFirstPoint->y(), mypLastPoint->y() ) );
1076 bool GLViewer_ViewPort2d::startPulling( GLViewer_Pnt point )
1078 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
1079 GLViewer_Context* aContext = aViewer->getGLContext();
1080 ObjList anObjects = aContext->getObjects();
1082 for( ObjList::Iterator it = anObjects.begin(); it != anObjects.end(); ++it )
1084 GLViewer_Object* anObject = *it;
1085 GLViewer_Rect aRect = anObject->getPullingRect();
1087 if( aRect.contains( point ) && anObject->startPulling( point ) )
1090 myPullingObject = anObject;
1091 setCursor( *getHandCursor() );
1099 void GLViewer_ViewPort2d::drawPulling( GLViewer_Pnt point )
1101 GLViewer_Viewer2d* aViewer = (GLViewer_Viewer2d*)getViewFrame()->getViewer();
1102 GLViewer_Context* aContext = aViewer->getGLContext();
1103 ObjList anObjects = aContext->getObjects();
1105 GLViewer_Object* aLockedObject = 0;
1106 for( ObjList::Iterator it = anObjects.begin(); it != anObjects.end(); ++it )
1108 GLViewer_Object* anObject = *it;
1109 if( !anObject->getVisible() )
1112 GLViewer_Rect aRect = anObject->getPullingRect();
1114 if( aRect.contains( point ) && anObject->portContains( point ) )
1116 aLockedObject = anObject;
1121 myPullingObject->pull( point, aLockedObject );
1124 void GLViewer_ViewPort2d::finishPulling()
1126 myIsPulling = false;
1127 myPullingObject->finishPulling();
1128 setCursor( *getDefaultCursor() );
1131 GLViewer_Rect GLViewer_ViewPort2d::win2GLV( const QRect& theRect ) const
1133 GLViewer_Rect aRect;
1135 GLdouble modelMatrix[16], projMatrix[16];
1138 GLdouble objx1, objy1;
1139 GLdouble objx2, objy2;
1142 glGetIntegerv (GL_VIEWPORT, viewport);
1143 glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
1144 glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
1146 gluUnProject( theRect.left(), viewport[3] - theRect.top(), 0, modelMatrix, projMatrix, viewport, &objx1, &objy1, &objz );
1147 gluUnProject( theRect.right(), viewport[3] - theRect.bottom(), 0, modelMatrix, projMatrix, viewport, &objx2, &objy2, &objz );
1149 aRect.setLeft( objx1 );
1150 aRect.setTop( objy1 );
1151 aRect.setRight( objx2 );
1152 aRect.setBottom( objy2 );
1157 QRect GLViewer_ViewPort2d::GLV2win( const GLViewer_Rect& theRect ) const
1161 GLdouble modelMatrix[16], projMatrix[16];
1164 GLdouble winx1, winy1;
1165 GLdouble winx2, winy2;
1168 glGetIntegerv (GL_VIEWPORT, viewport);
1169 glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix);
1170 glGetDoublev (GL_PROJECTION_MATRIX, projMatrix);
1172 gluProject( theRect.left(), theRect.top(), 0, modelMatrix, projMatrix, viewport, &winx1, &winy1, &winz );
1173 gluProject( theRect.right(), theRect.bottom(), 0, modelMatrix, projMatrix, viewport, &winx2, &winy2, &winz );
1175 aRect.setLeft( (int)winx1 );
1176 aRect.setTop( viewport[3] - (int)winy1 );
1177 aRect.setRight( (int)winx2 );
1178 aRect.setBottom( viewport[3] - (int)winy2 );
1183 void GLViewer_ViewPort2d::onMaybeTip( QPoint thePoint, QString& theText, QFont& theFont, QRect& theTextReg, QRect& theRegion )
1185 GLViewer_Context* aContext = ((GLViewer_Viewer2d*)getViewFrame()->getViewer())->getGLContext();
1187 GLViewer_Object* anObj = aContext->getCurrentObject();
1190 theText = anObj->getToolTipText();
1191 if( theText.isEmpty() )
1192 theText = anObj->getName();
1195 if( anObj->isTooTipHTML() )
1196 aList = QStringList::split( "<br>", theText );
1198 aList = QStringList::split( "\n", theText );
1200 if( !aList.isEmpty() )
1203 int str_size = aList.first().length();
1204 for( int i = 1, size = aList.count(); i < size; i++ )
1206 if( str_size < aList[i].length() )
1209 str_size = aList[i].length();
1213 int cur_height = 24;
1214 QCursor* aCursor = QApplication::overrideCursor();
1217 const QBitmap* aBitmap = aCursor->bitmap();
1219 cur_height = aBitmap->height();
1223 QSize aSize = QLabel( theText, 0 ).sizeHint();
1224 theTextReg = QRect( thePoint.x(), thePoint.y() + cur_height,
1225 aSize.width(), aSize.height() );
1226 theRegion = QRect( thePoint.x(), thePoint.y(), 1, 1 );