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