DMonde.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #from __future__ import unicode_literals
  2. # -*- coding: utf-8 -*-
  3. """Interface principale du programme DMonde
  4. """
  5. import os
  6. from sys import exit, argv
  7. from PyQt4.QtCore import *
  8. from PyQt4.QtGui import *
  9. from lib.EcranChargerPlateau import EcranChargerPlateau
  10. from lib.EcranCreerPlateau import EcranCreerPlateau
  11. from lib.EcranEditionCombattant import EcranEditionCombattant
  12. from lib.EcranFondPlateau import EcranFondPlateau
  13. from lib.Plateau import Plateau
  14. from lib.framePj import FramePj
  15. from lib.outilsSvg import *
  16. from lib.ui.ecran_principal import Ui_principal
  17. from lib.Actions import Ligne
  18. class DMonde(QMainWindow):
  19. """interface comprenant: chat ecrit, fenetre d'infos, lancer de des, echange de fichiers, lancement du chat vocal"""
  20. def __init__(self, parent=None):
  21. """initialisation de la fenetre"""
  22. super (DMonde, self).__init__()
  23. self.plateau = None
  24. self.partie = "partie1"
  25. self.idPlateauEnCours = ""
  26. self.plateauConnecte = False
  27. self._compteurPj = 0
  28. self.createWidgets()
  29. def createWidgets(self):
  30. """construction de l'interface"""
  31. self.ui = Ui_principal()
  32. self.ui.setupUi(self)
  33. self.afficherPanneauxPlateau(False)
  34. self.connect(self.ui.cbt_sauver, SIGNAL("clicked()"), self.sauverPlateau)
  35. self.connect(self.ui.cbt_fermer, SIGNAL("clicked()"), self.fermerPlateau)
  36. self.ui.cbt_vue.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)
  37. self.ui.cp_ongletsListes.setStyleSheet("QTabBar::tab { width: 38px; }")
  38. self.ui.pi_ongletsListes.setStyleSheet("QTabBar::tab { width: 38px; }")
  39. self.majFichierInfosSvg()
  40. self.creerEcranFondPlateau()
  41. self.chargerListePj()
  42. self.connect(self.ui.grp_nouveauPj, SIGNAL("clicked()"), self.nouveauPj)
  43. ## self.showMaximized()
  44. ########## onglet plateau
  45. def creerEcranFondPlateau(self):
  46. ecranFondPlateau = EcranFondPlateau(self)
  47. self.ui.cbt_vue.resetTransform()
  48. self.ui.cbt_vue.setScene(ecranFondPlateau)
  49. def nouveauPlateau(self):
  50. """ouvre la fenetre de creation de plateau"""
  51. if self.plateau != None:
  52. if self.plateau.estCree() == True:
  53. return
  54. self.creationPlateau = EcranCreerPlateau()
  55. idPlateau = str(len(afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))))
  56. plateau = Plateau(self)
  57. plateau.id = idPlateau
  58. self.creationPlateau.afficher(plateau)
  59. r = self.creationPlateau.exec_()
  60. if r == 1:
  61. QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  62. self.plateau = self.creationPlateau.resultat()
  63. self.plateau.creer()
  64. QApplication.restoreOverrideCursor()
  65. # def creerPlateau(self, nom, chapitre, formeCases, largeur, hauteur, couleur, description, presentation):
  66. # """cree le plateau entre en parametre"""
  67. # nouvelId = str(len(afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))))
  68. # QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  69. # self.plateau = Plateau(self)
  70. # self.plateau.creer(nouvelId, nom, chapitre, formeCases, largeur, hauteur, couleur, description, presentation)
  71. # QApplication.restoreOverrideCursor()
  72. # del self.creationPlateau
  73. def afficherEcranChargerPlateau(self):
  74. """ouvre la fenetre de chargement de plateau"""
  75. valide = True
  76. if self.plateau != None:
  77. if self.plateau.estCree() == True:
  78. valide = False
  79. if valide:
  80. self.chargementPlateau = EcranChargerPlateau(self)
  81. self.chargementPlateau.setWindowModality(Qt.ApplicationModal)
  82. self.chargementPlateau.show()
  83. self.chargementPlateau.raise_()
  84. def chargerPlateau(self, nomFichierSvg):
  85. if self.plateau != None:
  86. if self.plateau.estCree() == True:
  87. self.fermerPlateau()
  88. QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  89. self.plateau = chargerUnique("parties\\{}\\svg\\{}.p".format(self.partie, nomFichierSvg))
  90. if self.plateau:
  91. self.plateau.recreer(self)
  92. else:
  93. self.plateau = Plateau(self)
  94. QApplication.restoreOverrideCursor()
  95. def chargerDernierPlateau(self):
  96. """charge le dernier plateau sauvegarde"""
  97. infosSvg = afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))
  98. dernier = None
  99. for id_svg in infosSvg:
  100. if not dernier or infosSvg[id_svg]["dateSvg"] > infosSvg[dernier]["dateSvg"]:
  101. dernier = id_svg
  102. if dernier:
  103. self.chargerPlateau(dernier)
  104. def sauverPlateau(self):
  105. QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  106. if self.plateau.id in afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie)):
  107. idPlateau = self.plateau.id
  108. else:
  109. idPlateau = str(len(afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))))
  110. enregistrerUnique(self.plateau, "parties\\{}\\svg\\{}.p".format(self.partie, idPlateau))
  111. infos = {"nom": self.plateau.nom, "chapitre": self.plateau.chapitre, "dateCreation":self.plateau.dateCreation, "dateSvg":self.plateau.dateSvg, \
  112. "public": self.plateau.public, "enCours": self.plateau.enCours}
  113. enregistrer(str(idPlateau), infos, "parties\\{}\\svg\\infos_sauvegarde".format(self.partie))
  114. QApplication.restoreOverrideCursor()
  115. def fermerPlateau(self):
  116. self.plateau.fermer()
  117. self.plateau = None
  118. self.creerEcranFondPlateau()
  119. self.afficherPanneauxPlateau(False)
  120. def majFichierInfosSvg(self):
  121. """construit/maj le fichier contenant la liste des
  122. plateaux sauvegardes et leurs informations"""
  123. #on parcourt les fichiers de sauvegarde
  124. f = []
  125. lstFichiersSvg = []
  126. for (dirpath, dirnames, filenames) in os.walk("parties\\{}\\svg\\".format(self.partie)):
  127. f.extend(filenames)
  128. break
  129. for fichier in f:
  130. fileName, fileExtension = os.path.splitext(fichier)
  131. if fileExtension == ".p":
  132. lstFichiersSvg.append(fileName)
  133. #on verifie leur presence dans le fichier 'infos_sauvegarde'
  134. infosSvg = afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))
  135. index = len(infosSvg)
  136. #on ajoute les sauvegardes manquantes si besoin
  137. for fichier in lstFichiersSvg:
  138. if not fichier in infosSvg:
  139. plateau = chargerUnique("parties\\{}\\svg\\{}.p".format(self.partie, fichier))
  140. enregistrer(fichier, {"nom": plateau.nom, "chapitre": plateau.chapitre, "dateCreation":plateau.dateCreation, "dateSvg":plateau.dateSvg, \
  141. "public": plateau.public, "enCours": plateau.enCours}, "svg\\infos_sauvegarde")
  142. index += 1
  143. #on supprime ceux qui ne sont plus trouves
  144. for fichier in infosSvg:
  145. if not fichier in lstFichiersSvg:
  146. supprSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie), fichier)
  147. ########## apparence de l'onglet plateau
  148. def afficherPanneauxPlateau(self, actif):
  149. index = int(actif) # 0 si faux, 1 si vrai
  150. self.ui.cbt_panneauBas.setCurrentIndex(index)
  151. self.ui.cbt_panneauHaut.setCurrentIndex(index)
  152. self.ui.cbt_panneauDroite.setCurrentIndex(index)
  153. self.ui.cbt_panneauGauche.setCurrentIndex(index)
  154. self.ui.cbt_etapeSuivante.setVisible(actif)
  155. def reinitialiserPanneauxPlateau(self):
  156. """remet a neuf les commandes liees au plateau"""
  157. for panneau in [self.ui.cbt_panneauHaut, \
  158. self.ui.cbt_panneauBas, \
  159. self.ui.cbt_panneauGauche, \
  160. self.ui.cbt_panneauDroite]:
  161. #listes
  162. for liste in panneau.findChildren(QTableWidget):
  163. while liste.rowCount() > 0:
  164. liste.removeRow(0)
  165. #textes
  166. for texte in panneau.findChildren(QLineEdit):
  167. texte.clear()
  168. for texte in panneau.findChildren(QTextEdit):
  169. texte.clear()
  170. #a cocher
  171. for bouton in panneau.findChildren(QToolButton):
  172. if bouton.isCheckable():
  173. bouton.setChecked(False)
  174. #autre
  175. for item in panneau.findChildren(QSlider):
  176. item.setValue(1)
  177. for item in panneau.findChildren(QDoubleSpinBox):
  178. item.setValue(0)
  179. ########## onglet groupe
  180. def chargerListePj(self):
  181. listePj = chargerUnique("parties\\{}\\groupe".format(self.partie))
  182. if not listePj: listePj = []
  183. self.ui.grp_deroulement_layout.setAlignment(Qt.AlignTop)
  184. for pj in listePj:
  185. self.pjAjouterAListe(pj)
  186. def pjAjouterAListe(self, pj = None):
  187. colonne = (self._compteurPj % 3) * 2
  188. ligne = int(self._compteurPj / 3)
  189. self._compteurPj += 1
  190. panneau = FramePj(self._compteurPj)
  191. if pj:
  192. panneau.chargerPj(pj)
  193. panneau.setObjectName(QString("pj_panneau_{}".format(self._compteurPj)))
  194. self.connect(panneau, SIGNAL("pjModifie(int)"), self.pjEnregistrer)
  195. self.ui.grp_deroulement_layout.addWidget(panneau, ligne, colonne)
  196. ## pour l'espacement entre les panneaux
  197. w = QWidget()
  198. self.ui.grp_deroulement_layout.addWidget(w, ligne, colonne + 1)
  199. def pjSupprimer(self, index):
  200. panneau = self.findChild(QFrame, "pj_panneau_{}".format(index))
  201. self.ui.grp_deroulement_layout.removeWidget(panneau)
  202. def nouveauPj(self):
  203. self.editionPj = EcranEditionCombattant()
  204. self.editionPj.setAttribute(Qt.WA_DeleteOnClose)
  205. self.editionPj.exec_()
  206. pj = self.editionPj.combattant
  207. self.pjAjouterAListe(pj)
  208. self.pjEnregistrer(self._compteurPj)
  209. def pjEnregistrer(self, idPj):
  210. listePj = chargerUnique("parties\\{}\\groupe".format(self.partie))
  211. if not listePj: listePj = []
  212. panneau = self.findChild(QFrame, "pj_panneau_{}".format(idPj))
  213. pj = panneau.pj()
  214. listePj.append(pj)
  215. enregistrerUnique(listePj, "parties\\{}\\groupe".format(self.partie))
  216. def resizeEvent(self, event):
  217. val = (self.ui.dm_panneauCentre.width() - 20) / 3
  218. self.ui.dm_panneauCentre.setStyleSheet("QTabBar::tab { height: 25; width: "+str(val)+"px; }")
  219. if __name__ == "__main__":
  220. app = QApplication(argv)
  221. #settrace(trace_calls)
  222. dm = DMonde()
  223. dm.show()
  224. r = app.exec_()
  225. exit(r)