Salome HOME
version 5_1_4 HOMARD_SRC
[modules/homard.git] / src / HOMARDGUI / HomardQtCommun.cxx
1 #include "HomardQtCommun.h"
2 #include "HOMARDGUI_Utils.h"
3
4 #include <utilities.h>
5
6 #include <qmessagebox.h>
7 #include <qcombobox.h>
8 #include <qfiledialog.h>
9 #include <qstring.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/stat.h>
13
14
15 #include "SalomeApp_Tools.h"
16
17 using namespace std;
18 extern "C" 
19 {
20 #include <med.h>
21 }
22
23
24 // ============================================================================
25 QString HOMARD_QT_COMMUN::SelectionArbreEtude(QString commentaire, int grave )
26 // ============================================================================
27 // Regarde si l'objet selectionne correspond a un objet de tyoe
28 // commentaire. si c'est le cas, retourne le nom de  cet objet,
29 // sinon retourne une QString("")
30 // Si grave = 0, ce n'est pas grave de ne rien trouver et pas de message
31 // Si grave = 1, ce n'est pas grave de ne rien trouver mais on emet un message
32 {
33    int nbSel = HOMARD_UTILS::IObjectCount() ;
34    if ( nbSel == 0 )
35    {
36       if ( grave == 1 )
37       {
38         QMessageBox::information( 0, "Bad selection",
39                                   QString("Select an object."),
40         QMessageBox::Ok + QMessageBox::Default );
41       }
42       return QString("");
43     }
44    if ( nbSel > 1 )
45    {
46       QMessageBox::information( 0, "Bad selection",
47                                 QString("Select one object only."),
48       QMessageBox::Ok + QMessageBox::Default );
49       return QString("");
50     }
51
52     Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
53     if ( aIO->hasEntry() )
54     {
55       _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
56       _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
57       _PTR(GenericAttribute) anAttr;
58       if (aSO->FindAttribute(anAttr, "AttributeComment") )
59       {
60            _PTR(AttributeComment) attributComment = anAttr;
61            QString aComment= QString(attributComment->Value().data());
62            int iteration = aComment.lastIndexOf(commentaire);
63            if ( iteration !=0  )
64            {
65                   QString message=QString("Select an object : ");
66                   message += commentaire;
67                   QMessageBox::information( 0, "Bad selection", message,
68                   QMessageBox::Ok + QMessageBox::Default );
69                   return QString("");
70            }
71            if (aSO->FindAttribute(anAttr, "AttributeName") )
72            {
73                 _PTR(AttributeName) attributName = anAttr;
74                  QString aName= QString(attributName->Value().data());
75                  return aName;
76            }
77       }
78     }
79     return QString("");
80 }
81
82 // =======================================================================
83 QString HOMARD_QT_COMMUN::SelectionCasEtude()
84 // =======================================================================
85 {
86    QString aName    = QString("");
87    int nbSel = HOMARD_UTILS::IObjectCount() ;
88    if ((nbSel > 1) or ( nbSel == 0 )) 
89    {
90       QMessageBox::information( 0, "Bad selection",
91                                 QString("Select one object only"),
92                                 QMessageBox::Ok + QMessageBox::Default );
93       return QString("");
94     }
95     Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
96     if ( aIO->hasEntry() )
97     {
98          _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
99          _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
100          _PTR(SObject) aSObjCas = aSO->GetFather();
101          _PTR(GenericAttribute) anAttr;
102           if (aSObjCas->FindAttribute(anAttr, "AttributeName") )
103           {
104              _PTR(AttributeName) attributName = anAttr;
105              aName= QString(attributName->Value().data());
106           }
107           return aName;
108       }
109       return QString("");
110 }
111
112 // =======================================================================
113 QString HOMARD_QT_COMMUN::PushNomFichier(bool avertir)
114 // =======================================================================
115 // Gestion les boutons qui permettent  de 
116 // 1) retourne le nom d'un fichier par une fenetre de dialogue si aucun
117 //    objet est selectionne dans l arbre d etude
118 // 2) retourne le nom du fichier asocie a l objet 
119 //    selectionne dans l arbre d etude
120 {
121    MESSAGE("HOMARD_QT_COMMUN::PushNomFichier");
122    QString aFile=QString::null;
123    int nbSel = HOMARD_UTILS::IObjectCount() ;
124    if ( nbSel == 0 )
125    {
126       aFile = QFileDialog::getOpenFileName(0,QString("File Selection"),QString("") ,QString("Med files (*.med);;all (*) ") );
127    }
128    if (nbSel > 1)
129    {
130       QMessageBox::information( 0, "Bad selection",
131                                 QString("Select one file only"),
132                                 QMessageBox::Ok + QMessageBox::Default );
133    }
134    if (nbSel == 1)
135    {
136       Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
137       if ( aIO->hasEntry() )
138       {
139          _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
140          _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
141          _PTR(GenericAttribute) anAttr;
142          _PTR(AttributeFileType) aFileType;
143          _PTR(AttributeExternalFileDef) aFileName;
144          if (aSO) {
145             if (aSO->FindAttribute(anAttr, "AttributeFileType") ) {
146                aFileType=anAttr;
147                QString fileType=QString(aFileType->Value().data());
148                if ( fileType==QString("FICHIERMED")) {
149                    if (aSO->FindAttribute(anAttr,"AttributeExternalFileDef")) {
150                        aFileName=anAttr;
151                       aFile= QString(aFileName->Value().data()); } } } } }
152         
153       if ( aFile==QString::null )
154       {
155          if (avertir ) { 
156          QMessageBox::information( 0, "Bad selection",
157                 QString(" Select a study object with associated MED file \n or a MED file"),
158                 QMessageBox::Ok + QMessageBox::Default );
159           }
160          aFile = QFileDialog::getOpenFileName();
161          if (!aFile.isEmpty())
162          {
163              aFile=aFile;
164          }
165       }
166    }
167    return aFile;
168
169 }
170
171
172 // =======================================================================
173 int HOMARD_QT_COMMUN::OuvrirFichier(QString aFile)
174 // =======================================================================
175 // renvoie le medId associe au fichier Med apres ouverture
176 {
177    int MedIdt = MEDouvrir(const_cast<char *>(aFile.toStdString().c_str()),MED_LECTURE);
178    if (MedIdt <0)
179    {
180       QMessageBox::information( 0, "Bad selection",
181       QString("MED File is unreadable"),
182       QMessageBox::Ok + QMessageBox::Default );
183    }
184    return MedIdt;
185 }
186
187 // ======================================================
188 QString HOMARD_QT_COMMUN::LireNomMaillage(QString aFile)
189 // ========================================================
190 {
191     
192     int MedIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
193     int numberOfMeshes = MEDnMaa(MedIdt) ;
194     if (numberOfMeshes != 1 )
195     {
196       QMessageBox::information( 0, "Bad selection",
197       QString("MED File contains more than one mesh"),
198       QMessageBox::Ok + QMessageBox::Default );
199     }
200
201     QString nomMaillage= HOMARD_QT_COMMUN::LireNomMaillage(MedIdt,1);
202     MEDfermer(MedIdt);
203     return nomMaillage;
204 }
205 // =======================================================================
206 QString HOMARD_QT_COMMUN::LireNomMaillage(int MedIdt ,int MeshId)
207 // =======================================================================
208 {
209      QString NomMaillage=QString::null;
210      char maa[MED_TAILLE_NOM+1];
211      char desc[MED_TAILLE_DESC+1];
212      med_int mdim;
213      med_maillage type;
214
215      if ( MEDmaaInfo(MedIdt,MeshId,maa,&mdim,&type,desc) < 0 )
216      {
217           QMessageBox::information( 0, "Bad selection",
218           QString("Error : Mesh is unreadable"),
219           QMessageBox::Ok + QMessageBox::Default );
220      }
221      else
222      {
223         NomMaillage=QString(maa);  
224      }
225      return NomMaillage;
226 }
227
228
229 // =======================================================================
230 std::list<QString> HOMARD_QT_COMMUN::GetListeChamps(QString aFile)
231 // =======================================================================
232 {
233 // Il faut voir si plusieurs maillages
234
235    MESSAGE("HOMARD_QT_COMMUN::GetListeChamps");
236    std::list<QString> ListeChamp;
237
238    char *comp, *unit;
239    char nomcha  [MED_TAILLE_NOM+1];
240    med_type_champ typcha;
241    med_int ncomp;
242
243    SCRUTE(aFile.toStdString());
244    int MedIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
245    if ( MedIdt < 0 ) { return ListeChamp; }
246    
247    // Le fichier Med est lisible
248    // Lecture du maillage
249
250    // Lecture du nombre de champs
251    med_int ncha = MEDnChamp(MedIdt, 0) ;
252    if (ncha < 1 )
253    {
254           QMessageBox::information( 0, "Bad selection",
255                       QString(" Error : Fields are unreadable"),
256                       QMessageBox::Ok + QMessageBox::Default );
257           MEDfermer(MedIdt);
258           return ListeChamp;
259    }
260
261    for (int i=0; i< ncha; i++)
262    {
263        if ((ncomp = MEDnChamp(MedIdt,i+1)) < 0)  
264        {
265             QMessageBox::information( 0, "Bad selection",
266                       QString("Error : field's componants are unreadable"),
267                       QMessageBox::Ok + QMessageBox::Default );
268             MEDfermer(MedIdt);
269             return ListeChamp;
270         }
271
272    /* Lecture du type du champ, des noms des composantes et du nom de l'unite*/
273         comp = (char*) malloc(ncomp*MED_TAILLE_PNOM+1);
274         unit = (char*) malloc(ncomp*MED_TAILLE_PNOM+1);
275         if ( MEDchampInfo(MedIdt,i+1,nomcha,&typcha,comp,unit,ncomp) < 0 ) 
276         {
277              QMessageBox::information( 0, "Bad selection",
278                        QString(" Error : Fields are unreadable"),
279                        QMessageBox::Ok + QMessageBox::Default );
280             MEDfermer(MedIdt);
281             return ListeChamp;
282          }
283
284       ListeChamp.push_back(QString(nomcha));
285       free(comp);
286       free(unit);
287    }
288    MEDfermer(MedIdt);
289    return ListeChamp;
290 }
291
292 // ======================================================================================
293 std::list<QString> HOMARD_QT_COMMUN::GetListeComposants(QString aFile, QString aChamp)
294 // ======================================================================================
295 {
296    MESSAGE ( "GetListeComposants pour le fichier " << aFile.toStdString().c_str());
297    MESSAGE ( "GetListeComposants pour le champ " << aChamp.toStdString().c_str());
298
299    std::list<QString> ListeComposants;
300
301    char *comp, *unit;
302    char nomcha  [MED_TAILLE_NOM+1];
303    med_type_champ typcha;
304    med_int ncomp;
305
306    int MedIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
307    if ( MedIdt < 0 ) { return ListeComposants; }
308    
309
310    // Lecture du nombre de champs
311    med_int ncha = MEDnChamp(MedIdt, 0) ;
312    if (ncha < 1 )
313    {
314           QMessageBox::information( 0, "Bad selection",
315                        QString(" Error : Fields are unreadable"),
316                        QMessageBox::Ok + QMessageBox::Default );
317           MEDfermer(MedIdt);
318           return ListeComposants;
319    }
320
321    for (int i=0; i< ncha; i++)
322    {
323      if ((ncomp = MEDnChamp(MedIdt,i+1)) < 0)  
324      {
325           QMessageBox::information( 0, "Bad selection",
326                       QString("Error : field's componants are unreadable"),
327                       QMessageBox::Ok + QMessageBox::Default );
328           MEDfermer(MedIdt);
329           return ListeComposants;
330       }
331
332    /* Lecture du type du champ, des noms des composantes et du nom de l'unite*/
333       comp = (char*) malloc(ncomp*MED_TAILLE_PNOM+1);
334       unit = (char*) malloc(ncomp*MED_TAILLE_PNOM+1);
335
336       if ( MEDchampInfo(MedIdt,i+1,nomcha,&typcha,comp,unit,ncomp) < 0 ) 
337       {
338           QMessageBox::information( 0, "Bad selection",
339                        QString(" Error : Fields are unreadable"),
340                        QMessageBox::Ok + QMessageBox::Default );
341            MEDfermer(MedIdt);
342            return ListeComposants;
343        }
344        if ( QString(nomcha) != aChamp ) {
345           free(comp);
346           free (unit);
347           continue;}
348
349        for (int j = 0; j <ncomp; j++) 
350        { 
351              char cible[MED_TAILLE_PNOM +1];
352              strncpy(cible,comp+j*MED_TAILLE_PNOM,MED_TAILLE_PNOM );
353              cible[MED_TAILLE_PNOM ]='\0';
354              ListeComposants.push_back(QString(cible));
355        }
356        break;
357    }
358    free(comp);
359    free(unit);
360    MEDfermer(MedIdt);
361    return ListeComposants;
362 }
363
364 // =======================================================================
365 void HOMARD_QT_COMMUN::Recopie(QString aDir, QString aFichier)
366 // =======================================================================
367 {
368    if (chdir(aDir.toLatin1()) != 0)
369    {
370         if (mkdir(aDir.toLatin1(), S_IRWXU|S_IRGRP|S_IXGRP) != 0)
371         {
372           QMessageBox::information( 0, "ERROR",
373                     QString("Unable to create directory."),
374                     QMessageBox::Ok + QMessageBox::Default );
375                     return;
376           //ASSERT("Pb a la creation de la directory" == 0);
377         }
378         if (chdir(aDir.toLatin1()) != 0)
379         {
380           QMessageBox::information( 0, "ERROR",
381                     QString("Unable to access directory."),
382                     QMessageBox::Ok + QMessageBox::Default );
383                     return;
384           //ASSERT("Pb pour acceder la directory" == 0);
385         }
386    }
387    QString copie = QString("cp ")+aFichier+QString(" ")+aDir;
388    system(copie.toLatin1());
389 }
390
391 // =======================================================================
392 void HOMARD_QT_COMMUN::Creetmp()
393 // =======================================================================
394 {
395    QString aDir=QString("/tmp/Homard/");
396    if (chdir(aDir.toLatin1()) != 0)
397    {
398         if (mkdir(aDir.toLatin1(), S_IRWXU|S_IRGRP|S_IXGRP) != 0)
399         {
400           QMessageBox::information( 0, "ERROR",
401                     QString("Unable to create directory /tmp/Homard."),
402                     QMessageBox::Ok + QMessageBox::Default );
403                     return;
404          }
405         if (chdir(aDir.toLatin1()) != 0)
406         {
407           QMessageBox::information( 0, "ERROR",
408                     QString("Unable to access directory /tmp/Homard."),
409                     QMessageBox::Ok + QMessageBox::Default );
410                     return;
411          }
412    }
413 }