Salome HOME
5b4a5e2bd7ae0f3458c4cdda225b0337be2a6fa6
[modules/gui.git] / src / SalomeApp / SalomeApp_Module.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:      SalomeApp_Module.cxx
24 // Created:   10/25/2004 11:39:56 AM
25 // Author:    Sergey LITONIN
26
27 #include "SalomeApp_Module.h"
28 #include "SalomeApp_DataModel.h"
29 #include "SalomeApp_Application.h"
30 #include "SalomeApp_Study.h"
31 #include "SalomeApp_DataObject.h"
32
33 #include "LightApp_Selection.h"
34
35 #include "CAM_DataModel.h"
36
37 // temporary commented
38 //#include "OB_Browser.h"
39
40 #include <SALOME_ListIO.hxx>
41 #include <SALOME_ListIteratorOfListIO.hxx>
42 #include <SALOME_InteractiveObject.hxx>
43           
44 #include <LightApp_DataObject.h>
45
46 #include <SUIT_Session.h>
47 #include <SUIT_DataBrowser.h>
48 #include <SUIT_ViewManager.h>
49 #include <SUIT_MessageBox.h>
50 #include <SUIT_Desktop.h>
51
52 #include <QString>
53
54 /*!Constructor.*/
55 SalomeApp_Module::SalomeApp_Module( const QString& name )
56   : LightApp_Module( name ),
57     myIsFirstActivate( true )
58 {
59 }
60
61 /*!Destructor.*/
62 SalomeApp_Module::~SalomeApp_Module()
63 {
64 }
65
66 /*!Gets application.*/
67 SalomeApp_Application* SalomeApp_Module::getApp() const
68 {
69   return (SalomeApp_Application*)application();
70 }
71
72 /*!Create new instance of data model and return it.*/
73 CAM_DataModel* SalomeApp_Module::createDataModel()
74 {
75   return new SalomeApp_DataModel(this);
76 }
77
78 /*!Create and return instance of LightApp_Selection.*/
79 LightApp_Selection* SalomeApp_Module::createSelection() const
80 {
81   return LightApp_Module::createSelection();
82 }
83
84 /*!
85   Converts objects-containers to list of objects, those are contained
86   Other objects must be added without conversion
87   \param source - source list of objects
88   \param dest - list of converted objects
89 */
90 void SalomeApp_Module::extractContainers( const SALOME_ListIO& source, SALOME_ListIO& dest ) const
91 {
92   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
93   if( !study )
94   {
95     dest = source;
96     return;
97   }
98
99   SALOME_ListIteratorOfListIO anIt( source );
100   for( ; anIt.More(); anIt.Next() )
101   {
102     Handle( SALOME_InteractiveObject ) obj = anIt.Value();
103     if( obj->hasEntry() )
104     {
105       _PTR(SObject) SO = study->studyDS()->FindObjectID( obj->getEntry() );
106       if( SO && QString( SO->GetID().c_str() ) == SO->GetFatherComponent()->GetID().c_str() )
107       { //component is selected
108         _PTR(SComponent) SC( SO->GetFatherComponent() );
109         _PTR(ChildIterator) anIter ( study->studyDS()->NewChildIterator( SC ) );
110         anIter->InitEx( true );
111         while( anIter->More() )
112         {
113           _PTR(SObject) valSO ( anIter->Value() );
114           _PTR(SObject) refSO;
115           if( !valSO->ReferencedObject( refSO ) )
116           {
117             QString id = valSO->GetID().c_str(),
118                     comp = SC->ComponentDataType().c_str(),
119                     val = valSO->GetName().c_str();
120
121             Handle( SALOME_InteractiveObject ) new_obj =
122               new SALOME_InteractiveObject( id.toLatin1(), comp.toLatin1(), val.toLatin1() );
123             dest.Append( new_obj );
124           }
125           anIter->Next();
126         }
127         continue;
128       }
129     }
130     dest.Append( obj );
131   }
132 }
133
134 /*!
135  * \brief Virtual public
136  *
137  * This method is called just before the study document is saved, so the module has a possibility
138  * to store visual parameters in AttributeParameter attribut
139  */
140 void SalomeApp_Module::storeVisualParameters(int savePoint)
141 {
142 }
143
144
145 /*!Activate module.*/
146 bool SalomeApp_Module::activateModule( SUIT_Study* theStudy )
147 {
148   bool state = LightApp_Module::activateModule( theStudy );
149
150   if (!myIsFirstActivate)
151     return state;
152   
153   updateModuleVisibilityState();
154
155   myIsFirstActivate = false;
156   
157   return state;
158 }
159
160 /*!
161  * \brief Virtual public
162  *
163  * This method is called after the study document is opened, so the module has a possibility to restore
164  * visual parameters
165  */
166 void SalomeApp_Module::restoreVisualParameters(int savePoint)
167 {
168 }
169
170 /*! Redefined to reset internal flags valid for study instance */
171 void SalomeApp_Module::studyClosed( SUIT_Study* theStudy )
172 {
173   LightApp_Module::studyClosed( theStudy );
174   
175   myIsFirstActivate = true;
176   
177   LightApp_Application* app = dynamic_cast<LightApp_Application*>(application());
178   if (!app)
179     return;
180   
181   SUIT_DataBrowser* ob = app->objectBrowser();
182   if (ob && ob->model())
183     disconnect( ob->model(), SIGNAL( clicked( SUIT_DataObject*, int ) ),
184                 this, SLOT( onObjectClicked( SUIT_DataObject*, int ) ) );
185 }
186
187
188 /*!
189  * \brief Virtual public slot
190  *
191  * This method is called after the object inserted into data view to update their visibility state
192  * This is default implementation
193  */
194 void SalomeApp_Module::onObjectClicked( SUIT_DataObject* theObject, int theColumn )
195 {
196   if (!isActiveModule())
197     return;
198   // change visibility of object
199   if (!theObject || theColumn != SUIT_DataObject::VisibilityId )
200     return;
201
202   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
203   if( !study )
204     return;
205
206   LightApp_DataObject* lo = dynamic_cast<LightApp_DataObject*>(theObject);
207   if(!lo)
208     return;
209   
210   // detect action index (from LightApp level)
211   int id = -1;
212   
213   if ( study->visibilityState(lo->entry()) == Qtx::ShownState )
214     id = myErase;
215   else if ( study->visibilityState(lo->entry()) == Qtx::HiddenState )
216     id = myDisplay;
217   
218   if ( id != -1 )
219     startOperation( id );
220 }
221
222
223 /*!
224   Called then study closed
225 */
226 void SalomeApp_Application::onStudyClosed( SUIT_Study* theStudy){
227   LightApp_Application::onStudyClosed(theStudy);
228
229   disconnect( this, SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
230               this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
231 }
232
233
234 void SalomeApp_Module::updateModuleVisibilityState() {
235
236   // update visibility state of objects
237   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
238   if (!app)
239     return;
240   
241   SUIT_DataBrowser* ob = app->objectBrowser();
242   if (!ob || !ob->model())
243     return;
244
245   // connect to click on item
246   connect( ob->model(), SIGNAL( clicked( SUIT_DataObject*, int ) ),
247            this, SLOT( onObjectClicked( SUIT_DataObject*, int ) ), Qt::UniqueConnection );
248
249   SUIT_DataObject* rootObj = ob->root();
250   if( !rootObj )
251     return;
252   
253   DataObjectList listObj = rootObj->children( true );
254   
255   SUIT_ViewModel* vmod = 0;
256   if ( SUIT_ViewManager* vman = app->activeViewManager() )
257     vmod = vman->getViewModel();
258   app->updateVisibilityState( listObj, vmod );
259 }