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