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 //#include <GLViewerAfx.h>
23 #include "GLViewer_Object.h"
24 #include "GLViewer_Drawer.h"
25 #include "GLViewer_AspectLine.h"
26 #include "GLViewer_Geom.h"
27 #include "GLViewer_Text.h"
28 #include "GLViewer_Group.h"
31 //using namespace std;
36 GLViewer_Object::GLViewer_Object()
47 myRect = new GLViewer_Rect();;
48 myUpdateRect = new GLViewer_Rect();;
49 myGLText = new GLViewer_Text( 0, 0, 0, QColor(0,0,0) );
51 myAspectLine = new GLViewer_AspectLine();
52 myType = "GLViewer_Object";
59 isToolTipHTML = false;
67 GLViewer_Object::~GLViewer_Object()
83 \return priority of object
85 int GLViewer_Object::getPriority() const
87 return myDrawer ? myDrawer->getPriority() : 0;
91 \return true if object is inside rectangle
92 \param theRect - rectangle
94 GLboolean GLViewer_Object::isInside( GLViewer_Rect theRect )
96 return theRect.toQRect().contains( myRect->toQRect() );
101 \param zoom - zoom factor
103 GLboolean GLViewer_Object::setZoom( GLfloat zoom, bool, bool )
113 Performs zoom change by step
114 \param zoomIn - to increase to decrease zoom
116 GLboolean GLViewer_Object::updateZoom( bool zoomIn )
119 float step = zoomIn ? 1 : -1;
120 double epsilon = 0.001;
122 if( myZoom - 1 > epsilon )
123 newZoom = ( myZoom * 2 + step ) / 2;
124 else if( 1 - myZoom > epsilon )
125 newZoom = 2 / ( 2 / myZoom - step );
127 newZoom = zoomIn ? 3./2. : 2./3.;
129 if( newZoom < 0.01 || newZoom > 100.0 )
132 return setZoom( newZoom, true );
136 Codes object as byte copy
139 QByteArray GLViewer_Object::getByteCopy()
142 int anISize = sizeof( int );
144 const char* aTypeStr = myType.data();
145 const char* aToolTipStr = myToolTipText.data();
147 int aTypeLength = myType.length();
148 int aToolTipLength = myToolTipText.length();
151 QByteArray aGLText = myGLText->getByteCopy();
152 QByteArray aAspect = myAspectLine->getByteCopy();
155 aRectData[ 0 ] = myRect->left();
156 aRectData[ 1 ] = myRect->top();
157 aRectData[ 2 ] = myRect->right();
158 aRectData[ 3 ] = myRect->bottom();
159 aRectData[ 4 ] = myXScale;
160 aRectData[ 5 ] = myYScale;
161 aRectData[ 6 ] = myXGap;
162 aRectData[ 7 ] = myYGap;
164 int sizeOf8Float = sizeof( aRectData );
166 QByteArray aResult( 2*anISize + sizeOf8Float +
167 aTypeLength + aToolTipLength +
168 aGLText.size() + aAspect.size() );
169 // puts 8 float values into the byte array
170 char* aPointer = (char*)&aRectData;
171 for( i = 0; i < sizeOf8Float; i++, aPointer++ )
172 aResult[i] = *aPointer;
173 // puts length of type string
174 aPointer = (char*)&aTypeLength;
175 for( ; i < anISize + sizeOf8Float; i++, aPointer++ )
176 aResult[i] = *aPointer;
178 for( ; i < anISize + sizeOf8Float + aTypeLength; i++ )
179 aResult[i] = aTypeStr[i - anISize - sizeOf8Float ];
180 // puts length of tooltiptext string
181 aPointer = (char*)&aToolTipLength;
182 for( ; i < 2*anISize + sizeOf8Float + aTypeLength; i++, aPointer++ )
183 aResult[i] = *aPointer;
184 // puts tooltiptext string
185 for( ; i < 2*anISize + sizeOf8Float + aTypeLength + aToolTipLength; i++ )
186 aResult[ i] = aToolTipStr[i - 2*anISize - sizeOf8Float - aTypeLength];
188 int aCurPos = 2*anISize + sizeOf8Float + aTypeLength + aToolTipLength;
189 // adds aspect byte array
190 for( i = aCurPos; i < aCurPos + aAspect.size(); i++ )
191 aResult[i] = aAspect[i - aCurPos];
193 aCurPos = aCurPos + aAspect.size();
194 // adds GL text byte array
195 for( i = aCurPos; i < aCurPos + aGLText.size(); i++ )
196 aResult[i] = aGLText[i - aCurPos];
198 aCurPos += aGLText.size();
199 aPointer = (char*)&myOwner;
200 for( i = 0; i < sizeof( GLViewer_Owner* ); i++, aPointer++ )
201 aResult[ aCurPos + i ] = *aPointer;
207 Initialize object by byte array
208 \param theArray - byte array
210 bool GLViewer_Object::initializeFromByteCopy( QByteArray theArray )
213 int anISize = sizeof( int );
214 int aFSize = sizeof( GLfloat );
216 float aLeft = 0, aTop = 0, aRight = 0, aBottom = 0;
218 //QString aTypeStr, aToolTipStr;
219 int aTypeLength = 0, aToolTipLength = 0;
221 int aSize = theArray.size();
223 GLViewer_Text* aGLText = new GLViewer_Text( 0, 0, 0, QColor(255,255,255));
224 int aGLTextMinSize = (aGLText->getByteCopy()).size();
225 GLViewer_AspectLine* aAspectLine = new GLViewer_AspectLine();
226 int aGLAspLineSize = (aAspectLine->getByteCopy()).size();
228 QByteArray aGLTextArray, aAspect( aGLAspLineSize );
230 if( aSize < 2*anISize + 8*aFSize + aGLTextMinSize + aGLAspLineSize )
233 char* aPointer = (char*)&aLeft;
234 for( i = 0; i < aFSize; i++, aPointer++ )
235 *aPointer = theArray[i];
236 aPointer = (char*)&aTop;
237 for( ; i < 2*aFSize; i++, aPointer++ )
238 *aPointer = theArray[i];
239 aPointer = (char*)&aRight;
240 for( ; i < 3*aFSize; i++, aPointer++ )
241 *aPointer = theArray[i];
242 aPointer = (char*)&aBottom;
243 for( ; i < 4*aFSize; i++, aPointer++ )
244 *aPointer = theArray[i];
246 //myRect = new QRect( aLeft, aTop, aRight - aLeft, aBottom - aTop );
247 myRect = new GLViewer_Rect( aLeft, aRight, aTop, aBottom );
249 aPointer = (char*)&myXScale;
250 for( ; i < 5*aFSize; i++, aPointer++ )
251 *aPointer = theArray[i];
252 aPointer = (char*)&myYScale;
253 for( ; i < 6*aFSize; i++, aPointer++ )
254 *aPointer = theArray[i];
255 aPointer = (char*)&myXGap;
256 for( ; i < 7*aFSize; i++, aPointer++ )
257 *aPointer = theArray[i];
258 aPointer = (char*)&myYGap;
259 for( ; i < 8*aFSize; i++, aPointer++ )
260 *aPointer = theArray[i];
266 aPointer = (char*)&aTypeLength;
267 for( ; i < anISize + 8*aFSize; i++, aPointer++ )
268 *aPointer = theArray[i];
270 for( ; i < anISize + 8*aFSize + aTypeLength; i++ )
272 QChar aChar( theArray[i] );
276 aPointer = (char*)&aToolTipLength;
277 for( ; i < 2*anISize + 8*aFSize + aTypeLength; i++, aPointer++ )
278 *aPointer = theArray[i];
280 for( ; i < 2*anISize + 8*aFSize + aTypeLength + aToolTipLength; i++ )
282 QChar aChar( theArray[i] );
283 myToolTipText += aChar;
286 int aCurPos = 2*anISize + 8*aFSize + aTypeLength + aToolTipLength;
287 if( aSize - aCurPos < aGLTextMinSize + aGLAspLineSize )
290 for( i = 0; i < aGLAspLineSize; i++ )
291 aAspect[i] = theArray[ aCurPos + i ];
292 myAspectLine = GLViewer_AspectLine::fromByteCopy( aAspect );
294 aCurPos = aCurPos + aGLAspLineSize;
295 aGLTextArray.resize( aSize - aCurPos );
296 for( i = 0; i + aCurPos < aSize; i++ )
297 aGLTextArray[i] = theArray[ aCurPos + i ];
298 // replace gl_text pointer by other
301 myGLText = GLViewer_Text::fromByteCopy( aGLTextArray );
308 \param theGroup - group
310 void GLViewer_Object::setGroup( GLViewer_Group* theGroup )
312 if ( myGroup == theGroup )
316 myGroup->removeObject( this );
320 myGroup->addObject( this );
324 \return object's group
326 GLViewer_Group* GLViewer_Object::getGroup() const