rsc.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #from __future__ import unicode_literals
  2. # -*- coding: utf-8 -*-
  3. """controle l'acces aux ressources variables (sons, images, sauvegardes)"""
  4. import os
  5. from shutil import copyfile
  6. import sys
  7. from PyQt4.QtCore import Qt, SIGNAL, QString, QSize
  8. from PyQt4.QtGui import QPixmap, QDialog, QFrame, QColor, QPalette, QApplication, \
  9. QFileDialog
  10. from commun import rep, uid, enregistrerSous, charger
  11. from dialogues import dmConfirmer
  12. from ui.ecran_editerImage import Ui_edi_fenetre
  13. from ui.ecran_explorateur import Ui_exr_fenetre
  14. from ui.panneauImage import Ui_exi_panneau
  15. from pygame.gfxdraw import pixel
  16. def selectionImage():
  17. retour = None
  18. expl = ExplorateurImages()
  19. expl.charger()
  20. expl.show()
  21. expl.exec_()
  22. img = expl.selection()
  23. if img:
  24. if img.estValide():
  25. retour = img
  26. del expl
  27. return retour
  28. class Ressource(object):
  29. """classe de base des ressources utilisees"""
  30. def __init__(self):
  31. """cette classe contient les infos relatives a une ressource importee"""
  32. super(Ressource, self).__init__()
  33. self._type = "rs"
  34. self._idR = ""
  35. self._nom = ""
  36. self._sType = 0 #sous type
  37. self._extension = ""
  38. def nom(self):
  39. return self._nom
  40. def majNom(self, nom):
  41. self._nom = nom
  42. def typ(self):
  43. return self._type
  44. def majType(self, typ):
  45. self._type = typ
  46. def sType(self):
  47. return self._sType
  48. def majSType(self, sType):
  49. self._sType = sType
  50. def idR(self):
  51. return self._idR
  52. def extension(self):
  53. return self._extension
  54. def libelleSType(self):
  55. lst = ["[Non defini]"]
  56. return lst[self._sType]
  57. def fichier(self):
  58. """retourne le chemin d'acces au fichier ressource"""
  59. return os.path.join(rep("rsc"), "{}{}".format(self._idR, self._extension))
  60. def importer(self, chemin):
  61. """importe la ressource demandee dans le repertoire de ressources Dm"""
  62. if not os.path.isfile(chemin): return
  63. #on verifie l'extension
  64. if not os.path.splitext(chemin)[1] in [".png", ".jpg", ".gif"]: return
  65. #nom du fichier importe
  66. if len(self._nom) == 0:
  67. nom = ""
  68. for car in reversed(chemin):
  69. if car in ["\\", "/"]:
  70. break
  71. else:
  72. nom = car + nom
  73. self._nom = os.path.splitext(nom)[0]
  74. self._extension = os.path.splitext(chemin)[1]
  75. #nouvel id
  76. self._idR = uid(self._type)
  77. #copie du fichier dans le repertoire des ressources
  78. copyfile(chemin, self.fichier())
  79. self.enregistrer()
  80. def enregistrer(self):
  81. cible = os.path.join(rep("rsc"), "{}.rsc".format(self._idR))
  82. enregistrerSous(self, cible)
  83. def estValide(self):
  84. return os.path.isfile(self.fichier())
  85. def supprimer(self):
  86. os.remove(self.fichier())
  87. os.remove(os.path.join(rep("rsc"), "{}.rsc".format(self._idR)))
  88. class RImage(Ressource):
  89. """classe de base des ressources de type image"""
  90. def __init__(self):
  91. super(RImage, self).__init__()
  92. self._type = "im"
  93. self._gommette = None #les gommettes sont des images crees a partir d'une couleur
  94. def __setstate__(self, state):
  95. """correctif 220915"""
  96. self.__dict__ = state
  97. if not "_gomette" in self.__dict__: self._gomette = None
  98. def libelleSType(self):
  99. lstSTypes = ["[Non defini]", "Creature (portrait)", "Creature (pion)", \
  100. "Decor (portrait)", "Decor (pion)", "Terrain", "Autre"]
  101. return lstSTypes[self._sType]
  102. def pix(self, l = 0, h = 0):
  103. if self._gomette != None:
  104. pix = QPixmap(QSize(16, 16))
  105. pix.fill(self._gomette)
  106. else:
  107. pix = QPixmap(self.fichier())
  108. if not pix.isNull():
  109. if l > 0 and h > 0:
  110. pix = pix.scaled(l, h, Qt.KeepAspectRatio, Qt.SmoothTransformation)
  111. elif l > 0 and h == 0:
  112. pix = pix.scaledToWidth(l, Qt.SmoothTransformation)
  113. elif l == 0 and h > 0:
  114. pix = pix.scaledToHeight(h, Qt.SmoothTransformation)
  115. return pix
  116. def gomette(self, couleur):
  117. self._gomette = couleur
  118. class ExplorateurImages(QDialog):
  119. def __init__(self, parent=None):
  120. """initialisation de la fenetre"""
  121. super (ExplorateurImages, self).__init__(parent)
  122. self.createWidgets()
  123. self._selection = None
  124. self._panneaux = []
  125. self._panneauSelectionne = None
  126. def createWidgets(self):
  127. """construction de l'interface"""
  128. self.ui = Ui_exr_fenetre()
  129. self.ui.setupUi(self)
  130. self.connect(self.ui.exr_ok, SIGNAL("clicked()"), self.valider)
  131. self.connect(self.ui.exr_annuler, SIGNAL("clicked()"), self.annuler)
  132. self.connect(self.ui.exr_editer, SIGNAL("clicked()"), self.editer)
  133. self.connect(self.ui.exr_supprimer, SIGNAL("clicked()"), self.supprimer)
  134. self.connect(self.ui.exr_ajouter, SIGNAL("clicked()"), self.ajouter)
  135. self.connect(self.ui.exr_filtreSType, SIGNAL("currentIndexChanged(int)"), self.majFiltre)
  136. self.connect(self.ui.exr_filtreNom, SIGNAL("textEdited(QString)"), self.majFiltre)
  137. self.majLayout()
  138. def charger(self):
  139. """charge les images disponibles dans l'explorateur"""
  140. fichiers = []
  141. for attributsFichier in os.walk(rep("rsc")):
  142. for f in attributsFichier[2]:
  143. if os.path.splitext(f)[1] == ".rsc":
  144. r = charger(os.path.join(rep("rsc"), f))
  145. if r.estValide():
  146. fichiers.append(r)
  147. for f in fichiers:
  148. self.ajouterImage(f)
  149. def ajouterImage(self, img):
  150. """ajoute un panneau image a la liste deroulante"""
  151. panneau = PanneauImage(self)
  152. panneau.chargerImage(img)
  153. self._panneaux.append(panneau)
  154. self.ui.exr_layout.addWidget(panneau)
  155. def selection(self):
  156. return self._selection
  157. def majSelection(self, panneau):
  158. if self._panneauSelectionne:
  159. self._panneauSelectionne.selectionner(False)
  160. self._panneauSelectionne = panneau
  161. self.majAffichage()
  162. def majAffichage(self):
  163. self.ui.exr_editer.setEnabled(self._panneauSelectionne != None)
  164. self.ui.exr_supprimer.setEnabled(self._panneauSelectionne != None)
  165. self.ui.exr_ok.setEnabled(self._panneauSelectionne != None)
  166. def majFiltre(self):
  167. filtreNom = self.ui.exr_filtreNom.texte()
  168. filtreSType = (self.ui.exr_filtreSType.currentIndex() - 1)
  169. for panneau in self._panneaux:
  170. panneau.appliquerFiltre(filtreNom, filtreSType)
  171. def majLayout(self):
  172. self.ui.exr_layout.setColumnMinimumWidth(0, 140)
  173. self.ui.exr_layout.setColumnStretch(0, 1)
  174. self.ui.exr_layout.setColumnMinimumWidth(1, 140)
  175. self.ui.exr_layout.setColumnStretch(1, 1)
  176. self.ui.exr_layout.setColumnMinimumWidth(2, 140)
  177. self.ui.exr_layout.setColumnStretch(2, 1)
  178. self.ui.exr_layout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
  179. def editer(self):
  180. self._panneauSelectionne.editer()
  181. def supprimer(self):
  182. msg = "Êtes vous sûr de vouloir supprimer l'image: \n{}".format(self._panneauSelectionne.image().nom())
  183. if not dmConfirmer(msg): return
  184. self._panneauSelectionne.image().supprimer()
  185. self._panneauSelectionne.setVisible(False)
  186. self._panneaux.remove(self._panneauSelectionne)
  187. self._panneauSelectionne = None
  188. self.majAffichage()
  189. self.ui.exr_layout.update()
  190. def ajouter(self):
  191. """permet de choisir des fichiers a importer"""
  192. fichiers = QFileDialog.getOpenFileNames(self,
  193. QString.fromUtf8("Sélectionnez un/des fichier(s) à importer"),
  194. "c:\\",
  195. "Images (*.png *.gif *.jpg)")
  196. for chemin in list(fichiers):
  197. img = RImage()
  198. img.importer(str(chemin.toUtf8()))
  199. self.ajouterImage(img)
  200. self.ui.exr_layout.update()
  201. def valider(self):
  202. self._selection = self._panneauSelectionne.image()
  203. self.done(1)
  204. def annuler(self):
  205. self.done(0)
  206. class PanneauImage(QFrame):
  207. def __init__(self, fenetre, parent=None):
  208. """initialisation de la fenetre"""
  209. self.fenetre = fenetre
  210. super (PanneauImage, self).__init__(parent)
  211. self.createWidgets()
  212. self._image = None
  213. self._selectionnee = False
  214. def createWidgets(self):
  215. """construction de l'interface"""
  216. self.ui = Ui_exi_panneau()
  217. self.ui.setupUi(self)
  218. self.connect(self.ui.exi_image, SIGNAL("clicked()"), self.clic)
  219. self.connect(self.ui.exi_nom, SIGNAL("clicked()"), self.clic)
  220. self.connect(self.ui.exi_details, SIGNAL("clicked()"), self.clic)
  221. self.connect(self.ui.exi_sType, SIGNAL("clicked()"), self.clic)
  222. self.connect(self.ui.exi_image, SIGNAL("doubleClicked()"), self.doubleClic)
  223. self.connect(self.ui.exi_nom, SIGNAL("doubleClicked()"), self.doubleClic)
  224. self.connect(self.ui.exi_details, SIGNAL("doubleClicked()"), self.doubleClic)
  225. self.connect(self.ui.exi_sType, SIGNAL("doubleClicked()"), self.doubleClic)
  226. def selectionner(self, actif):
  227. if actif:
  228. couleur = QColor(250, 250, 250, 250)
  229. else:
  230. couleur = QColor(240, 240, 240, 240)
  231. palette = QPalette()
  232. palette.setColor(QPalette.Window, couleur)
  233. self.setPalette(palette)
  234. def chargerImage(self, image):
  235. self._image = image
  236. self.maj()
  237. def maj(self):
  238. self.ui.exi_image.chargerImage(self._image)
  239. self.ui.exi_nom.majTexte(self._image.nom())
  240. self.ui.exi_details.majTexte((self._image.extension().replace(".", "")).upper())
  241. self.ui.exi_sType.majTexte(self._image.libelleSType())
  242. def image(self):
  243. return self._image
  244. def editer(self):
  245. fen = Editerimage()
  246. fen.charger(self.image())
  247. fen.show()
  248. r = fen.exec_()
  249. if r == 1:
  250. self._image = fen.rimage()
  251. self._image.enregistrer()
  252. del fen
  253. self.maj()
  254. def appliquerFiltre(self, filtreNom, filtreSType = -1):
  255. self.setVisible(filtreNom in self._image.nom() and \
  256. (filtreSType == -1 or self._image.sType() == filtreSType))
  257. def clic(self):
  258. if not self._selectionnee:
  259. self.fenetre.majSelection(self)
  260. self.selectionner(True)
  261. def doubleClic(self):
  262. self.fenetre.valider()
  263. def mousePressEvent(self, event):
  264. if event.button() == 1:
  265. self.clic()
  266. def mouseDoubleClickEvent(self, event):
  267. if event.button() == 1:
  268. self.doubleClic()
  269. class Editerimage(QDialog):
  270. def __init__(self, parent=None):
  271. """initialisation de la fenetre"""
  272. super (Editerimage, self).__init__(parent)
  273. self.createWidgets()
  274. self._rimage = None
  275. def createWidgets(self):
  276. """construction de l'interface"""
  277. self.ui = Ui_edi_fenetre()
  278. self.ui.setupUi(self)
  279. self.connect(self.ui.edi_enregistrer, SIGNAL("clicked()"), self.enregistrer)
  280. self.connect(self.ui.edi_annuler, SIGNAL("clicked()"), self.annuler)
  281. def charger(self, rimage):
  282. """charge la rimage en parametre et affiche ses caracteristiques"""
  283. self._rimage = rimage
  284. self.ui.edi_nom.majTexte(self._rimage.nom())
  285. self.ui.edi_sType.setCurrentIndex((self._rimage.sType() - 1))
  286. def rimage(self):
  287. """retourne l'image modifiee"""
  288. return self._rimage
  289. def enregistrer(self):
  290. self._rimage.majNom(self.ui.edi_nom.texte())
  291. self._rimage.majSType((self.ui.edi_sType.currentIndex() + 1))
  292. self.done(1)
  293. def annuler(self):
  294. self.done(0)
  295. if __name__ == "__main__":
  296. app = QApplication(sys.argv)
  297. # img = RImage()
  298. # img.importer("C:\\python_tmp\\dm\\DMonde\\rsc\\colonne.png")
  299. # img = RImage()
  300. # img.importer("C:\\python_tmp\\dm\\DMonde\\rsc\\orc.png")
  301. # img = RImage()
  302. # img.importer("C:\\python_tmp\\dm\\DMonde\\rsc\\dragon.png")
  303. img = selectionImage()
  304. if img:
  305. print img.nom()
  306. exit()