Back to index

salome-gui  6.5.0
GLViewer_Text.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  Author : OPEN CASCADE
00024 //#include <GLViewerAfx.h>
00025 //
00026 #include "GLViewer_Text.h"
00027 
00028 #include <QApplication>
00029 #include <QFontMetrics>
00030 
00034 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color )
00035 {
00036   myText = text;
00037   myXPos = xPos;
00038   myYPos = yPos;
00039   myColor = color;
00040   myQFont = QApplication::font();//QFont::defaultFont();
00041   mySeparator = 2;
00042   myDTF = DTF_BITMAP;
00043 }
00044 
00048 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color, QFont theFont, int theSeparator )
00049 {
00050   myText = text;
00051   myXPos = xPos;
00052   myYPos = yPos;
00053   myColor = color;
00054   myQFont = theFont;
00055   mySeparator = theSeparator;
00056   myDTF = DTF_BITMAP;
00057 }
00058 
00062 GLViewer_Text::~GLViewer_Text()
00063 {
00064 }
00065 
00069 int GLViewer_Text::getWidth()
00070 {
00071     int aResult = 0;
00072     QFontMetrics aFM( myQFont );
00073     for( uint i = 0; i < myText.length(); i++ )
00074         aResult += aFM.width( myText.at(i) ) + mySeparator;
00075     return aResult;
00076 }
00077 
00081 int GLViewer_Text::getHeight()
00082 {
00083     QFontMetrics aFM( myQFont );
00084     return aFM.height();
00085 }
00086 
00091 QByteArray GLViewer_Text::getByteCopy() const
00092 {
00093     int i;
00094     int aSize = 5*sizeof( int ) + myText.length();
00095 
00096     int aR = myColor.red();
00097     int aG = myColor.green();
00098     int aB = myColor.blue();
00099     const char* aStr = myText.toLatin1().constData();
00100 
00101     int anISize = sizeof( int );    
00102     QByteArray aResult;
00103     aResult.resize( aSize );
00104 
00105     char* aPointer = (char*)&myXPos;
00106     for( i = 0; i < anISize; i++, aPointer++ )
00107         aResult[i] = *aPointer;
00108     aPointer = (char*)&myYPos;
00109     for( ; i < 2*anISize; i++, aPointer++ )
00110         aResult[i] = *aPointer;
00111 
00112     aPointer = (char*)&aR;
00113     for( ; i < 3*anISize; i++, aPointer++ )
00114         aResult[i] = *aPointer;
00115     aPointer = (char*)&aG;
00116     for( ; i < 4*anISize; i++, aPointer++ )
00117         aResult[i] = *aPointer;
00118     aPointer = (char*)&aB;
00119     for( ; i < 5*anISize; i++, aPointer++ )
00120         aResult[i] = *aPointer;
00121 
00122     int aTextSize = myText.length();
00123     aPointer = (char*)&aTextSize;
00124     for( ; i < 6*anISize; i++, aPointer++ )
00125         aResult[i] = *aPointer;
00126 
00127     for( i = 0; i < aTextSize; i++ )
00128         aResult[6*anISize + i] = aStr[i];
00129 
00130     aPointer = (char*)&mySeparator;
00131     for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
00132         aResult[i] = *aPointer;
00133 
00134     const char* aFontStr = myQFont.toString().toLatin1().constData();
00135     int aFontSize = myQFont.toString().length();
00136 
00137     for( i = 0; i < aFontSize; i++ )
00138         aResult[7*anISize + aTextSize + i] = aFontStr[i];
00139 
00140     return aResult;
00141 }
00142 
00147 GLViewer_Text* GLViewer_Text::fromByteCopy( QByteArray theBuf )
00148 {
00149     int i = 0;
00150     int aSize = (int)theBuf.size();
00151     int aR = 0, aG = 0, aB = 0;
00152 
00153     int xPos = 0, yPos = 0;
00154 
00155     int anISize = sizeof( int );
00156     char* aPointer = (char*)&xPos;
00157     for ( i = 0; i < anISize; i++, aPointer++ )
00158         *aPointer = theBuf[i];
00159 
00160     aPointer = (char*)&yPos;
00161     for ( ; i < 2*anISize; i++, aPointer++ )
00162         *aPointer = theBuf[i];
00163 
00164     aPointer = (char*)&aR;
00165     for( ; i < 3*anISize; i++, aPointer++ )
00166         *aPointer = theBuf[i];
00167     aPointer = (char*)&aG;
00168     for( ; i < 4*anISize; i++, aPointer++ )
00169         *aPointer = theBuf[i];
00170     aPointer = (char*)&aB;
00171     for( ; i < 5*anISize; i++, aPointer++ )
00172         *aPointer = theBuf[i];
00173 
00174     int aTextSize = 0;
00175     aPointer = (char*)&aTextSize;
00176     for( ; i < 6*anISize; i++, aPointer++ )
00177         *aPointer = theBuf[i];
00178 
00179     QString aText;
00180     for( ; i < 6*anISize + aTextSize; i++ )
00181     {
00182         QChar aChar( theBuf[i] );
00183         aText += aChar;
00184     }
00185 
00186     int aSeparator = 0;
00187     aPointer = (char*)&aSeparator;
00188     for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
00189         *aPointer = theBuf[i];
00190 
00191     QString aFontStr;
00192     for( ; i < aSize; i++ )
00193     {
00194         QChar aChar( theBuf[i] );
00195         aFontStr += aChar;
00196     }
00197     QFont aFont;
00198 
00199     if( !aFont.fromString( aFontStr ) )
00200         return NULL;    
00201 
00202     GLViewer_Text* aGlText = new GLViewer_Text( aText, xPos, yPos, QColor( aR,aG,aB ), aFont, aSeparator  );
00203 
00204     return aGlText;    
00205 }