rsc.py 12 KB

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