Salome HOME
Merge from V6_main 01/04/2013
[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 //================================================================
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 //================================================================
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 // Function : isXmgrace
165 // Retourne vrai si le fichier associe est de type ASCII
166 //================================================================
167 bool HOMARD_UTILS::isXmgrace(_PTR(SObject) MonObj)
168 {
169    _PTR(GenericAttribute) anAttr;
170    if (!MonObj->FindAttribute(anAttr, "AttributeComment")) return false;
171    _PTR(AttributeComment) aFileComment (anAttr);
172    std::string Type = aFileComment->Value();
173    if (QString(Type.c_str()) == QString("HomardOuputQual")) return true;
174    return false;
175 }
176 //================================================================
177 // Retourne vrai si l objet est du type voulu
178 // . Dans le cas d'un cas, d'une hypothese, d'une zone, on se contente
179 // d'une comparaison simple entre le type stocke et TypeObject.
180 // . Pour l'iteration, le type stocke en attribut est sous la forme
181 // "IterationHomard" + le nom de l'iteration precedente.
182 // Il faut donc regarder si la chaine commence par TypeObject.
183 // . Idem pour le fichier de messages : "Mess " + numero d'iteration
184 //
185 // On filtre ce genre de situation avec option :
186 //  -1 : comparaison totale
187 //   0 : comparaison sur le debut de la chaine
188 //================================================================
189 bool HOMARD_UTILS::isObject(_PTR(SObject) MonObj, QString TypeObject, int option)
190 {
191 //   MESSAGE("isObject, TypeObject = "<<TypeObject.toStdString().c_str()<<", option = "<<option);
192 // Existence d'un attribut ?
193   _PTR(GenericAttribute) anAttr;
194   if ( !MonObj->FindAttribute(anAttr, "AttributeComment") ) return false;
195 // Quel type ?
196   _PTR(AttributeComment) aComment (anAttr);
197   QString Type = QString(aComment->Value().c_str());
198 //   MESSAGE("Type = "<<Type.toStdString().c_str());
199 // Est-ce le bon ?
200   bool bOK = false ;
201   if ( option == 0 )
202   {
203     int position = Type.lastIndexOf(TypeObject);
204     if ( position == 0 ) { bOK = true ; }
205   }
206   else
207   {
208     if ( Type == TypeObject ) { bOK = true ; }
209   }
210   return bOK ;
211 }
212 //================================================================
213 // Retourne vrai si l objet est une frontiere analytique
214 //================================================================
215 bool HOMARD_UTILS::isBoundaryAn(_PTR(SObject) MonObj)
216 {
217    return isObject( MonObj, QString("BoundaryAnHomard"), -1 ) ;
218 }
219 //================================================================
220 // Retourne vrai si l objet est une frontiere discrete
221 //================================================================
222 bool HOMARD_UTILS::isBoundaryDi(_PTR(SObject) MonObj)
223 {
224    return isObject( MonObj, QString("BoundaryDiHomard"), -1 ) ;
225 }
226 //================================================================
227 // Retourne vrai si l objet est une zone
228 //================================================================
229 bool HOMARD_UTILS::isZone(_PTR(SObject) MonObj)
230 {
231    return isObject( MonObj, QString("ZoneHomard"), -1 ) ;
232 }
233 //================================================================
234 // Retourne vrai si l objet est une Hypothese
235 //================================================================
236 bool HOMARD_UTILS::isHypo(_PTR(SObject) MonObj)
237 {
238    return isObject( MonObj, QString("HypoHomard"), -1 ) ;
239 }
240 //================================================================
241 // Retourne vrai si l objet est une iteration
242 //================================================================
243 bool HOMARD_UTILS::isIter(_PTR(SObject) MonObj)
244 {
245    return isObject( MonObj, QString("IterationHomard"), 0 ) ;
246 }
247 //================================================================
248 // Retourne vrai si l objet est un cas
249 //================================================================
250 bool HOMARD_UTILS::isCase(_PTR(SObject) MonObj)
251 {
252    return isObject( MonObj, QString("CasHomard"), -1 ) ;
253 }
254 //================================================================
255 // Retourne vrai si l objet est un fichier de messages
256 //================================================================
257 bool HOMARD_UTILS::isFilelog(_PTR(SObject) MonObj)
258 {
259    return isObject( MonObj, QString("log"), 0 ) ;
260 }
261 //================================================================
262 // Retourne vrai si l objet est un fichier de bilan
263 //================================================================
264 bool HOMARD_UTILS::isFileSummary(_PTR(SObject) MonObj)
265 {
266    return isObject( MonObj, QString("Summary"), 0 ) ;
267 }
268
269 //=========================================================================================================
270 void HOMARD_UTILS::PushOnHelp(QString monFichierAide)
271 {
272      LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
273      if (app) {
274         HOMARDGUI* aHomardGUI = dynamic_cast<HOMARDGUI*>( app->module( "Homard" ) );
275         app->onHelpContextModule(aHomardGUI ? app->moduleName(aHomardGUI->moduleName()) : QString(""), monFichierAide);
276      }
277
278 }
279 //=========================================================================================================