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