Salome HOME
647b84a17c058d039283f58676b987670c384e2c
[modules/homard.git] / src / HOMARDGUI / HOMARDGUI_Utils.cxx
1 // Copyright (C) 2011-2013  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
20
21 #include "HOMARDGUI_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 #include "SALOME_ListIteratorOfListIO.hxx"
36
37 #include "SALOMEconfig.h"
38 #include "utilities.h"
39 #include <string>
40 #include <HOMARDGUI.h>
41
42
43 #include <qstring.h>
44 #include <qstringlist.h>
45 #include <sys/stat.h>
46 #include <dirent.h>
47
48 SALOME_ListIO HOMARD_UTILS::mySelected;
49
50 //================================================================
51 // Function : GetCStudy
52 // Returne un pointeur sur l'etude courante
53 //================================================================
54  _PTR(Study) HOMARD_UTILS::GetCStudy(const SalomeApp_Study* theStudy)
55 {
56     return theStudy->studyDS();
57 }
58
59 //================================================================
60 // Function : GetActiveStudy
61 // Returne un pointeur sur l'etude active
62 //================================================================
63 SUIT_Study* HOMARD_UTILS::GetActiveStudy()
64 {
65     SUIT_Application* app = SUIT_Session::session()->activeApplication();
66     if (app)
67       return app->activeStudy();
68     else
69       return NULL;
70 }
71
72 //================================================================
73 // Function : GetActiveStudyDocument
74 // Returne un pointeur sur l'etude active
75 //================================================================
76 _PTR(Study) HOMARD_UTILS::GetActiveStudyDocument()
77 {
78     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
79     if (aStudy){
80       return aStudy->studyDS();
81     }
82     else
83       return _PTR(Study)();
84 }
85
86 //================================================================
87 // Function : updateObjBrowser
88 // Purpose  : met a jour l arbre d 'etude pour Homard
89 //================================================================
90 void HOMARD_UTILS::updateObjBrowser()
91 {
92     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
93     if (app) {
94       //  Le nom identifiant doit etre la valeur du parametre
95       // name de la section HOMARD du fichier SalomeApp.xml
96       CAM_Module* module = app->module("Homard" );
97       SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
98       if ( appMod ) {
99         app->updateObjectBrowser();
100         appMod->updateObjBrowser( true );
101       }
102       else
103         MESSAGE( "---   HOMARD::updateObjBrowser: appMod = NULL");
104     }
105 }
106
107 //================================================================
108 // Function : selectedIO
109 // Return the list of selected SALOME_InteractiveObject's
110 //================================================================
111 const SALOME_ListIO& HOMARD_UTILS::selectedIO()
112 {
113       SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* > ( SUIT_Session::session()->activeApplication() );
114       LightApp_SelectionMgr* aSelectionMgr = app->selectionMgr();
115       if( aSelectionMgr )
116       {
117               aSelectionMgr->selectedObjects( mySelected );
118               for (SALOME_ListIteratorOfListIO it (mySelected); it.More(); it.Next())
119                  SCRUTE(it.Value()->getEntry());
120       };
121       return mySelected;
122 }
123
124 //================================================================
125 // Function : IObjectCount
126 // Return the number of selected objects
127 //================================================================
128 int HOMARD_UTILS::IObjectCount()
129 {
130       SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
131       LightApp_SelectionMgr* aSelectionMgr = app->selectionMgr();
132       if( aSelectionMgr )
133       {
134               aSelectionMgr->selectedObjects( mySelected );
135               SCRUTE(mySelected.Extent());
136               return mySelected.Extent();
137       }
138       return 0;
139 }
140
141 //================================================================
142 // Function : firstIObject
143 // Purpose  :  Return the first selected object in the selected object list
144 //================================================================
145 Handle(SALOME_InteractiveObject) HOMARD_UTILS::firstIObject()
146 {
147       const SALOME_ListIO& aList = selectedIO();
148       return aList.Extent() > 0 ? aList.First() : Handle(SALOME_InteractiveObject)();
149 }
150
151 //================================================================
152 // Function : lastIObject
153 // Return the last selected object in the selected object list
154 //================================================================
155 Handle(SALOME_InteractiveObject) HOMARD_UTILS::lastIObject()
156 {
157       const SALOME_ListIO& aList = selectedIO();
158       return aList.Extent() > 0 ? aList.Last() : Handle(SALOME_InteractiveObject)();
159 }
160
161 //================================================================
162 // Retourne vrai si l objet est du type voulu
163 // . Dans le cas d'un cas, d'une hypothese, d'une zone, on se contente
164 // d'une comparaison simple entre le type stocke et TypeObject.
165 // . Pour l'iteration, le type stocke en attribut est sous la forme
166 // "IterationHomard" + le nom de l'iteration precedente.
167 // Il faut donc regarder si la chaine commence par TypeObject.
168 // . Idem pour le fichier de messages : "Mess " + numero d'iteration
169 //
170 // On filtre ce genre de situation avec option :
171 //  -1 : comparaison totale
172 //   0 : comparaison sur le debut de la chaine
173 //================================================================
174 bool HOMARD_UTILS::isObject(_PTR(SObject) MonObj, QString TypeObject, int option)
175 {
176 //   MESSAGE("isObject, TypeObject = "<<TypeObject.toStdString().c_str()<<", option = "<<option);
177 // Existence d'un attribut ?
178   _PTR(GenericAttribute) anAttr;
179   if ( !MonObj->FindAttribute(anAttr, "AttributeComment") ) return false;
180 // Quel type ?
181   _PTR(AttributeComment) aComment (anAttr);
182   QString Type = QString(aComment->Value().c_str());
183 //   MESSAGE("Type = "<<Type.toStdString().c_str());
184 // Est-ce le bon ?
185   bool bOK = false ;
186   if ( option == 0 )
187   {
188     int position = Type.lastIndexOf(TypeObject);
189     if ( position == 0 ) { bOK = true ; }
190   }
191   else
192   {
193     if ( Type == TypeObject ) { bOK = true ; }
194   }
195   return bOK ;
196 }
197 //================================================================
198 // Retourne vrai si l objet est une frontiere analytique
199 //================================================================
200 bool HOMARD_UTILS::isBoundaryAn(_PTR(SObject) MonObj)
201 {
202    return isObject( MonObj, QString("BoundaryAnHomard"), -1 ) ;
203 }
204 //================================================================
205 // Retourne vrai si l objet est une frontiere discrete
206 //================================================================
207 bool HOMARD_UTILS::isBoundaryDi(_PTR(SObject) MonObj)
208 {
209    return isObject( MonObj, QString("BoundaryDiHomard"), -1 ) ;
210 }
211 //================================================================
212 // Retourne vrai si l objet est un cas
213 //================================================================
214 bool HOMARD_UTILS::isCase(_PTR(SObject) MonObj)
215 {
216    return isObject( MonObj, QString("CasHomard"), -1 ) ;
217 }
218 //================================================================
219 // Retourne vrai si l objet est une Hypothese
220 //================================================================
221 bool HOMARD_UTILS::isHypo(_PTR(SObject) MonObj)
222 {
223    return isObject( MonObj, QString("HypoHomard"), -1 ) ;
224 }
225 //================================================================
226 // Retourne vrai si l objet est une iteration
227 //================================================================
228 bool HOMARD_UTILS::isIter(_PTR(SObject) MonObj)
229 {
230    return isObject( MonObj, QString("IterationHomard"), 0 ) ;
231 }
232 //================================================================
233 // Retourne vrai si l objet est un schema YACS
234 //================================================================
235 bool HOMARD_UTILS::isYACS(_PTR(SObject) MonObj)
236 {
237    return isObject( MonObj, QString("YACSHomard"), -1 ) ;
238 }
239 //================================================================
240 // Retourne vrai si l objet est une zone
241 //================================================================
242 bool HOMARD_UTILS::isZone(_PTR(SObject) MonObj)
243 {
244    return isObject( MonObj, QString("ZoneHomard"), -1 ) ;
245 }
246 //================================================================
247 // Retourne vrai si l objet est un fichier de type TypeFile
248 //================================================================
249 bool HOMARD_UTILS::isFileType(_PTR(SObject) MonObj, QString TypeFile)
250 {
251    return isObject( MonObj, TypeFile, 0 ) ;
252 }
253
254 //=========================================================================================================
255 void HOMARD_UTILS::PushOnHelp(QString monFichierAide, QString contexte, QString LanguageShort)
256 {
257   MESSAGE("Debut de PushOnHelp avec monFichierAide = "<< monFichierAide.toStdString().c_str());
258   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
259   if (app)
260   {
261     HOMARDGUI* aHomardGUI = dynamic_cast<HOMARDGUI*>( app->module( "Homard" ) );
262     // Repertoire de reference de la documentation
263     QString rep = aHomardGUI ? app->moduleName(aHomardGUI->moduleName()) : QString("") ;
264     // WARNING/ATTENTION : si on savait recuperer la langue depuis les preferences, on ne fera pas le passage par argument
265 //     SUIT_ResourceMgr* resMgr = getApp()->resourceMgr();
266 //     SUIT_ResourceMgr* resMgr = myModule->getApp()->resourceMgr();
267 //     QString langue = resMgr->stringValue("language", "language", "en");
268 //     QString langue = "fr" ;
269     MESSAGE(". LanguageShort " << LanguageShort.toStdString().c_str()) ;
270     // Complement du fichier
271     QString fichier = QString(LanguageShort+"/"+monFichierAide) ;
272     MESSAGE(". Appel de onHelpContextModule avec :");
273     MESSAGE("    rep      = "<< rep.toStdString().c_str());
274     MESSAGE("    fichier  = "<< fichier.toStdString().c_str());
275     MESSAGE("    contexte = "<< contexte.toStdString().c_str());
276
277     app->onHelpContextModule(rep, fichier, contexte);
278   }
279 }
280 //=========================================================================================================