Salome HOME
Update copyright information
[modules/filter.git] / src / FILTERGUI / FILTERGUI_Utils.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include <qstring.h>
20
21 #include "FILTERGUI_Utils.h"
22
23 #include "OB_Browser.h"
24
25 #include "SUIT_Desktop.h"
26 #include "SUIT_Application.h"
27 #include "SUIT_Session.h"
28
29 #include "LightApp_SelectionMgr.h"
30 #include "SalomeApp_Application.h"
31 #include "SalomeApp_Module.h"
32 #include "SalomeApp_Study.h"
33
34 #include "SALOME_ListIO.hxx"
35
36 #include "SALOMEconfig.h"
37 // #include CORBA_CLIENT_HEADER(GEOM_Gen)
38
39 #include "utilities.h"
40
41 //using namespace std;
42
43 namespace FILTER{
44
45   SUIT_Desktop*
46   GetDesktop(const CAM_Module* theModule)
47   {
48     return theModule->application()->desktop();
49   }
50
51   LightApp_SelectionMgr*
52   GetSelectionMgr(const SalomeApp_Module* theModule)
53   {
54     return theModule->getApp()->selectionMgr();
55   }
56
57   SUIT_ResourceMgr*
58   GetResourceMgr( const SalomeApp_Module* )
59   {
60     return SUIT_Session::session()->resourceMgr();
61   }
62
63   _PTR(Study)
64   GetCStudy(const SalomeApp_Study* theStudy)
65   {
66     return theStudy->studyDS();
67   }
68
69   CORBA::Object_var 
70   DataOwnerToObject(const LightApp_DataOwnerPtr& theOwner)
71   {
72     CORBA::Object_var anObj;
73     if(theOwner){
74       const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
75       if(!anIO.IsNull()){
76         if(anIO->hasEntry()){
77           _PTR(Study) aStudy = GetActiveStudyDocument();
78           _PTR(SObject) aSObj = aStudy->FindObjectID(anIO->getEntry());
79           anObj = SObjectToObject(aSObj,aStudy);
80         }
81       }
82     }
83     return anObj;
84   }
85
86
87   SUIT_Study* GetActiveStudy()
88   {
89     SUIT_Application* app = SUIT_Session::session()->activeApplication();
90     if (app)
91       return app->activeStudy();
92     else
93       return NULL;
94   }
95
96   SUIT_ViewWindow* GetActiveWindow()
97   {
98     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
99       (SUIT_Session::session()->activeApplication());
100     if (app && app->desktop() )
101       return app->desktop()->activeWindow();
102     else
103       return NULL;
104   }
105
106   _PTR(Study) GetActiveStudyDocument()
107   {
108     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
109     if (aStudy)
110       return aStudy->studyDS();
111     else
112       return _PTR(Study)();
113   }
114
115   _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
116   {
117     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
118       (SUIT_Session::session()->activeApplication());
119     if (app && !CORBA::is_nil(theObject)) {
120       if(_PTR(Study) aStudy = GetActiveStudyDocument()){
121         CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
122         if (strcmp(anIOR.in(), "") != 0)
123           return aStudy->FindObjectIOR(anIOR.in());
124       }
125     }
126     return _PTR(SObject)();
127   }
128
129   void SetName (_PTR(SObject) theSObject, const char* theName)
130   {
131     _PTR(Study) aStudy = GetActiveStudyDocument();
132     if (aStudy->GetProperties()->IsLocked())
133       return;
134     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
135     _PTR(GenericAttribute) anAttr =
136       aBuilder->FindOrCreateAttribute(theSObject, "AttributeName");
137     _PTR(AttributeName) aName = anAttr;
138     if (aName)
139       aName->SetValue(theName);
140   }
141
142   void SetValue (_PTR(SObject) theSObject, const char* theValue)
143   {
144     _PTR(Study) aStudy = GetActiveStudyDocument();
145     if (aStudy->GetProperties()->IsLocked())
146       return;
147     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
148     _PTR(GenericAttribute) anAttr =
149       aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
150     _PTR(AttributeComment) aComment = anAttr;
151     if (aComment)
152       aComment->SetValue(theValue);
153   }
154   
155   void setFileName (_PTR(SObject) theSObject, const char* theValue)
156   {
157     _PTR(Study) aStudy = GetActiveStudyDocument();
158     if (aStudy->GetProperties()->IsLocked())
159       return;
160     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
161     _PTR(GenericAttribute) anAttr =
162       aBuilder->FindOrCreateAttribute(theSObject, "AttributeExternalFileDef");
163     _PTR(AttributeExternalFileDef) aFileName = anAttr;
164     if (aFileName)
165       aFileName->SetValue(theValue);
166   }
167   
168   void setFileType (_PTR(SObject) theSObject, const char* theValue)
169   {
170     _PTR(Study) aStudy = GetActiveStudyDocument();
171     if (aStudy->GetProperties()->IsLocked())
172       return;
173     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
174     _PTR(GenericAttribute) anAttr =
175       aBuilder->FindOrCreateAttribute(theSObject, "AttributeFileType");
176     _PTR(AttributeFileType) aFileType = anAttr;
177     if (aFileType)
178       aFileType->SetValue(theValue);
179   }
180
181   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject,
182                                      _PTR(Study)   theStudy)
183   {
184     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
185       (SUIT_Session::session()->activeApplication());
186     if (theSObject) {
187       _PTR(GenericAttribute) anAttr;
188       if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
189         _PTR(AttributeIOR) anIOR = anAttr;
190         CORBA::String_var aVal = anIOR->Value().c_str();
191         return app->orb()->string_to_object(aVal);
192       }
193     }
194     return CORBA::Object::_nil();
195   }
196
197   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
198   {
199     _PTR(Study) aStudy = GetActiveStudyDocument();
200     return SObjectToObject(theSObject,aStudy);
201   }
202
203   CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
204   {
205     if (!theIO.IsNull()) {
206       if (theIO->hasEntry()) {
207         _PTR(Study) aStudy = GetActiveStudyDocument();
208         _PTR(SObject) anObj = aStudy->FindObjectID(theIO->getEntry());
209         return SObjectToObject(anObj,aStudy);
210       }
211     }
212     return CORBA::Object::_nil();
213   }
214
215   CORBA::Object_var IORToObject (const char* theIOR)
216   {
217     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
218       (SUIT_Session::session()->activeApplication());
219     return app->orb()->string_to_object(theIOR);
220   }
221
222   int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
223   {
224     if (!theMgr)
225       return 0;
226
227     SALOME_ListIO selected;
228     theMgr->selectedObjects(selected);
229     int aNbSel = selected.Extent();
230     if (aNbSel == 1) {
231       Handle(SALOME_InteractiveObject) anIObject = selected.First();
232       theName = anIObject->getName();
233     } else {
234       theName = QObject::tr("FILTER_OBJECTS_SELECTED").arg(aNbSel);
235     }
236     return aNbSel;
237   }
238
239 //  void UpdateObjBrowser (bool)
240 //  {
241 //    //FILTERGUI::activeStudy()->updateObjBrowser(true);
242 //    //SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
243 //    //  (SUIT_Session::session()->activeApplication());
244 //    //if (anApp) anApp->objectBrowser()->updateTree();
245 //    FILTERGUI::GetSMESHGUI()->updateObjBrowser();
246 //  }
247 }