1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_VisualStateOp.h"
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_Tool.h"
27 #include "HYDROGUI_UpdateFlags.h"
29 #include <HYDROData_Document.h>
30 #include <HYDROData_VisualState.h>
32 #include <GraphicsView_Viewer.h>
34 #include <LightApp_Application.h>
36 #include <QtxWorkstack.h>
38 #include <STD_TabDesktop.h>
40 #include <SUIT_Desktop.h>
41 #include <SUIT_ViewManager.h>
42 #include <SUIT_ViewWindow.h>
44 #include <QApplication.h>
46 HYDROGUI_VisualStateOp::HYDROGUI_VisualStateOp( HYDROGUI_Module* theModule,
47 const bool theIsLoad )
48 : HYDROGUI_Operation( theModule ),
51 setName( theIsLoad ? tr( "LOAD_VISUAL_STATE" ) : tr( "SAVE_VISUAL_STATE" ) );
54 HYDROGUI_VisualStateOp::~HYDROGUI_VisualStateOp()
58 void HYDROGUI_VisualStateOp::startOperation()
60 HYDROGUI_Operation::startOperation();
64 aResult = loadVisualState();
66 aResult = saveVisualState();
71 abort(); // do not commit the document command
74 bool HYDROGUI_VisualStateOp::saveVisualState()
76 LightApp_Application* anApp = module()->getApp();
78 Handle(HYDROData_VisualState) aVisualState =
79 Handle(HYDROData_VisualState)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
80 if( aVisualState.IsNull() )
82 // Create new visual state
83 aVisualState = Handle(HYDROData_VisualState)::DownCast( doc()->CreateObject( KIND_VISUAL_STATE ) );
84 if( aVisualState.IsNull() )
87 QString aName = HYDROGUI_Tool::GenerateObjectName( module(), "VisualState" );
88 aVisualState->SetName( aName );
92 PropertyMap aPropertyMap;
94 // A. Setting unique names for view windows in order to save this view inside
95 // workstack's structure (see below). On restore the views with the same names will
96 // be placed to the same place inside the workstack's splitters.
97 ViewManagerList aViewManagerList;
98 anApp->viewManagers( aViewManagerList );
99 nameViewWindows( aViewManagerList );
101 // B. Store active window.
102 if( SUIT_ViewWindow* aViewWindow = anApp->desktop()->activeWindow() )
103 setVisualProperty( aPropertyMap, "AP_ACTIVE_VIEW", aViewWindow->objectName() );
105 // C. Store view properties.
107 QListIterator<SUIT_ViewManager*> anIter( aViewManagerList );
108 while( anIter.hasNext() )
110 if( SUIT_ViewManager* aViewManager = anIter.next() )
112 int aViewId = (int)aViewManager->getViewModel();
113 if( SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView() )
115 QString aType = aViewManager->getType();
116 QString aViewerEntry = QString( "%1_%2" ).arg( aType ).arg( ++aViewerId );
117 setVisualProperty( aPropertyMap, "AP_VIEWERS_LIST", aViewerEntry, true );
119 setVisualProperty( aPropertyMap, aViewerEntry, aViewWindow->windowTitle(), true );
120 setVisualProperty( aPropertyMap, aViewerEntry, aViewWindow->getVisualParameters(), true );
122 // C1. Store parameters of presentations.
123 HYDROData_SequenceOfObjects aSeq;
124 if( aType == GraphicsView_Viewer::Type() )
125 HYDROGUI_Tool::GetPrsSubObjects( module(), aSeq );
127 for( int anObjIndex = 1, aLength = aSeq.Length(); anObjIndex <= aLength; anObjIndex++ )
129 Handle(HYDROData_Object) anObject = aSeq.Value( anObjIndex );
130 if( !anObject.IsNull() )
132 // Format: "Name|Visibility[|CoordX|CoordY]"
133 QString aParameters = anObject->GetName();
135 int aVisibility = (int)anObject->IsVisible( aViewId );
136 aParameters.append( QString( "|%1" ).arg( aVisibility ) );
138 setVisualProperty( aPropertyMap, aViewerEntry, aParameters, true );
145 // D. Store split areas.
146 if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
148 QtxWorkstack* aWorkstack = ( (STD_TabDesktop*)anApp->desktop() )->workstack();
149 QByteArray aWorkstackState = aWorkstack->saveState( 0 );
150 QString aWorkstackInfo = aWorkstackState.toHex();
151 setVisualProperty( aPropertyMap, "AP_WORKSTACK_INFO", aWorkstackInfo );
154 // E. Store module preferences.
155 //ouv: currently, nothing to do
157 QString aState = encodePropertyMap( aPropertyMap );
158 //printf( "--- SetState -----------\n" );
159 //printf( "%s\n", qPrintable( aState ) );
160 //printf( "------------------------\n" );
161 aVisualState->SetState( aState );
163 module()->update( UF_Model );
168 bool HYDROGUI_VisualStateOp::loadVisualState()
170 LightApp_Application* anApp = module()->getApp();
172 Handle(HYDROData_VisualState) aVisualState =
173 Handle(HYDROData_VisualState)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
174 if( aVisualState.IsNull() )
177 QString aState = aVisualState->GetState();
178 //printf( "--- GetState -----------\n" );
179 //printf( "%s\n", qPrintable( aState ) );
180 //printf( "------------------------\n" );
181 PropertyMap aPropertyMap = decodePropertyMap( aState );
183 // Restore parameters.
185 // Preparation. Remove the existing viewers.
186 anApp->clearViewManagers();
188 // E. Restore module preferences
189 //ouv: currently, nothing to do
191 // C. Restore view properties (step 1).
192 QMap<SUIT_ViewWindow*, QString> aViewParameters;
193 int aNbViewers = nbVisualProperties( aPropertyMap, "AP_VIEWERS_LIST" );
194 for( int anIndex = 0; anIndex < aNbViewers; anIndex++ )
196 QString aViewerEntry = getVisualProperty( aPropertyMap, "AP_VIEWERS_LIST", anIndex );
197 QString aType = aViewerEntry.section( '_', 0, -2 );
198 QString aViewerId = aViewerEntry.section( '_', -1 ); // unused
199 if( SUIT_ViewManager* aViewManager = anApp->createViewManager( aType ) )
201 int aViewId = (int)aViewManager->getViewModel();
202 if( SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView() )
204 // Wait until the window is really shown. This step fixes MANY bugs.
205 //while( !aViewManager->isVisible() )
206 // qApp->processEvents();
208 int aNbViewerProps = nbVisualProperties( aPropertyMap, aViewerEntry );
210 aViewWindow->setWindowTitle( getVisualProperty( aPropertyMap, aViewerEntry, 0 ) );
212 // Parameters of view windows are restoring after the workstack (see below).
213 aViewParameters[ aViewWindow ] = getVisualProperty( aPropertyMap, aViewerEntry, 1 );
215 // C1. Restore parameters of presentations.
216 QMap< QString, QStringList > anObject2ParametersMap;
217 for( int aPropIndex1 = 2; aPropIndex1 < aNbViewerProps; aPropIndex1++ )
219 QString aProperty = getVisualProperty( aPropertyMap, aViewerEntry, aPropIndex1 );
220 QStringList aParameters = aProperty.split( "|" );
221 if( aParameters.count() > 1 )
223 QString aName = aParameters.front();
224 aParameters.pop_front();
225 anObject2ParametersMap[ aName ] = aParameters;
229 HYDROData_SequenceOfObjects aSeq;
230 if( aType == GraphicsView_Viewer::Type() )
231 HYDROGUI_Tool::GetPrsSubObjects( module(), aSeq );
233 for( int anObjIndex = 1, aLength = aSeq.Length(); anObjIndex <= aLength; anObjIndex++ )
235 Handle(HYDROData_Object) anObject = aSeq.Value( anObjIndex );
236 if( !anObject.IsNull() )
238 QString aName = anObject->GetName();
239 if( anObject2ParametersMap.find( aName ) != anObject2ParametersMap.end() )
241 QStringList aParameters = anObject2ParametersMap[ aName ];
242 int aParamCount = aParameters.count();
243 QVector<bool> anIsOk( aParamCount, false );
245 int aParamIndex = -1;
246 if( aParamCount - aParamIndex - 1 >= 1 )
248 bool anIsVisible = aParameters[ aParamIndex ].toInt( &anIsOk[ ++aParamIndex ] );
250 anObject->SetVisible( aViewId, anIsVisible );
259 // A. Setting unique names for view windows in order to restore positions of view windows inside
260 // workstack's structure (see below). During save the same naming algorithm was used,
261 // so the same views will get the same names.
262 ViewManagerList aViewManagerList;
263 anApp->viewManagers( aViewManagerList );
264 nameViewWindows( aViewManagerList );
266 qApp->processEvents();
268 // D. Restore split areas.
269 QtxWorkstack* aWorkstack = 0;
270 if( anApp->desktop()->inherits( "STD_TabDesktop" ) )
272 if( aWorkstack = ((STD_TabDesktop*)anApp->desktop())->workstack() )
274 QString aWorkstackInfo = getVisualProperty( aPropertyMap, "AP_WORKSTACK_INFO" );
275 QByteArray aWorkstackState = QByteArray::fromHex( aWorkstackInfo.toLatin1() );
276 aWorkstack->restoreState( aWorkstackState, 0 );
280 qApp->processEvents();
282 // C. Restore view properties (step 2).
283 // B. Restore active window. (currently doesn't work)
284 QString anActiveViewName = getVisualProperty( aPropertyMap, "AP_ACTIVE_VIEW" );
285 QMap<SUIT_ViewWindow*, QString>::Iterator aMapIter;
286 for( aMapIter = aViewParameters.begin(); aMapIter != aViewParameters.end(); ++aMapIter )
288 if( SUIT_ViewWindow* aViewWindow = aMapIter.key() )
290 aViewWindow->setVisualParameters( aMapIter.value() );
291 if( anActiveViewName == aViewWindow->objectName() )
293 aWorkstack->setActiveWindow( aViewWindow );
297 module()->update( UF_Viewer );
302 void HYDROGUI_VisualStateOp::nameViewWindows( const ViewManagerList& theList )
304 QMap<QString, int> aViewersCounter;
305 for( ViewManagerList::const_iterator anIter = theList.begin(); anIter != theList.end(); ++anIter )
307 SUIT_ViewManager* aViewManager = *anIter;
311 int aViewCount = aViewManager->getViewsCount();
312 QString aType = aViewManager->getType();
316 if( !aViewersCounter.contains( aType ) )
317 aViewersCounter.insert( aType, 0 );
319 int& aViewerId = aViewersCounter[ aType ];
321 QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
322 for( int i = 0; i < aViewCount; i++ )
324 QString aName = QString( "%1_%2_%3" ).arg( aType ).arg( aViewerId ).arg( i );
325 aViews[i]->setObjectName( aName );
331 QString HYDROGUI_VisualStateOp::encodePropertyMap( const PropertyMap& thePropertyMap )
333 QStringList aPropertyDataStringList;
334 PropertyMapIterator anIter( thePropertyMap );
335 while( anIter.hasNext() )
337 QString aPropertyName = anIter.next().key();
338 QStringList aPropertyData = anIter.value();
340 if( !aPropertyData.isEmpty() )
342 aPropertyData.prepend( aPropertyName );
343 QString aPropertyDataString = aPropertyData.join( "||" );
344 aPropertyDataStringList.append( aPropertyDataString );
348 QString aResult = aPropertyDataStringList.join( "|||" );
352 HYDROGUI_VisualStateOp::PropertyMap
353 HYDROGUI_VisualStateOp::decodePropertyMap( const QString& theString )
355 PropertyMap aPropertyMap;
356 QStringList aPropertyDataStringList = theString.split( "|||" );
357 QStringListIterator anIter( aPropertyDataStringList );
358 while( anIter.hasNext() )
360 QString aPropertyDataString = anIter.next();
361 QStringList aPropertyData = aPropertyDataString.split( "||" );
362 if( aPropertyData.count() >= 2 )
364 QString aPropertyName = aPropertyData.front();
365 aPropertyData.pop_front();
366 aPropertyMap[ aPropertyName ] = aPropertyData;
372 void HYDROGUI_VisualStateOp::setVisualProperty( PropertyMap& thePropertyMap,
373 const QString& thePropertyName,
374 const QString& thePropertyData,
375 const bool theIsAppend )
377 QStringList& aPropertyDataList = thePropertyMap[ thePropertyName ];
379 aPropertyDataList.clear();
380 aPropertyDataList.append( thePropertyData );
383 QString HYDROGUI_VisualStateOp::getVisualProperty( const PropertyMap& thePropertyMap,
384 const QString& thePropertyName,
387 PropertyMap::const_iterator anIter = thePropertyMap.find( thePropertyName );
388 if( anIter != thePropertyMap.end() )
390 const QStringList aPropertyData = anIter.value();
391 if( theIndex >= 0 && theIndex < aPropertyData.count() )
392 return aPropertyData[ theIndex ];
397 int HYDROGUI_VisualStateOp::nbVisualProperties( const PropertyMap& thePropertyMap,
398 const QString& thePropertyName )
400 PropertyMap::const_iterator anIter = thePropertyMap.find( thePropertyName );
401 if( anIter != thePropertyMap.end() )
403 const QStringList aPropertyData = anIter.value();
404 return aPropertyData.count();