DMonde.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #from __future__ import unicode_literals
  2. # -*- coding: utf-8 -*-
  3. """Interface principale du programme DMonde
  4. """
  5. from __future__ import division
  6. import os
  7. from sys import exit, argv, getsizeof, settrace
  8. from time import time, sleep, strftime, localtime
  9. import pydoc
  10. from PyQt4.QtCore import *
  11. from PyQt4.QtGui import *
  12. from lib.ui.ecran_principal import Ui_principal
  13. from lib.EcranCreationPlateau import EcranCreationPlateau
  14. from lib.EcranFondPlateau import EcranFondPlateau
  15. from lib.Plateau import Plateau
  16. from lib.outilsSvg import *
  17. from lib.Case import Case
  18. from lib.Pion import Pion
  19. from lib.PionDecor import PionDecor
  20. from lib.Decor import Decor
  21. from lib.Terrain import Terrain
  22. from lib.Creature import Creature
  23. from lib.Attaque import Attaque
  24. from lib.Cache import Cache
  25. from lib.EntreeSortie import EntreeSortie
  26. #fonction ci dessous: deboguage, suit les appels faits aux fonctions
  27. #settrace(trace_calls)
  28. def trace_calls(frame, event, arg):
  29. if event != 'call':
  30. return
  31. co = frame.f_code
  32. func_name = co.co_name
  33. if func_name == 'write':
  34. # Ignore write() calls from print statements
  35. return
  36. func_line_no = frame.f_lineno
  37. func_filename = co.co_filename
  38. caller = frame.f_back
  39. caller_line_no = caller.f_lineno
  40. caller_filename = caller.f_code.co_filename
  41. #print 'Call to %s on line %s of %s from line %s of %s' % \
  42. # (func_name, func_line_no, func_filename,
  43. # caller_line_no, caller_filename)
  44. print("{} ({})".format(func_name, func_filename))
  45. return
  46. class DMonde(QMainWindow):
  47. """interface comprenant: chat ecrit, fenetre d'infos, lancer de des, echange de fichiers, lancement du chat vocal"""
  48. def __init__(self, parent=None):
  49. """initialisation de la fenetre"""
  50. super (DMonde, self).__init__()
  51. self.plateau = None
  52. self.partie = "partie1"
  53. self.idPlateauEnCours = ""
  54. self.plateauConnecte = False
  55. self.createWidgets()
  56. def createWidgets(self):
  57. """construction de l'interface"""
  58. self.ui = Ui_principal()
  59. self.ui.setupUi(self)
  60. self.majVisibilitePanneauxPlateau("")
  61. self.connect(self.ui.cbt_sauver, SIGNAL("clicked()"), self.sauverPlateau)
  62. self.connect(self.ui.cbt_fermer, SIGNAL("clicked()"), self.fermerPlateau)
  63. self.ui.cbt_vue.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)
  64. self.majFichierInfosSvg()
  65. self.creerEcranFondPlateau()
  66. def creerEcranFondPlateau(self):
  67. ecranFondPlateau = EcranFondPlateau(self)
  68. self.ui.cbt_vue.resetTransform()
  69. self.ui.cbt_vue.setScene(ecranFondPlateau)
  70. def afficherEcranCreationPlateau(self):
  71. """ouvre la fenetre de creation de plateau"""
  72. valide = True
  73. if self.plateau != None:
  74. if self.plateau.estCree() == True:
  75. valide = False
  76. if valide:
  77. self.creationPlateau = EcranCreationPlateau(self)
  78. self.creationPlateau.setWindowModality(Qt.ApplicationModal)
  79. self.creationPlateau.show()
  80. self.creationPlateau.raise_()
  81. def creerPlateau(self, nom, chapitre, formeCases, largeur, hauteur, couleur):
  82. """cree le plateau entre en parametre"""
  83. nouvelId = str(len(afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))))
  84. QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  85. self.plateau = Plateau(self)
  86. self.plateau.creer(nouvelId, nom, chapitre, formeCases, largeur, hauteur, couleur)
  87. QApplication.restoreOverrideCursor()
  88. del self.creationPlateau
  89. def sauverPlateau(self):
  90. QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  91. if self.plateau.id in afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie)):
  92. idPlateau = self.plateau.id
  93. else:
  94. idPlateau = str(len(afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))))
  95. enregistrerUnique(self.plateau, "parties\\{}\\svg\\{}.p".format(self.partie, idPlateau))
  96. infos = {"nom": self.plateau.nom, "chapitre": self.plateau.chapitre, "dateCreation":self.plateau.dateCreation, "dateSvg":self.plateau.dateSvg, \
  97. "public": self.plateau.public, "enCours": self.plateau.enCours}
  98. enregistrer(str(idPlateau), infos, "parties\\{}\\svg\\infos_sauvegarde".format(self.partie))
  99. QApplication.restoreOverrideCursor()
  100. def chargerPlateau(self, nomFichierSvg):
  101. if self.plateau != None:
  102. if self.plateau.estCree() == True:
  103. self.fermerPlateau()
  104. QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  105. self.plateau = chargerUnique("parties\\{}\\svg\\{}.p".format(self.partie, nomFichierSvg))
  106. if self.plateau:
  107. self.plateau.recreer(self)
  108. else:
  109. self.plateau = Plateau(self)
  110. QApplication.restoreOverrideCursor()
  111. def fermerPlateau(self):
  112. self.plateau.fermer()
  113. self.plateau = None
  114. self.creerEcranFondPlateau()
  115. ## self.majVisibilitePanneauxPlateau("")
  116. def majFichierInfosSvg(self):
  117. """construit/maj le fichier contenant la liste des
  118. plateaux sauvegardes et leurs informations"""
  119. #on parcourt les fichiers de sauvegarde
  120. index = 0
  121. f = []
  122. lstFichiersSvg = []
  123. for (dirpath, dirnames, filenames) in os.walk("parties\\{}\\svg\\".format(self.partie)):
  124. f.extend(filenames)
  125. break
  126. for fichier in f:
  127. fileName, fileExtension = os.path.splitext(fichier)
  128. if fileExtension == ".p":
  129. lstFichiersSvg.append(fileName)
  130. #on verifie leur presence dans le fichier 'infos_sauvegarde'
  131. infosSvg = afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))
  132. index = len(infosSvg)
  133. #on ajoute les sauvegardes manquantes si besoin
  134. for fichier in lstFichiersSvg:
  135. if not fichier in infosSvg:
  136. plateau = chargerUnique("parties\\{}\\svg\\{}.p".format(self.partie, fichier))
  137. enregistrer(fichier, {"nom": plateau.nom, "chapitre": plateau.chapitre, "dateCreation":plateau.dateCreation, "dateSvg":plateau.dateSvg, \
  138. "public": plateau.public, "enCours": plateau.enCours}, "svg\\infos_sauvegarde")
  139. index += 1
  140. #on supprime ceux qui ne sont plus trouves
  141. for fichier in infosSvg:
  142. if not fichier in lstFichiersSvg:
  143. supprSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie), fichier)
  144. def majVisibilitePanneauxPlateau(self, mode):
  145. """affiche ou cache les panneaux d'edition, d'information et de gestion du plateau"""
  146. #self.ui.panneauInfosPlateau.setVisible(False)
  147. conditionPlateau = (len(mode) > 0)
  148. conditionModeCreation = (len(mode) > 0 and mode == "creation")
  149. conditionModeCombat = (len(mode) > 0 and mode == "combat")
  150. #panneaux d'infos
  151. self.ui.inf_panneau.setVisible(conditionPlateau)
  152. self.ui.cbt_barreHaut.setVisible(conditionPlateau)
  153. #modes creation/combat
  154. self.ui.cbt_modeCombat.setVisible(conditionModeCreation)
  155. self.ui.cp_panneau.setVisible(conditionModeCreation)
  156. self.ui.cbt_modeCreation.setVisible(conditionModeCombat)
  157. self.ui.pi_panneau.setVisible(conditionModeCombat)
  158. def reinitialiserPanneauxPlateau(self):
  159. """remet a neuf les commandes liees au plateau"""
  160. for panneau in [self.ui.inf_panneau, \
  161. self.ui.cbt_barreHaut, \
  162. self.ui.cp_panneau, \
  163. self.ui.pi_panneau]:
  164. #listes
  165. for liste in panneau.findChildren(QTableWidget):
  166. while liste.rowCount() > 0:
  167. liste.removeRow(0)
  168. #textes
  169. for texte in panneau.findChildren(QLineEdit):
  170. texte.clear()
  171. for texte in panneau.findChildren(QTextEdit):
  172. texte.clear()
  173. #a cocher
  174. for bouton in panneau.findChildren(QToolButton):
  175. if bouton.isCheckable():
  176. bouton.setChecked(False)
  177. #autre
  178. for item in panneau.findChildren(QSlider):
  179. item.setValue(1)
  180. for item in panneau.findChildren(QDoubleSpinBox):
  181. item.setValue(0)
  182. if __name__ == "__main__":
  183. app = QApplication(argv)
  184. #settrace(trace_calls)
  185. dm = DMonde()
  186. dm.show()
  187. r = app.exec_()
  188. exit(r)