Back to index

salome-gui  6.5.0
SUIT_ViewManager.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 #include "SUIT_ViewManager.h"
00024 
00025 #include "SUIT_Desktop.h"
00026 #include "SUIT_ViewModel.h"
00027 #include "SUIT_ViewWindow.h"
00028 #include "SUIT_Study.h"
00029 #include "SUIT_Session.h"
00030 
00031 #include <QMap>
00032 #include <QRegExp>
00033 #include <QIcon>
00034 
00035 #ifdef WIN32
00036 #include <windows.h>
00037 #endif
00038 
00039 QMap<QString, int> SUIT_ViewManager::_ViewMgrId;
00040 
00046 SUIT_ViewManager::SUIT_ViewManager( SUIT_Study* theStudy,
00047                                     SUIT_Desktop* theDesktop,
00048                                     SUIT_ViewModel* theViewModel )
00049 : QObject( 0 ),
00050   myDesktop( theDesktop ),
00051   myTitle( "Default: %M - viewer %V" ),
00052   myStudy( NULL )
00053 {
00054   myViewModel = 0;
00055   myActiveView = 0;
00056   setViewModel( theViewModel );
00057 
00058   myId = useNewId( getType() );
00059 
00060   connect( theDesktop, SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
00061            this,       SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
00062 
00063   myStudy = theStudy;
00064   if ( myStudy )
00065     connect( myStudy, SIGNAL( destroyed() ), this, SLOT( onDeleteStudy() ) );
00066 }
00067 
00069 SUIT_ViewManager::~SUIT_ViewManager()
00070 {
00071   if ( myViewModel )
00072   {
00073     myViewModel->setViewManager( 0 );
00074     delete myViewModel;
00075   }
00076 }
00077 
00078 int SUIT_ViewManager::useNewId( const QString& type )
00079 {
00080   if ( !_ViewMgrId.contains( type ) )
00081     _ViewMgrId.insert( type, 0 );
00082 
00083   int id = _ViewMgrId[type];
00084   _ViewMgrId[type]++;
00085   return id;
00086 }
00087 
00088 
00089 int SUIT_ViewManager::getGlobalId() const {
00090   int id = -1;
00091   SUIT_Application* app = SUIT_Session::session()->activeApplication();
00092   if(app) {
00093     id = app->viewManagerId(this);
00094   }
00095   return id;
00096 }
00097 
00098 void SUIT_ViewManager::setTitle( const QString& theTitle )
00099 {
00100   if ( myTitle == theTitle )
00101     return;
00102 
00103   myTitle = theTitle;
00104   for ( int i = 0; i < myViews.count(); i++ )
00105     setViewName( myViews[i] );
00106 }
00107 
00108 void SUIT_ViewManager::setIcon( const QPixmap& theIcon )
00109 {
00110   myIcon = theIcon;
00111   for ( int i = 0; i < myViews.count(); i++ )
00112     myViews[i]->setWindowIcon( QIcon( myIcon ) );
00113 }
00114 
00116 void SUIT_ViewManager::setViewModel(SUIT_ViewModel* theViewModel)
00117 {
00118   if (myViewModel && myViewModel != theViewModel) {
00119     myViewModel->setViewManager(0);
00120     delete myViewModel;
00121   }
00122   myViewModel = theViewModel;
00123   if (myViewModel)
00124     myViewModel->setViewManager(this);
00125 }
00126 
00128 void SUIT_ViewManager::setViewName( SUIT_ViewWindow* theView )
00129 {
00130   QString title = prepareTitle( getTitle(), myId + 1, myViews.indexOf( theView ) + 1 );
00131   theView->setWindowTitle( title );
00132 }
00133 
00134 QString SUIT_ViewManager::prepareTitle( const QString& title, const int mId, const int vId )
00135 {
00136   QString res = title;
00137   QRegExp re( "%[%MV]" );
00138   int i = 0;
00139   while ( ( i = re.indexIn( res, i ) ) != -1 )
00140   {
00141     QString rplc;
00142     QString str = res.mid( i, re.matchedLength() );
00143     if ( str == QString( "%%" ) )
00144       rplc = QString( "%" );
00145     else if ( str == QString( "%M" ) )
00146       rplc = QString::number( mId );
00147     else if ( str == QString( "%V" ) )
00148       rplc = QString::number( vId );
00149     res.replace( i, re.matchedLength(), rplc );
00150     i += rplc.length();
00151   }
00152   return res;
00153 }
00154 
00156 SUIT_ViewWindow* SUIT_ViewManager::createViewWindow()
00157 {
00158   SUIT_ViewWindow* aView = myViewModel->createView(myDesktop);
00159 
00160   if ( !insertView( aView ) ){
00161     delete aView;
00162     return 0;
00163   }
00164 
00165   setViewName( aView );
00166   aView->setWindowIcon( QIcon( myIcon ) );
00167 
00168   //myDesktop->addViewWindow( aView );
00169   //it is done automatically during creation of view
00170 
00171   aView->setViewManager( this );
00172   emit viewCreated( aView );
00173 
00174   // Special treatment for the case when <aView> is the first one in this view manager
00175   // -> call onWindowActivated() directly, because somebody may always want
00176   // to use getActiveView()
00177   if ( !myActiveView )
00178     onWindowActivated( aView );
00179 
00180   return aView;
00181 }
00182 
00184 int SUIT_ViewManager::getId() const
00185 {
00186   return myId;
00187 }
00188 
00190 void SUIT_ViewManager::createView()
00191 {
00192   createViewWindow();
00193 }
00194 
00195 QVector<SUIT_ViewWindow*> SUIT_ViewManager::getViews() const
00196 {
00197   QVector<SUIT_ViewWindow*> res;
00198   for ( int i = 0; i < myViews.count(); i++ )
00199   {
00200     if ( myViews[i] )
00201       res.append( myViews[i] );
00202   }
00203 
00204   return res;
00205 }
00206 
00210 bool SUIT_ViewManager::insertView(SUIT_ViewWindow* theView)
00211 {
00212   unsigned int aSize = myViews.size();
00213   unsigned int aNbItems = myViews.count() + 1;
00214   if ( aNbItems > aSize )
00215   {
00216     myViews.resize( aNbItems );
00217     aSize = myViews.size();
00218   }
00219 
00220   connect(theView, SIGNAL(closing(SUIT_ViewWindow*)),
00221           this,    SLOT(onClosingView(SUIT_ViewWindow*)));
00222 
00223   connect(theView, SIGNAL(tryClosing(SUIT_ViewWindow*)),
00224           this,    SIGNAL(tryCloseView(SUIT_ViewWindow*)));
00225 
00226   connect(theView, SIGNAL(mousePressed(SUIT_ViewWindow*, QMouseEvent*)),
00227           this,    SLOT(onMousePressed(SUIT_ViewWindow*, QMouseEvent*)));
00228 
00229   connect(theView, SIGNAL(mouseReleased(SUIT_ViewWindow*, QMouseEvent*)),
00230           this,    SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
00231 
00232   connect(theView, SIGNAL(mouseDoubleClicked(SUIT_ViewWindow*, QMouseEvent*)),
00233           this,    SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
00234 
00235   connect(theView, SIGNAL(mouseMoving(SUIT_ViewWindow*, QMouseEvent*)),
00236           this,    SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)));
00237 
00238   connect(theView, SIGNAL(wheeling(SUIT_ViewWindow*, QWheelEvent*)),
00239           this,    SIGNAL(wheel(SUIT_ViewWindow*, QWheelEvent*)));
00240 
00241   connect(theView, SIGNAL(keyPressed(SUIT_ViewWindow*, QKeyEvent*)),
00242           this,    SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)));
00243 
00244   connect(theView, SIGNAL(keyReleased(SUIT_ViewWindow*, QKeyEvent*)),
00245           this,    SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)));
00246 
00247   connect(theView, SIGNAL(contextMenuRequested( QContextMenuEvent * )),
00248           this,    SLOT  (onContextMenuRequested( QContextMenuEvent * )));
00249 
00250   for ( uint i = 0; i < aSize; i++ )
00251   {
00252     if ( myViews[i] == 0 )
00253     {
00254       myViews[i] = theView;
00255       return true;
00256     }
00257   }
00258   return false;
00259 }
00260 
00263 void SUIT_ViewManager::onClosingView( SUIT_ViewWindow* theView )
00264 {
00265   closeView( theView );
00266 }
00267 
00271 void SUIT_ViewManager::closeView( SUIT_ViewWindow* theView )
00272 {
00273   if ( !theView )
00274     return;
00275 
00276   QPointer<SUIT_ViewWindow> view( theView );
00277 
00278   view->setClosable( false );
00279   view->hide();
00280 
00281   if ( !view->testAttribute( Qt::WA_DeleteOnClose ) )
00282     return;
00283 
00284   emit deleteView( view );
00285   removeView( view );
00286 
00287   if ( view )
00288     delete view;
00289 }
00290 
00294 void SUIT_ViewManager::removeView( SUIT_ViewWindow* theView )
00295 {
00296   theView->disconnect( this );
00297   myViews.remove( myViews.indexOf( theView ) );
00298   if ( myActiveView == theView )
00299     myActiveView = 0;
00300   if ( !myViews.count() )
00301     emit lastViewClosed( this );
00302 }
00303 
00307 void SUIT_ViewManager::setDestructiveClose( const bool on )
00308 {
00309   for ( int i = 0; i < myViews.count(); i++ )
00310     myViews[i]->setDestructiveClose( on );
00311 }
00312 
00316 bool SUIT_ViewManager::isVisible() const
00317 {
00318   bool res = false;
00319   for ( int i = 0; i < myViews.count() && !res; i++ )
00320     res = myViews[i]->isVisibleTo( myViews[i]->parentWidget() );
00321   return res;
00322 }
00323 
00327 void SUIT_ViewManager::setShown( const bool on )
00328 {
00329   for ( int i = 0; i < myViews.count(); i++ )
00330     myViews.at( i )->setShown( on );
00331 }
00332 
00334 void SUIT_ViewManager::onMousePressed(SUIT_ViewWindow* theView, QMouseEvent* theEvent)
00335 {
00336   emit mousePress(theView, theEvent);
00337 }
00338 
00341 void SUIT_ViewManager::onWindowActivated(SUIT_ViewWindow* view)
00342 {
00343   if (view) {
00344     unsigned int aSize = myViews.size();
00345     for (uint i = 0; i < aSize; i++) {
00346       if (myViews[i] && myViews[i] == view) {
00347         myActiveView = view;
00348         emit activated( this );
00349         return;
00350       }
00351     }
00352   }
00353 }
00354 
00357 void SUIT_ViewManager::closeAllViews()
00358 {
00359   for ( int i = 0; i < myViews.size(); i++ )
00360     delete myViews[i];
00361   myViews.clear();
00362 }
00363 
00367 QString SUIT_ViewManager::getType() const
00368 {
00369   return (!myViewModel)? "": myViewModel->getType();
00370 }
00371 
00375 SUIT_Study* SUIT_ViewManager::study() const
00376 {
00377     return myStudy;
00378 }
00379 
00383 void SUIT_ViewManager::onDeleteStudy()
00384 {
00385     myStudy = NULL;
00386 }
00387 
00389 void SUIT_ViewManager::onContextMenuRequested( QContextMenuEvent* e )
00390 {
00391   contextMenuRequest( e );
00392 }
00393 
00395 void SUIT_ViewManager::contextMenuPopup( QMenu* popup )
00396 {
00397   SUIT_ViewModel* vm = getViewModel();
00398   if ( vm )
00399     vm->contextMenuPopup( popup );
00400 }