From ab4dece926573e8ef61cc0d23893be0ba4a4e869 Mon Sep 17 00:00:00 2001 From: ouv Date: Wed, 9 Nov 2011 12:06:39 +0000 Subject: [PATCH] Drawing legend inside GL view --- src/GLViewer/GLViewer_Drawer.cxx | 21 +++++++-- src/GLViewer/GLViewer_Drawer.h | 6 +++ src/GLViewer/GLViewer_ViewPort2d.cxx | 68 ++++++++++++++++++++++++++++ src/GLViewer/GLViewer_ViewPort2d.h | 16 +++++++ src/GLViewer/GLViewer_Widget.cxx | 2 + 5 files changed, 110 insertions(+), 3 deletions(-) diff --git a/src/GLViewer/GLViewer_Drawer.cxx b/src/GLViewer/GLViewer_Drawer.cxx index 11f0235f2..b8f125187 100644 --- a/src/GLViewer/GLViewer_Drawer.cxx +++ b/src/GLViewer/GLViewer_Drawer.cxx @@ -797,9 +797,7 @@ void GLViewer_Drawer::drawText( const QString& text, GLfloat xPos, GLfloat yPos, } else { - glRasterPos2f( xPos, yPos ); - glListBase( displayListBase( theFont ) ); - glCallLists( text.length(), GL_UNSIGNED_BYTE, text.toLocal8Bit().data() ); + drawBitmapText( text, theFont, xPos, yPos ); } } @@ -940,6 +938,23 @@ void GLViewer_Drawer::drawRectangle( GLViewer_Rect* rect, QColor color ) glEnd(); } +/*! + Draws simple bitmap text + \param theText - text string + \param theFont - font of the text + \param theXPos - x coordinate of the text + \param theYPos - y coordinate of the text +*/ +void GLViewer_Drawer::drawBitmapText( const QString& theText, + QFont* theFont, + GLfloat theXPos, + GLfloat theYPos ) +{ + glRasterPos2f( theXPos, theYPos ); + glListBase( displayListBase( theFont ) ); + glCallLists( theText.length(), GL_UNSIGNED_BYTE, theText.toLocal8Bit().data() ); +} + /*! Draws filled rectangle \param rect - instance of primitive diff --git a/src/GLViewer/GLViewer_Drawer.h b/src/GLViewer/GLViewer_Drawer.h index 446c90cac..8c9f82510 100644 --- a/src/GLViewer/GLViewer_Drawer.h +++ b/src/GLViewer/GLViewer_Drawer.h @@ -407,6 +407,12 @@ public: //! Draw filled rectangle with predefined color static void drawFilledRectangle( GLViewer_Rect*, QColor = Qt::black ); + //! Draw simple bitmap text + static void drawBitmapText( const QString& theText, + QFont* theFont, + GLfloat theXPos, + GLfloat theYPos ); + protected: static void drawContour( GLViewer_Rect*, QColor, GLfloat, GLushort, bool ); static void drawContour( const GLViewer_PntList&, QColor, GLfloat ); diff --git a/src/GLViewer/GLViewer_ViewPort2d.cxx b/src/GLViewer/GLViewer_ViewPort2d.cxx index 64d67f148..09a0a08b9 100644 --- a/src/GLViewer/GLViewer_ViewPort2d.cxx +++ b/src/GLViewer/GLViewer_ViewPort2d.cxx @@ -112,6 +112,11 @@ GLViewer_ViewPort2d::GLViewer_ViewPort2d( QWidget* parent, GLViewer_ViewFrame* t connect( myObjectTip, SIGNAL( maybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ), this, SLOT( onMaybeTip( QPoint, QString&, QFont&, QRect&, QRect& ) ) ); //myGLWidget->installEventFilter( myObjectTip ); + + myIsLegendEnabled = false; + myLegendXOffset = 10.0; + myLegendYOffset = 10.0; + myLegendFont = QFont( "Arial", 9 ); } /*! @@ -1256,6 +1261,69 @@ void GLViewer_ViewPort2d::drawCompass() glCallList( aTextList ); } +/*! + Draws legend +*/ +void GLViewer_ViewPort2d::drawLegend() +{ + if( !myIsLegendEnabled || myLegendText.isEmpty() ) + return; + + int w = getWidth(); + int h = getHeight(); + + GLfloat xScale, yScale, xPan, yPan; + getScale( xScale, yScale ); + getPan( xPan, yPan ); + + GLdouble x1 = -w / 2. / xScale - xPan; + GLdouble y1 = -h / 2. / yScale - yPan; + GLdouble z1 = 0; + + GLint viewport[4]; + glGetIntegerv( GL_VIEWPORT, viewport ); + + GLdouble modelMatrix[16], projMatrix[16]; + glGetDoublev( GL_MODELVIEW_MATRIX, modelMatrix ); + glGetDoublev( GL_PROJECTION_MATRIX, projMatrix ); + + GLdouble x2, y2, z2; + gluProject( x1, y1, z1, modelMatrix, projMatrix, viewport, &x2, &y2, &z2 ); + + glColor3f( 0.0, 0.0, 0.0 ); + + glMatrixMode( GL_PROJECTION ); + glPushMatrix(); + glLoadIdentity(); + glOrtho( 0, viewport[2], 0, viewport[3], -100, 100 ); + glMatrixMode( GL_MODELVIEW ); + glPushMatrix(); + glLoadIdentity(); + + QStringList aStringList = myLegendText.split( "\n" ); + + int aCounter = 0; + int aTextHeight = 0; + QStringListIterator anIter( aStringList ); + anIter.toBack(); + while( anIter.hasPrevious() ) + { + const QString& aString = anIter.previous(); + if( aCounter > 0 && aTextHeight == 0 ) + aTextHeight = QFontMetrics( myLegendFont ).height(); + GLViewer_Drawer::drawBitmapText( aString, + &myLegendFont, + x2 + myLegendXOffset, + y2 + myLegendYOffset + aCounter * aTextHeight ); + aCounter++; + } + + glMatrixMode( GL_PROJECTION ); + glPopMatrix(); + glMatrixMode( GL_MODELVIEW ); + glPopMatrix(); +} + /*! \return blocking status for current started operations */ diff --git a/src/GLViewer/GLViewer_ViewPort2d.h b/src/GLViewer/GLViewer_ViewPort2d.h index c18e962df..ded12b05b 100644 --- a/src/GLViewer/GLViewer_ViewPort2d.h +++ b/src/GLViewer/GLViewer_ViewPort2d.h @@ -158,6 +158,9 @@ public: //! Draws compass void drawCompass(); + //! Draws legend + void drawLegend(); + //! Returns unique ID of ViewPort int getViewPortId(){ return myViewPortId; } @@ -195,6 +198,12 @@ public: QImage dumpContents( bool theWholeScene = false, double theScale = 1.0 ); + //! Sets the legend enable state + void setLegendEnabled( const bool theState ) { myIsLegendEnabled = theState; } + + //! Sets the legend text + void setLegendText( const QString& theText ) { myLegendText = theText; } + signals: //! Emits after any transformation void vpUpdateValues(); @@ -282,6 +291,13 @@ protected: bool myIsMouseReleaseBlock; QRubberBand* myRectBand; //!< selection rectangle rubber band + + // legend + bool myIsLegendEnabled; + QString myLegendText; + GLfloat myLegendXOffset; + GLfloat myLegendYOffset; + QFont myLegendFont; }; #ifdef WIN32 diff --git a/src/GLViewer/GLViewer_Widget.cxx b/src/GLViewer/GLViewer_Widget.cxx index d5493add2..165d910a7 100644 --- a/src/GLViewer/GLViewer_Widget.cxx +++ b/src/GLViewer/GLViewer_Widget.cxx @@ -473,6 +473,8 @@ void GLViewer_Widget::paintGL() v->updateDrawers( GL_FALSE, myXScale, myYScale ); else v->repaintView( getViewPort()->getViewFrame() ); + + myViewPort->drawLegend(); } /*! -- 2.39.2