1 // Copyright (C) 2005 OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
19 // Author : OPEN CASCADE
22 //#include <GLViewerAfx.h>
23 #include "GLViewer_Text.h"
28 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color )
34 myQFont = QFont::defaultFont();
42 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color, QFont theFont, int theSeparator )
49 mySeparator = theSeparator;
56 GLViewer_Text::~GLViewer_Text()
63 int GLViewer_Text::getWidth()
66 QFontMetrics aFM( myQFont );
67 for( uint i = 0; i < myText.length(); i++ )
68 aResult += aFM.width( myText.at(i) ) + mySeparator;
73 \return height of text
75 int GLViewer_Text::getHeight()
77 QFontMetrics aFM( myQFont );
82 Codes object as byte copy
85 QByteArray GLViewer_Text::getByteCopy() const
88 int aSize = 5*sizeof( int ) + myText.length();
90 int aR = myColor.red();
91 int aG = myColor.green();
92 int aB = myColor.blue();
93 const char* aStr = myText.data();
95 int anISize = sizeof( int );
96 QByteArray aResult( aSize );
98 char* aPointer = (char*)&myXPos;
99 for( i = 0; i < anISize; i++, aPointer++ )
100 aResult[i] = *aPointer;
101 aPointer = (char*)&myYPos;
102 for( ; i < 2*anISize; i++, aPointer++ )
103 aResult[i] = *aPointer;
105 aPointer = (char*)&aR;
106 for( ; i < 3*anISize; i++, aPointer++ )
107 aResult[i] = *aPointer;
108 aPointer = (char*)&aG;
109 for( ; i < 4*anISize; i++, aPointer++ )
110 aResult[i] = *aPointer;
111 aPointer = (char*)&aB;
112 for( ; i < 5*anISize; i++, aPointer++ )
113 aResult[i] = *aPointer;
115 int aTextSize = myText.length();
116 aPointer = (char*)&aTextSize;
117 for( ; i < 6*anISize; i++, aPointer++ )
118 aResult[i] = *aPointer;
120 for( i = 0; i < aTextSize; i++ )
121 aResult[6*anISize + i] = aStr[i];
123 aPointer = (char*)&mySeparator;
124 for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
125 aResult[i] = *aPointer;
127 const char* aFontStr = myQFont.toString().data();
128 int aFontSize = myQFont.toString().length();
130 for( i = 0; i < aFontSize; i++ )
131 aResult[7*anISize + aTextSize + i] = aFontStr[i];
137 Initialize text from binary representation
138 \param theBuf - byte array
140 GLViewer_Text* GLViewer_Text::fromByteCopy( QByteArray theBuf )
143 int aSize = (int)theBuf.size();
144 int aR = 0, aG = 0, aB = 0;
146 int xPos = 0, yPos = 0;
148 int anISize = sizeof( int );
149 char* aPointer = (char*)&xPos;
150 for ( i = 0; i < anISize; i++, aPointer++ )
151 *aPointer = theBuf[i];
153 aPointer = (char*)&yPos;
154 for ( ; i < 2*anISize; i++, aPointer++ )
155 *aPointer = theBuf[i];
157 aPointer = (char*)&aR;
158 for( ; i < 3*anISize; i++, aPointer++ )
159 *aPointer = theBuf[i];
160 aPointer = (char*)&aG;
161 for( ; i < 4*anISize; i++, aPointer++ )
162 *aPointer = theBuf[i];
163 aPointer = (char*)&aB;
164 for( ; i < 5*anISize; i++, aPointer++ )
165 *aPointer = theBuf[i];
168 aPointer = (char*)&aTextSize;
169 for( ; i < 6*anISize; i++, aPointer++ )
170 *aPointer = theBuf[i];
173 for( ; i < 6*anISize + aTextSize; i++ )
175 QChar aChar( theBuf[i] );
180 aPointer = (char*)&aSeparator;
181 for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
182 *aPointer = theBuf[i];
185 for( ; i < aSize; i++ )
187 QChar aChar( theBuf[i] );
192 if( !aFont.fromString( aFontStr ) )
195 GLViewer_Text* aGlText = new GLViewer_Text( aText, xPos, yPos, QColor( aR,aG,aB ), aFont, aSeparator );