]> SALOME platform Git repositories - modules/gui.git/commitdiff
Salome HOME
Drawing legend inside GL view
authorouv <ouv@opencascade.com>
Wed, 9 Nov 2011 12:06:39 +0000 (12:06 +0000)
committerouv <ouv@opencascade.com>
Wed, 9 Nov 2011 12:06:39 +0000 (12:06 +0000)
src/GLViewer/GLViewer_Drawer.cxx
src/GLViewer/GLViewer_Drawer.h
src/GLViewer/GLViewer_ViewPort2d.cxx
src/GLViewer/GLViewer_ViewPort2d.h
src/GLViewer/GLViewer_Widget.cxx

index 11f0235f2a2f52d84573c0520816729bbcdaf69a..b8f1251874ac0cb28d5f1fca136ef353f3baf81c 100644 (file)
@@ -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
index 446c90cac00242af0805538269b99e2e362f0ac8..8c9f8251016351849bf376f24e20d3cc56602c92 100644 (file)
@@ -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 );
index 64d67f148b8e006623ea28917af9307679ff090e..09a0a08b97363b2435dbfe3f6324f9c00024f244 100644 (file)
@@ -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
 */
index c18e962df3b785b6a86915644eb06ae3bfccf4e0..ded12b05b58e0544ad628ac9651eb4679d23dcfe 100644 (file)
@@ -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
index d5493add2e793800d9b8e69a8560a190718739d2..165d910a7d125299b14777149dad0189b265f7d3 100644 (file)
@@ -473,6 +473,8 @@ void GLViewer_Widget::paintGL()
         v->updateDrawers( GL_FALSE, myXScale, myYScale );
     else
         v->repaintView( getViewPort()->getViewFrame() );
+
+    myViewPort->drawLegend();
 }
 
 /*!