1 // File: GLViewer_Viewer2d.cxx
2 // Created: November, 2004
4 // Copyright (C) CEA 2004
6 /***************************************************************************
7 ** Class: GLViewer_Viewer2d
8 ** Descr: OpenGL Viewer 2D
10 ** Created: UI team, 04.09.02
11 ****************************************************************************/
13 #include "GLViewer_Viewer2d.h"
15 #include "GLViewer_Object.h"
16 #include "GLViewer_Context.h"
17 #include "GLViewer_Drawer.h"
18 #include "GLViewer_Selector2d.h"
19 //#include "GLViewer_Sketcher.h"
20 #include "GLViewer_ViewPort2d.h"
22 #include "SUIT_Desktop.h"
23 #include "SUIT_ViewWindow.h"
25 #include <OSD_Timer.hxx>
26 #include <TColStd_MapOfInteger.hxx>
28 #include <qpointarray.h>
29 #include <qcolordialog.h>
30 #include <qpopupmenu.h>
32 GLViewer_Viewer2d::GLViewer_Viewer2d( const QString& title) :
33 GLViewer_Viewer( title )
35 myGLContext = new GLViewer_Context( this );
36 //myGLSketcher = new GLViewer_Sketcher( this );
37 mySelMode = GLViewer_Viewer::Multiple;
42 GLViewer_Viewer2d::~GLViewer_Viewer2d()
45 //delete myGLSketcher;
48 SUIT_ViewWindow* GLViewer_Viewer2d::createView( SUIT_Desktop* theDesktop )
50 return new GLViewer_ViewFrame( theDesktop, this );
53 void GLViewer_Viewer2d::contextMenuPopup( QPopupMenu* thePopup )
55 // "Change background color" menu item is available if there are no selected objects
56 if ( getSelector() == 0 || getSelector()->numSelected() == 0 )
58 if( thePopup->count() > 0 )
59 thePopup->insertSeparator();
60 thePopup->insertItem( tr( "CHANGE_BGCOLOR" ), this, SLOT( onChangeBgColor() ) );
64 void GLViewer_Viewer2d::onChangeBgColor()
66 if( !getActiveView() )
68 GLViewer_ViewPort2d* vp = ( ( GLViewer_ViewPort2d* )getActiveView()->getViewPort() );
70 QColor selColor = QColorDialog::getColor( vp->backgroundColor(), vp );
71 if ( selColor.isValid() ) {
72 vp->setBackgroundColor( selColor );
76 void GLViewer_Viewer2d::updateColors( QColor colorH, QColor colorS )
78 // cout << "GLViewer_Viewer2d::updateColors" << endl;
81 for ( DrawerMap::Iterator it = myDrawers.begin(); it != myDrawers.end(); ++it )
83 it.key()->setHColor( colorH );
84 it.key()->setSColor( colorS );
87 ObjectMap anObjects = myGLContext->getObjects();
88 ObjectMap::Iterator beginIt = anObjects.begin();
89 ObjectMap::Iterator endIt = anObjects.end();
90 for ( ObjectMap::Iterator it = beginIt; it != endIt; ++it )
92 //GLViewer_Drawer* aDrawer = it.key()->getDrawer();
93 //aDrawer->setHColor( colorH );
94 //aDrawer->setSColor( colorS );
98 activateAllDrawers( TRUE );
101 void GLViewer_Viewer2d::updateBorders( const QRect& rect )
103 float xa = rect.left();
104 float xb = rect.right();
105 float ya = rect.top();
106 float yb = rect.bottom();
108 QPtrVector<SUIT_ViewWindow> views = getViewManager()->getViews();
109 for ( int i = 0, n = views.count(); i < n; i++ )
111 QRect* border = ( ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)views[i])->getViewPort() )->getBorder();
113 if ( xa < border->left() ) border->setLeft( (int)xa );
114 if ( xb > border->right() ) border->setRight( (int)xb );
115 if ( ya < border->top() ) border->setTop( (int)ya );
116 if ( yb > border->bottom() ) border->setBottom( (int)yb );
120 void GLViewer_Viewer2d::updateBorders()
122 QPtrVector<SUIT_ViewWindow> views = getViewManager()->getViews();
124 ObjectMap anObjects = myGLContext->getObjects();
125 ObjectMap::Iterator beginIt = anObjects.begin();
126 ObjectMap::Iterator endIt = anObjects.end();
127 for ( int i = 0, n = views.count(); i < n; i++ )
129 QRect* border = ( ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)views[i])->getViewPort() )->getBorder();
130 border->setRect( 0, 0, 0, 0 );
131 for ( ObjectMap::Iterator it = beginIt; it != endIt; ++it )
133 QRect* aRect = it.key()->getRect()->toQRect();
134 if( !it.key()->getVisible() || aRect->isNull() )
137 if( border->isNull() )
138 border->setRect( aRect->left(), aRect->top(), aRect->width(), aRect->height() );
141 border->setLeft( QMIN( border->left(), aRect->left() ) );
142 border->setRight( QMAX( border->right(), aRect->right() ) );
143 border->setTop( QMIN( border->top(), aRect->top() ) );
144 border->setBottom( QMAX( border->bottom(), aRect->bottom() ) );
148 float gap = QMAX( border->width(), border->height() ) / 20;
149 border->setLeft( border->left() - gap );
150 border->setRight( border->right() + gap );
151 border->setTop( border->top() - gap );
152 border->setBottom( border->bottom() + gap );
157 void GLViewer_Viewer2d::updateAll()
159 if ( !getActiveView() )
162 QPtrVector<SUIT_ViewWindow> views = getViewManager()->getViews();
163 for ( int i = 0, n = views.count(); i < n; i++ )
164 ( ( GLViewer_ViewPort2d* )( ( GLViewer_ViewFrame* )views[i] )->getViewPort() )->getGLWidget()->updateGL();
167 void GLViewer_Viewer2d::updateDrawers( GLboolean update, GLfloat scX, GLfloat scY )
169 // cout << "GLViewer_Viewer2d::updateDrawers" << endl;
171 //myGLContext->updateScales( scX, scY );
172 //myGLSketcher->drawContour();
173 activateAllDrawers( update );
176 void GLViewer_Viewer2d::activateDrawers( TColStd_SequenceOfInteger& sequence, bool onlyUpdate, GLboolean swap )
178 // cout << "GLViewer_Viewer2d::activateDrawers" << endl;
180 // cout << "Only update" << endl;
182 // cout << "Not only update" << endl;
184 TColStd_MapOfInteger aMap;
185 for ( int i = 1, n = sequence.Length(); i <= n; i++)
186 if ( !aMap.Contains( sequence( i ) ) )
187 aMap.Add( sequence( i ) );
189 const ObjectMap& anObjects = myGLContext->getObjects();
190 const ObjList& objList = myGLContext->getObjList();
191 ObjList anActiveObjects;
192 for( ObjList::const_iterator it = objList.begin(); it != objList.end(); ++it )
194 if ( (*it)->getVisible() && aMap.Contains( anObjects[*it] ) )
195 anActiveObjects.append( *it );
198 activateDrawers( anActiveObjects, onlyUpdate, swap );
201 void GLViewer_Viewer2d::activateDrawers( QValueList<GLViewer_Object*>& theObjects, bool onlyUpdate, GLboolean swap )
203 //cout << "GLViewer_Viewer2d::activateDrawers " << (int)onlyUpdate << " " << (int)swap << endl;
207 QValueList<GLViewer_Drawer*>::Iterator anIt = myDrawers.begin();
208 QValueList<GLViewer_Drawer*>::Iterator endDIt = myDrawers.end();
209 for( ; anIt != endDIt; anIt++ )
212 QValueList<GLViewer_Drawer*> anActiveDrawers;
213 QValueList<GLViewer_Object*>::Iterator endOIt = theObjects.end();
215 for( QValueList<GLViewer_Object*>::Iterator oit = theObjects.begin(); oit != endOIt; ++oit )
217 GLViewer_Drawer* aDrawer = (*oit)->getDrawer();
220 anIt = myDrawers.begin();
222 for( ; anIt != endDIt; anIt++ )
223 if( (*anIt)->getObjectType() == (*oit)->getObjectType() )
225 (*oit)->setDrawer( *anIt );
230 if( !aDrawer ) //are not exists
232 myDrawers.append( (*oit)->createDrawer() );
233 aDrawer = (*oit)->getDrawer();
236 aDrawer->addObject( (*oit) );
237 if( anActiveDrawers.findIndex( aDrawer ) == -1 )
238 anActiveDrawers.append( aDrawer );
241 QValueList<GLViewer_Drawer*>::Iterator aDIt = anActiveDrawers.begin();
242 QValueList<GLViewer_Drawer*>::Iterator aDEndIt = anActiveDrawers.end();
244 QPtrVector<SUIT_ViewWindow> views = getViewManager()->getViews();
245 for ( int i = 0, n = views.count(); i < n; i++ )
247 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)views[i])->getViewPort();
248 vp->getScale( xScale, yScale );
249 vp->getGLWidget()->makeCurrent();
252 for( ; aDIt != aDEndIt; aDIt++ )
253 (*aDIt)->create( xScale, yScale, onlyUpdate );
257 QRect* border = ( ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)views[i])->getViewPort() )->getBorder();
258 float x1 = border->left();
259 float x2 = border->right();
260 float y1 = border->bottom();
261 float y2 = border->top();
263 QColor color = Qt::blue;
264 glColor3f( ( GLfloat )color.red() / 255,
265 ( GLfloat )color.green() / 255,
266 ( GLfloat )color.blue() / 255 );
269 glBegin( GL_LINE_LOOP );
270 glVertex2f( x1, y1 );
271 glVertex2f( x1, y2 );
272 glVertex2f( x2, y2 );
273 glVertex2f( x2, y1 );
276 QString coords = QString::number( border->left() ) + " " + QString::number( border->right() ) + " " +
277 QString::number( border->top() ) + " " + QString::number( border->bottom() );
278 (*aDIt)->drawText( "Border : " + coords, x1, y1+10/yScale, Qt::blue, &QFont( "Courier", 8, QFont::Normal ), 2 );
281 vp->getGLWidget()->swapBuffers();
284 ( ( GLViewer_ViewPort2d* )getActiveView()->getViewPort() )->getGLWidget()->makeCurrent();
287 void GLViewer_Viewer2d::activateDrawer( int index, bool onlyUpdate, GLboolean swap )
289 GLViewer_Object* anObj = 0;
290 const ObjectMap& anObjects = myGLContext->getObjects();
291 for ( ObjectMap::const_iterator it = anObjects.begin(); it != anObjects.end(); ++it )
292 if ( it.data() == index )
294 anObj = (GLViewer_Object*)it.key();
299 activateDrawer( anObj, onlyUpdate, swap );
302 void GLViewer_Viewer2d::activateDrawer( GLViewer_Object* theObject, bool onlyUpdate, GLboolean swap )
305 aList.append( theObject );
306 activateDrawers( aList, onlyUpdate, swap );
309 void GLViewer_Viewer2d::activateAllDrawers( bool onlyUpdate, GLboolean swap )
311 if ( !getActiveView() )
314 ObjList anActiveObjs;
315 const ObjList& objs = myGLContext->getObjList();
316 for( ObjList::const_iterator it = objs.begin(); it != objs.end(); ++it )
318 GLViewer_Object* obj = (GLViewer_Object*)(*it);
319 if( obj->getVisible() )
320 anActiveObjs.append( obj );
323 activateDrawers( anActiveObjs, onlyUpdate, swap );
326 void GLViewer_Viewer2d::onCreateGLMarkers( int theMarkersNum, int theMarkersRad )
328 if ( !getActiveView() )
331 GLViewer_MarkerSet* aMarkerSet = new GLViewer_MarkerSet( theMarkersNum, theMarkersRad );
332 getGLContext()->insertObject( aMarkerSet );
334 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
335 int vpWidth = vp->getWidth();
336 int vpHeight = vp->getHeight();
338 float* aXCoord = new float[ theMarkersNum ];
339 float* anYCoord = new float[ theMarkersNum ];
342 for ( long i = 0; i < theMarkersNum; i++ )
344 aXCoord[i] = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpWidth / 2.);
345 anYCoord[i] = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpHeight / 2.);
348 aMarkerSet->setXCoord( aXCoord, theMarkersNum );
349 aMarkerSet->setYCoord( anYCoord, theMarkersNum );
350 aMarkerSet->compute();
352 QRect* rect = aMarkerSet->getRect()->toQRect();
353 updateBorders( *rect );
355 activateAllDrawers( false );
356 activateTransform( GLViewer_Viewer::FitAll );
362 void GLViewer_Viewer2d::onCreateGLPolyline( int theAnglesNum, int theRadius, int thePolylineNumber )
364 if ( !getActiveView() )
367 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
368 int vpWidth = vp->getWidth();
369 int vpHeight = vp->getHeight();
371 float* aXCoord = new float[ theAnglesNum ];
372 float* anYCoord = new float[ theAnglesNum ];
374 //srand( ( unsigned )time( NULL ) );
376 for( int j = 0; j < thePolylineNumber; j++)
378 GLViewer_Polyline* aPolyline = new GLViewer_Polyline( theAnglesNum, theRadius );
379 getGLContext()->insertObject( aPolyline );
381 float aXOffset = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpWidth / 2.);
382 float anYOffset = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpHeight / 2.);
383 for( int i = 0; i < theAnglesNum; i++ )
385 aXCoord[i] = cos( 2. * PI * i / theAnglesNum ) * theRadius + aXOffset;
386 anYCoord[i] = sin( 2. * PI * i / theAnglesNum ) * theRadius + anYOffset;
389 aPolyline->setHighSelAll( true );
390 aPolyline->setClosed( true );
391 aPolyline->setXCoord( aXCoord, theAnglesNum );
392 aPolyline->setYCoord( anYCoord, theAnglesNum );
393 aPolyline->compute();
395 QRect* rect = aPolyline->getRect()->toQRect();
396 updateBorders( *rect );
399 activateAllDrawers( false );
400 activateTransform( GLViewer_Viewer::FitAll );
406 void GLViewer_Viewer2d::onCreateGLText( QString theStr, int theTextNumber )
408 if ( !getActiveView() )
411 if( theTextNumber <= 0 )
414 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
415 int vpWidth = vp->getWidth();
416 int vpHeight = vp->getHeight();
418 //srand( ( unsigned )time( NULL ) );
420 for( int j = 0; j < theTextNumber; j++)
422 float aXPos = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpWidth / 2.);
423 float anYPos = cos( PI * (rand() / (GLfloat)RAND_MAX) ) * ((GLfloat)vpHeight / 2.);
424 QColor aColor( 255, 0, 255 );
426 GLViewer_TextObject* aText = new GLViewer_TextObject( theStr, aXPos, anYPos, aColor );
428 getGLContext()->insertObject( aText );
430 QRect* rect = aText->getRect()->toQRect();
431 updateBorders( *rect );
434 activateAllDrawers( false );
437 void GLViewer_Viewer2d::transPoint( GLfloat& x, GLfloat& y )
439 if ( !getActiveView() )
442 GLfloat xScale, yScale;
445 GLViewer_ViewPort2d* curvp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
447 curvp->getScale( xScale, yScale );
448 curvp->getPan( xPan, yPan );
450 GLfloat a = curvp->getGLWidget()->getRotationAngle() * PI / 180.;
452 x = ( x - ( GLfloat )curvp->getWidth() / 2 ) / xScale;
453 y = ( -y + ( GLfloat )curvp->getHeight() / 2 ) / yScale;
458 x = x1 * cos(a) + y1 * sin(a);
459 y = -x1 * sin(a) + y1 * cos(a);
465 QRect* GLViewer_Viewer2d::getWinObjectRect( GLViewer_Object* theObject )
467 if ( !getActiveView() )
470 GLfloat xScale, yScale;
473 GLViewer_ViewPort2d* curvp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
474 GLfloat aWidth = curvp->getWidth();
475 GLfloat aHeight = curvp->getHeight();
478 curvp->getScale( xScale, yScale );
479 curvp->getPan( xPan, yPan );
481 QRect* aObjRect = theObject->getRect()->toQRect();
482 float aLeft = aObjRect->left() + xPan, aRight = aObjRect->right() + xPan;
483 float aTop = aObjRect->top() + yPan, aBot = aObjRect->bottom() + yPan;
485 GLfloat anAngle = curvp->getGLWidget()->getRotationAngle() * PI / 180.;
487 QPointArray aPointArray(4);
488 int ls = (int)(aLeft*sin(anAngle)), lc = (int)(aLeft*cos(anAngle)),
489 rs = (int)(aRight*sin(anAngle)), rc = (int)(aRight*cos(anAngle)),
490 ts = (int)(aTop*sin(anAngle)), tc = (int)(aTop*cos(anAngle)),
491 bs = (int)(aBot*sin(anAngle)), bc = (int)(aBot*cos(anAngle));
492 aPointArray[0] = QPoint( lc - ts, ls + tc );
493 aPointArray[1] = QPoint( rc - ts, rs + tc );
494 aPointArray[2] = QPoint( rc - bs, rs + bc );
495 aPointArray[3] = QPoint( lc - bs, ls + bc );
497 int aMinLeft = aPointArray[0].x(), aMaxRight = aPointArray[0].x(),
498 aMinTop = aPointArray[0].y(), aMaxBottom = aPointArray[0].y();
499 for( int i = 1; i < 4; i++ )
501 int x = aPointArray[i].x();
502 int y = aPointArray[i].y();
503 aMinLeft = QMIN( aMinLeft,x );
504 aMaxRight = QMAX( aMaxRight, x );
505 aMinTop = QMIN( aMinTop, y );
506 aMaxBottom = QMAX( aMaxBottom, y );
509 aLeft = (aMinLeft/* + xPan*/)*xScale + aWidth / 2;
510 aRight = (aMaxRight/* + xPan*/)*xScale + aWidth / 2;
512 aTop = -( (aMaxBottom/* + yPan*/)*yScale - aHeight / 2 );
513 aBot = -( (aMinTop/* + yPan*/)*yScale - aHeight / 2 );
515 QRect* newRect = new QRect( (int)aLeft, (int)aTop, (int)(aRight-aLeft), (int)(aBot-aTop) );
520 GLViewer_Rect GLViewer_Viewer2d::getGLVRect( const QRect& theRect ) const
522 if ( !getActiveView() )
523 return GLViewer_Rect();
525 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
528 return GLViewer_Rect();
530 return vp->win2GLV( theRect );
533 QRect GLViewer_Viewer2d::getQRect( const GLViewer_Rect& theRect ) const
535 if ( !getActiveView() )
538 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort();
543 return vp->GLV2win( theRect );
546 GLViewer_Selector* GLViewer_Viewer2d::createSelector()
548 return new GLViewer_Selector2d( this, getGLContext() );
551 GLViewer_ViewTransformer* GLViewer_Viewer2d::createTransformer( int type )
553 return new GLViewer_View2dTransformer( this, type );
556 GLViewer_Sketcher* GLViewer_Viewer2d::createGLSketcher( int type )
558 return new GLViewer_Sketcher( this, type );
561 void GLViewer_Viewer2d::activateGLSketching( int type )
563 GLViewer_ViewPort2d* vp = 0;
564 if ( !getActiveView() || !( vp = ( GLViewer_ViewPort2d* )getActiveView()->getViewPort() ) )
567 // Finish current sketching
570 if ( myGLSketcher->getType() != None )
572 myGLSketcher->setType( None );
576 // Activate new sketching
579 activateGLSketching( None ); // concurrency not supported
580 myGLSketcher->setType( type );
585 void GLViewer_Viewer2d::startSketching()
587 GLViewer_ViewPort2d* avp = (GLViewer_ViewPort2d*)getActiveView()->getViewPort();
588 avp->setCursor( *avp->getSketchCursor() );
589 //avp->enablePopup( false );
590 myGLSketcher->startSketching();
593 void GLViewer_Viewer2d::finishSketching()
595 GLViewer_ViewPort2d* avp = (GLViewer_ViewPort2d*)getActiveView()->getViewPort();
596 avp->setCursor( *avp->getDefaultCursor() );
597 //avp->enablePopup( true );
598 myGLSketcher->finishSketching();
601 bool GLViewer_Viewer2d::isSketchingActive()
603 return myGLSketcher->getType() != None;
606 int GLViewer_Viewer2d::getSketchingType()
608 return myGLSketcher->getType();
611 void GLViewer_Viewer2d::onSketchDelObject()
613 GLViewer_ViewPort2d* avp = (GLViewer_ViewPort2d*)getActiveView()->getViewPort();
614 avp->setCursor( *avp->getDefaultCursor() );
615 myGLSketcher->finishSketching( true );
618 void GLViewer_Viewer2d::onSketchUndoLast()
623 void GLViewer_Viewer2d::onSketchFinish()
628 void GLViewer_Viewer2d::onMouseEvent( SUIT_ViewWindow*, QMouseEvent* e )
630 if ( !getActiveView() )
633 //if ( testRotation( e ) )
638 case QEvent::MouseButtonPress :
639 case QEvent::MouseMove :
640 case QEvent::MouseButtonRelease :
641 //if( myGLSketcher->getType() != None )
642 // myGLSketcher->sketch( e );
646 GLViewer_Viewer::onMouseEvent( 0, e );
649 bool GLViewer_Viewer2d::testRotation( QMouseEvent* e )
651 if ( ( e->button() == GLViewer_View2dTransformer::rotateButton() ) &&
652 ( e->type() == QEvent::MouseButtonPress ) &&
653 ( e->state() & GLViewer_ViewTransformer::accelKey() ) )
655 activateTransform( GLViewer_Viewer::Rotate );
662 void GLViewer_Viewer2d::insertHeader( VectorFileType aType, QFile& hFile )
664 if( aType == POST_SCRIPT )
666 QString header = "%!PS-Adobe-3.0\n";
667 header += "%%Creator: OpenCascade 2004\n";
668 header += "%%Title: Our document\n";
669 header += "%%PageOrder: Ascend\n";
670 header += "%%Orientation: Portrait\n";
671 header += "%%LanguageLevel: 2\n";
673 header += "%%Pages: 1\n";
674 header += "%%Page: 1\n\n";
676 hFile.writeBlock( header.ascii(), header.length() );
678 else if( aType == HPGL )
680 QString header = "[Esc].(;\n";
681 header += "[Esc].I81;;17:\n";
682 header += "[Esc].N;19:\n";
690 hFile.writeBlock( header.ascii(), header.length() );
694 void GLViewer_Viewer2d::insertEnding( VectorFileType aType, QFile& hFile )
696 if( aType == POST_SCRIPT )
698 QString ending = "showpage\n\n%%EOF";
699 hFile.writeBlock( ending.ascii(), ending.length() );
701 else if( aType == HPGL )
703 QString ending = "PU;PA0,0;SP;EC;PG1;EC1;OE\n";
704 hFile.writeBlock( ending.ascii(), ending.length() );
708 inline void mm2custom( GLViewer_Viewer2d::VectorFileType aType, double& value )
710 if( aType==GLViewer_Viewer2d::POST_SCRIPT )
711 value*=2.8346; //mm to pt
713 else if( aType==GLViewer_Viewer2d::HPGL )
714 value*=40; //mm to plu (there are 40 plues in mm)
716 else if( aType==GLViewer_Viewer2d::ENH_METAFILE )
717 value*=100; //this unit is 1/100 mm
721 bool GLViewer_Viewer2d::translateTo( VectorFileType aType, QString FileName, PaperType aPType,
722 double mmLeft, double mmRight, double mmTop, double mmBottom )
724 if ( !getActiveView() )
727 QFile hFile( FileName.ascii() );
733 GLViewer_ViewPort2d* aCurVP = (GLViewer_ViewPort2d*) getActiveView()->getViewPort();
736 aCurVP->getPan( xPan, yPan );
737 GLfloat aRotation = aCurVP->getGLWidget()->getRotationAngle() * 3.14159265 / 180.0;
739 GLViewer_CoordSystem aViewerCS( GLViewer_CoordSystem::Cartesian, xPan, yPan, 1.0, 1.0, aRotation );
741 double AW = Sizes[2*int(aPType)],
742 AH = Sizes[2*int(aPType)+1]; //size of Axx paper in mm
744 mm2custom( aType, mmLeft ); //we translate mm to custom units
745 mm2custom( aType, mmRight );
746 mm2custom( aType, mmTop );
747 mm2custom( aType, mmBottom );
748 mm2custom( aType, AW );
749 mm2custom( aType, AH );
751 float xScale, yScale;
752 aCurVP->getScale( xScale, yScale );
754 double VPWidth = aCurVP->getWidth()/xScale, //the width in reference units
755 VPHeight = aCurVP->getHeight()/yScale;
757 double k1 = ( AW-mmLeft-mmRight ) / VPWidth,
758 k2 = ( AH-mmTop-mmBottom ) / VPHeight;
761 k1 = k2; //We select the minimum
763 double hdelta = ( AW-mmLeft-mmRight - VPWidth * k1 )/2.0, //addition in horizontal
764 vdelta = ( AH-mmTop-mmBottom - VPHeight * k1 )/2.0; //addition in vertical
766 mmLeft += hdelta; //The real free space on the left and right borders
771 GLViewer_CoordSystem aPaperCS( GLViewer_CoordSystem::Cartesian,
772 -(mmLeft/k1+VPWidth/2.0), -(mmBottom/k1+VPHeight/2.0), 1/k1, 1/k1 );
774 if( aType==POST_SCRIPT || aType==HPGL )
776 hFile.open( IO_ReadWrite | IO_Truncate );
778 insertHeader( aType, hFile );
781 else if( aType==ENH_METAFILE )
784 r.left = 0; r.right = AW;
785 r.top = 0; r.bottom = AH;
786 HDC screen_dc = GetDC( 0 ); //The screen device context
787 HDC bitDC = CreateCompatibleDC ( screen_dc ); //The context compatible with screen
789 hMetaFileDC = CreateEnhMetaFile( bitDC, FileName.ascii(), &r, "" );
790 SetMapMode( hMetaFileDC, MM_HIMETRIC );
791 SetWindowOrgEx( hMetaFileDC, 0, r.bottom, NULL );
792 HRGN ClipRgn = CreateRectRgn( 0, 0, AW, AH );
793 SelectClipRgn( hMetaFileDC, ClipRgn );
796 aBrushData.lbColor = RGB( 255, 255, 255 );
797 aBrushData.lbStyle = BS_SOLID;
799 FillRect( hMetaFileDC, &r, CreateBrushIndirect( &aBrushData ) );
801 ReleaseDC( 0, screen_dc );
804 aCurVP->getGLWidget()->translateBackgroundToEMF( hMetaFileDC, &aViewerCS, &aPaperCS );
808 if( aType==POST_SCRIPT )
810 QString temp = "%1 %2 %3 %4 rectclip\n\n",
811 aBuffer = temp.arg( mmLeft ).arg( mmBottom ).
812 arg( AW-mmLeft-mmRight ).arg( AH-mmBottom-mmTop );
813 //It is set clipping path
815 hFile.writeBlock( aBuffer.ascii(), aBuffer.length() );
817 aCurVP->getGLWidget()->translateBackgroundToPS( hFile, &aViewerCS, &aPaperCS );
821 for( int i=0, n=myDrawers.count(); i<n; i++ )
822 if( aType==POST_SCRIPT )
823 result &= myDrawers[ i ]->translateToPS( hFile, &aViewerCS, &aPaperCS );
824 else if( aType==HPGL )
825 result &= myDrawers[ i ]->translateToHPGL( hFile, &aViewerCS, &aPaperCS );
827 else if( aType==ENH_METAFILE )
828 result &= myDrawers[ i ]->translateToEMF( hMetaFileDC, &aViewerCS, &aPaperCS );
831 if( aType==POST_SCRIPT || aType==HPGL )
833 insertEnding( aType, hFile);
837 else if( aType==ENH_METAFILE )
838 DeleteEnhMetaFile( CloseEnhMetaFile( hMetaFileDC ) );
845 void GLViewer_Viewer2d::repaintView( GLViewer_ViewFrame* theView, bool makeCurrent )
847 GLViewer_ViewFrame* aCurView;
849 aCurView = (GLViewer_ViewFrame*)getActiveView();
856 ObjList anActiveObjs;
857 const ObjList& objs = myGLContext->getObjList();
858 for( ObjList::const_iterator it = objs.begin(); it != objs.end(); ++it )
860 GLViewer_Object* obj = (GLViewer_Object*)(*it);
861 if( obj->getVisible() )
862 anActiveObjs.append( obj );
868 QValueList<GLViewer_Drawer*>::Iterator anIt = myDrawers.begin();
869 QValueList<GLViewer_Drawer*>::Iterator endDIt = myDrawers.end();
870 for( ; anIt != endDIt; anIt++ )
873 QValueList<GLViewer_Drawer*> anActiveDrawers;
874 QValueList<GLViewer_Object*>::Iterator endOIt = anActiveObjs.end();
876 for( QValueList<GLViewer_Object*>::Iterator oit = anActiveObjs.begin(); oit != endOIt; ++oit )
878 GLViewer_Drawer* aDrawer = (*oit)->getDrawer();
881 anIt = myDrawers.begin();
883 for( ; anIt != endDIt; anIt++ )
884 if( (*anIt)->getObjectType() == (*oit)->getObjectType() )
886 (*oit)->setDrawer( *anIt );
891 if( !aDrawer ) //are not exists
893 myDrawers.append( (*oit)->createDrawer() );
894 aDrawer = (*oit)->getDrawer();
897 aDrawer->addObject( (*oit) );
898 if( anActiveDrawers.findIndex( aDrawer ) == -1 )
899 anActiveDrawers.append( aDrawer );
902 QValueList<GLViewer_Drawer*>::Iterator aDIt = anActiveDrawers.begin();
903 QValueList<GLViewer_Drawer*>::Iterator aDEndIt = anActiveDrawers.end();
905 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )aCurView->getViewPort();
906 vp->getScale( xScale, yScale );
909 vp->getGLWidget()->makeCurrent();
911 for( ; aDIt != aDEndIt; aDIt++ )
912 (*aDIt)->create( xScale, yScale, false );
915 vp->getGLWidget()->swapBuffers();
917 // ( ( GLViewer_ViewPort2d* )getActiveView()->getViewPort() )->getGLWidget()->makeCurrent();
920 void GLViewer_Viewer2d::startOperations( QMouseEvent* e )
922 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)getActiveView())->getViewPort();
924 float x = e->pos().x();
925 float y = e->pos().y();
927 GLViewer_Pnt point( x, y );
929 if( e->button() == Qt::LeftButton && !myGLContext->getCurrentObject() && vp->startPulling( point ) )
932 if( e->button() == Qt::LeftButton && !(vp->currentBlock() & BS_Selection) && !myGLContext->getCurrentObject() )
933 vp->startSelectByRect( e->x(), e->y() );
936 bool GLViewer_Viewer2d::updateOperations( QMouseEvent* e )
938 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)getActiveView())->getViewPort();
940 if( vp->isPulling() )
942 float x = e->pos().x();
943 float y = e->pos().y();
946 vp->drawPulling( GLViewer_Pnt( x, y ) );
951 if( !myGLContext->getCurrentObject() )
953 vp->drawSelectByRect( e->x(), e->y() );
959 void GLViewer_Viewer2d::finishOperations( QMouseEvent* e )
961 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )((GLViewer_ViewFrame*)getActiveView())->getViewPort();
963 if( vp->isPulling() )
970 if( !myGLContext->getCurrentObject() )
972 QRect aSelRect = vp->selectionRect();
973 vp->finishSelectByRect();
974 if ( getSelector() && !aSelRect.isNull() )
976 bool append = bool ( e->state() & GLViewer_Selector::appendKey() );
977 getSelector()->select( aSelRect, append );
982 void GLViewer_Viewer2d::startOperations( QWheelEvent* e )
984 bool zoomIn = e->delta() > 0;
986 for( myGLContext->InitSelected(); myGLContext->MoreSelected(); myGLContext->NextSelected() )
988 GLViewer_Object* anObject = myGLContext->SelectedObject();
989 update = anObject->updateZoom( zoomIn ) || update;
997 /****************************************************************
998 ** Class: GLViewer_View2dTransformer
1000 *****************************************************************/
1002 int GLViewer_View2dTransformer::rotateBtn = RightButton;
1004 GLViewer_View2dTransformer::GLViewer_View2dTransformer( GLViewer_Viewer* viewer, int typ )
1005 : GLViewer_ViewTransformer( viewer, typ )
1007 if ( type() == GLViewer_Viewer::Rotate )
1008 initTransform( true );
1011 GLViewer_View2dTransformer::~GLViewer_View2dTransformer()
1013 if ( type() == GLViewer_Viewer::Rotate )
1014 initTransform( false );
1018 Redefined to provide specific 3D transfomations. [ virtual public ]
1020 void GLViewer_View2dTransformer::exec()
1022 if ( !myViewer->getActiveView() )
1025 /* additional transforms */
1026 GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
1027 //QAD_ASSERT( vp->inherits( "QAD_ViewPort3d" ) );
1028 GLViewer_ViewPort2d* avp = (GLViewer_ViewPort2d*)vp;
1031 case GLViewer_Viewer::Rotate:
1032 myMajorBtn = rotateButton();
1033 avp->setCursor( *avp->getRotCursor() );
1036 GLViewer_ViewTransformer::exec();
1041 Handles rotation. [ protected virtual ]
1043 void GLViewer_View2dTransformer::onTransform( TransformState state )
1045 if ( !myViewer->getActiveView() )
1048 GLViewer_ViewPort* vp = myViewer->getActiveView()->getViewPort();
1049 //QAD_ASSERT( vp->inherits( "QAD_ViewPort3d" ) );
1050 GLViewer_ViewPort2d* avp = (GLViewer_ViewPort2d*)vp;
1051 if ( type() == GLViewer_Viewer::Rotate )
1056 if ( myButtonState & myMajorBtn )
1057 avp->startRotation( myStart.x(), myStart.y() );
1060 if ( myButtonState & myMajorBtn )
1061 avp->rotate( myCurr.x(), myCurr.y() );
1069 GLViewer_ViewTransformer::onTransform( state );