rsc.py 12 KB

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