1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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
23 // Author : OPEN CASCADE
24 //#include <GLViewerAfx.h>
26 #include "GLViewer_Text.h"
28 #include <QApplication>
29 #include <QFontMetrics>
34 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color )
40 myQFont = QApplication::font();//QFont::defaultFont();
48 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color, QFont theFont, int theSeparator )
55 mySeparator = theSeparator;
62 GLViewer_Text::~GLViewer_Text()
69 int GLViewer_Text::getWidth()
72 QFontMetrics aFM( myQFont );
73 for( uint i = 0; i < myText.length(); i++ )
74 aResult += aFM.width( myText.at(i) ) + mySeparator;
79 \return height of text
81 int GLViewer_Text::getHeight()
83 QFontMetrics aFM( myQFont );
88 Codes object as byte copy
91 QByteArray GLViewer_Text::getByteCopy() const
94 int aSize = 5*sizeof( int ) + myText.length();
96 int aR = myColor.red();
97 int aG = myColor.green();
98 int aB = myColor.blue();
99 const char* aStr = myText.toLatin1().constData();
101 int anISize = sizeof( int );
103 aResult.resize( aSize );
105 char* aPointer = (char*)&myXPos;
106 for( i = 0; i < anISize; i++, aPointer++ )
107 aResult[i] = *aPointer;
108 aPointer = (char*)&myYPos;
109 for( ; i < 2*anISize; i++, aPointer++ )
110 aResult[i] = *aPointer;
112 aPointer = (char*)&aR;
113 for( ; i < 3*anISize; i++, aPointer++ )
114 aResult[i] = *aPointer;
115 aPointer = (char*)&aG;
116 for( ; i < 4*anISize; i++, aPointer++ )
117 aResult[i] = *aPointer;
118 aPointer = (char*)&aB;
119 for( ; i < 5*anISize; i++, aPointer++ )
120 aResult[i] = *aPointer;
122 int aTextSize = myText.length();
123 aPointer = (char*)&aTextSize;
124 for( ; i < 6*anISize; i++, aPointer++ )
125 aResult[i] = *aPointer;
127 for( i = 0; i < aTextSize; i++ )
128 aResult[6*anISize + i] = aStr[i];
130 aPointer = (char*)&mySeparator;
131 for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
132 aResult[i] = *aPointer;
134 const char* aFontStr = myQFont.toString().toLatin1().constData();
135 int aFontSize = myQFont.toString().length();
137 for( i = 0; i < aFontSize; i++ )
138 aResult[7*anISize + aTextSize + i] = aFontStr[i];
144 Initialize text from binary representation
145 \param theBuf - byte array
147 GLViewer_Text* GLViewer_Text::fromByteCopy( QByteArray theBuf )
150 int aSize = (int)theBuf.size();
151 int aR = 0, aG = 0, aB = 0;
153 int xPos = 0, yPos = 0;
155 int anISize = sizeof( int );
156 char* aPointer = (char*)&xPos;
157 for ( i = 0; i < anISize; i++, aPointer++ )
158 *aPointer = theBuf[i];
160 aPointer = (char*)&yPos;
161 for ( ; i < 2*anISize; i++, aPointer++ )
162 *aPointer = theBuf[i];
164 aPointer = (char*)&aR;
165 for( ; i < 3*anISize; i++, aPointer++ )
166 *aPointer = theBuf[i];
167 aPointer = (char*)&aG;
168 for( ; i < 4*anISize; i++, aPointer++ )
169 *aPointer = theBuf[i];
170 aPointer = (char*)&aB;
171 for( ; i < 5*anISize; i++, aPointer++ )
172 *aPointer = theBuf[i];
175 aPointer = (char*)&aTextSize;
176 for( ; i < 6*anISize; i++, aPointer++ )
177 *aPointer = theBuf[i];
180 for( ; i < 6*anISize + aTextSize; i++ )
182 QChar aChar( theBuf[i] );
187 aPointer = (char*)&aSeparator;
188 for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
189 *aPointer = theBuf[i];
192 for( ; i < aSize; i++ )
194 QChar aChar( theBuf[i] );
199 if( !aFont.fromString( aFontStr ) )
202 GLViewer_Text* aGlText = new GLViewer_Text( aText, xPos, yPos, QColor( aR,aG,aB ), aFont, aSeparator );