1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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 #include "SalomeApp_VisualState.h"
21 #include "SalomeApp_Module.h"
22 #include "SalomeApp_Study.h"
23 #include "SalomeApp_Application.h"
25 #include <SUIT_ResourceMgr.h>
26 #include <QtxWorkstack.h>
28 #include <STD_TabDesktop.h>
31 #include <qapplication.h>
34 #include <SALOMEDSClient_ClientFactory.hxx>
35 #include <SALOMEDSClient_IParameters.hxx>
43 SalomeApp_VisualState::SalomeApp_VisualState( SalomeApp_Application* app )
51 SalomeApp_VisualState::~SalomeApp_VisualState()
56 Sets names of all view windows in given list. This is used
57 in order to apply the same naming algorithm when saving and restoring
58 view windows. Names of view windows must be the same before saving
59 workstack (splitters) information, and before its restoring!
60 Naming rule: ViewerType_IndexOfViewerOfThisType_IndexOfViewInThisViewer
62 OCCViewer_0_0 OCCViewer_0_1 OCCViewer_0_2
65 void nameViewWindows( const ViewManagerList& lst )
67 QDict<int> viewersCounter; // map viewerType - to - index_of_this_viewer_type
68 viewersCounter.setAutoDelete( true );
69 for ( QPtrListIterator<SUIT_ViewManager> it(lst); it.current(); ++it) {
70 int view_count = it.current()->getViewsCount();
71 QString vType = it.current()->getType();
73 continue; //No views is opened in the viewer
75 int* viewerID = viewersCounter[ vType ];
77 viewerID = new int( 0 );
78 viewersCounter.insert( vType, viewerID );
83 QPtrVector<SUIT_ViewWindow> views = it.current()->getViews();
84 for ( int i = 0; i < view_count; i++ ) {
85 QString vName = QString( "%1_%2_%3" ).arg( vType ).arg( *viewerID ).arg( i );
86 views[i]->setName( vName );
92 Stores the visual parameters of the viewers
94 int SalomeApp_VisualState::storeState()
96 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
101 std::vector<int> savePoints = study->getSavePoints();
102 //Calculate a new savePoint number = the last save point number + 1
103 if ( savePoints.size() > 0)
104 savePoint = savePoints[savePoints.size()-1] + 1;
106 _PTR(AttributeParameter) ap = study->studyDS()->GetCommonParameters( study->getVisualComponentName(), savePoint );
107 _PTR(IParameters) ip = ClientFactory::getIParameters( ap );
110 myApp->viewManagers( lst );
112 // setting unique names for view windows in order to save this view inside
113 // workstack's structure (see below). On restore the views with the same names will
114 // be placed to the same place inside the workstack's splitters.
115 nameViewWindows( lst );
117 // store active window's name
118 SUIT_ViewWindow* win = myApp->desktop()->activeWindow();
120 ip->setProperty("AP_ACTIVE_VIEW", win->name() );
123 SUIT_ViewManager* vm = 0;
124 for (QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it ) {
126 int view_count = vm->getViewsCount();
128 continue; //No views is opened in the viewer
130 std::string viewerEntry = QString( "%1_%2" ).arg( vm->getType() ).arg( ++viewerID ).latin1();
131 ip->append("AP_VIEWERS_LIST", viewerEntry);
133 QPtrVector<SUIT_ViewWindow> views = vm->getViews();
134 for(int i = 0; i<view_count; i++) {
135 ip->append( viewerEntry, views[i]->caption().latin1() );
136 ip->append( viewerEntry, views[i]->getVisualParameters().latin1() );
140 //Save information about split areas
141 if ( myApp->desktop()->inherits( "STD_TabDesktop" ) ) {
142 QtxWorkstack* workstack = ((STD_TabDesktop*)myApp->desktop())->workstack();
143 QString workstackInfo;
144 (*workstack) >> workstackInfo;
145 ip->setProperty( "AP_WORKSTACK_INFO", workstackInfo.latin1() );
148 //Save a name of the active module
149 if ( CAM_Module* activeModule = myApp->activeModule() )
150 ip->setProperty( "AP_ACTIVE_MODULE", activeModule->moduleName().latin1() );
152 //Store visual parameters of the modules
153 QPtrList<CAM_Module> mlist;
154 myApp->modules( mlist );
155 CAM_Module* module = 0;
156 for ( module = mlist.first(); module; module = mlist.next() ) {
157 if ( SalomeApp_Module* sModule = dynamic_cast<SalomeApp_Module*>( module ) ) {
158 ip->append( "AP_MODULES_LIST", sModule->moduleName().latin1() );
159 sModule->storeVisualParameters( savePoint );
163 // set default name of new savePoint
164 study->setNameOfSavePoint( savePoint, QObject::tr( "SAVE_POINT_DEF_NAME" ) + QString::number( savePoint ) );
170 Restores the visual parameters of the viewers
172 void SalomeApp_VisualState::restoreState(int savePoint)
174 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
178 _PTR(AttributeParameter) ap = study->studyDS()->GetCommonParameters( study->getVisualComponentName(), savePoint );
179 _PTR(IParameters) ip = ClientFactory::getIParameters(ap);
181 //Remove all already existent veiwers and their views
183 myApp->viewManagers( lst );
184 for ( QPtrListIterator<SUIT_ViewManager> it(lst); it.current(); ++it ) {
185 myApp->removeViewManager( it.current() );
186 qApp->processEvents();
188 //Restore the viewers and view windows
189 int nbViewers = ip->nbValues( "AP_VIEWERS_LIST" );
190 SUIT_ViewWindow* viewWin = 0;
192 // parameters of view windows are stored in a map for restoring after restoring of the workstack
193 QMap<SUIT_ViewWindow*, QString> viewersParameters;
195 for ( int i = 0; i < nbViewers; i++ ) {
196 std::string viewerEntry = ip->getValue( "AP_VIEWERS_LIST", i );
197 std::vector<std::string> veiewerParams = ip->parseValue(viewerEntry,'_');
198 std::string type = veiewerParams[0];
199 std::string viewerID = veiewerParams[1];
200 SUIT_ViewManager* vm = myApp->newViewManager( type.c_str() );
202 continue; //Unknown viewer
204 int nbViews = (ip->nbValues(viewerEntry))/2;
206 //Create nbViews-1 view (-1 because 1 view is created by createViewManager)
207 for ( int i = 1; i< nbViews; i++ ) {
208 SUIT_ViewWindow* aView = vm->createViewWindow();
212 int viewCount = vm->getViewsCount();
213 if (viewCount != nbViews) {
214 printf( "\nRestore visual state: Unknow error, Can't create a view!\n" );
218 //Resize the views, set their captions and apply visual parameters.
219 QPtrVector<SUIT_ViewWindow> views = vm->getViews();
220 for (int i = 0, j = 0; i<viewCount; i++, j++) {
225 // wait untill the window is really shown. This step fixes MANY bugs..
226 while ( !viewWin->isVisible() )
227 qApp->processEvents();
229 viewWin->setCaption(ip->getValue(viewerEntry, j).c_str());
231 // printf ( "VP for viewWin \"%s\": %s\n", viewerEntry.c_str(), ip->getValue(viewerEntry, j+1).c_str() );
232 viewersParameters[ viewWin ] = ip->getValue(viewerEntry, j+1).c_str();
233 //viewWin->setVisualParameters(ip->getValue(viewerEntry, j+1).c_str());
237 // restore modules' visual parameters
238 std::vector<std::string> v = ip->getValues("AP_MODULES_LIST");
239 for ( int i = 0; i < v.size(); i++ ) {
240 myApp->activateModule( v[i].c_str() );
241 if ( SalomeApp_Module* module = dynamic_cast<SalomeApp_Module*>( myApp->activeModule() ) )
242 module->restoreVisualParameters( savePoint );
245 // new view windows may have been created in module->restoreVisualParameters() [GAUSS]
246 // so here we store their visual parameters for later restoring..
248 myApp->viewManagers(lst);
249 QPtrListIterator<SUIT_ViewManager> it( lst );
250 for ( ; it.current(); ++it ) {
251 int view_count = it.current()->getViewsCount();
252 QPtrVector<SUIT_ViewWindow> views = it.current()->getViews();
253 for ( int i = 0; i < view_count; i++ ) {
254 if ( !viewersParameters.contains( views[i] ) ) {
255 viewersParameters[ views[i] ] = views[i]->getVisualParameters();
256 // printf ( "store VP for viewWin \"%s\": %s\n", views[i]->name(), views[i]->getVisualParameters().latin1() );
261 // activate module that was active on save
262 QString activeModuleName( ip->getProperty("AP_ACTIVE_MODULE" ).c_str() );
263 if ( !activeModuleName.isEmpty() )
264 myApp->activateModule( activeModuleName );
266 // setting unique names for view windows in order to restore positions of view windows inside
267 // workstack's structure (see below). During save the same naming algorithm was used,
268 // so the same views will get the same names.
269 nameViewWindows( lst );
271 // restore workstack parameters. should be done after module's restoreVisualParameters(), because
272 // some modules can create their own viewers (like VISU creates GaussViewers)
273 if ( myApp->desktop()->inherits( "STD_TabDesktop" ) ) {
274 QtxWorkstack* workstack = ((STD_TabDesktop*)myApp->desktop())->workstack();
275 (*workstack) << ip->getProperty( "AP_WORKSTACK_INFO" ).c_str();
278 // restore visual parameters of view windows. it must be done AFTER restoring workstack.
279 // also set active view
280 std::string activeViewName = ip->getProperty("AP_ACTIVE_VIEW");
281 QMap<SUIT_ViewWindow*, QString>::Iterator mapIt;
282 for ( mapIt = viewersParameters.begin(); mapIt != viewersParameters.end(); ++mapIt ) {
283 mapIt.key()->setVisualParameters( mapIt.data() );
284 if ( activeViewName == mapIt.key()->name() )
285 mapIt.key()->setFocus();
288 // for ( it.toFirst(); it.current(); ++it ) {
289 // int view_count = it.current()->getViewsCount();
290 // QPtrVector<SUIT_ViewWindow> views = it.current()->getViews();
291 // for ( int i = 0; i < view_count; i++ )
292 // views[i]->setVisualParameters( viewersParameters[ views[i]->name() ] );
295 // set focus to previously saved active view window
296 // std::string activeViewName = ip->getProperty("AP_ACTIVE_VIEW");
297 // for ( it.toFirst(); it.current(); ++it ) {
298 // int view_count = it.current()->getViewsCount();
299 // QPtrVector<SUIT_ViewWindow> views = it.current()->getViews();
300 // for ( int i = 0; i < view_count; i++ ) {
301 // if ( activeViewName == views[i]->name() )
302 // views[i]->setFocus();