DMonde.py 8.8 KB

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