Back to index

salome-gui  6.5.0
GLViewer_Viewer.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  Author : OPEN CASCADE
00024 // File:      GLViewer_Viewer.cxx
00025 // Created:   November, 2004
00026 //#include <GLViewerAfx.h>
00027 //
00028 #include "GLViewer_Viewer.h"
00029 #include "GLViewer_Selector.h"
00030 #include "GLViewer_ViewPort.h"
00031 #include "GLViewer_ViewFrame.h"
00032 
00033 //#include "SUIT_Desktop.h"
00034 #include "SUIT_ViewWindow.h"
00035 #include "SUIT_ViewManager.h"
00036 
00037 #include <QApplication>
00038 #include <QMenu>
00039 #include <QColorDialog>
00040 #include <QMouseEvent>
00041 #include <QWheelEvent>
00042 #include <QRect>
00043 #include <QRubberBand>
00044 
00045 /* used for sketching */
00046 static QEvent* l_mbPressEvent = 0;
00047 
00051 GLViewer_Viewer::GLViewer_Viewer( const QString& title )
00052 : SUIT_ViewModel(),
00053 mySelector( 0 ),
00054 mySketcher( 0 ),
00055 myTransformer( 0 ),
00056 mySelMode( NoSelection )
00057 {
00058 }
00059 
00063 GLViewer_Viewer::~GLViewer_Viewer()
00064 {
00065     delete mySelector;
00066 }
00067 
00072 void GLViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
00073 {
00074   SUIT_ViewModel::setViewManager(theViewManager);
00075   if (theViewManager) 
00076   {
00077     connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
00078             this, SLOT(onMouseEvent(SUIT_ViewWindow*, QMouseEvent*)));
00079 
00080     connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
00081             this, SLOT(onMouseEvent(SUIT_ViewWindow*, QMouseEvent*)));
00082 
00083     connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
00084             this, SLOT(onMouseEvent(SUIT_ViewWindow*, QMouseEvent*)));
00085 
00086     connect(theViewManager, SIGNAL(wheel(SUIT_ViewWindow*, QWheelEvent*)), 
00087             this, SLOT(onWheelEvent(SUIT_ViewWindow*, QWheelEvent*)));
00088   }
00089 }
00090 
00094 void GLViewer_Viewer::contextMenuPopup( QMenu* thePopup )
00095 {
00096   if( thePopup->actions().count() > 0 )
00097       thePopup->addSeparator();
00098 
00099   thePopup->addAction( tr( "CHANGE_BGCOLOR" ), this, SLOT( onChangeBgColor() ) );
00100 }
00101 
00105 void GLViewer_Viewer::onChangeBgColor()
00106 {
00107   GLViewer_ViewPort* vp = getActiveView()->getViewPort();
00108   QColor selColor = QColorDialog::getColor( vp->backgroundColor() );
00109 
00110   if( selColor.isValid() )
00111     vp->setBackgroundColor( selColor );
00112 }
00113 
00117 GLViewer_ViewFrame* GLViewer_Viewer::getActiveView() const
00118 {
00119   SUIT_ViewManager* aMgr = getViewManager();
00120   return (GLViewer_ViewFrame*)( aMgr != 0 ? aMgr->getActiveView() : 0 );
00121 }
00122 
00123 
00127 void GLViewer_Viewer::setSelectionMode( GLViewer_Viewer::SelectionMode mode )
00128 {
00129     if ( mySelMode != mode )
00130     {
00131         mySelMode = mode;
00132         onSelectionModeChanged();
00133     }
00134 }
00135 
00139 GLViewer_Selector* GLViewer_Viewer::getSelector() const
00140 {
00141     if ( !mySelector )
00142     {
00143         GLViewer_Viewer* mthis = (GLViewer_Viewer*)this;
00144         mthis->mySelector = mthis->createSelector();
00145         if ( mySelector )
00146         {
00147             connect( mySelector, SIGNAL( selSelectionDone( bool, SelectionChangeStatus ) ), SLOT( onSelectionDone( bool, SelectionChangeStatus ) ) );
00148             connect( mySelector, SIGNAL( selSelectionCancel() ), SLOT( onSelectionCancel() ) );
00149         }
00150     }
00151     return mySelector;
00152 }
00153 
00157 GLViewer_Viewer::SelectionMode GLViewer_Viewer::getSelectionMode() const
00158 {
00159     return mySelMode;
00160 }
00161 
00165 void GLViewer_Viewer::activateSketching( int type )
00166 {
00167     GLViewer_ViewPort* vp = 0;
00168     if ( !getActiveView() || !( vp = getActiveView()->getViewPort() ) )
00169         return;
00170 
00171     if ( !vp->isSketchingEnabled() )
00172         return;
00173 
00174     /* Finish current sketching */
00175     if ( type == NoSketching )
00176     {
00177         if ( mySketcher )
00178         {
00179             onSketchingFinished();
00180             delete mySketcher;
00181             mySketcher = 0;
00182         }
00183     }
00184     /* Activate new sketching */
00185     else
00186     {
00187         activateSketching( NoSketching );  /* concurrency not suported */
00188         mySketcher = createSketcher( type );
00189         onSketchingStarted();
00190     }
00191 }
00192 
00196 void GLViewer_Viewer::activateTransform( int type )
00197 {
00198     GLViewer_ViewPort* vp = 0;
00199     if ( !getActiveView() || !( vp = getActiveView()->getViewPort() ) )
00200         return;
00201 
00202     if ( !vp->isTransformEnabled() )
00203         return;
00204 
00205     /* Finish current transform */
00206     if ( type == NoTransform )
00207     {
00208         if ( myTransformer )
00209         {
00210             onTransformationFinished();
00211             delete myTransformer;
00212             myTransformer = 0;
00213         }
00214     }
00215     /* Activate new transform */
00216     else
00217     {
00218         activateTransform( NoTransform );  /* concurrency not suported */
00219         myTransformer = createTransformer( type );
00220         onTransformationStarted();
00221         myTransformer->exec();
00222     }
00223 }
00224 
00228 GLViewer_ViewTransformer* GLViewer_Viewer::createTransformer( int type )
00229 {
00230     return new GLViewer_ViewTransformer( this, type );
00231 }
00232 
00236 GLViewer_ViewSketcher* GLViewer_Viewer::createSketcher( int type )
00237 {
00238     return new GLViewer_ViewSketcher( this, type );
00239 }
00240 
00244 GLViewer_Selector* GLViewer_Viewer::createSelector()
00245 {
00246     return 0;
00247 }
00248 
00253 void GLViewer_Viewer::onTransformationStarted()
00254 {
00255     unhilightDetected();
00256     if ( getSelector() )
00257         getSelector()->lock( true );        /* disable selection */
00258 
00259     /*  Watch events: any mouse/key event outside the
00260         viewport will be considered as the end of
00261         transform */
00262     if( !myTransformer )
00263         return;
00264     qApp->installEventFilter( this );
00265 }
00266 
00270 void GLViewer_Viewer::onTransformationFinished()
00271 {
00272     if ( getSelector() )
00273         getSelector()->lock( false );           /* enable selection */
00274 
00275     /* Stop watch events */
00276     if( !myTransformer )
00277         return;
00278     qApp->removeEventFilter( this );
00279 }
00280 
00284 void GLViewer_Viewer::onSketchingStarted()
00285 {
00286     unhilightDetected();
00287 }
00288 
00292 void GLViewer_Viewer::onSketchingFinished()
00293 {
00294     if ( !getSelector() )
00295         return;
00296 
00297     if( !mySketcher )
00298         return;
00299     if ( mySketcher->type() == Rect )
00300     {
00301         QRect* selRect = (QRect*)mySketcher->data();
00302         if ( selRect )
00303         {
00304             bool append = bool( mySketcher->buttonState() & GLViewer_Selector::appendKey() );
00305             getSelector()->select( *selRect, append );
00306         }
00307     }
00308 }
00309 
00313 bool GLViewer_Viewer::eventFilter( QObject* o, QEvent* e )
00314 {
00315     if( !getActiveView() )
00316         return false;
00317 
00318     if( getActiveView()->getViewPort() == o->parent() )
00319       o = o->parent();
00320 
00321     bool mouseClickedOutside = ( e->type() == QEvent::MouseButtonPress &&
00322                                  o != getActiveView()->getViewPort() );
00323     bool anyKeyPressed = ( e->type() == QEvent::KeyPress );
00324     if ( mouseClickedOutside || anyKeyPressed )
00325     {   /* terminate all */
00326         activateTransform( NoTransform );
00327         activateSketching( NoSketching );
00328         //cout << "mouseClickedOutside || anyKeyPressed" << endl;
00329     }
00330     return QObject::eventFilter( o, e );
00331 }
00332 
00336 void GLViewer_Viewer::onSelectionDone( bool bAdded, SelectionChangeStatus status  )
00337 {
00338     emit selectionChanged( status );
00339 }
00340 
00344 void GLViewer_Viewer::onSelectionCancel()
00345 {
00346     emit selectionChanged( SCS_Invalid );
00347 }
00348 
00352 void GLViewer_Viewer::onKeyEvent( SUIT_ViewWindow*, QKeyEvent* )
00353 {
00354 }
00355 
00359 void GLViewer_Viewer::onMouseEvent( SUIT_ViewWindow*, QMouseEvent* e )
00360 {
00361     //cout << "GLViewer_Viewer::onMouseEvent" << endl;
00362     switch( e->type() )
00363     {
00364         case QEvent::MouseButtonPress:
00365             handleMousePress( e );
00366             break;
00367         case QEvent::MouseMove:
00368             handleMouseMove( e );
00369             break;
00370         case QEvent::MouseButtonRelease:
00371             handleMouseRelease( e );
00372             break;
00373         default: break;
00374     }
00375 }
00376 
00380 void GLViewer_Viewer::onWheelEvent( SUIT_ViewWindow*, QWheelEvent* e )
00381 {
00382     //cout << "GLViewer_Viewer::onMouseEvent" << endl;
00383     switch( e->type() )
00384     {
00385         case QEvent::Wheel:
00386             handleWheel( e );
00387             break;
00388         default: break;
00389     }
00390 }
00391 
00395 void GLViewer_Viewer::onSelectionModeChanged()
00396 {
00397     bool enable = ( mySelMode == Multiple );    
00398     QVector<SUIT_ViewWindow*> views = getViewManager()->getViews();
00399     for ( int i = 0, n = views.count(); i < n; i++ )
00400     {
00401         GLViewer_ViewPort* vp = ((GLViewer_ViewFrame*)views[i])->getViewPort();
00402         if ( vp )
00403             vp->setSketchingEnabled( enable );
00404     }
00405 }
00406 
00410 void GLViewer_Viewer::update( int flags )
00411 {
00412     QVector<SUIT_ViewWindow*> views = getViewManager()->getViews();
00413     for ( int i = 0, n = views.count(); i < n; i++ )
00414         ((GLViewer_ViewFrame*)views[i])->onUpdate( flags );
00415 }
00416 
00420 void GLViewer_Viewer::unhilightDetected()
00421 {
00422     if ( getSelector() )
00423         getSelector()->undetectAll();
00424 }
00425 
00430 void GLViewer_Viewer::handleMousePress( QMouseEvent* e )
00431 {
00432     /* test accel for transforms */
00433     if ( e->modifiers() & GLViewer_ViewTransformer::accelKey() )
00434     {
00435         Qt::MouseButton bs = e->button();
00436         if ( bs == GLViewer_ViewTransformer::zoomButton() )
00437             activateTransform( Zoom );
00438         else if ( bs == GLViewer_ViewTransformer::panButton() )
00439             activateTransform( Pan );
00440     }
00441     else
00442     {
00443         //checking for other operations before selection in release event
00444         startOperations( e );
00445     }
00446 
00447     /* we may need it for sketching... */
00448     l_mbPressEvent = new QMouseEvent( *e );
00449 
00450     //checking for other operations before selection in release event
00451 //    startOperations( e );
00452 
00453     /*e->button() == LeftButton && getSelector() )
00454     {
00455         bool append = bool ( e->state() & GLViewer_Selector::appendKey() );
00456         getSelector()->select( append );
00457     }*/
00458 }
00459 
00464 void GLViewer_Viewer::handleMouseMove( QMouseEvent* e )
00465 {
00466     /* Highlight for selection */
00467     bool dragged = ( e->buttons() & ( Qt::LeftButton | Qt::MidButton | Qt::RightButton ) );
00468     if ( !dragged )
00469     {
00470         if ( getSelector() )
00471             getSelector()->detect( e->x(), e->y() );
00472     }
00473     /* Try to activate default sketching
00474     */
00475     else if ( e->button() == GLViewer_ViewSketcher::sketchButton() )
00476     {
00477         activateSketching( Rect );
00478         if ( mySketcher )
00479         {
00480             /*  Activated OK. We should not forget initial mousepress
00481                 event and this mousemove event to start sketching from
00482                 the point of initial click */
00483             if ( l_mbPressEvent )
00484             {
00485                 QApplication::sendEvent( getActiveView()->getViewPort(), l_mbPressEvent );
00486                 delete l_mbPressEvent;
00487                 l_mbPressEvent = 0;
00488             }
00489             QApplication::sendEvent( getActiveView()->getViewPort(), e );
00490         }
00491     }
00492     
00493     //Try to activate other operations
00494     updateOperations( e );
00495 }
00496 
00500 void GLViewer_Viewer::handleMouseRelease( QMouseEvent* e )
00501 {
00502     /* selection */
00503     /* tmp - in handleMousePress*/  
00504     if( e->button() == Qt::LeftButton && !(getActiveView()->getViewPort()->currentBlock() & BS_Selection) )
00505     {
00506         if ( getSelector() )
00507         {
00508             bool append = bool ( e->modifiers() & GLViewer_Selector::appendKey() );
00509             getSelector()->select( append );
00510         }
00511     }
00512 
00513     //Try to done active operations
00514     finishOperations( e );        
00515 
00516     if ( l_mbPressEvent )
00517     {
00518         delete l_mbPressEvent;
00519         l_mbPressEvent = 0;
00520     }
00521 }
00522 
00526 void GLViewer_Viewer::handleWheel( QWheelEvent* e )
00527 {
00528     startOperations( e );
00529 }
00530 
00531 int GLViewer_ViewTransformer::panBtn = Qt::MidButton;
00532 int GLViewer_ViewTransformer::zoomBtn = Qt::LeftButton;
00533 int GLViewer_ViewTransformer::fitRectBtn = Qt::LeftButton;
00534 int GLViewer_ViewTransformer::panGlobalBtn = Qt::LeftButton;
00535 int GLViewer_ViewTransformer::acccelKey = Qt::ControlModifier;
00536 
00540 GLViewer_ViewTransformer::GLViewer_ViewTransformer( GLViewer_Viewer* v, int type )
00541 : QObject( 0 ),
00542 myViewer( v ),
00543 myType( type ),
00544 myMajorBtn( Qt::NoButton ),
00545 myButtonState( 0 ),
00546 myRectBand( 0 )
00547 {
00548     if ( myType == GLViewer_Viewer::Pan ||
00549          myType == GLViewer_Viewer::Zoom ||
00550          myType == GLViewer_Viewer::PanGlobal ||
00551          myType == GLViewer_Viewer::FitRect )
00552     {
00553         /* 'long' transforms need this */
00554         initTransform( true );
00555     }
00556 }
00557 
00561 GLViewer_ViewTransformer::~GLViewer_ViewTransformer()
00562 {
00563     if ( myType == GLViewer_Viewer::Pan ||
00564          myType == GLViewer_Viewer::Zoom ||
00565          myType == GLViewer_Viewer::PanGlobal ||
00566          myType == GLViewer_Viewer::FitRect )
00567     {
00568         /* 'long' transforms need this */
00569         initTransform( false );
00570     }
00571 
00572     //QAD_Application::getDesktop()->clearInfo();
00573 
00574     endDrawRect();
00575 }
00576 
00583 void GLViewer_ViewTransformer::initTransform( bool init )
00584 {
00585     GLViewer_ViewPort* avp = myViewer->getActiveView()->getViewPort();
00586     if ( init )
00587     {
00588         mySavedCursor = avp->cursor();
00589         mySavedMouseTrack = avp->hasMouseTracking();
00590         avp->setMouseTracking( false );
00591         avp->installEventFilter( this );
00592     }
00593     else
00594     {
00595         avp->removeEventFilter( this );
00596         avp->setMouseTracking( mySavedMouseTrack );
00597         avp->setCursor( mySavedCursor );
00598     }
00599 }
00600 
00605 void GLViewer_ViewTransformer::exec()
00606 {
00607     GLViewer_ViewPort* avp = myViewer->getActiveView()->getViewPort();
00608     if( !avp )
00609         return;
00610 
00611     switch( myType )
00612     {
00613         case GLViewer_Viewer::Zoom:
00614             myMajorBtn = zoomButton();
00615             avp->setCursor( *avp->getZoomCursor() );
00616             break;
00617         case GLViewer_Viewer::Pan:
00618             myMajorBtn = panButton();
00619             avp->setCursor( *avp->getPanCursor() );
00620             break;
00621         case GLViewer_Viewer::PanGlobal:
00622             myMajorBtn = panGlobalButton();
00623             avp->setCursor( *avp->getPanglCursor() );
00624             avp->fitAll( true, false );  /* view is ready now */
00625             break;
00626         case GLViewer_Viewer::FitRect:
00627             myMajorBtn = fitRectButton();
00628             avp->setCursor( *avp->getHandCursor() );
00629             break;
00630         case GLViewer_Viewer::Reset:
00631             avp->reset(); onTransform( Fin );
00632             break;
00633         case GLViewer_Viewer::FitAll:
00634             avp->fitAll(); onTransform( Fin );
00635             break;
00636         case GLViewer_Viewer::FitSelect:
00637             avp->fitSelect(); onTransform( Fin );
00638             break;
00639         default: break;
00640     }
00641 }
00642 
00646 bool GLViewer_ViewTransformer::eventFilter( QObject* o, QEvent* e )
00647 {
00648     switch ( e->type() )
00649     {
00650         case QEvent::MouseMove:
00651         case QEvent::MouseButtonPress:
00652         case QEvent::MouseButtonRelease:
00653         {
00654             TransformState state = EnTrain;
00655             QMouseEvent* me = ( QMouseEvent* )e;
00656 
00657             myButtonState = me->modifiers();
00658             if ( e->type() == QEvent::MouseButtonPress )
00659                 myButtonState |= me->button();  /* add pressed button */
00660 
00661             int mouseOnlyState = ( myButtonState & ( Qt::LeftButton | Qt::MidButton | Qt::RightButton ) );
00662             if ( myStart.isNull() )
00663             {
00664                 state = Debut;
00665                 myStart = me->pos();
00666                 myMajorBtn = mouseOnlyState;
00667             }
00668 
00669             if ( e->type() == QEvent::MouseButtonRelease && mouseOnlyState == myMajorBtn )
00670             {
00671                 state = Fin;
00672             }
00673             myCurr = me->pos();
00674             onTransform( state );
00675             return true;
00676         }
00677         default: break;
00678     }
00679     return QObject::eventFilter( o, e );
00680 }
00681 
00686 void GLViewer_ViewTransformer::onTransform( TransformState state )
00687 {
00688     GLViewer_ViewPort* avp = myViewer->getActiveView()->getViewPort();
00689     bool doTrsf = ( myButtonState & myMajorBtn );
00690     switch ( myType )
00691     {
00692         case GLViewer_Viewer::Zoom:
00693             if ( state != Fin && doTrsf )
00694             {
00695                 avp->zoom( myStart.x(), myStart.y(), myCurr.x(), myCurr.y() );
00696                 myStart = myCurr;
00697             }
00698             break;
00699         case GLViewer_Viewer::Pan:
00700             if ( state != Fin && doTrsf )
00701             {
00702                 avp->pan( myCurr.x() - myStart.x(), myStart.y() - myCurr.y() );
00703                 myStart = myCurr;
00704             }
00705             break;
00706         case GLViewer_Viewer::PanGlobal:
00707         {
00708             if ( state == Fin )
00709             avp->setCenter( myCurr.x(), myCurr.y() );
00710             break;
00711         }
00712         case GLViewer_Viewer::FitRect:
00713         {
00714             if ( doTrsf )
00715             {
00716                 QRect rect( qMin( myStart.x(), myCurr.x() ), qMin( myStart.y(), myCurr.y() ),
00717                             qAbs( myStart.x() - myCurr.x() ), qAbs( myStart.y() - myCurr.y() ) );
00718                 if ( !rect.isEmpty() )
00719                 {
00720                     switch ( state )
00721                     {
00722                         case Fin:
00723                             avp->fitRect( rect );
00724                             break;
00725                         default:
00726                         {
00727                             drawRect( rect );
00728                             break;
00729                         }
00730                     }
00731                 }
00732             }
00733             break;
00734         }
00735         default:
00736             break;
00737     }
00738 
00739     if ( state == Fin )
00740         myViewer->activateTransform( GLViewer_Viewer::NoTransform );
00741 }
00742 
00746 void GLViewer_ViewTransformer::drawRect(const QRect& theRect)
00747 {
00748   if ( !myRectBand ) {
00749     myRectBand = new QRubberBand( QRubberBand::Rectangle, myViewer->getActiveView()->getViewPort() );
00750     QPalette palette;
00751     palette.setColor(myRectBand->foregroundRole(), Qt::white);
00752     myRectBand->setPalette(palette);
00753   }
00754   myRectBand->hide();
00755 
00756   myRectBand->setGeometry( theRect );
00757   myRectBand->setVisible( theRect.isValid() );
00758 }
00759 
00763 void GLViewer_ViewTransformer::endDrawRect()
00764 {
00765   if ( myRectBand ) myRectBand->hide();
00766 
00767   delete myRectBand;
00768   myRectBand = 0;
00769 }
00770 
00774 int GLViewer_ViewTransformer::type() const
00775 {
00776     return myType;
00777 }
00778 
00779 
00780 int GLViewer_ViewSketcher::sketchBtn = Qt::LeftButton;
00781 
00785 GLViewer_ViewSketcher::GLViewer_ViewSketcher( GLViewer_Viewer* viewer, int type )
00786 : QObject( 0 ),
00787 myViewer( viewer ),
00788 myData( 0 ),
00789 myType( type ),
00790 myRectBand( 0 )
00791 {
00792     if( !myViewer )
00793         return;
00794     GLViewer_ViewPort* avp = myViewer->getActiveView()->getViewPort();
00795     if( !avp )
00796         return;
00797 
00798     mySavedCursor = avp->cursor();
00799     avp->setCursor( *GLViewer_ViewPort::getHandCursor() );
00800     avp->installEventFilter( this );
00801 
00802     if ( myType == GLViewer_Viewer::Rect )
00803         myData = new QRect();
00804 }
00805 
00809 GLViewer_ViewSketcher::~GLViewer_ViewSketcher()
00810 {
00811     GLViewer_ViewPort* avp = myViewer->getActiveView()->getViewPort();
00812     avp->removeEventFilter( this );
00813     avp->setCursor( mySavedCursor );
00814 
00815     if ( myType == GLViewer_Viewer::Rect )
00816         delete ( QRect* ) myData;
00817 
00818     endDrawRect();
00819 }
00820 
00824 bool GLViewer_ViewSketcher::eventFilter( QObject* o, QEvent* e )
00825 {
00826     switch ( e->type() )
00827     {
00828         case QEvent::MouseMove:
00829         case QEvent::MouseButtonPress:
00830         case QEvent::MouseButtonRelease:
00831         {
00832             SketchState state = EnTrain;
00833             QMouseEvent* me = (QMouseEvent*)e;
00834 
00835             myButtonState = me->modifiers();
00836             if ( e->type() == QEvent::MouseButtonPress )
00837                 myButtonState |= me->button();  /* add pressed button */
00838 
00839             if ( myStart.isNull() )
00840             {
00841                 state = Debut;
00842                 myStart = me->pos();
00843             }
00844 
00845             int mouseOnlyState = ( myButtonState & ( Qt::LeftButton | Qt::MidButton | Qt::RightButton ) );
00846             if ( e->type() == QEvent::MouseButtonRelease && mouseOnlyState == sketchButton() )
00847             {
00848                 state = Fin;
00849             }
00850             myCurr = me->pos();
00851             onSketch( state );
00852             return true;
00853         }
00854         default: break;
00855     }
00856     return QObject::eventFilter( o, e );
00857 }
00858 
00862 void GLViewer_ViewSketcher::onSketch( SketchState state )
00863 {
00864     GLViewer_ViewPort* avp = myViewer->getActiveView()->getViewPort();
00865     if( !avp )
00866         return;
00867 
00868     if( myType == GLViewer_Viewer::Rect )
00869     {
00870         QRect* sketchRect = ( QRect* )data();
00871         if ( myButtonState & sketchButton() )
00872         {
00873             QRect rect( qMin( myStart.x(), myCurr.x() ), qMin( myStart.y(), myCurr.y() ),
00874                         qAbs( myStart.x() - myCurr.x() ), qAbs( myStart.y() - myCurr.y() ) );
00875             if ( !rect.isEmpty() )
00876             {
00877                 if ( !sketchRect->isEmpty() && myRectBand )
00878                     myRectBand->hide();    /* erase */
00879                 *sketchRect = rect;
00880                 if ( state != Fin )
00881                     drawRect( *sketchRect );
00882             }
00883         }
00884     }
00885 
00886     if ( state == Fin )
00887     {
00888         QApplication::syncX();  /* force rectangle redrawing */
00889         myViewer->activateSketching( GLViewer_Viewer::NoSketching );
00890     }
00891 }
00892 
00896 void GLViewer_ViewSketcher::drawRect(const QRect& theRect)
00897 {
00898   if ( !myRectBand ) {
00899     myRectBand = new QRubberBand( QRubberBand::Rectangle, myViewer->getActiveView()->getViewPort() );
00900     QPalette palette;
00901     palette.setColor(myRectBand->foregroundRole(), Qt::white);
00902     myRectBand->setPalette(palette);
00903   }
00904   myRectBand->hide();
00905 
00906   myRectBand->setGeometry( theRect );
00907   myRectBand->setVisible( theRect.isValid() );
00908 }
00909 
00913 void GLViewer_ViewSketcher::endDrawRect()
00914 {
00915   if ( myRectBand ) myRectBand->hide();
00916 
00917   delete myRectBand;
00918   myRectBand = 0;
00919 }