Salome HOME
version 5_1_4 HOMARD_SRC
[modules/homard.git] / src / HOMARDGUI / HOMARDGUI_Utils.cxx
1
2 #include "HOMARDGUI_Utils.h"
3
4 #include "OB_Browser.h"
5
6 #include "SUIT_Desktop.h"
7 #include "SUIT_Application.h"
8 #include "SUIT_Session.h"
9
10 #include "LightApp_SelectionMgr.h"
11 #include "SalomeApp_Application.h"
12 #include "SalomeApp_Module.h"
13 #include "SalomeApp_Study.h"
14
15 #include "SALOME_ListIO.hxx"
16 #include "SALOME_ListIteratorOfListIO.hxx"
17
18 #include "SALOMEconfig.h"
19 #include "utilities.h"
20 #include <string>
21 #include <HOMARDGUI.h>
22
23
24 #include <qstring.h>
25 #include <qstringlist.h>
26 #include <sys/stat.h>
27 #include <dirent.h>
28
29 SALOME_ListIO HOMARD_UTILS::mySelected;
30
31 //================================================================
32 // Function : GetCStudy
33 // Returne un pointeur sur l'etude courante
34 //================================================================
35  _PTR(Study) HOMARD_UTILS::GetCStudy(const SalomeApp_Study* theStudy)
36 {
37     return theStudy->studyDS();
38 }
39     
40     
41 //================================================================
42 // Function : GetActiveStudy
43 // Returne un pointeur sur l'etude active
44 //================================================================
45 SUIT_Study* HOMARD_UTILS::GetActiveStudy()
46 {
47     SUIT_Application* app = SUIT_Session::session()->activeApplication();
48     if (app)
49       return app->activeStudy();
50     else
51       return NULL;
52 }
53     
54 //================================================================
55 // Function : GetActiveStudyDocument
56 // Returne un pointeur sur l'etude active
57 //================================================================
58 _PTR(Study) HOMARD_UTILS::GetActiveStudyDocument()
59 {
60     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
61     if (aStudy){
62       return aStudy->studyDS();
63     }
64     else
65       return _PTR(Study)();
66 }
67     
68 //================================================================
69 // Function : updateObjBrowser
70 // Purpose  : met a jour l arbre d 'etude pour Homard
71 //================================================================
72 void HOMARD_UTILS::updateObjBrowser()
73 {
74     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
75     if (app) {
76       //  Le nom identifiant doit etre la valeur du parametre
77       // name de la section HOMARD du fichier SalomeApp.xml 
78       CAM_Module* module = app->module("Homard" );
79       SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
80       if ( appMod ) {
81         app->updateObjectBrowser();
82         appMod->updateObjBrowser( true );
83       }
84       else 
85         MESSAGE( "---   HOMARD::updateObjBrowser: appMod = NULL");
86     }
87 }
88
89 //================================================================
90 // Function : selectedIO
91 // Return the list of selected SALOME_InteractiveObject's
92 //================================================================
93 const SALOME_ListIO& HOMARD_UTILS::selectedIO()
94 {
95       SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* > ( SUIT_Session::session()->activeApplication() );
96       LightApp_SelectionMgr* aSelectionMgr = app->selectionMgr();
97       if( aSelectionMgr )
98       {
99               aSelectionMgr->selectedObjects( mySelected );
100               for (SALOME_ListIteratorOfListIO it (mySelected); it.More(); it.Next()) 
101                  SCRUTE(it.Value()->getEntry());
102       };
103       return mySelected;
104                
105 }
106     
107 //================================================================
108 // Function : IObjectCount
109 // Return the number of selected objects
110 //================================================================
111 int HOMARD_UTILS::IObjectCount()
112 {
113       SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
114       LightApp_SelectionMgr* aSelectionMgr = app->selectionMgr();
115       if( aSelectionMgr )
116       {
117               aSelectionMgr->selectedObjects( mySelected );
118               SCRUTE(mySelected.Extent());
119               return mySelected.Extent();
120       }
121       return 0;
122 }
123     
124 //================================================================
125 // Function : firstIObject
126 // Purpose  :  Return the first selected object in the selected object list
127 //================================================================
128 Handle(SALOME_InteractiveObject) HOMARD_UTILS::firstIObject()
129 {
130       const SALOME_ListIO& aList = selectedIO();
131       return aList.Extent() > 0 ? aList.First() : Handle(SALOME_InteractiveObject)();
132 }
133
134 //================================================================
135 // Function : lastIObject
136 // Return the last selected object in the selected object list
137 //================================================================
138 Handle(SALOME_InteractiveObject) HOMARD_UTILS::lastIObject()
139 {
140       const SALOME_ListIO& aList = selectedIO();
141       return aList.Extent() > 0 ? aList.Last() : Handle(SALOME_InteractiveObject)();
142 }
143
144 //================================================================
145 // Function : isXmgrace
146 // Retourne vrai si le fichier associe est de type ASCII
147 //================================================================
148 bool HOMARD_UTILS::isXmgrace(_PTR(SObject) MonObj)
149 {
150    _PTR(GenericAttribute) anAttr;
151    if (!MonObj->FindAttribute(anAttr, "AttributeComment")) return false;
152    _PTR(AttributeComment) aFileComment (anAttr);
153    std::string Type = aFileComment->Value();
154    if (QString(Type.c_str()) == QString("HomardOuputQual")) return true;
155    return false;
156 }
157 //================================================================
158 // Retourne vrai si l objet est du type voulu
159 // . Dans le cas d'un cas, d'une hypothese, d'une zone, on se contente
160 // d'une comparaison simple entre le type stocke et TypeObject.
161 // . Pour l'iteration, le type stocke en attribut est sous la forme
162 // "IterationHomard" + le nom de l'iteration precedente.
163 // Il faut donc regarder si la chaine commence par TypeObject.
164 // . Idem pour le fichier de messages : "Mess " + numero d'iteration
165 //
166 // On filtre ce genre de situation avec option :
167 //  -1 : comparaison totale
168 //   0 : comparaison sur le debut de la chaine
169 //================================================================
170 bool HOMARD_UTILS::isObject(_PTR(SObject) MonObj, QString TypeObject, int option)
171 {
172 //   MESSAGE("isObject, TypeObject = "<<TypeObject.toStdString().c_str()<<", option = "<<option);
173 // Existence d'un attribut ?
174   _PTR(GenericAttribute) anAttr;
175   if ( !MonObj->FindAttribute(anAttr, "AttributeComment") ) return false;
176 // Quel type ?
177   _PTR(AttributeComment) aComment (anAttr);
178   QString Type = QString(aComment->Value().c_str());
179 //   MESSAGE("Type = "<<Type.toStdString().c_str());
180 // Est-ce le bon ?
181   bool bOK = false ;
182   if ( option == 0 )
183   {
184     int position = Type.lastIndexOf(TypeObject);
185     if ( position == 0 ) { bOK = true ; }
186   }
187   else
188   {
189     if ( Type == TypeObject ) { bOK = true ; }
190   }
191   return bOK ;
192 }
193 //================================================================
194 // Retourne vrai si l objet est une frontiere discrete
195 //================================================================
196 bool HOMARD_UTILS::isBoundaryDi(_PTR(SObject) MonObj)
197 {
198    return isObject( MonObj, QString("BoundaryDiHomard"), -1 ) ;
199 }
200 //================================================================
201 // Retourne vrai si l objet est une zone
202 //================================================================
203 bool HOMARD_UTILS::isZone(_PTR(SObject) MonObj)
204 {
205    return isObject( MonObj, QString("ZoneHomard"), -1 ) ;
206 }
207 //================================================================
208 // Retourne vrai si l objet est une Hypothese
209 //================================================================
210 bool HOMARD_UTILS::isHypo(_PTR(SObject) MonObj)
211 {
212    return isObject( MonObj, QString("HypoHomard"), -1 ) ;
213 }
214 //================================================================
215 // Retourne vrai si l objet est une iteration
216 //================================================================
217 bool HOMARD_UTILS::isIter(_PTR(SObject) MonObj)
218 {
219    return isObject( MonObj, QString("IterationHomard"), 0 ) ;
220 }
221 //================================================================
222 // Retourne vrai si l objet est un cas
223 //================================================================
224 bool HOMARD_UTILS::isCase(_PTR(SObject) MonObj)
225 {
226    return isObject( MonObj, QString("CasHomard"), -1 ) ;
227 }
228 //================================================================
229 // Retourne vrai si l objet est un fichier de messages
230 //================================================================
231 bool HOMARD_UTILS::isFileMess(_PTR(SObject) MonObj)
232 {
233    return isObject( MonObj, QString("Mess"), 0 ) ;
234 }
235 //================================================================
236 // Retourne vrai si l objet est un fichier de bilan
237 //================================================================
238 bool HOMARD_UTILS::isFileSummary(_PTR(SObject) MonObj)
239 {
240    return isObject( MonObj, QString("Summary"), 0 ) ;
241 }
242 //================================================================
243 // Function : ChercheFileDansSelection
244 // Retourne Le nom du fichier associe
245 //================================================================
246 QString HOMARD_UTILS::ChercheFileDansSelection()
247 {
248    MESSAGE("ChercheFileDansSelection");
249    int nbSel = HOMARD_UTILS::IObjectCount() ;
250    if (nbSel > 1 or nbSel == 0) return QString::null;
251
252    Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
253    ASSERT(aIO);
254    if (!( aIO->hasEntry())) return QString::null; 
255
256    _PTR(Study) myStudy = HOMARD_UTILS::GetActiveStudyDocument();
257    _PTR(SObject) aSObj ( myStudy->FindObjectID( aIO->getEntry() ) );
258    if (!aSObj) return QString::null;
259
260    _PTR(GenericAttribute) anAttr;
261    if (!aSObj->FindAttribute(anAttr, "AttributeExternalFileDef")) return QString::null;
262    _PTR(AttributeExternalFileDef) aFileName (anAttr);
263    std::string aFile = aFileName->Value();
264    SCRUTE(aFile);
265    return QString(aFile.c_str());
266 }
267
268 //============================================================
269 // Function ChercheDansDir
270 // Cherche tous les fichiers dans la directory qui correspondent 
271 // a la fois a la base et au numero d iteration, numero d iteration +1
272 // exple info.00.bilan
273 // ou qual.tetr.00.dat
274 //================================================================================
275 QStringList HOMARD_UTILS::ChercheDansDir(QString direct, QString base, int numIter)
276 //================================================================================
277 {
278     QStringList liste;
279
280
281     QString strNum = QString("");
282     if (numIter != -1) strNum = HOMARD_UTILS::Transforme(numIter);
283     QString strNum1 = QString("");
284     if (numIter != -1) strNum1 = HOMARD_UTILS::Transforme(numIter+1);
285
286     struct dirent **namelist;
287     int n = scandir(direct.toLatin1(), &namelist, 0, alphasort);
288     while(n--) 
289     {
290        QString fich =QString( namelist[n]->d_name);
291        if (fich.contains(base) > 0) 
292        {
293          if (numIter != -1)
294          {
295            if ((fich.contains(strNum)>0) or (fich.contains(strNum1)>0))
296               liste.append(fich);
297          }
298          else
299          {
300            liste.append(fich);
301          }
302        }
303     }
304     return liste;
305 }
306
307 QString HOMARD_UTILS::Transforme(int num)
308 {
309     std::ostringstream nb ;
310     nb << std::setw(2) << std::setfill('0') << num ;
311     QString retour= QString(nb.str().c_str());
312     return retour;
313
314 }
315
316 //=============================================================================
317 void HOMARD_UTILS::AddNewRoot(int Tag, QString Racine)
318 //=============================================================================
319 {
320      _PTR(Study) myStudy = HOMARD_UTILS::GetActiveStudyDocument();
321      ASSERT(myStudy);
322      _PTR(StudyBuilder) myBuilder( myStudy->NewBuilder() );
323      ASSERT(myBuilder);
324      _PTR(SComponent)  aFather=myStudy->FindComponent("HOMARD");
325      ASSERT(aFather);
326     
327      _PTR(SObject) InfoRoot = myBuilder->NewObjectToTag (aFather, Tag);
328      _PTR(GenericAttribute) anAttr;
329     anAttr = myBuilder->FindOrCreateAttribute(InfoRoot, "AttributeName");
330     _PTR(AttributeName) aName (anAttr);
331     aName->SetValue(Racine.toStdString());
332 }
333 //=============================================================================
334 void HOMARD_UTILS::RangeSousTag(int Tag,QString FileName, int TagFichier, QString Comment,QString FileDir)
335 //=============================================================================
336 {
337     _PTR(SObject) Root;
338      _PTR(Study) myStudy = HOMARD_UTILS::GetActiveStudyDocument();
339      ASSERT(myStudy);
340      _PTR(StudyBuilder) myBuilder( myStudy->NewBuilder() );
341      ASSERT(myBuilder);
342      _PTR(SComponent)  aFather=myStudy->FindComponent("HOMARD");
343      ASSERT(aFather);
344      bool found = aFather->FindSubObject(Tag,Root);
345      ASSERT(found);
346
347      _PTR(SObject) MonInfo = myBuilder->NewObjectToTag (Root, TagFichier);
348      ASSERT(MonInfo);
349      _PTR(GenericAttribute) anAttr;
350      anAttr = myBuilder->FindOrCreateAttribute(MonInfo, "AttributeName");
351     _PTR(AttributeName) aName (anAttr);
352      aName->SetValue(FileName.toStdString());
353      QString FileComplet = FileDir +QString("/") +FileName;
354      anAttr = myBuilder->FindOrCreateAttribute(MonInfo, "AttributeExternalFileDef");
355     _PTR(AttributeExternalFileDef) aFile (anAttr);
356      aFile->SetValue((FileComplet).toStdString());
357      anAttr = myBuilder->FindOrCreateAttribute(MonInfo, "AttributeComment");
358     _PTR(AttributeComment) aComment (anAttr);
359      aComment->SetValue(Comment.toStdString());
360 }
361
362 //=========================================================================================================
363 void HOMARD_UTILS::PushOnHelp(QString monFichierAide)
364 {
365      LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
366      if (app) {
367         HOMARDGUI* aHomardGUI = dynamic_cast<HOMARDGUI*>( app->module( "Homard" ) );
368         app->onHelpContextModule(aHomardGUI ? app->moduleName(aHomardGUI->moduleName()) : QString(""), monFichierAide);
369      }
370
371 }
372 //=========================================================================================================