rsc.py 12 KB

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