Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / GLViewer / GLViewer_Text.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  Author : OPEN CASCADE
23 //#include <GLViewerAfx.h>
24 //
25 #include "GLViewer_Text.h"
26
27 #include <QApplication>
28 #include <QFontMetrics>
29
30 /*!
31   Constructor
32 */
33 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color )
34 {
35   myText = text;
36   myXPos = xPos;
37   myYPos = yPos;
38   myColor = color;
39   myQFont = QApplication::font();//QFont::defaultFont();
40   mySeparator = 2;
41   myDTF = DTF_BITMAP;
42 }
43
44 /*!
45   Constructor
46 */
47 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color, QFont theFont, int theSeparator )
48 {
49   myText = text;
50   myXPos = xPos;
51   myYPos = yPos;
52   myColor = color;
53   myQFont = theFont;
54   mySeparator = theSeparator;
55   myDTF = DTF_BITMAP;
56 }
57
58 /*!
59   Destructor
60 */
61 GLViewer_Text::~GLViewer_Text()
62 {
63 }
64
65 /*!
66   \return width of text
67 */
68 int GLViewer_Text::getWidth()
69 {
70     int aResult = 0;
71     QFontMetrics aFM( myQFont );
72     for( uint i = 0; i < myText.length(); i++ )
73         aResult += aFM.width( myText.at(i) ) + mySeparator;
74     return aResult;
75 }
76
77 /*!
78   \return height of text
79 */
80 int GLViewer_Text::getHeight()
81 {
82     QFontMetrics aFM( myQFont );
83     return aFM.height();
84 }
85
86 /*!
87   Codes object as byte copy
88   \return byte array
89 */
90 QByteArray GLViewer_Text::getByteCopy() const
91 {
92     int i;
93     int aSize = 5*sizeof( int ) + myText.length();
94
95     int aR = myColor.red();
96     int aG = myColor.green();
97     int aB = myColor.blue();
98     const char* aStr = myText.toLatin1().constData();
99
100     int anISize = sizeof( int );    
101     QByteArray aResult;
102     aResult.resize( aSize );
103
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;
110
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;
120
121     int aTextSize = myText.length();
122     aPointer = (char*)&aTextSize;
123     for( ; i < 6*anISize; i++, aPointer++ )
124         aResult[i] = *aPointer;
125
126     for( i = 0; i < aTextSize; i++ )
127         aResult[6*anISize + i] = aStr[i];
128
129     aPointer = (char*)&mySeparator;
130     for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
131         aResult[i] = *aPointer;
132
133     const char* aFontStr = myQFont.toString().toLatin1().constData();
134     int aFontSize = myQFont.toString().length();
135
136     for( i = 0; i < aFontSize; i++ )
137         aResult[7*anISize + aTextSize + i] = aFontStr[i];
138
139     return aResult;
140 }
141
142 /*!
143   Initialize text from binary representation
144   \param theBuf - byte array
145 */
146 GLViewer_Text* GLViewer_Text::fromByteCopy( QByteArray theBuf )
147 {
148     int i = 0;
149     int aSize = (int)theBuf.size();
150     int aR = 0, aG = 0, aB = 0;
151
152     int xPos = 0, yPos = 0;
153
154     int anISize = sizeof( int );
155     char* aPointer = (char*)&xPos;
156     for ( i = 0; i < anISize; i++, aPointer++ )
157         *aPointer = theBuf[i];
158
159     aPointer = (char*)&yPos;
160     for ( ; i < 2*anISize; i++, aPointer++ )
161         *aPointer = theBuf[i];
162
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];
172
173     int aTextSize = 0;
174     aPointer = (char*)&aTextSize;
175     for( ; i < 6*anISize; i++, aPointer++ )
176         *aPointer = theBuf[i];
177
178     QString aText;
179     for( ; i < 6*anISize + aTextSize; i++ )
180     {
181         QChar aChar( theBuf[i] );
182         aText += aChar;
183     }
184
185     int aSeparator = 0;
186     aPointer = (char*)&aSeparator;
187     for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
188         *aPointer = theBuf[i];
189
190     QString aFontStr;
191     for( ; i < aSize; i++ )
192     {
193         QChar aChar( theBuf[i] );
194         aFontStr += aChar;
195     }
196     QFont aFont;
197
198     if( !aFont.fromString( aFontStr ) )
199         return NULL;    
200
201     GLViewer_Text* aGlText = new GLViewer_Text( aText, xPos, yPos, QColor( aR,aG,aB ), aFont, aSeparator  );
202
203     return aGlText;    
204 }