DMonde.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.EcranCreerPlateau import EcranCreerPlateau
  14. from lib.EcranChargerPlateau import EcranChargerPlateau
  15. from lib.EcranFondPlateau import EcranFondPlateau
  16. from lib.Plateau import Plateau
  17. from lib.EcranEditionCombattant import EcranEditionCombattant
  18. from lib.framePj import FramePj
  19. from lib.outilsSvg import *
  20. from lib.Case import Case
  21. from lib.Combattant import Combattant
  22. from lib.Decor import Decor
  23. from lib.Terrain import Terrain
  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._compteurPj = 0
  56. self.createWidgets()
  57. def createWidgets(self):
  58. """construction de l'interface"""
  59. self.ui = Ui_principal()
  60. self.ui.setupUi(self)
  61. self.majVisibilitePanneauxPlateau("")
  62. self.connect(self.ui.cbt_sauver, SIGNAL("clicked()"), self.sauverPlateau)
  63. self.connect(self.ui.cbt_fermer, SIGNAL("clicked()"), self.fermerPlateau)
  64. self.ui.cbt_vue.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)
  65. self.ui.cp_ongletsListes.setStyleSheet("QTabBar::tab { width: 38px; }")
  66. self.ui.pi_ongletsListes.setStyleSheet("QTabBar::tab { width: 38px; }")
  67. self.majFichierInfosSvg()
  68. self.creerEcranFondPlateau()
  69. self.chargerListePj()
  70. self.connect(self.ui.grp_nouveauPj, SIGNAL("clicked()"), self.nouveauPj)
  71. self.showMaximized()
  72. def creerEcranFondPlateau(self):
  73. ecranFondPlateau = EcranFondPlateau(self)
  74. self.ui.cbt_vue.resetTransform()
  75. self.ui.cbt_vue.setScene(ecranFondPlateau)
  76. def chargerListePj(self):
  77. listePj = chargerUnique("parties\\{}\\groupe".format(self.partie))
  78. if not listePj: listePj = []
  79. self.ui.grp_deroulement_layout.setAlignment(Qt.AlignTop)
  80. for pj in listePj:
  81. self.pjAjouterAListe(pj)
  82. def afficherEcranCreerPlateau(self):
  83. """ouvre la fenetre de creation de plateau"""
  84. valide = True
  85. if self.plateau != None:
  86. if self.plateau.estCree() == True:
  87. valide = False
  88. if valide:
  89. self.creationPlateau = EcranCreerPlateau(self)
  90. self.creationPlateau.setWindowModality(Qt.ApplicationModal)
  91. self.creationPlateau.show()
  92. self.creationPlateau.raise_()
  93. def creerPlateau(self, nom, chapitre, formeCases, largeur, hauteur, couleur):
  94. """cree le plateau entre en parametre"""
  95. nouvelId = str(len(afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))))
  96. QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  97. self.plateau = Plateau(self)
  98. self.plateau.creer(nouvelId, nom, chapitre, formeCases, largeur, hauteur, couleur)
  99. QApplication.restoreOverrideCursor()
  100. del self.creationPlateau
  101. def afficherEcranChargerPlateau(self):
  102. """ouvre la fenetre de chargement de plateau"""
  103. valide = True
  104. if self.plateau != None:
  105. if self.plateau.estCree() == True:
  106. valide = False
  107. if valide:
  108. self.chargementPlateau = EcranChargerPlateau(self)
  109. self.chargementPlateau.setWindowModality(Qt.ApplicationModal)
  110. self.chargementPlateau.show()
  111. self.chargementPlateau.raise_()
  112. def chargerPlateau(self, nomFichierSvg):
  113. if self.plateau != None:
  114. if self.plateau.estCree() == True:
  115. self.fermerPlateau()
  116. QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  117. self.plateau = chargerUnique("parties\\{}\\svg\\{}.p".format(self.partie, nomFichierSvg))
  118. if self.plateau:
  119. self.plateau.recreer(self)
  120. else:
  121. self.plateau = Plateau(self)
  122. QApplication.restoreOverrideCursor()
  123. def chargerDernierPlateau(self):
  124. """charge le dernier plateau sauvegarde"""
  125. infosSvg = afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))
  126. ligne = 0
  127. dernier = None
  128. for id_svg in infosSvg:
  129. if not dernier or infosSvg[id_svg]["dateSvg"] > infosSvg[dernier]["dateSvg"]:
  130. dernier = id_svg
  131. if dernier:
  132. self.chargerPlateau(dernier)
  133. def sauverPlateau(self):
  134. QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
  135. if self.plateau.id in afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie)):
  136. idPlateau = self.plateau.id
  137. else:
  138. idPlateau = str(len(afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))))
  139. enregistrerUnique(self.plateau, "parties\\{}\\svg\\{}.p".format(self.partie, idPlateau))
  140. infos = {"nom": self.plateau.nom, "chapitre": self.plateau.chapitre, "dateCreation":self.plateau.dateCreation, "dateSvg":self.plateau.dateSvg, \
  141. "public": self.plateau.public, "enCours": self.plateau.enCours}
  142. enregistrer(str(idPlateau), infos, "parties\\{}\\svg\\infos_sauvegarde".format(self.partie))
  143. QApplication.restoreOverrideCursor()
  144. def fermerPlateau(self):
  145. self.plateau.fermer()
  146. self.plateau = None
  147. self.creerEcranFondPlateau()
  148. self.majVisibilitePanneauxPlateau("")
  149. def majFichierInfosSvg(self):
  150. """construit/maj le fichier contenant la liste des
  151. plateaux sauvegardes et leurs informations"""
  152. #on parcourt les fichiers de sauvegarde
  153. index = 0
  154. f = []
  155. lstFichiersSvg = []
  156. for (dirpath, dirnames, filenames) in os.walk("parties\\{}\\svg\\".format(self.partie)):
  157. f.extend(filenames)
  158. break
  159. for fichier in f:
  160. fileName, fileExtension = os.path.splitext(fichier)
  161. if fileExtension == ".p":
  162. lstFichiersSvg.append(fileName)
  163. #on verifie leur presence dans le fichier 'infos_sauvegarde'
  164. infosSvg = afficheSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie))
  165. index = len(infosSvg)
  166. #on ajoute les sauvegardes manquantes si besoin
  167. for fichier in lstFichiersSvg:
  168. if not fichier in infosSvg:
  169. plateau = chargerUnique("parties\\{}\\svg\\{}.p".format(self.partie, fichier))
  170. enregistrer(fichier, {"nom": plateau.nom, "chapitre": plateau.chapitre, "dateCreation":plateau.dateCreation, "dateSvg":plateau.dateSvg, \
  171. "public": plateau.public, "enCours": plateau.enCours}, "svg\\infos_sauvegarde")
  172. index += 1
  173. #on supprime ceux qui ne sont plus trouves
  174. for fichier in infosSvg:
  175. if not fichier in lstFichiersSvg:
  176. supprSvg("parties\\{}\\svg\\infos_sauvegarde".format(self.partie), fichier)
  177. def majVisibilitePanneauxPlateau(self, mode):
  178. """affiche ou cache les panneaux d'edition, d'information et de gestion du plateau"""
  179. #self.ui.panneauInfosPlateau.setVisible(False)
  180. conditionPlateau = (len(mode) > 0)
  181. conditionModeCreation = (len(mode) > 0 and mode == "creation")
  182. conditionModeCombat = (len(mode) > 0 and mode == "combat")
  183. #panneaux d'infos, barre du haut et journal
  184. self.ui.inf_panneau.setVisible(conditionPlateau)
  185. self.ui.cbt_barreHaut.setVisible(conditionPlateau)
  186. self.ui.histo_agrandir.setVisible(conditionPlateau)
  187. self.ui.histo_liste.setVisible(conditionPlateau)
  188. #modes creation/combat
  189. self.ui.cbt_modeCombat.setChecked(conditionModeCombat)
  190. self.ui.cp_panneau.setVisible(conditionModeCreation)
  191. self.ui.cbt_modeCreation.setChecked(conditionModeCreation)
  192. self.ui.pi_panneau.setVisible(conditionModeCombat)
  193. def reinitialiserPanneauxPlateau(self):
  194. """remet a neuf les commandes liees au plateau"""
  195. for panneau in [self.ui.inf_panneau, \
  196. self.ui.cbt_barreHaut, \
  197. self.ui.cp_panneau, \
  198. self.ui.pi_panneau]:
  199. #listes
  200. for liste in panneau.findChildren(QTableWidget):
  201. while liste.rowCount() > 0:
  202. liste.removeRow(0)
  203. #textes
  204. for texte in panneau.findChildren(QLineEdit):
  205. texte.clear()
  206. for texte in panneau.findChildren(QTextEdit):
  207. texte.clear()
  208. #a cocher
  209. for bouton in panneau.findChildren(QToolButton):
  210. if bouton.isCheckable():
  211. bouton.setChecked(False)
  212. #autre
  213. for item in panneau.findChildren(QSlider):
  214. item.setValue(1)
  215. for item in panneau.findChildren(QDoubleSpinBox):
  216. item.setValue(0)
  217. def pjAjouterAListe(self, pj = None):
  218. self._compteurPj += 1
  219. panneau = FramePj(self._compteurPj)
  220. if pj:
  221. panneau.chargerPj(pj)
  222. panneau.setObjectName(QString("pj_panneau_{}".format(self._compteurPj)))
  223. self.connect(panneau, SIGNAL("pjModifie(int)"), self.pjEnregistrer)
  224. self.ui.grp_deroulement_layout.addWidget(panneau)
  225. def pjSupprimer(self, index):
  226. panneau = self.findChild(QFrame, "pj_panneau_{}".format(index))
  227. self.ui.grp_deroulement_layout.removeWidget(panneau)
  228. def nouveauPj(self):
  229. self.editionPj = EcranEditionCombattant()
  230. self.editionPj.setAttribute(Qt.WA_DeleteOnClose)
  231. r = self.editionPj.exec_()
  232. pj = self.editionPj.combattant
  233. self.pjAjouterAListe(pj)
  234. self.pjEnregistrer(self._compteurPj)
  235. def pjEnregistrer(self, idPj):
  236. listePj = chargerUnique("parties\\{}\\groupe".format(self.partie))
  237. if not listePj: listePj = []
  238. panneau = self.findChild(QFrame, "pj_panneau_{}".format(idPj))
  239. pj = panneau.pj()
  240. listePj.append(pj)
  241. enregistrerUnique(listePj, "parties\\{}\\groupe".format(self.partie))
  242. def resizeEvent(self, event):
  243. val = (self.ui.dm_panneauCentre.width() - 20) / 3
  244. self.ui.dm_panneauCentre.setStyleSheet("QTabBar::tab { height: 25; width: "+str(val)+"px; }")
  245. if __name__ == "__main__":
  246. app = QApplication(argv)
  247. #settrace(trace_calls)
  248. dm = DMonde()
  249. dm.show()
  250. r = app.exec_()
  251. exit(r)