1 # -*- coding: utf-8 -*-
2 # ======================================================================
3 # COPYRIGHT (C) 1991 - 2002 EDF R&D WWW.CODE-ASTER.ORG
4 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
5 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
6 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
7 # (AT YOUR OPTION) ANY LATER VERSION.
9 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
10 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
11 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
12 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
14 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
15 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
16 # 1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
19 # ======================================================================
25 from PyQt4.QtGui import *
26 from PyQt4.QtCore import *
30 import convert,generator
31 from Editeur import session
32 from Editeur import comploader
33 from Editeur import Objecttreeitem
39 VERSION_EFICAS = "EFICAS v1.16"
42 class JDCEditor(QSplitter):
43 # -------------------------- #
48 def __init__ (self,appli,fichier = None, jdc = None, QWParent=None, units = None, include=0 , vm=None):
49 #----------------------------------------------------------------------------------------------------------#
51 #print "fichier", fichier,"jdc",jdc,"units",units,"include",include
52 QSplitter.__init__(self, QWParent)
53 self.appliEficas = appli
54 self.appli = appli #---- attendu par IHM
56 self.fichier = fichier
58 self.QWParent = QWParent
61 VERSION_CODE = session.d_env.cata
63 self.salome = self.appliEficas.salome
64 self.format = self.appliEficas.format_fichier
67 print "dans JDC pas d appli ????????"
69 self.code = self.appliEficas.CONFIGURATION.code
70 self.version_code = VERSION_CODE
71 self.titre=VERSION_EFICAS + ' pour '+ self.code
74 self.liste_simp_reel=[]
78 nameConf='configuration_'+prefs.code
79 configuration=__import__(nameConf)
80 self.CONFIGURATION = self.appliEficas.CONFIGURATION
81 self.CONFIGStyle = self.appliEficas.CONFIGStyle
84 if hasattr(self.appliEficas,"statusBar"):
85 self.sb = self.appliEficas.statusBar()
91 self.isReadOnly = False
93 self.node_selected = None
95 if not hasattr( readercata, 'reader' ) :
96 readercata.reader = readercata.READERCATA( self, self.appliEficas )
97 self.readercata = readercata.reader
98 self.Commandes_Ordre_Catalogue =self.readercata.Commandes_Ordre_Catalogue
100 #------- construction du jdc --------------
103 self.mode_nouv_commande=self.readercata.mode_nouv_commande
106 if self.fichier is not None: # fichier jdc fourni
107 self.fileInfo = QFileInfo(self.fichier)
108 self.fileInfo.setCaching(0)
110 self.jdc = self.readFile(self.fichier)
113 if units is not None:
114 self.jdc.recorded_units=units
115 self.jdc.old_recorded_units=units
117 if not self.jdc: # nouveau jdc
119 self.jdc = self._newJDC(units=units)
121 self.jdc = self._newJDCInclude(units=units)
125 self.jdc.appli = self
129 txt_exception = self.jdc.cr.get_mess_exception()
132 qApp.restoreOverrideCursor()
133 self.affiche_infos("Erreur fatale au chargement de %s" %fichier)
134 QMessageBox.critical( self, "Erreur fatale au chargement d'un fichier", txt_exception)
136 comploader.charger_composants("QT")
137 jdc_item=Objecttreeitem.make_objecttreeitem( self, "nom", self.jdc )
139 if (not self.jdc.isvalid()) and (not nouveau) :
140 self.viewJdcRapport()
142 self.tree = browser.JDCTree( jdc_item, self )
144 #--------------------------------#
145 def _newJDC( self ,units = None):
146 #--------------------------------#
148 Initialise un nouveau JDC vierge
150 CONTEXT.unset_current_step()
151 jdc=self.readercata.cata[0].JdC( procedure="",
153 cata=self.readercata.cata,
154 cata_ord_dico=self.readercata.cata_ordonne_dico,
155 rep_mat=self.CONFIGURATION.rep_mat
157 if units is not None:
158 jdc.recorded_units=units
159 jdc.old_recorded_units=units
163 #--------------------------------#
164 def _newJDCInclude( self ,units = None):
165 #--------------------------------#
167 Initialise un nouveau JDC vierge
169 import Extensions.jdc_include
170 JdC_aux=Extensions.jdc_include.JdC_include
171 CONTEXT.unset_current_step()
173 jaux=self.readercata.cata[0].JdC( procedure="",
175 cata=self.readercata.cata,
176 cata_ord_dico=self.readercata.cata_ordonne_dico,
177 rep_mat=self.CONFIGURATION.rep_mat,
181 J=JdC_aux( procedure="",
183 cata=self.readercata.cata,
184 cata_ord_dico=self.readercata.cata_ordonne_dico,
186 rep_mat=self.CONFIGURATION.rep_mat,
189 if units is not None:
190 J.recorded_units=units
191 J.old_recorded_units=units
194 #-----------------------#
195 def readFile(self, fn):
196 #--------------------------------#
198 Public slot to read the text from a file.
199 @param fn filename to read from (string or QString)
203 # ------------------------------------------------------------------------------------
205 # ------------------------------------------------------------------------------------
207 jdcName=os.path.basename(fn)
208 # Il faut convertir le contenu du fichier en fonction du format
209 if convert.plugins.has_key( self.appliEficas.format_fichier ):
210 # Le convertisseur existe on l'utilise
212 p=convert.plugins[self.appliEficas.format_fichier]()
214 text=p.convert('exec',appli)
215 if not p.cr.estvide():
216 self.affiche_infos("Erreur à la conversion")
218 self.affiche_infos("Type de fichier non reconnu")
219 QMessageBox.critical( self, "Type de fichier non reconnu","EFICAS ne sait pas ouvrir ce type de fichier")
222 CONTEXT.unset_current_step()
223 jdc=self.readercata.cata[0].JdC(procedure=text,
225 cata=self.readercata.cata,
226 cata_ord_dico=self.readercata.cata_ordonne_dico,
228 rep_mat=self.CONFIGURATION.rep_mat
230 # ----------------------------------------------------
232 # ----------------------------------------------------
233 self.modified = False
235 # qApp.restoreOverrideCursor()
236 if self.fileInfo!= None :
237 self.lastModified = self.fileInfo.lastModified()
239 self.lastModified = 1
243 #-----------------------#
244 def get_source(self,file):
245 #-----------------------#
246 format=self.appliEficas.format_fichier
248 # Il faut convertir le contenu du fichier en fonction du format
249 if convert.plugins.has_key(format):
250 # Le convertisseur existe on l'utilise
251 p=convert.plugins[format]()
253 text=p.convert('execnoparseur')
254 if not p.cr.estvide():
255 self.affiche_infos("Erreur a la conversion")
258 # Il n'existe pas c'est une erreur
259 self.affiche_infos("Type de fichier non reconnu")
260 QMessageBox.critical( self, "Type de fichier non reconnu","EFICAS ne sait pas ouvrir ce type de fichier")
263 #----------------------------------------------#
264 def _viewText(self, txt, caption = "FILE_VIEWER"):
265 #----------------------------------------------#
266 w = qtCommun.ViewText( self.QWParent )
267 w.setWindowTitle( caption )
271 #-----------------------#
272 def viewJdcSource(self):
273 #-----------------------#
274 format = self.appliEficas.format_fichier
275 f=open(self.fichier,'r')
278 self._viewText(texteSource, "JDC_SOURCE")
280 #-----------------------#
282 #-----------------------#
283 format = self.appliEficas.format_fichier
284 strSource = str( self.get_text_JDC(format) )
285 self._viewText(strSource, "JDC_RESULTAT")
287 #-----------------------#
288 def viewJdcRapport(self):
289 #-----------------------#
290 strRapport = str( self.jdc.report() )
291 self._viewText(strRapport, "JDC_RAPPORT")
297 Public method called by the viewmanager to finally get rid of us.
303 #------------------------------#
304 def affiche_infos(self,message):
305 #------------------------------#
306 #PN --> devenu inutile avec QT4
308 # if not hasattr(self.appliEficas,'MessageLabel') :
309 # self.appliEficas.leLayout=QDockWidget(self.appliEficas)
310 # self.appliEficas.MessageLabel = QLabel("MessageLabel",self.appliEficas.leLayout)
311 # self.appliEficas.MessageLabel.setAlignment(Qt.AlignBottom)
312 # self.appliEficas.leLayout.setAllowedAreas(Qt.BottomDockWidgetArea)
313 # self.appliEficas.leLayout.setWidget(self.appliEficas.MessageLabel)
314 # #self.appliEficas.moveDockWindow(self.appliEficas.leLayout,Qt.DockBottom)
315 # self.appliEficas.MessageLabel.setText(message)
316 # self.appliEficas.MessageLabel.show()
317 # self.appliEficas.leLayout.show()
319 self.sb.showMessage(message)#,2000)
321 #------------------------------#
322 def affiche_alerte(self,titre,message):
323 #------------------------------#
324 # appele par I_MACRO_ETAPE
325 QMessageBox.information( self, titre, message)
327 #-------------------#
328 def init_modif(self):
329 #-------------------#
331 Met l'attribut modified a 'o' : utilise par Eficas pour savoir
332 si un JDC doit etre sauvegarde avant destruction ou non
336 #---------------------------------------#
337 def chercheNoeudSelectionne(self,copie=1):
338 #---------------------------------------#
340 appele par Cut et Copy pour positionner self.node_selected
342 self.node_selected=None
343 if len(self.tree.selectedItems()) == 0 : return
344 if len(self.tree.selectedItems()) != 1 :
345 QMessageBox.information( self,
347 "Cette version d'EFICAS permet uniquement la copie d un seul objet")
349 self.node_selected=self.tree.selectedItems()[0]
350 if copie == 0 : return
351 if not self.node_selected.item.iscopiable():
352 QMessageBox.information( self,
354 "Cette version d'EFICAS ne permet pas la copie de cet Objet")
355 self.node_selected=None
359 #---------------------#
360 def handleEditCut(self):
361 #---------------------#
363 Stocke dans Eficas.noeud_a_editer le noeud à couper
365 self.chercheNoeudSelectionne()
366 self.QWParent.edit="couper"
367 self.QWParent.noeud_a_editer = self.node_selected
369 #-----------------------#
370 def handleEditCopy(self):
371 #-----------------------#
373 Stocke dans Eficas.noeud_a_editer le noeud a copier
375 self.chercheNoeudSelectionne()
376 self.QWParent.edit="copier"
377 self.QWParent.noeud_a_editer = self.node_selected
379 #------------------------#
380 def handleEditPaste(self):
381 #------------------------#
383 Lance la copie de l'objet place dans self.QWParent.noeud_a_editer
384 Ne permet que la copie d'objets de type Commande ou MCF
386 self.chercheNoeudSelectionne()
387 index_noeud_a_couper=self.QWParent.noeud_a_editer.treeParent.children.index(self.QWParent.noeud_a_editer)
388 if self.QWParent.noeud_a_editer == None :
389 QMessageBox.information( self,
391 "Aucun Objet n a ete copie ou colle ")
394 child=self.QWParent.noeud_a_editer.doPaste(self.node_selected)
396 traceback.print_exc()
397 QMessageBox.information( self,
399 "L'action de coller apres un tel objet n'est pas permise")
404 if self.message != '':
405 QMessageBox.critical( self, "Copie refusee", self.message)
407 self.affiche_infos("Copie refusée")
410 # il faut declarer le JDCDisplay_courant modifie
412 # suppression eventuelle du noeud selectionne
413 # si possible on renomme l objet comme le noeud couper
415 if self.QWParent.edit == "couper":
416 print self.QWParent.noeud_a_editer.child
417 index_ajoute=child.treeParent.children.index(child)
418 if index_ajoute <= index_noeud_a_couper :
419 index_noeud_a_couper=index_noeud_a_couper + 1
420 item=self.QWParent.noeud_a_editer.item
421 noeud_a_supprimer=self.QWParent.noeud_a_editer.treeParent.children[index_noeud_a_couper]
422 noeud_a_supprimer.delete()
423 child.item.update(item)
424 #test,mess = child.item.nomme_sd(nom)
427 # on rend la copie a nouveau possible en liberant le flag edit
428 self.QWParent.edit="copier"
430 #---------------------#
431 def getFileName(self):
432 #---------------------#
435 #---------------------------#
436 def get_file_variable(self) :
437 #---------------------------#
438 titre = "Choix d'un fichier XML"
439 texte = "Le fichier contient une commande INCLUDE\n"
440 texte = texte+'Donnez le nom du fichier XML qui contient la description des variables'
441 QMessageBox.information( self, titre,texte)
443 fichier = QFileDialog.getOpenFileName(self.appliEficas,
444 self.appliEficas.trUtf8('Ouvrir Fichier'),
445 self.appliEficas.CONFIGURATION.savedir,
446 self.appliEficas.trUtf8('Wrapper Files (*.xml);;''All Files (*)'))
450 #----------------------------------#
451 def writeFile(self, fn, txt = None):
452 #----------------------------------#
454 Public slot to write the text to a file.
456 @param fn filename to write to (string or QString)
457 @return flag indicating success
463 txt = self.get_text_JDC(self.appliEficas.format_fichier)
465 if len(txt) >= len(eol):
466 if txt[-len(eol):] != eol:
476 QMessageBox.critical(self, self.trUtf8('Save File'),
477 self.trUtf8('The file <b>%1</b> could not be saved.<br>Reason: %2')
478 .arg(unicode(fn)).arg(str(why)))
481 #-----------------------------#
482 def get_text_JDC(self,format):
483 #-----------------------------#
484 if generator.plugins.has_key(format):
485 # Le generateur existe on l'utilise
486 self.generator=generator.plugins[format]()
487 jdc_formate=self.generator.gener(self.jdc,format='beautifie')
488 if not self.generator.cr.estvide():
489 self.affiche_infos("Erreur à la generation")
490 QMessageBox.critical( self, "Erreur a la generation","EFICAS ne sait pas convertir ce JDC")
495 # Il n'existe pas c'est une erreur
496 self.affiche_infos("Format %s non reconnu" % format)
497 QMessageBox.critical( self, "Format "+format+" non reconnu","EFICAS ne sait pas convertir le JDC selon le format "+format)
501 #-----------------------------------------#
502 def cherche_Groupes(self):
503 liste=self.get_text_JDC("GroupMA")
505 #-----------------------------------------#
507 #-----------------------------------------#
508 def saveFile(self, path = None, saveas= 0):
509 #-----------------------------------------#
511 Public slot to save the text to a file.
513 @param path directory to save the file in (string or QString)
514 @return tuple of two values (boolean, string) giving a success indicator and
515 the name of the saved file
518 if not self.modified and not saveas:
519 return (0, None) # do nothing if text wasn't changed
522 if self.fichier is None or saveas:
524 path=self.CONFIGURATION.savedir
525 selectedFilter = QString('')
526 fn = QFileDialog.getSaveFileName( self,
527 self.trUtf8("sauvegarde"), path,
528 self.trUtf8("JDC (*.comm);;" "All Files (*)"),None,
529 QFileDialog.DontConfirmOverwrite)
530 if fn.isNull(): return (0, None)
532 ext = QFileInfo(fn).suffix()
533 if ext.isEmpty(): fn.append(".comm")
535 if QFileInfo(fn).exists():
536 abort = QMessageBox.warning(self,
537 self.trUtf8("Sauvegarde du Fichier"),
538 self.trUtf8("Le fichier <b>%1</b> existe deja.").arg(fn),
539 self.trUtf8("&Ecraser"),
540 self.trUtf8("&Abandonner"))
541 if abort == 1 : return (0, None)
543 fn = unicode(QDir.convertSeparators(fn))
549 if self.writeFile(fn):
551 self.modified = False
552 if self.fileInfo is None or saveas:
553 self.fileInfo = QFileInfo(self.fichier)
554 self.fileInfo.setCaching(0)
555 self.lastModified = self.fileInfo.lastModified()
556 if newName is not None:
557 self.appliEficas.addToRecentList(newName)
558 self.tree.racine.item.getObject().nom=os.path.basename(newName)
559 self.tree.racine.update_node_label()
563 fileXML = fn[:fn.rfind(".")] + '.xml'
564 self.generator.writeOpenturnsXML( fileXML )
571 fileSTD = fn[:fn.rfind(".")] + '.py'
572 self.generator.writeOpenturnsSTD( fileSTD )
578 self.generator.writeCuve2DG()
586 self.tubePy=self.generator.getTubePy()
587 fileTube = '/tmp/tube.py'
588 if self.tubePy != '' :
597 self.appliEficas.addJdcInSalome( self.fichier)
598 # if self.code == 'ASTER':
599 # self.QWParent.appli.createOrUpdateMesh(self)
602 return (1, self.fichier)
606 #---------------------------------#
607 def saveFileAs(self, path = None):
608 #---------------------------------#
610 Public slot to save a file with a new name.
612 @param path directory to save the file in (string or QString)
613 @return tuple of two values (boolean, string) giving a success indicator and
614 the name of the saved file
616 return self.saveFile(path,1)
620 #---------------------------------------------#
621 def get_file(self,unite=None,fic_origine = ''):
622 #---------------------------------------------#
630 titre = "Choix unite %d " %unite
631 texte = "Le fichier %s contient une commande INCLUDE \n" % fic_origine
632 texte = texte+'Donnez le nom du fichier correspondant\n à l unité logique %d' % unite
633 labeltexte = 'Fichier pour unite %d :' % unite
635 titre = "Choix d'un fichier de poursuite"
636 texte = "Le fichier %s contient une commande %s\n" %(fic_origine,'POURSUITE')
637 texte = texte+'Donnez le nom du fichier dont vous \n voulez faire une poursuite'
639 QMessageBox.information( self, titre,texte)
640 path=self.CONFIGURATION.savedir
641 fn = QFileDialog.getOpenFileName( self, titre,path)
644 # ce retour est impose par le get_file d'I_JDC
647 ulfile = os.path.abspath(unicode(fn))
648 # On utilise le convertisseur défini par format_fichier
649 source=self.get_source(ulfile)
651 # On a réussi à convertir le fichier self.ulfile
654 # Une erreur a été rencontrée
656 return ulfile, jdcText
659 if __name__=='__main__':
660 import prefs # dans main
661 name='prefs_'+prefs.code
662 prefsCode=__import__(name)
664 if hasattr(prefsCode,'encoding'):
665 # Hack pour changer le codage par defaut des strings
668 sys.setdefaultencoding(prefs.encoding)
669 del sys.setdefaultencoding
672 # #CS_pbruno note: fait implicitement des trucs ces imports (grr)
677 # # Analyse des arguments de la ligne de commande
678 # options=session.parse(sys.argv)
681 app = QApplication(sys.argv)
682 mw = JDCEditor(None,'azAster.comm')
683 app.setMainWidget(mw)
684 app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
687 res = app.exec_loop()