1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // Author : OPEN CASCADE
23 //#include <GLViewerAfx.h>
25 #include "GLViewer_Text.h"
27 #include <QApplication>
28 #include <QFontMetrics>
33 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color )
39 myQFont = QApplication::font();//QFont::defaultFont();
47 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color, QFont theFont, int theSeparator )
54 mySeparator = theSeparator;
61 GLViewer_Text::~GLViewer_Text()
68 int GLViewer_Text::getWidth()
71 QFontMetrics aFM( myQFont );
72 for( uint i = 0; i < myText.length(); i++ )
73 aResult += aFM.width( myText.at(i) ) + mySeparator;
78 \return height of text
80 int GLViewer_Text::getHeight()
82 QFontMetrics aFM( myQFont );
87 Codes object as byte copy
90 QByteArray GLViewer_Text::getByteCopy() const
93 int aSize = 5*sizeof( int ) + myText.length();
95 int aR = myColor.red();
96 int aG = myColor.green();
97 int aB = myColor.blue();
98 const char* aStr = myText.toLatin1().constData();
100 int anISize = sizeof( int );
102 aResult.resize( aSize );
104 char* aPointer = (char*)&myXPos;
105 for( i = 0; i < anISize; i++, aPointer++ )
106 aResult[i] = *aPointer;
107 aPointer = (char*)&myYPos;
108 for( ; i < 2*anISize; i++, aPointer++ )
109 aResult[i] = *aPointer;
111 aPointer = (char*)&aR;
112 for( ; i < 3*anISize; i++, aPointer++ )
113 aResult[i] = *aPointer;
114 aPointer = (char*)&aG;
115 for( ; i < 4*anISize; i++, aPointer++ )
116 aResult[i] = *aPointer;
117 aPointer = (char*)&aB;
118 for( ; i < 5*anISize; i++, aPointer++ )
119 aResult[i] = *aPointer;
121 int aTextSize = myText.length();
122 aPointer = (char*)&aTextSize;
123 for( ; i < 6*anISize; i++, aPointer++ )
124 aResult[i] = *aPointer;
126 for( i = 0; i < aTextSize; i++ )
127 aResult[6*anISize + i] = aStr[i];
129 aPointer = (char*)&mySeparator;
130 for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
131 aResult[i] = *aPointer;
133 const char* aFontStr = myQFont.toString().toLatin1().constData();
134 int aFontSize = myQFont.toString().length();
136 for( i = 0; i < aFontSize; i++ )
137 aResult[7*anISize + aTextSize + i] = aFontStr[i];
143 Initialize text from binary representation
144 \param theBuf - byte array
146 GLViewer_Text* GLViewer_Text::fromByteCopy( QByteArray theBuf )
149 int aSize = (int)theBuf.size();
150 int aR = 0, aG = 0, aB = 0;
152 int xPos = 0, yPos = 0;
154 int anISize = sizeof( int );
155 char* aPointer = (char*)&xPos;
156 for ( i = 0; i < anISize; i++, aPointer++ )
157 *aPointer = theBuf[i];
159 aPointer = (char*)&yPos;
160 for ( ; i < 2*anISize; i++, aPointer++ )
161 *aPointer = theBuf[i];
163 aPointer = (char*)&aR;
164 for( ; i < 3*anISize; i++, aPointer++ )
165 *aPointer = theBuf[i];
166 aPointer = (char*)&aG;
167 for( ; i < 4*anISize; i++, aPointer++ )
168 *aPointer = theBuf[i];
169 aPointer = (char*)&aB;
170 for( ; i < 5*anISize; i++, aPointer++ )
171 *aPointer = theBuf[i];
174 aPointer = (char*)&aTextSize;
175 for( ; i < 6*anISize; i++, aPointer++ )
176 *aPointer = theBuf[i];
179 for( ; i < 6*anISize + aTextSize; i++ )
181 QChar aChar( theBuf[i] );
186 aPointer = (char*)&aSeparator;
187 for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
188 *aPointer = theBuf[i];
191 for( ; i < aSize; i++ )
193 QChar aChar( theBuf[i] );
198 if( !aFont.fromString( aFontStr ) )
201 GLViewer_Text* aGlText = new GLViewer_Text( aText, xPos, yPos, QColor( aR,aG,aB ), aFont, aSeparator );