Salome HOME
This file is given from DESCARTES project
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
18 //
19 //  Author : OPEN CASCADE
20 //
21
22 /***************************************************************************
23 **  Class:   GLViewer_Text
24 **  Descr:   Substitution of Prs3d_Text for OpenGL
25 **  Module:  GLViewer
26 **  Created: UI team, 10.07.03
27 ****************************************************************************/
28
29 //#include <GLViewerAfx.h>
30 #include "GLViewer_Text.h"
31
32 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color )
33 {
34   myText = text;
35   myXPos = xPos;
36   myYPos = yPos;
37   myColor = color;
38   myQFont = QFont::defaultFont();
39   mySeparator = 2;
40   myDTF = DTF_BITMAP;
41 }
42
43 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color, QFont theFont, int theSeparator )
44 {
45   myText = text;
46   myXPos = xPos;
47   myYPos = yPos;
48   myColor = color;
49   myQFont = theFont;
50   mySeparator = theSeparator;
51   myDTF = DTF_BITMAP;
52 }
53
54 GLViewer_Text::~GLViewer_Text()
55 {
56 }
57
58 int GLViewer_Text::getWidth()
59 {
60     int aResult = 0;
61     QFontMetrics aFM( myQFont );
62     for( uint i = 0; i < myText.length(); i++ )
63         aResult += aFM.width( myText.at(i) ) + mySeparator;
64     return aResult;
65 }
66
67 int GLViewer_Text::getHeight()
68 {
69     QFontMetrics aFM( myQFont );
70     return aFM.height();
71 }
72
73 QByteArray GLViewer_Text::getByteCopy() const
74 {
75     int i;
76     int aSize = 5*sizeof( int ) + myText.length();
77
78     int aR = myColor.red();
79     int aG = myColor.green();
80     int aB = myColor.blue();
81     const char* aStr = myText.data();
82
83     int anISize = sizeof( int );    
84     QByteArray aResult( aSize );
85
86     char* aPointer = (char*)&myXPos;
87     for( i = 0; i < anISize; i++, aPointer++ )
88         aResult[i] = *aPointer;
89     aPointer = (char*)&myYPos;
90     for( ; i < 2*anISize; i++, aPointer++ )
91         aResult[i] = *aPointer;
92
93     aPointer = (char*)&aR;
94     for( ; i < 3*anISize; i++, aPointer++ )
95         aResult[i] = *aPointer;
96     aPointer = (char*)&aG;
97     for( ; i < 4*anISize; i++, aPointer++ )
98         aResult[i] = *aPointer;
99     aPointer = (char*)&aB;
100     for( ; i < 5*anISize; i++, aPointer++ )
101         aResult[i] = *aPointer;
102
103     int aTextSize = myText.length();
104     aPointer = (char*)&aTextSize;
105     for( ; i < 6*anISize; i++, aPointer++ )
106         aResult[i] = *aPointer;
107
108     for( i = 0; i < aTextSize; i++ )
109         aResult[6*anISize + i] = aStr[i];
110
111     aPointer = (char*)&mySeparator;
112     for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
113         aResult[i] = *aPointer;
114
115     const char* aFontStr = myQFont.toString().data();
116     int aFontSize = myQFont.toString().length();
117
118     for( i = 0; i < aFontSize; i++ )
119         aResult[7*anISize + aTextSize + i] = aFontStr[i];
120
121     return aResult;
122 }
123
124 GLViewer_Text* GLViewer_Text::fromByteCopy( QByteArray theBuf )
125 {
126     int i = 0;
127     int aSize = (int)theBuf.size();
128     int aR = 0, aG = 0, aB = 0;
129
130     int xPos = 0, yPos = 0;
131
132     int anISize = sizeof( int );
133     char* aPointer = (char*)&xPos;
134     for ( i = 0; i < anISize; i++, aPointer++ )
135         *aPointer = theBuf[i];
136
137     aPointer = (char*)&yPos;
138     for ( ; i < 2*anISize; i++, aPointer++ )
139         *aPointer = theBuf[i];
140
141     aPointer = (char*)&aR;
142     for( ; i < 3*anISize; i++, aPointer++ )
143         *aPointer = theBuf[i];
144     aPointer = (char*)&aG;
145     for( ; i < 4*anISize; i++, aPointer++ )
146         *aPointer = theBuf[i];
147     aPointer = (char*)&aB;
148     for( ; i < 5*anISize; i++, aPointer++ )
149         *aPointer = theBuf[i];
150
151     int aTextSize = 0;
152     aPointer = (char*)&aTextSize;
153     for( ; i < 6*anISize; i++, aPointer++ )
154         *aPointer = theBuf[i];
155
156     QString aText;
157     for( ; i < 6*anISize + aTextSize; i++ )
158     {
159         QChar aChar( theBuf[i] );
160         aText += aChar;
161     }
162
163     int aSeparator = 0;
164     aPointer = (char*)&aSeparator;
165     for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
166         *aPointer = theBuf[i];
167
168     QString aFontStr;
169     for( ; i < aSize; i++ )
170     {
171         QChar aChar( theBuf[i] );
172         aFontStr += aChar;
173     }
174     QFont aFont;
175
176     if( !aFont.fromString( aFontStr ) )
177         return NULL;    
178
179     GLViewer_Text* aGlText = new GLViewer_Text( aText, xPos, yPos, QColor( aR,aG,aB ), aFont, aSeparator  );
180
181     return aGlText;    
182 }