Back to index

salome-gui  6.5.0
SalomeApp_VisualState.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 "SalomeApp_VisualState.h"
00024 
00025 #include "SalomeApp_Module.h"
00026 #include "SalomeApp_Study.h"
00027 #include "SalomeApp_Application.h"
00028 
00029 //#include <SUIT_ResourceMgr.h>
00030 #include <SUIT_ViewManager.h>
00031 #include <SUIT_ViewWindow.h>
00032 #include <QtxWorkstack.h>
00033 
00034 #include <STD_TabDesktop.h>
00035 
00036 #include <QList>
00037 #include <QApplication>
00038 #include <QMultiHash>
00039 
00040 #include <SALOMEDSClient_ClientFactory.hxx>//?
00041 #include <SALOMEDSClient_IParameters.hxx>//?
00042 
00043 #include <vector>//?
00044 #include <string>//?
00045 #include <cstdio>
00046 
00050 SalomeApp_VisualState::SalomeApp_VisualState( SalomeApp_Application* app )
00051   : QObject(),
00052     myApp( app )
00053 {
00054 }
00055 
00059 SalomeApp_VisualState::~SalomeApp_VisualState()
00060 {
00061 }
00062 
00073 void nameViewWindows( const ViewManagerList& lst )
00074 {
00075   QMap<QString, int> viewersCounter;
00076   for ( QList<SUIT_ViewManager*>::const_iterator it = lst.begin(); it != lst.end(); ++it )
00077   {
00078     SUIT_ViewManager* aVM = *it;
00079     if ( !aVM )
00080       continue;
00081 
00082     int view_count = aVM->getViewsCount();
00083     QString vType = aVM->getType();
00084     if ( !view_count )
00085       continue; //No views is opened in the viewer
00086 
00087     if ( !viewersCounter.contains( vType ) )
00088       viewersCounter.insert( vType, 0 );
00089 
00090     int& viewerID = viewersCounter[vType];
00091 
00092     QVector<SUIT_ViewWindow*> views = aVM->getViews();
00093     for ( int i = 0; i < view_count; i++ )
00094     {
00095       QString vName = QString( "%1_%2_%3" ).arg( vType ).arg( viewerID ).arg( i );
00096       views[i]->setObjectName( vName );
00097     }
00098     viewerID++;
00099   }
00100 }
00101 
00105 int SalomeApp_VisualState::storeState()
00106 {
00107   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
00108   if ( !study )
00109     return -1;
00110 
00111   // unlock study if it is locked
00112   bool aLocked = study->studyDS()->GetProperties()->IsLocked();
00113   if (aLocked) study->studyDS()->GetProperties()->SetLocked(false);
00114 
00115   int savePoint = 1;
00116   std::vector<int> savePoints = study->getSavePoints();
00117   //Calculate a new savePoint number = the last save point number + 1
00118   if ( savePoints.size() > 0)
00119     savePoint = savePoints[savePoints.size()-1] + 1;
00120 
00121   _PTR(AttributeParameter) ap = study->studyDS()->GetCommonParameters( study->getVisualComponentName().toLatin1().constData(), 
00122                                                                savePoint );
00123   _PTR(IParameters) ip = ClientFactory::getIParameters( ap );
00124 
00125   ViewManagerList lst;
00126   myApp->viewManagers( lst );
00127 
00128   // setting unique names for view windows in order to save this view inside
00129   // workstack's structure (see below).  On restore the views with the same names will
00130   // be placed to the same place inside the workstack's splitters.
00131   nameViewWindows( lst );
00132 
00133   // store active window's name
00134   SUIT_ViewWindow* win = myApp->desktop()->activeWindow();
00135   if ( win )
00136     ip->setProperty("AP_ACTIVE_VIEW", win->objectName().toStdString() );
00137 
00138   int viewerID = 0;
00139   SUIT_ViewManager* vm = 0;
00140   QListIterator<SUIT_ViewManager*> it( lst );
00141   while ( it.hasNext() ) {
00142     vm = it.next();
00143     if ( !vm ) continue;
00144 
00145     int view_count = vm->getViewsCount();
00146     if ( !view_count )
00147       continue; //No views is opened in the viewer
00148 
00149     std::string viewerEntry = QString( "%1_%2" ).arg( vm->getType() ).arg( ++viewerID ).toStdString();
00150     ip->append("AP_VIEWERS_LIST", viewerEntry);
00151 
00152     QVector<SUIT_ViewWindow*> views = vm->getViews();
00153     for(int i = 0; i<view_count; i++) {
00154       ip->append( viewerEntry, views[i]->windowTitle().toStdString() );
00155       ip->append( viewerEntry, views[i]->getVisualParameters().toStdString() );
00156     }
00157   }
00158 
00159   //Save information about split areas
00160   if ( myApp->desktop()->inherits( "STD_TabDesktop" ) )
00161   {
00162     QString workstackInfo;
00163     QtxWorkstack* workstack = ((STD_TabDesktop*)myApp->desktop())->workstack();
00164     QByteArray geomState = workstack->saveState( 0 );
00165     ip->setProperty( "AP_WORKSTACK_INFO", QString( geomState.toHex() ).toStdString() );
00166   }
00167 
00168   //Save a name of the active module
00169   if ( CAM_Module* activeModule = myApp->activeModule() )
00170     ip->setProperty( "AP_ACTIVE_MODULE", activeModule->moduleName().toStdString() );
00171 
00172   //Store visual parameters of the modules
00173   QList<CAM_Module*> mlist;
00174   myApp->modules( mlist );
00175   QListIterator<CAM_Module*> itM( mlist );
00176   CAM_Module* module = 0;
00177   while ( itM.hasNext() ) {
00178     module = itM.next();
00179     if ( !module ) continue;
00180 
00181     if ( SalomeApp_Module* sModule = dynamic_cast<SalomeApp_Module*>( module ) ) {
00182       ip->append( "AP_MODULES_LIST", sModule->moduleName().toStdString() );
00183       sModule->storeVisualParameters( savePoint );
00184     }
00185   }
00186 
00187   // set default name of new savePoint
00188   study->setNameOfSavePoint( savePoint, QObject::tr( "SAVE_POINT_DEF_NAME" ) + QString::number( savePoint ) );
00189 
00190   if (aLocked) study->studyDS()->GetProperties()->SetLocked(true);
00191 
00192   return savePoint;
00193 }
00194 
00198 void SalomeApp_VisualState::restoreState(int savePoint)
00199 {
00200   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
00201   if ( !study )
00202     return;
00203 
00204   _PTR(AttributeParameter) ap = study->studyDS()->GetCommonParameters( study->getVisualComponentName().toLatin1().constData(),
00205                                                                savePoint );
00206   _PTR(IParameters) ip = ClientFactory::getIParameters(ap);
00207 
00208   qApp->installEventFilter( this );
00209 
00210   //Remove all already existent veiwers and their views
00211   //myApp->clearViewManagers();
00212   myApp->clearKnownViewManagers();
00213 
00214   //Restore the viewers and view windows
00215   int nbViewers = ip->nbValues( "AP_VIEWERS_LIST" );
00216   SUIT_ViewWindow* viewWin = 0;
00217 
00218   // parameters of view windows are stored in a map for restoring after restoring of the workstack
00219   QMap<SUIT_ViewWindow*, QString> viewersParameters;
00220 
00221   for ( int i = 0; i < nbViewers; i++ )
00222   {
00223     std::string viewerEntry = ip->getValue( "AP_VIEWERS_LIST", i );
00224     std::vector<std::string> veiewerParams = ip->parseValue(viewerEntry,'_');
00225     std::string type = veiewerParams[0];
00226     std::string viewerID = veiewerParams[1];
00227     SUIT_ViewManager* vm = myApp->newViewManager( type.c_str() );
00228     if ( !vm )
00229       continue; //Unknown viewer
00230 
00231     int nbViews = (ip->nbValues(viewerEntry))/2;
00232 
00233     //Create nbViews-1 view (-1 because 1 view is created by createViewManager)
00234     for ( int i = 1; i< nbViews; i++ )
00235     {
00236       SUIT_ViewWindow* aView = vm->createViewWindow();
00237       aView->show();
00238     }
00239 
00240     int viewCount = vm->getViewsCount();
00241     if ( viewCount != nbViews )
00242     {
00243       printf( "\nRestore visual state: Unknow error, Can't create a view!\n" );
00244       continue;
00245     }
00246 
00247     //Resize the views, set their captions and apply visual parameters.
00248     QVector<SUIT_ViewWindow*> views = vm->getViews();
00249     for ( int i = 0, j = 0; i<viewCount; i++, j+=2 )
00250     {
00251       viewWin = views[i];
00252       if ( !viewWin )
00253         continue;
00254 
00255       // wait untill the window is really shown.  This step fixes MANY bugs..
00256       //      while ( !vm->isVisible() )
00257       //        qApp->processEvents();
00258 
00259       viewWin->setWindowTitle( ip->getValue( viewerEntry, j ).c_str() );
00260 
00261       //      printf ( "VP for viewWin \"%s\": %s\n", viewerEntry.c_str(), ip->getValue(viewerEntry, j+1).c_str() );
00262       viewersParameters[ viewWin ] = ip->getValue( viewerEntry, j + 1 ).c_str();
00263       //viewWin->setVisualParameters(ip->getValue(viewerEntry, j+1).c_str());
00264     }
00265   }
00266 
00267   // restore modules' visual parameters
00268   std::vector<std::string> v = ip->getValues( "AP_MODULES_LIST" );
00269   for ( int i = 0; i < v.size(); i++ )
00270   {
00271     myApp->activateModule( v[i].c_str() );
00272     if ( SalomeApp_Module* module = dynamic_cast<SalomeApp_Module*>( myApp->activeModule() ) )
00273       module->restoreVisualParameters( savePoint );
00274   }
00275 
00276   // new view windows may have been created in  module->restoreVisualParameters() [GAUSS]
00277   // so here we store their visual parameters for later restoring..
00278   QList<SUIT_ViewManager*> lst;
00279   myApp->viewManagers( lst );
00280   for ( QList<SUIT_ViewManager*>::iterator itVM = lst.begin(); itVM != lst.end(); ++itVM )
00281   {
00282     SUIT_ViewManager* aVM = *itVM;
00283     if ( !aVM )
00284       continue;
00285 
00286     int view_count = aVM->getViewsCount();
00287     QVector<SUIT_ViewWindow*> views = aVM->getViews();
00288     for ( int i = 0; i < view_count; i++ )
00289     {
00290       if ( !viewersParameters.contains( views[i] ) )
00291       {
00292         viewersParameters[ views[i] ] = views[i]->getVisualParameters();
00293         //      printf ( "store VP for viewWin \"%s\": %s\n", views[i]->name(), views[i]->getVisualParameters().toLatin1().constData() );
00294       }
00295     }
00296   }
00297 
00298   // activate module that was active on save
00299   QString activeModuleName( ip->getProperty("AP_ACTIVE_MODULE" ).c_str() );
00300   if ( !activeModuleName.isEmpty() )
00301     myApp->activateModule( activeModuleName );
00302 
00303   // setting unique names for view windows in order to restore positions of view windows inside
00304   // workstack's structure (see below).  During save the same naming algorithm was used,
00305   // so the same views will get the same names.
00306   nameViewWindows( lst );
00307 
00308   qApp->processEvents();
00309 
00310   // restore workstack parameters.  should be done after module's restoreVisualParameters(), because
00311   // some modules can create their own viewers (like VISU creates GaussViewers)
00312   if ( myApp->desktop()->inherits( "STD_TabDesktop" ) )
00313   {
00314     QtxWorkstack* workstack = ((STD_TabDesktop*)myApp->desktop())->workstack();
00315     workstack->restoreState( QByteArray::fromHex( QByteArray( ip->getProperty( "AP_WORKSTACK_INFO" ).c_str() ) ), 0 );
00316   }
00317 
00318   // restore visual parameters of view windows.  it must be done AFTER restoring workstack.
00319   // also set active view
00320   std::string activeViewName = ip->getProperty("AP_ACTIVE_VIEW");
00321   QMap<SUIT_ViewWindow*, QString>::Iterator mapIt;
00322   for ( mapIt = viewersParameters.begin(); mapIt != viewersParameters.end(); ++mapIt ) {
00323     mapIt.key()->setVisualParameters( mapIt.value() );
00324     if ( activeViewName == mapIt.key()->objectName().toStdString() )
00325       mapIt.key()->setFocus();
00326   }
00327 
00328   qApp->removeEventFilter( this );
00329 
00330   //  for ( it.toFirst(); it.current(); ++it ) {
00331   //    int view_count = it.current()->getViewsCount();
00332   //    QPtrVector<SUIT_ViewWindow> views = it.current()->getViews();
00333   //    for ( int i = 0; i < view_count; i++ )
00334   //      views[i]->setVisualParameters( viewersParameters[ views[i]->name() ] );
00335   //  }
00336 
00337   // set focus to previously saved active view window
00338   //  std::string activeViewName = ip->getProperty("AP_ACTIVE_VIEW");
00339   //  for ( it.toFirst(); it.current(); ++it ) {
00340   //    int view_count = it.current()->getViewsCount();
00341   //    QPtrVector<SUIT_ViewWindow> views = it.current()->getViews();
00342   //    for ( int i = 0; i < view_count; i++ )  {
00343   //      if ( activeViewName == views[i]->name() )
00344   //    views[i]->setFocus();
00345   //    }
00346   //  }
00347 }
00348 
00352 bool SalomeApp_VisualState::eventFilter( QObject* o, QEvent* e )
00353 {
00354   // eat keyboard and mouse events
00355   QEvent::Type aType = e->type();
00356   if ( aType == QEvent::MouseButtonDblClick ||
00357        aType == QEvent::MouseButtonPress ||
00358        aType == QEvent::MouseButtonRelease ||
00359        aType == QEvent::MouseMove ||
00360        aType == QEvent::KeyPress ||
00361        aType == QEvent::KeyRelease )
00362     return true;
00363 
00364   return QObject::eventFilter( o, e );
00365 }