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 /***************************************************************************
23 ** Class: GLViewer_Object
24 ** Descr: OpenGL Object
26 ** Created: UI team, 03.09.02
27 ****************************************************************************/
29 //#include <GLViewerAfx.h>
30 #include "GLViewer_Object.h"
31 #include "GLViewer_Drawer.h"
32 #include "GLViewer_AspectLine.h"
33 #include "GLViewer_Geom.h"
34 #include "GLViewer_Text.h"
35 #include "GLViewer_Group.h"
38 //using namespace std;
40 GLViewer_Object::GLViewer_Object()
51 myRect = new GLViewer_Rect();;
52 myUpdateRect = new GLViewer_Rect();;
53 myGLText = new GLViewer_Text( 0, 0, 0, QColor(0,0,0) );
55 myAspectLine = new GLViewer_AspectLine();
56 myType = "GLViewer_Object";
63 isToolTipHTML = false;
68 GLViewer_Object::~GLViewer_Object()
83 int GLViewer_Object::getPriority() const
85 return myDrawer ? myDrawer->getPriority() : 0;
88 GLboolean GLViewer_Object::isInside( GLViewer_Rect theRect )
90 return theRect.toQRect().contains( myRect->toQRect() );
93 GLboolean GLViewer_Object::setZoom( GLfloat zoom, bool, bool )
102 GLboolean GLViewer_Object::updateZoom( bool zoomIn )
105 float step = zoomIn ? 1 : -1;
106 double epsilon = 0.001;
108 if( myZoom - 1 > epsilon )
109 newZoom = ( myZoom * 2 + step ) / 2;
110 else if( 1 - myZoom > epsilon )
111 newZoom = 2 / ( 2 / myZoom - step );
113 newZoom = zoomIn ? 3./2. : 2./3.;
115 if( newZoom < 0.01 || newZoom > 100.0 )
118 return setZoom( newZoom, true );
121 QByteArray GLViewer_Object::getByteCopy()
124 int anISize = sizeof( int );
126 const char* aTypeStr = myType.data();
127 const char* aToolTipStr = myToolTipText.data();
129 int aTypeLength = myType.length();
130 int aToolTipLength = myToolTipText.length();
133 QByteArray aGLText = myGLText->getByteCopy();
134 QByteArray aAspect = myAspectLine->getByteCopy();
137 aRectData[ 0 ] = myRect->left();
138 aRectData[ 1 ] = myRect->top();
139 aRectData[ 2 ] = myRect->right();
140 aRectData[ 3 ] = myRect->bottom();
141 aRectData[ 4 ] = myXScale;
142 aRectData[ 5 ] = myYScale;
143 aRectData[ 6 ] = myXGap;
144 aRectData[ 7 ] = myYGap;
146 int sizeOf8Float = sizeof( aRectData );
148 QByteArray aResult( 2*anISize + sizeOf8Float +
149 aTypeLength + aToolTipLength +
150 aGLText.size() + aAspect.size() );
151 // puts 8 float values into the byte array
152 char* aPointer = (char*)&aRectData;
153 for( i = 0; i < sizeOf8Float; i++, aPointer++ )
154 aResult[i] = *aPointer;
155 // puts length of type string
156 aPointer = (char*)&aTypeLength;
157 for( ; i < anISize + sizeOf8Float; i++, aPointer++ )
158 aResult[i] = *aPointer;
160 for( ; i < anISize + sizeOf8Float + aTypeLength; i++ )
161 aResult[i] = aTypeStr[i - anISize - sizeOf8Float ];
162 // puts length of tooltiptext string
163 aPointer = (char*)&aToolTipLength;
164 for( ; i < 2*anISize + sizeOf8Float + aTypeLength; i++, aPointer++ )
165 aResult[i] = *aPointer;
166 // puts tooltiptext string
167 for( ; i < 2*anISize + sizeOf8Float + aTypeLength + aToolTipLength; i++ )
168 aResult[ i] = aToolTipStr[i - 2*anISize - sizeOf8Float - aTypeLength];
170 int aCurPos = 2*anISize + sizeOf8Float + aTypeLength + aToolTipLength;
171 // adds aspect byte array
172 for( i = aCurPos; i < aCurPos + aAspect.size(); i++ )
173 aResult[i] = aAspect[i - aCurPos];
175 aCurPos = aCurPos + aAspect.size();
176 // adds GL text byte array
177 for( i = aCurPos; i < aCurPos + aGLText.size(); i++ )
178 aResult[i] = aGLText[i - aCurPos];
180 aCurPos += aGLText.size();
181 aPointer = (char*)&myOwner;
182 for( i = 0; i < sizeof( GLViewer_Owner* ); i++, aPointer++ )
183 aResult[ aCurPos + i ] = *aPointer;
188 bool GLViewer_Object::initializeFromByteCopy( QByteArray theArray )
191 int anISize = sizeof( int );
192 int aFSize = sizeof( GLfloat );
194 float aLeft = 0, aTop = 0, aRight = 0, aBottom = 0;
196 //QString aTypeStr, aToolTipStr;
197 int aTypeLength = 0, aToolTipLength = 0;
199 int aSize = theArray.size();
201 GLViewer_Text* aGLText = new GLViewer_Text( 0, 0, 0, QColor(255,255,255));
202 int aGLTextMinSize = (aGLText->getByteCopy()).size();
203 GLViewer_AspectLine* aAspectLine = new GLViewer_AspectLine();
204 int aGLAspLineSize = (aAspectLine->getByteCopy()).size();
206 QByteArray aGLTextArray, aAspect( aGLAspLineSize );
208 if( aSize < 2*anISize + 8*aFSize + aGLTextMinSize + aGLAspLineSize )
211 char* aPointer = (char*)&aLeft;
212 for( i = 0; i < aFSize; i++, aPointer++ )
213 *aPointer = theArray[i];
214 aPointer = (char*)&aTop;
215 for( ; i < 2*aFSize; i++, aPointer++ )
216 *aPointer = theArray[i];
217 aPointer = (char*)&aRight;
218 for( ; i < 3*aFSize; i++, aPointer++ )
219 *aPointer = theArray[i];
220 aPointer = (char*)&aBottom;
221 for( ; i < 4*aFSize; i++, aPointer++ )
222 *aPointer = theArray[i];
224 //myRect = new QRect( aLeft, aTop, aRight - aLeft, aBottom - aTop );
225 myRect = new GLViewer_Rect( aLeft, aRight, aTop, aBottom );
227 aPointer = (char*)&myXScale;
228 for( ; i < 5*aFSize; i++, aPointer++ )
229 *aPointer = theArray[i];
230 aPointer = (char*)&myYScale;
231 for( ; i < 6*aFSize; i++, aPointer++ )
232 *aPointer = theArray[i];
233 aPointer = (char*)&myXGap;
234 for( ; i < 7*aFSize; i++, aPointer++ )
235 *aPointer = theArray[i];
236 aPointer = (char*)&myYGap;
237 for( ; i < 8*aFSize; i++, aPointer++ )
238 *aPointer = theArray[i];
244 aPointer = (char*)&aTypeLength;
245 for( ; i < anISize + 8*aFSize; i++, aPointer++ )
246 *aPointer = theArray[i];
248 for( ; i < anISize + 8*aFSize + aTypeLength; i++ )
250 QChar aChar( theArray[i] );
254 aPointer = (char*)&aToolTipLength;
255 for( ; i < 2*anISize + 8*aFSize + aTypeLength; i++, aPointer++ )
256 *aPointer = theArray[i];
258 for( ; i < 2*anISize + 8*aFSize + aTypeLength + aToolTipLength; i++ )
260 QChar aChar( theArray[i] );
261 myToolTipText += aChar;
264 int aCurPos = 2*anISize + 8*aFSize + aTypeLength + aToolTipLength;
265 if( aSize - aCurPos < aGLTextMinSize + aGLAspLineSize )
268 for( i = 0; i < aGLAspLineSize; i++ )
269 aAspect[i] = theArray[ aCurPos + i ];
270 myAspectLine = GLViewer_AspectLine::fromByteCopy( aAspect );
272 aCurPos = aCurPos + aGLAspLineSize;
273 aGLTextArray.resize( aSize - aCurPos );
274 for( i = 0; i + aCurPos < aSize; i++ )
275 aGLTextArray[i] = theArray[ aCurPos + i ];
276 // replace gl_text pointer by other
279 myGLText = GLViewer_Text::fromByteCopy( aGLTextArray );
284 void GLViewer_Object::setGroup( GLViewer_Group* theGroup )
286 if ( myGroup == theGroup )
290 myGroup->removeObject( this );
294 myGroup->addObject( this );
297 GLViewer_Group* GLViewer_Object::getGroup() const