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