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.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : OPEN CASCADE
22 // File: GLViewer_Viewer2d.cxx
23 // Created: November, 2004
25 //#include <GLViewerAfx.h>
26 #include "GLViewer_Viewer2d.h"
27 #include "GLViewer_Object.h"
28 #include "GLViewer_BaseObjects.h"
29 #include "GLViewer_CoordSystem.h"
30 #include "GLViewer_Context.h"
31 #include "GLViewer_Drawer.h"
32 #include "GLViewer_Selector2d.h"
33 //#include "GLViewer_Sketcher.h"
34 #include "GLViewer_ViewPort2d.h"
36 #include "SUIT_Desktop.h"
37 #include "SUIT_ViewWindow.h"
39 #include "OSD_Timer.hxx"
40 #include <TColStd_MapOfInteger.hxx>
42 #include <qpopupmenu.h>
43 #include <qpointarray.h>
44 #include <qcolordialog.h>
48 \param title - viewer title
50 GLViewer_Viewer2d::GLViewer_Viewer2d( const QString& title) :
51 GLViewer_Viewer( title )
53 myGLContext = new GLViewer_Context( this );
55 //myGLSketcher = new GLViewer_Sketcher( this );
59 mySelMode = GLViewer_Viewer::Multiple;
67 GLViewer_Viewer2d::~GLViewer_Viewer2d()
70 //delete myGLSketcher;
71 GLViewer_TexFont::clearTextBases();
74 /*!Create new instance of view window on desktop \a theDesktop.
75 *\retval SUIT_ViewWindow* - created view window pointer.
77 SUIT_ViewWindow* GLViewer_Viewer2d::createView( SUIT_Desktop* theDesktop )
79 return new GLViewer_ViewFrame( theDesktop, this );
83 Adds item for change background color
84 \param thePopup - menu
86 void GLViewer_Viewer2d::addPopupItems( QPopupMenu* thePopup )
88 // CTH8434. "Change background color" menu item is available if there are no selected objects
89 if ( getSelector() == 0 || getSelector()->numSelected() == 0 )
91 if( thePopup->count() > 0 )
92 thePopup->insertSeparator();
93 thePopup->insertItem( tr( "CHANGE_BGCOLOR" ), this, SLOT( onChangeBgColor() ) );
98 Changes background color
100 void GLViewer_Viewer2d::onChangeBgColor()
102 if( !getActiveView() )
104 GLViewer_ViewPort2d* vp = ( ( GLViewer_ViewPort2d* )getActiveView()->getViewPort() );
106 QColor selColor = QColorDialog::getColor( vp->backgroundColor(), vp );
107 if ( selColor.isValid() ) {
108 vp->setBackgroundColor( selColor );
113 Updates colors for all drawers (does not work)
115 void GLViewer_Viewer2d::updateColors( QColor colorH, QColor colorS )
117 // cout << "GLViewer_Viewer2d::updateColors" << endl;
120 for ( DrawerMap::Iterator it = myDrawers.begin(); it != myDrawers.end(); ++it )
122 it.key()->setHColor( colorH );
123 it.key()->setSColor( colorS );
127 ObjList anObjects = myGLContext->getObjects();
128 ObjList::Iterator beginIt = anObjects.begin();
129 ObjList::Iterator endIt = anObjects.end();
130 for ( ObjList::Iterator it = beginIt; it != endIt; ++it )
132 //GLViewer_Drawer* aDrawer = (*it)->getDrawer();
133 //aDrawer->setHColor( colorH );
134 //aDrawer->setSColor( colorS );
139 activateAllDrawers( TRUE );
143 Updates rect of global scene by adding new rectangle
144 \param theRect - rectangle
146 void GLViewer_Viewer2d::updateBorders( GLViewer_Rect* theRect )
148 QPtrVector<SUIT_ViewWindow> views = getViewManager()->getViews();
149 for ( int i = 0, n = views.count(); i < n; i++ )
151 GLViewer_Rect* border = ( ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)views[i])->getViewPort() )->getBorder();
153 border->setLeft( QMIN( border->left(), theRect->left() ) );
154 border->setRight( QMAX( border->right(), theRect->right() ) );
155 border->setBottom( QMIN( border->bottom(), theRect->bottom() ) );
156 border->setTop( QMAX( border->top(), theRect->top() ) );
161 Recomputes global scene rect
163 void GLViewer_Viewer2d::updateBorders()
165 QPtrVector<SUIT_ViewWindow> views = getViewManager()->getViews();
167 ObjList anObjects = myGLContext->getObjects();
168 ObjList::Iterator beginIt = anObjects.begin();
169 ObjList::Iterator endIt = anObjects.end();
170 for ( int i = 0, n = views.count(); i < n; i++ )
172 GLViewer_Rect* border = ( ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)views[i])->getViewPort() )->getBorder();
175 border->setIsEmpty( true );
176 // initialise border by default values to avoid old values
177 border->setCoords( 0, 0, 0, 0 );
178 for ( ObjList::Iterator it = beginIt; it != endIt; ++it )
180 GLViewer_Object* anObject = *it;
181 GLViewer_Rect* aRect = anObject->getRect();
182 if( !anObject->isSelectable() || !anObject->getVisible() )
185 if( border->isEmpty() )
187 border->setIsEmpty( false );
188 border->setCoords( aRect->left(), aRect->right(), aRect->bottom(), aRect->top() );
192 border->setLeft( QMIN( border->left(), aRect->left() ) );
193 border->setRight( QMAX( border->right(), aRect->right() ) );
194 border->setBottom( QMIN( border->bottom(), aRect->bottom() ) );
195 border->setTop( QMAX( border->top(), aRect->top() ) );
202 Redraws all active objects by updating all drawers in all views
204 void GLViewer_Viewer2d::updateAll()
206 if ( !getActiveView() )
209 QPtrVector<SUIT_ViewWindow> views = getViewManager()->getViews();
210 for ( int i = 0, n = views.count(); i < n; i++ )
211 ( ( GLViewer_ViewPort2d* )( ( GLViewer_ViewFrame* )views[i] )->getViewPort() )->getGLWidget()->updateGL();
215 \param onlyUpdate is passed to method activateAllDrawers drawers
217 void GLViewer_Viewer2d::updateDrawers( GLboolean update, GLfloat scX, GLfloat scY )
219 // cout << "GLViewer_Viewer2d::updateDrawers" << endl;
221 //myGLContext->updateScales( scX, scY );
222 //myGLSketcher->drawContour();
223 activateAllDrawers( update );
227 Activates drawers for objects from list \param theObjects only
229 void GLViewer_Viewer2d::activateDrawers( QValueList<GLViewer_Object*>& theObjects, bool onlyUpdate, GLboolean swap )
231 //cout << "GLViewer_Viewer2d::activateDrawers " << (int)onlyUpdate << " " << (int)swap << endl;
232 QValueList<GLViewer_Drawer*>::Iterator anIt = myDrawers.begin();
233 QValueList<GLViewer_Drawer*>::Iterator endDIt = myDrawers.end();
234 for( ; anIt != endDIt; anIt++ )
237 QValueList<GLViewer_Drawer*> anActiveDrawers;
238 QValueList<GLViewer_Object*>::Iterator endOIt = theObjects.end();
240 for( QValueList<GLViewer_Object*>::Iterator oit = theObjects.begin(); oit != endOIt; ++oit )
242 GLViewer_Drawer* aDrawer = (*oit)->getDrawer();
245 anIt = myDrawers.begin();
246 endDIt = myDrawers.end();
248 for( ; anIt != endDIt; anIt++ )
249 if( (*anIt)->getObjectType() == (*oit)->getObjectType() )
251 (*oit)->setDrawer( *anIt );
258 myDrawers.append( (*oit)->createDrawer() );
259 aDrawer = (*oit)->getDrawer();
264 aDrawer->addObject( (*oit) );
266 int aPriority = aDrawer->getPriority();
268 if( anActiveDrawers.findIndex( aDrawer ) != -1 )
271 QValueList<GLViewer_Drawer*>::Iterator aDIt = anActiveDrawers.begin();
272 QValueList<GLViewer_Drawer*>::Iterator aDEndIt = anActiveDrawers.end();
273 for( ; aDIt != aDEndIt; ++aDIt )
274 if( (*aDIt)->getPriority() > aPriority )
277 anActiveDrawers.insert( aDIt, aDrawer );
280 QValueList<GLViewer_Drawer*>::Iterator aDIt = anActiveDrawers.begin();
281 QValueList<GLViewer_Drawer*>::Iterator aDEndIt = anActiveDrawers.end();
283 QPtrVector<SUIT_ViewWindow> views = getViewManager()->getViews();
284 for ( int i = 0, n = views.count(); i < n; i++ )
286 float xScale, yScale;
287 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)views[i])->getViewPort();
288 vp->getScale( xScale, yScale );
289 vp->getGLWidget()->makeCurrent();
291 for( ; aDIt != aDEndIt; aDIt++ )
293 GLViewer_Drawer* aDrawer = *aDIt;
295 aDrawer->create( xScale, yScale, onlyUpdate );
299 GLViewer_Rect* border = ( ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)views[i])->getViewPort() )->getBorder();
300 (*aDIt)->drawRectangle( border, Qt::blue );
302 QString coords = QString::number( border->left() ) + " " + QString::number( border->right() ) + " " +
303 QString::number( border->bottom() ) + " " + QString::number( border->top() );
304 (*aDIt)->drawText( "Border : " + coords, border->left(), border->top() + 10 / yScale,
305 Qt::blue, &QFont( "Courier", 8, QFont::Normal ), 2 );
308 vp->getGLWidget()->swapBuffers();
311 ( ( GLViewer_ViewPort2d* )getActiveView()->getViewPort() )->getGLWidget()->makeCurrent();
315 Activates drawer for \param theObject
317 void GLViewer_Viewer2d::activateDrawer( GLViewer_Object* theObject, bool onlyUpdate, GLboolean swap )
320 aList.append( theObject );
321 activateDrawers( aList, onlyUpdate, swap );
325 \param onlyUpdate is passed to drawers
327 void GLViewer_Viewer2d::activateAllDrawers( bool onlyUpdate, GLboolean swap )
329 if ( !getActiveView() )
332 ObjList anActiveObjs;
333 const ObjList& objs = myGLContext->getObjects();
334 for( ObjList::const_iterator it = objs.begin(); it != objs.end(); ++it )
336 GLViewer_Object* obj = (GLViewer_Object*)(*it);
337 if( obj->getVisible() )
338 anActiveObjs.append( obj );
341 activateDrawers( anActiveObjs, onlyUpdate, swap );
345 Creates set of marker
346 \param theMarkersNum - number of markers
347 \param theMarkersRad - radius of markers
349 void GLViewer_Viewer2d::onCreateGLMarkers( int theMarkersNum, int theMarkersRad )
351 if ( !getActiveView() )
354 GLViewer_MarkerSet* aMarkerSet = new GLViewer_MarkerSet( theMarkersNum, theMarkersRad );
355 getGLContext()->insertObject( aMarkerSet );
357 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
358 int vpWidth = vp->getWidth();
359 int vpHeight = vp->getHeight();
361 float* aXCoord = new float[ theMarkersNum ];
362 float* anYCoord = new float[ theMarkersNum ];
365 for ( long i = 0; i < theMarkersNum; i++ )
367 aXCoord[i] = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpWidth / 2.);
368 anYCoord[i] = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpHeight / 2.);
371 aMarkerSet->setXCoord( aXCoord, theMarkersNum );
372 aMarkerSet->setYCoord( anYCoord, theMarkersNum );
373 aMarkerSet->compute();
375 updateBorders( aMarkerSet->getRect() );
377 activateAllDrawers( false );
378 activateTransform( GLViewer_Viewer::FitAll );
386 \param theAnglesNum - number of angles
387 \param theRadius - radius
388 \param thePolylineNumber - number
390 void GLViewer_Viewer2d::onCreateGLPolyline( int theAnglesNum, int theRadius, int thePolylineNumber )
392 if ( !getActiveView() )
395 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
396 int vpWidth = vp->getWidth();
397 int vpHeight = vp->getHeight();
399 float* aXCoord = new float[ theAnglesNum ];
400 float* anYCoord = new float[ theAnglesNum ];
402 //srand( ( unsigned )time( NULL ) );
404 for( int j = 0; j < thePolylineNumber; j++)
406 GLViewer_Polyline* aPolyline = new GLViewer_Polyline( theAnglesNum, theRadius );
407 getGLContext()->insertObject( aPolyline );
409 float aXOffset = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpWidth / 2.);
410 float anYOffset = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpHeight / 2.);
411 for( int i = 0; i < theAnglesNum; i++ )
413 aXCoord[i] = cos( 2. * PI * i / theAnglesNum ) * theRadius + aXOffset;
414 anYCoord[i] = sin( 2. * PI * i / theAnglesNum ) * theRadius + anYOffset;
417 aPolyline->setHighSelAll( true );
418 aPolyline->setClosed( true );
419 aPolyline->setXCoord( aXCoord, theAnglesNum );
420 aPolyline->setYCoord( anYCoord, theAnglesNum );
421 aPolyline->compute();
423 updateBorders( aPolyline->getRect() );
426 activateAllDrawers( false );
427 activateTransform( GLViewer_Viewer::FitAll );
435 \param theStr - text string
436 \param theTextNumber - number
438 void GLViewer_Viewer2d::onCreateGLText( QString theStr, int theTextNumber )
440 if ( !getActiveView() )
443 if( theTextNumber <= 0 )
446 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
447 int vpWidth = vp->getWidth();
448 int vpHeight = vp->getHeight();
450 //srand( ( unsigned )time( NULL ) );
452 for( int j = 0; j < theTextNumber; j++)
454 float aXPos = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpWidth / 2.);
455 float anYPos = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpHeight / 2.);
456 QColor aColor( 255, 0, 255 );
458 GLViewer_TextObject* aText = new GLViewer_TextObject( theStr, aXPos, anYPos, aColor );
460 getGLContext()->insertObject( aText );
462 updateBorders( aText->getRect() );
465 activateAllDrawers( false );
469 Translates point from global CS to curreent viewer CS
470 \param x, y - co-ordinates to be translated
472 void GLViewer_Viewer2d::transPoint( GLfloat& x, GLfloat& y )
474 if ( !getActiveView() )
477 GLfloat xScale, yScale;
480 GLViewer_ViewPort2d* curvp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
482 curvp->getScale( xScale, yScale );
483 curvp->getPan( xPan, yPan );
485 GLfloat a = curvp->getGLWidget()->getRotationAngle() * PI / 180.;
487 x = ( x - ( GLfloat )curvp->getWidth() / 2 ) / xScale;
488 y = ( -y + ( GLfloat )curvp->getHeight() / 2 ) / yScale;
493 x = x1 * cos(a) + y1 * sin(a);
494 y = -x1 * sin(a) + y1 * cos(a);
501 \return object rect in window CS
502 \param theObject - object
504 QRect* GLViewer_Viewer2d::getWinObjectRect( GLViewer_Object* theObject )
506 if ( !getActiveView() )
509 GLfloat xScale, yScale;
512 GLViewer_ViewPort2d* curvp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
513 GLfloat aWidth = curvp->getWidth();
514 GLfloat aHeight = curvp->getHeight();
517 curvp->getScale( xScale, yScale );
518 curvp->getPan( xPan, yPan );
520 QRect aObjRect = theObject->getRect()->toQRect();
521 float aLeft = aObjRect.left() + xPan, aRight = aObjRect.right() + xPan;
522 float aTop = aObjRect.top() + yPan, aBot = aObjRect.bottom() + yPan;
524 GLfloat anAngle = curvp->getGLWidget()->getRotationAngle() * PI / 180.;
526 QPointArray aPointArray(4);
527 aPointArray[0] = QPoint( (int)(aLeft*cos(anAngle) - aTop*sin(anAngle)),
528 (int)(aLeft*sin(anAngle) + aTop*cos(anAngle)) );
529 aPointArray[1] = QPoint( (int)(aRight*cos(anAngle) - aTop*sin(anAngle)),
530 (int)(aRight*sin(anAngle) + aTop*cos(anAngle)) );
531 aPointArray[2] = QPoint( (int)(aRight*cos(anAngle) - aBot*sin(anAngle)),
532 (int)(aRight*sin(anAngle) + aBot*cos(anAngle)) );
533 aPointArray[3] = QPoint( (int)(aLeft*cos(anAngle) - aBot*sin(anAngle)),
534 (int)(aLeft*sin(anAngle) + aBot*cos(anAngle)) );
536 int aMinLeft = aPointArray[0].x(), aMaxRight = aPointArray[0].x(),
537 aMinTop = aPointArray[0].y(), aMaxBottom = aPointArray[0].y();
538 for( int i = 1; i < 4; i++ )
540 int x = aPointArray[i].x();
541 int y = aPointArray[i].y();
542 aMinLeft = QMIN( aMinLeft,x );
543 aMaxRight = QMAX( aMaxRight, x );
544 aMinTop = QMIN( aMinTop, y );
545 aMaxBottom = QMAX( aMaxBottom, y );
548 aLeft = (aMinLeft/* + xPan*/)*xScale + aWidth / 2;
549 aRight = (aMaxRight/* + xPan*/)*xScale + aWidth / 2;
551 aTop = -( (aMaxBottom/* + yPan*/)*yScale - aHeight / 2 );
552 aBot = -( (aMinTop/* + yPan*/)*yScale - aHeight / 2 );
554 QRect* newRect = new QRect( (int)aLeft, (int)aTop, (int)(aRight-aLeft), (int)(aBot-aTop) );
560 Translates rect in window CS to rect in global CS
561 \param theRect - rectangle to be translated
562 \return transformed rect
564 GLViewer_Rect GLViewer_Viewer2d::getGLVRect( const QRect& theRect ) const
566 if ( !getActiveView() )
567 return GLViewer_Rect();
569 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
572 return GLViewer_Rect();
574 return vp->win2GLV( theRect );
578 Translates rect in global CS to rect in window CS
579 \param theRect - rectangle to be translated
580 \return transformed rect
582 QRect GLViewer_Viewer2d::getQRect( const GLViewer_Rect& theRect ) const
584 if ( !getActiveView() )
587 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
592 return vp->GLV2win( theRect );
598 GLViewer_Selector* GLViewer_Viewer2d::createSelector()
600 return new GLViewer_Selector2d( this, getGLContext() );
604 \return new Transformer
605 \param type - type of new transformer
607 GLViewer_ViewTransformer* GLViewer_Viewer2d::createTransformer( int type )
609 return new GLViewer_View2dTransformer( this, type );
613 Custom mouse event handler
615 void GLViewer_Viewer2d::onMouseEvent( SUIT_ViewWindow*, QMouseEvent* e )
617 if ( !getActiveView() )
620 //if ( testRotation( e ) )
625 case QEvent::MouseButtonPress :
626 case QEvent::MouseMove :
627 case QEvent::MouseButtonRelease :
628 //if( myGLSketcher->getType() != None )
629 // myGLSketcher->sketch( e );
633 GLViewer_Viewer::onMouseEvent( 0, e );
637 Rotation transformation
639 bool GLViewer_Viewer2d::testRotation( QMouseEvent* e )
641 if ( ( e->button() == GLViewer_View2dTransformer::rotateButton() ) &&
642 ( e->type() == QEvent::MouseButtonPress ) &&
643 ( e->state() & GLViewer_ViewTransformer::accelKey() ) )
645 activateTransform( GLViewer_Viewer::Rotate );
652 Inserts text lines as header for file
653 \param aType - file type
654 \param hFile - file instance
656 void GLViewer_Viewer2d::insertHeader( VectorFileType aType, QFile& hFile )
658 if( aType == POST_SCRIPT )
660 QString header = "%!PS-Adobe-3.0\n";
661 header += "%%Creator: OpenCascade 2004\n";
662 header += "%%Title: Our document\n";
663 header += "%%PageOrder: Ascend\n";
664 header += "%%Orientation: Portrait\n";
665 header += "%%LanguageLevel: 2\n";
667 header += "%%Pages: 1\n";
668 header += "%%Page: 1\n\n";
670 hFile.writeBlock( header.ascii(), header.length() );
672 else if( aType == HPGL )
674 QString header = "[Esc].(;\n";
675 header += "[Esc].I81;;17:\n";
676 header += "[Esc].N;19:\n";
684 hFile.writeBlock( header.ascii(), header.length() );
689 Inserts text lines as ending for file
690 \param aType - file type
691 \param hFile - file instance
693 void GLViewer_Viewer2d::insertEnding( VectorFileType aType, QFile& hFile )
695 if( aType == POST_SCRIPT )
697 QString ending = "showpage\n\n%%EOF";
698 hFile.writeBlock( ending.ascii(), ending.length() );
700 else if( aType == HPGL )
702 QString ending = "PU;PA0,0;SP;EC;PG1;EC1;OE\n";
703 hFile.writeBlock( ending.ascii(), ending.length() );
707 inline void mm2custom( GLViewer_Viewer2d::VectorFileType aType, double& value )
709 if( aType==GLViewer_Viewer2d::POST_SCRIPT )
710 value*=2.8346; //mm to pt
712 else if( aType==GLViewer_Viewer2d::HPGL )
713 value*=40; //mm to plu (there are 40 plues in mm)
715 else if( aType==GLViewer_Viewer2d::ENH_METAFILE )
716 value*=100; //this unit is 1/100 mm
721 Translates current view content to vector file
722 \param aType - type of file
723 \param FileName - name of file,
724 \param aPType - paper size type
725 \param mmLeft, mmRight, mmTop, mmBottom - margins
727 bool GLViewer_Viewer2d::translateTo( VectorFileType aType, QString FileName, PaperType aPType,
728 double mmLeft, double mmRight, double mmTop, double mmBottom )
730 if ( !getActiveView() )
733 QFile hFile( FileName.ascii() );
739 GLViewer_ViewPort2d* aCurVP = (GLViewer_ViewPort2d*) getActiveView()->getViewPort();
742 aCurVP->getPan( xPan, yPan );
743 GLfloat aRotation = aCurVP->getGLWidget()->getRotationAngle() * 3.14159265 / 180.0;
745 GLViewer_CoordSystem aViewerCS( GLViewer_CoordSystem::Cartesian, xPan, yPan, 1.0, 1.0, aRotation );
747 double AW = Sizes[2*int(aPType)],
748 AH = Sizes[2*int(aPType)+1]; //size of Axx paper in mm
750 mm2custom( aType, mmLeft ); //we translate mm to custom units
751 mm2custom( aType, mmRight );
752 mm2custom( aType, mmTop );
753 mm2custom( aType, mmBottom );
754 mm2custom( aType, AW );
755 mm2custom( aType, AH );
757 float xScale, yScale;
758 aCurVP->getScale( xScale, yScale );
760 double VPWidth = aCurVP->getWidth()/xScale, //the width in reference units
761 VPHeight = aCurVP->getHeight()/yScale;
763 double k1 = ( AW-mmLeft-mmRight ) / VPWidth,
764 k2 = ( AH-mmTop-mmBottom ) / VPHeight;
767 k1 = k2; //We select the minimum
769 double hdelta = ( AW-mmLeft-mmRight - VPWidth * k1 )/2.0, //addition in horizontal
770 vdelta = ( AH-mmTop-mmBottom - VPHeight * k1 )/2.0; //addition in vertical
772 mmLeft += hdelta; //The real free space on the left and right borders
777 GLViewer_CoordSystem aPaperCS( GLViewer_CoordSystem::Cartesian,
778 -(mmLeft/k1+VPWidth/2.0), -(mmBottom/k1+VPHeight/2.0), 1/k1, 1/k1 );
780 if( aType==POST_SCRIPT || aType==HPGL )
782 hFile.open( IO_ReadWrite | IO_Truncate );
784 insertHeader( aType, hFile );
787 else if( aType==ENH_METAFILE )
790 r.left = 0; r.right = AW;
791 r.top = 0; r.bottom = AH;
792 HDC screen_dc = GetDC( 0 ); //The screen device context
793 HDC bitDC = CreateCompatibleDC ( screen_dc ); //The context compatible with screen
795 hMetaFileDC = CreateEnhMetaFile( bitDC, FileName.ascii(), &r, "" );
796 SetMapMode( hMetaFileDC, MM_HIMETRIC );
797 SetWindowOrgEx( hMetaFileDC, 0, r.bottom, NULL );
798 HRGN ClipRgn = CreateRectRgn( 0, 0, AW, AH );
799 SelectClipRgn( hMetaFileDC, ClipRgn );
802 aBrushData.lbColor = RGB( 255, 255, 255 );
803 aBrushData.lbStyle = BS_SOLID;
805 FillRect( hMetaFileDC, &r, CreateBrushIndirect( &aBrushData ) );
807 ReleaseDC( 0, screen_dc );
810 aCurVP->getGLWidget()->translateBackgroundToEMF( hMetaFileDC, &aViewerCS, &aPaperCS );
814 if( aType==POST_SCRIPT )
816 QString temp = "%1 %2 %3 %4 rectclip\n\n",
817 aBuffer = temp.arg( mmLeft ).arg( mmBottom ).
818 arg( AW-mmLeft-mmRight ).arg( AH-mmBottom-mmTop );
819 //It is set clipping path
821 hFile.writeBlock( aBuffer.ascii(), aBuffer.length() );
823 aCurVP->getGLWidget()->translateBackgroundToPS( hFile, &aViewerCS, &aPaperCS );
827 for( int i=0, n=myDrawers.count(); i<n; i++ )
828 if( aType==POST_SCRIPT )
829 result &= myDrawers[ i ]->translateToPS( hFile, &aViewerCS, &aPaperCS );
830 else if( aType==HPGL )
831 result &= myDrawers[ i ]->translateToHPGL( hFile, &aViewerCS, &aPaperCS );
833 else if( aType==ENH_METAFILE )
834 result &= myDrawers[ i ]->translateToEMF( hMetaFileDC, &aViewerCS, &aPaperCS );
837 if( aType==POST_SCRIPT || aType==HPGL )
839 insertEnding( aType, hFile);
843 else if( aType==ENH_METAFILE )
844 DeleteEnhMetaFile( CloseEnhMetaFile( hMetaFileDC ) );
852 \param theView - view to be repainted. If it is NULL then all views will be repainted
854 void GLViewer_Viewer2d::repaintView( GLViewer_ViewFrame* theView, bool makeCurrent )
856 GLViewer_ViewFrame* aCurView;
858 aCurView = (GLViewer_ViewFrame*)getActiveView();
865 ObjList anActiveObjs;
866 const ObjList& objs = myGLContext->getObjects();
867 for( ObjList::const_iterator it = objs.begin(); it != objs.end(); ++it )
869 GLViewer_Object* obj = (GLViewer_Object*)(*it);
870 if( obj->getVisible() )
871 anActiveObjs.append( obj );
877 QValueList<GLViewer_Drawer*>::Iterator anIt = myDrawers.begin();
878 QValueList<GLViewer_Drawer*>::Iterator endDIt = myDrawers.end();
879 for( ; anIt != endDIt; anIt++ )
882 QValueList<GLViewer_Drawer*> anActiveDrawers;
883 QValueList<GLViewer_Object*>::Iterator endOIt = anActiveObjs.end();
885 for( QValueList<GLViewer_Object*>::Iterator oit = anActiveObjs.begin(); oit != endOIt; ++oit )
887 GLViewer_Drawer* aDrawer = (*oit)->getDrawer();
890 anIt = myDrawers.begin();
892 for( ; anIt != endDIt; anIt++ )
893 if( (*anIt)->getObjectType() == (*oit)->getObjectType() )
895 (*oit)->setDrawer( *anIt );
900 if( !aDrawer ) //are not exists
902 myDrawers.append( (*oit)->createDrawer() );
903 aDrawer = (*oit)->getDrawer();
906 aDrawer->addObject( (*oit) );
907 if( anActiveDrawers.findIndex( aDrawer ) == -1 )
908 anActiveDrawers.append( aDrawer );
911 QValueList<GLViewer_Drawer*>::Iterator aDIt = anActiveDrawers.begin();
912 QValueList<GLViewer_Drawer*>::Iterator aDEndIt = anActiveDrawers.end();
914 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )aCurView->getViewPort();
915 vp->getScale( xScale, yScale );
918 vp->getGLWidget()->makeCurrent();
920 for( ; aDIt != aDEndIt; aDIt++ )
921 (*aDIt)->create( xScale, yScale, false );
924 vp->getGLWidget()->swapBuffers();
926 // ( ( GLViewer_ViewPort2d* )getActiveView()->getViewPort() )->getGLWidget()->makeCurrent();
930 Starts some operation on mouse event
932 void GLViewer_Viewer2d::startOperations( QMouseEvent* e )
934 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)getActiveView())->getViewPort();
936 float x = e->pos().x();
937 float y = e->pos().y();
939 GLViewer_Pnt point( x, y );
941 if( e->button() == Qt::LeftButton && !myGLContext->getCurrentObject() && vp->startPulling( point ) )
944 if( e->button() == Qt::LeftButton && !(vp->currentBlock() & BS_Selection) && !myGLContext->getCurrentObject() )
945 vp->startSelectByRect( e->x(), e->y() );
949 Updates started operation on mouse event
951 bool GLViewer_Viewer2d::updateOperations( QMouseEvent* e )
953 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)getActiveView())->getViewPort();
955 if( vp->isPulling() )
957 float x = e->pos().x();
958 float y = e->pos().y();
961 vp->drawPulling( GLViewer_Pnt( x, y ) );
966 if( !myGLContext->getCurrentObject() )
968 vp->drawSelectByRect( e->x(), e->y() );
975 Completes started operation on mouse event
977 void GLViewer_Viewer2d::finishOperations( QMouseEvent* e )
979 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)getActiveView())->getViewPort();
981 if( vp->isPulling() )
988 if( !myGLContext->getCurrentObject() )
990 QRect aSelRect = vp->selectionRect();
991 vp->finishSelectByRect();
992 if ( getSelector() && !aSelRect.isNull() )
994 bool append = bool ( e->state() & GLViewer_Selector::appendKey() );
995 getSelector()->select( aSelRect, append );
1001 Starts some operation on mouse wheel event
1003 void GLViewer_Viewer2d::startOperations( QWheelEvent* e )
1005 bool zoomIn = e->delta() > 0;
1006 bool update = false;
1007 for( myGLContext->InitSelected(); myGLContext->MoreSelected(); myGLContext->NextSelected() )
1009 GLViewer_Object* anObject = myGLContext->SelectedObject();
1010 update = anObject->updateZoom( zoomIn ) || update;
1013 emit wheelZoomChange( zoomIn );
1020 int GLViewer_View2dTransformer::rotateBtn = RightButton;
1025 GLViewer_View2dTransformer::GLViewer_View2dTransformer( GLViewer_Viewer* viewer, int typ )
1026 : GLViewer_ViewTransformer( viewer, typ )
1028 if ( type() == GLViewer_Viewer::Rotate )
1029 initTransform( true );
1035 GLViewer_View2dTransformer::~GLViewer_View2dTransformer()
1037 if ( type() == GLViewer_Viewer::Rotate )
1038 initTransform( false );
1042 Redefined to provide specific 3D transfomations. [ virtual public ]
1044 void GLViewer_View2dTransformer::exec()
1046 if ( !myViewer->getActiveView() )
1049 /* additional transforms */
1050 GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
1051 GLViewer_ViewPort2d* avp = (GLViewer_ViewPort2d*)vp;
1054 case GLViewer_Viewer::Rotate:
1055 myMajorBtn = rotateButton();
1056 avp->setCursor( *avp->getRotCursor() );
1059 GLViewer_ViewTransformer::exec();
1064 Handles rotation. [ protected virtual ]
1066 void GLViewer_View2dTransformer::onTransform( TransformState state )
1068 if ( !myViewer->getActiveView() )
1071 GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
1072 GLViewer_ViewPort2d* avp = (GLViewer_ViewPort2d*)vp;
1073 if ( type() == GLViewer_Viewer::Rotate )
1078 if ( myButtonState & myMajorBtn )
1079 avp->startRotation( myStart.x(), myStart.y() );
1082 if ( myButtonState & myMajorBtn )
1083 avp->rotate( myCurr.x(), myCurr.y() );
1091 GLViewer_ViewTransformer::onTransform( state );