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