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