1 // Copyright (C) 2007-2020 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 #include "GLViewer_Object.h"
24 #include "GLViewer_Drawer.h"
25 #include "GLViewer_AspectLine.h"
26 #include "GLViewer_Text.h"
27 #include "GLViewer_Group.h"
29 #include <SUIT_DataOwner.h>
34 GLViewer_Object::GLViewer_Object()
45 myRect = new GLViewer_Rect();;
46 myUpdateRect = new GLViewer_Rect();;
47 myGLText = new GLViewer_Text( 0, 0, 0, QColor(0,0,0) );
49 myAspectLine = new GLViewer_AspectLine();
50 myType = "GLViewer_Object";
57 isToolTipHTML = false;
65 GLViewer_Object::~GLViewer_Object()
81 \return priority of object
83 int GLViewer_Object::getPriority() const
85 return myDrawer ? myDrawer->getPriority() : 0;
89 \return true if object is inside rectangle
90 \param theRect - rectangle
92 GLboolean GLViewer_Object::isInside( GLViewer_Rect theRect )
94 return theRect.toQRect().contains( myRect->toQRect() );
99 \param zoom - zoom factor
101 GLboolean GLViewer_Object::setZoom( GLfloat zoom, bool, bool )
111 Performs zoom change by step
112 \param zoomIn - to increase to decrease zoom
114 GLboolean GLViewer_Object::updateZoom( bool zoomIn )
117 float step = zoomIn ? 1 : -1;
118 double epsilon = 0.001;
120 if( myZoom - 1 > epsilon )
121 newZoom = ( myZoom * 2 + step ) / 2;
122 else if( 1 - myZoom > epsilon )
123 newZoom = 2 / ( 2 / myZoom - step );
125 newZoom = zoomIn ? 3./2. : 2./3.;
127 if( newZoom < 0.01 || newZoom > 100.0 )
130 return setZoom( newZoom, true );
134 Codes object as byte copy
137 QByteArray GLViewer_Object::getByteCopy()
140 int anISize = sizeof( int );
142 const char* aTypeStr = myType.toLatin1().constData();
143 const char* aToolTipStr = myToolTipText.toUtf8().constData();
145 int aTypeLength = myType.length();
146 int aToolTipLength = myToolTipText.length();
149 QByteArray aGLText = myGLText->getByteCopy();
150 QByteArray aAspect = myAspectLine->getByteCopy();
153 aRectData[ 0 ] = myRect->left();
154 aRectData[ 1 ] = myRect->top();
155 aRectData[ 2 ] = myRect->right();
156 aRectData[ 3 ] = myRect->bottom();
157 aRectData[ 4 ] = myXScale;
158 aRectData[ 5 ] = myYScale;
159 aRectData[ 6 ] = myXGap;
160 aRectData[ 7 ] = myYGap;
162 int sizeOf8Float = sizeof( aRectData );
165 aResult.resize( 2*anISize + sizeOf8Float +
166 aTypeLength + aToolTipLength +
167 aGLText.size() + aAspect.size() );
168 // puts 8 float values into the byte array
169 char* aPointer = (char*)&aRectData;
170 for( i = 0; i < sizeOf8Float; i++, aPointer++ )
171 aResult[i] = *aPointer;
172 // puts length of type string
173 aPointer = (char*)&aTypeLength;
174 for( ; i < anISize + sizeOf8Float; i++, aPointer++ )
175 aResult[i] = *aPointer;
177 for( ; i < anISize + sizeOf8Float + aTypeLength; i++ )
178 aResult[i] = aTypeStr[i - anISize - sizeOf8Float ];
179 // puts length of tooltiptext string
180 aPointer = (char*)&aToolTipLength;
181 for( ; i < 2*anISize + sizeOf8Float + aTypeLength; i++, aPointer++ )
182 aResult[i] = *aPointer;
183 // puts tooltiptext string
184 for( ; i < 2*anISize + sizeOf8Float + aTypeLength + aToolTipLength; i++ )
185 aResult[ i] = aToolTipStr[i - 2*anISize - sizeOf8Float - aTypeLength];
187 int aCurPos = 2*anISize + sizeOf8Float + aTypeLength + aToolTipLength;
188 // adds aspect byte array
189 for ( i = aCurPos; i < (int)( aCurPos + aAspect.size() ); i++ )
190 aResult[i] = aAspect[i - aCurPos];
192 aCurPos = aCurPos + aAspect.size();
193 // adds GL text byte array
194 for ( i = aCurPos; i < (int)( aCurPos + aGLText.size() ); i++ )
195 aResult[i] = aGLText[i - aCurPos];
197 aCurPos += aGLText.size();
198 aPointer = (char*)&myOwner;
199 for( i = 0; i < (int)sizeof( SUIT_DataOwner* ); i++, aPointer++ )
200 aResult[ aCurPos + i ] = *aPointer;
206 Initialize object by byte array
207 \param theArray - byte array
209 bool GLViewer_Object::initializeFromByteCopy( QByteArray theArray )
212 int anISize = sizeof( int );
213 int aFSize = sizeof( GLfloat );
215 float aLeft = 0, aTop = 0, aRight = 0, aBottom = 0;
217 //QString aTypeStr, aToolTipStr;
218 int aTypeLength = 0, aToolTipLength = 0;
220 int aSize = theArray.size();
222 GLViewer_Text* aGLText = new GLViewer_Text( 0, 0, 0, QColor(255,255,255));
223 int aGLTextMinSize = (aGLText->getByteCopy()).size();
224 GLViewer_AspectLine* aAspectLine = new GLViewer_AspectLine();
225 int aGLAspLineSize = (aAspectLine->getByteCopy()).size();
227 QByteArray aGLTextArray, aAspect;
228 aGLTextArray.resize( aGLAspLineSize );
229 aAspect.resize( aGLAspLineSize );
231 if( aSize < 2*anISize + 8*aFSize + aGLTextMinSize + aGLAspLineSize )
234 char* aPointer = (char*)&aLeft;
235 for( i = 0; i < aFSize; i++, aPointer++ )
236 *aPointer = theArray[i];
237 aPointer = (char*)&aTop;
238 for( ; i < 2*aFSize; i++, aPointer++ )
239 *aPointer = theArray[i];
240 aPointer = (char*)&aRight;
241 for( ; i < 3*aFSize; i++, aPointer++ )
242 *aPointer = theArray[i];
243 aPointer = (char*)&aBottom;
244 for( ; i < 4*aFSize; i++, aPointer++ )
245 *aPointer = theArray[i];
247 //myRect = new QRect( aLeft, aTop, aRight - aLeft, aBottom - aTop );
248 myRect = new GLViewer_Rect( aLeft, aRight, aTop, aBottom );
250 aPointer = (char*)&myXScale;
251 for( ; i < 5*aFSize; i++, aPointer++ )
252 *aPointer = theArray[i];
253 aPointer = (char*)&myYScale;
254 for( ; i < 6*aFSize; i++, aPointer++ )
255 *aPointer = theArray[i];
256 aPointer = (char*)&myXGap;
257 for( ; i < 7*aFSize; i++, aPointer++ )
258 *aPointer = theArray[i];
259 aPointer = (char*)&myYGap;
260 for( ; i < 8*aFSize; i++, aPointer++ )
261 *aPointer = theArray[i];
267 aPointer = (char*)&aTypeLength;
268 for( ; i < anISize + 8*aFSize; i++, aPointer++ )
269 *aPointer = theArray[i];
271 for( ; i < anISize + 8*aFSize + aTypeLength; i++ )
273 QChar aChar( theArray[i] );
277 aPointer = (char*)&aToolTipLength;
278 for( ; i < 2*anISize + 8*aFSize + aTypeLength; i++, aPointer++ )
279 *aPointer = theArray[i];
281 for( ; i < 2*anISize + 8*aFSize + aTypeLength + aToolTipLength; i++ )
283 QChar aChar( theArray[i] );
284 myToolTipText += aChar;
287 int aCurPos = 2*anISize + 8*aFSize + aTypeLength + aToolTipLength;
288 if( aSize - aCurPos < aGLTextMinSize + aGLAspLineSize )
291 for( i = 0; i < aGLAspLineSize; i++ )
292 aAspect[i] = theArray[ aCurPos + i ];
293 myAspectLine = GLViewer_AspectLine::fromByteCopy( aAspect );
295 aCurPos = aCurPos + aGLAspLineSize;
296 aGLTextArray.resize( aSize - aCurPos );
297 for( i = 0; i + aCurPos < aSize; i++ )
298 aGLTextArray[i] = theArray[ aCurPos + i ];
299 // replace gl_text pointer by other
302 myGLText = GLViewer_Text::fromByteCopy( aGLTextArray );
309 \param theGroup - group
311 void GLViewer_Object::setGroup( GLViewer_Group* theGroup )
313 if ( myGroup == theGroup )
317 myGroup->removeObject( this );
321 myGroup->addObject( this );
325 \return object's group
327 GLViewer_Group* GLViewer_Object::getGroup() const