1 /***************************************************************************
2 ** Class: GLViewer_Object
3 ** Descr: OpenGL Object
5 ** Created: UI team, 03.09.02
6 ****************************************************************************/
8 //#include <GLViewerAfx.h>
9 #include "GLViewer_Object.h"
10 #include "GLViewer_Drawer.h"
11 #include "GLViewer_AspectLine.h"
12 #include "GLViewer_Geom.h"
13 #include "GLViewer_Text.h"
14 #include "GLViewer_Group.h"
17 //using namespace std;
19 GLViewer_Object::GLViewer_Object()
30 myRect = new GLViewer_Rect();;
31 myUpdateRect = new GLViewer_Rect();;
32 myGLText = new GLViewer_Text( 0, 0, 0, QColor(0,0,0) );
34 myAspectLine = new GLViewer_AspectLine();
35 myType = "GLViewer_Object";
42 isToolTipHTML = false;
47 GLViewer_Object::~GLViewer_Object()
62 int GLViewer_Object::getPriority() const
64 return myDrawer ? myDrawer->getPriority() : 0;
67 GLboolean GLViewer_Object::isInside( GLViewer_Rect theRect )
69 return theRect.toQRect().contains( myRect->toQRect() );
72 GLboolean GLViewer_Object::setZoom( GLfloat zoom, bool, bool )
81 GLboolean GLViewer_Object::updateZoom( bool zoomIn )
84 float step = zoomIn ? 1 : -1;
85 double epsilon = 0.001;
87 if( myZoom - 1 > epsilon )
88 newZoom = ( myZoom * 2 + step ) / 2;
89 else if( 1 - myZoom > epsilon )
90 newZoom = 2 / ( 2 / myZoom - step );
92 newZoom = zoomIn ? 3./2. : 2./3.;
94 if( newZoom < 0.01 || newZoom > 100.0 )
97 return setZoom( newZoom, true );
100 QByteArray GLViewer_Object::getByteCopy()
103 int anISize = sizeof( int );
105 const char* aTypeStr = myType.data();
106 const char* aToolTipStr = myToolTipText.data();
108 int aTypeLength = myType.length();
109 int aToolTipLength = myToolTipText.length();
112 QByteArray aGLText = myGLText->getByteCopy();
113 QByteArray aAspect = myAspectLine->getByteCopy();
116 aRectData[ 0 ] = myRect->left();
117 aRectData[ 1 ] = myRect->top();
118 aRectData[ 2 ] = myRect->right();
119 aRectData[ 3 ] = myRect->bottom();
120 aRectData[ 4 ] = myXScale;
121 aRectData[ 5 ] = myYScale;
122 aRectData[ 6 ] = myXGap;
123 aRectData[ 7 ] = myYGap;
125 int sizeOf8Float = sizeof( aRectData );
127 QByteArray aResult( 2*anISize + sizeOf8Float +
128 aTypeLength + aToolTipLength +
129 aGLText.size() + aAspect.size() );
130 // puts 8 float values into the byte array
131 char* aPointer = (char*)&aRectData;
132 for( i = 0; i < sizeOf8Float; i++, aPointer++ )
133 aResult[i] = *aPointer;
134 // puts length of type string
135 aPointer = (char*)&aTypeLength;
136 for( ; i < anISize + sizeOf8Float; i++, aPointer++ )
137 aResult[i] = *aPointer;
139 for( ; i < anISize + sizeOf8Float + aTypeLength; i++ )
140 aResult[i] = aTypeStr[i - anISize - sizeOf8Float ];
141 // puts length of tooltiptext string
142 aPointer = (char*)&aToolTipLength;
143 for( ; i < 2*anISize + sizeOf8Float + aTypeLength; i++, aPointer++ )
144 aResult[i] = *aPointer;
145 // puts tooltiptext string
146 for( ; i < 2*anISize + sizeOf8Float + aTypeLength + aToolTipLength; i++ )
147 aResult[ i] = aToolTipStr[i - 2*anISize - sizeOf8Float - aTypeLength];
149 int aCurPos = 2*anISize + sizeOf8Float + aTypeLength + aToolTipLength;
150 // adds aspect byte array
151 for( i = aCurPos; i < aCurPos + aAspect.size(); i++ )
152 aResult[i] = aAspect[i - aCurPos];
154 aCurPos = aCurPos + aAspect.size();
155 // adds GL text byte array
156 for( i = aCurPos; i < aCurPos + aGLText.size(); i++ )
157 aResult[i] = aGLText[i - aCurPos];
159 aCurPos += aGLText.size();
160 aPointer = (char*)&myOwner;
161 for( i = 0; i < sizeof( GLViewer_Owner* ); i++, aPointer++ )
162 aResult[ aCurPos + i ] = *aPointer;
167 bool GLViewer_Object::initializeFromByteCopy( QByteArray theArray )
170 int anISize = sizeof( int );
171 int aFSize = sizeof( GLfloat );
173 float aLeft = 0, aTop = 0, aRight = 0, aBottom = 0;
175 //QString aTypeStr, aToolTipStr;
176 int aTypeLength = 0, aToolTipLength = 0;
178 int aSize = theArray.size();
180 GLViewer_Text* aGLText = new GLViewer_Text( 0, 0, 0, QColor(255,255,255));
181 int aGLTextMinSize = (aGLText->getByteCopy()).size();
182 GLViewer_AspectLine* aAspectLine = new GLViewer_AspectLine();
183 int aGLAspLineSize = (aAspectLine->getByteCopy()).size();
185 QByteArray aGLTextArray, aAspect( aGLAspLineSize );
187 if( aSize < 2*anISize + 8*aFSize + aGLTextMinSize + aGLAspLineSize )
190 char* aPointer = (char*)&aLeft;
191 for( i = 0; i < aFSize; i++, aPointer++ )
192 *aPointer = theArray[i];
193 aPointer = (char*)&aTop;
194 for( ; i < 2*aFSize; i++, aPointer++ )
195 *aPointer = theArray[i];
196 aPointer = (char*)&aRight;
197 for( ; i < 3*aFSize; i++, aPointer++ )
198 *aPointer = theArray[i];
199 aPointer = (char*)&aBottom;
200 for( ; i < 4*aFSize; i++, aPointer++ )
201 *aPointer = theArray[i];
203 //myRect = new QRect( aLeft, aTop, aRight - aLeft, aBottom - aTop );
204 myRect = new GLViewer_Rect( aLeft, aRight, aTop, aBottom );
206 aPointer = (char*)&myXScale;
207 for( ; i < 5*aFSize; i++, aPointer++ )
208 *aPointer = theArray[i];
209 aPointer = (char*)&myYScale;
210 for( ; i < 6*aFSize; i++, aPointer++ )
211 *aPointer = theArray[i];
212 aPointer = (char*)&myXGap;
213 for( ; i < 7*aFSize; i++, aPointer++ )
214 *aPointer = theArray[i];
215 aPointer = (char*)&myYGap;
216 for( ; i < 8*aFSize; i++, aPointer++ )
217 *aPointer = theArray[i];
223 aPointer = (char*)&aTypeLength;
224 for( ; i < anISize + 8*aFSize; i++, aPointer++ )
225 *aPointer = theArray[i];
227 for( ; i < anISize + 8*aFSize + aTypeLength; i++ )
229 QChar aChar( theArray[i] );
233 aPointer = (char*)&aToolTipLength;
234 for( ; i < 2*anISize + 8*aFSize + aTypeLength; i++, aPointer++ )
235 *aPointer = theArray[i];
237 for( ; i < 2*anISize + 8*aFSize + aTypeLength + aToolTipLength; i++ )
239 QChar aChar( theArray[i] );
240 myToolTipText += aChar;
243 int aCurPos = 2*anISize + 8*aFSize + aTypeLength + aToolTipLength;
244 if( aSize - aCurPos < aGLTextMinSize + aGLAspLineSize )
247 for( i = 0; i < aGLAspLineSize; i++ )
248 aAspect[i] = theArray[ aCurPos + i ];
249 myAspectLine = GLViewer_AspectLine::fromByteCopy( aAspect );
251 aCurPos = aCurPos + aGLAspLineSize;
252 aGLTextArray.resize( aSize - aCurPos );
253 for( i = 0; i + aCurPos < aSize; i++ )
254 aGLTextArray[i] = theArray[ aCurPos + i ];
255 // replace gl_text pointer by other
258 myGLText = GLViewer_Text::fromByteCopy( aGLTextArray );
263 void GLViewer_Object::setGroup( GLViewer_Group* theGroup )
266 myGroup->removeObject( this );
270 myGroup->addObject( this );
273 GLViewer_Group* GLViewer_Object::getGroup() const