DMonde.py 11 KB

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