| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- #from __future__ import unicode_literals
- # -*- coding: utf-8 -*-
- """controle l'acces aux ressources variables (sons, images, sauvegardes)"""
- import os
- from shutil import copyfile
- import sys
- from PyQt4.QtCore import Qt, SIGNAL, QString
- from PyQt4.QtGui import QPixmap, QDialog, QFrame, QColor, QPalette, QApplication, \
- QFileDialog
- from commun import rep, uid, enregistrerSous, charger, dmConfirmer
- from ui.ecran_editerImage import Ui_edi_fenetre
- from ui.ecran_explorateur import Ui_exr_fenetre
- from ui.panneauImage import Ui_exi_panneau
- def selectionImage():
- retour = None
- expl = ExplorateurImages()
- expl.charger()
- expl.show()
- expl.exec_()
- img = expl.selection()
- if img:
- if img.estValide():
- retour = img
- del expl
- return retour
-
- class Ressource(object):
- """classe de base des ressources utilisees"""
- def __init__(self):
- """cette classe contient les infos relatives a une ressource importee"""
- self._type = "rs"
- self._idR = ""
- self._nom = ""
- self._sType = 0 #sous type
- self._extension = ""
- def nom(self):
- return self._nom
-
- def majNom(self, nom):
- self._nom = nom
- def typ(self):
- return self._type
-
- def majType(self, typ):
- self._type = typ
- def sType(self):
- return self._sType
-
- def majSType(self, sType):
- self._sType = sType
- def idR(self):
- return self._idR
- def extension(self):
- return self._extension
- def libelleSType(self):
- lst = ["[Non defini]"]
- return lst[self._sType]
- def fichier(self):
- """retourne le chemin d'acces au fichier ressource"""
- return os.path.join(rep("rsc"), "{}{}".format(self._idR, self._extension))
- def importer(self, chemin):
- """importe la ressource demandee dans le repertoire de ressources Dm"""
- if not os.path.isfile(chemin): return
-
- #on verifie l'extension
- if not os.path.splitext(chemin)[1] in [".png", ".jpg", ".gif"]: return
-
- #nom du fichier importe
- if len(self._nom) == 0:
- nom = ""
- for car in reversed(chemin):
- if car in ["\\", "/"]:
- break
- else:
- nom = car + nom
- self._nom = os.path.splitext(nom)[0]
- self._extension = os.path.splitext(chemin)[1]
- #nouvel id
- self._idR = uid(self._type)
- #copie du fichier dans le repertoire des ressources
- copyfile(chemin, self.fichier())
- self.enregistrer()
- def enregistrer(self):
- cible = os.path.join(rep("rsc"), "{}.rsc".format(self._idR))
- enregistrerSous(self, cible)
-
- def estValide(self):
- return os.path.isfile(self.fichier())
- def supprimer(self):
- os.remove(self.fichier())
- os.remove(os.path.join(rep("rsc"), "{}.rsc".format(self._idR)))
- class RImage(Ressource):
- """classe de base des ressources de type image"""
- def __init__(self):
- super(RImage, self).__init__()
- self._type = "im"
- def libelleSType(self):
- lstSTypes = ["[Non defini]", "Creature (portrait)", "Creature (pion)", \
- "Decor (portrait)", "Decor (pion)", "Terrain", "Autre"]
- return lstSTypes[self._sType]
- def pix(self, l = 0, h = 0):
- pix = QPixmap(self.fichier())
- if not pix.isNull():
- if l > 0 and h > 0:
- pix = pix.scaled(l, h, Qt.KeepAspectRatio, Qt.SmoothTransformation)
- elif l > 0 and h == 0:
- pix = pix.scaledToWidth(l, Qt.SmoothTransformation)
- elif l == 0 and h > 0:
- pix = pix.scaledToHeight(h, Qt.SmoothTransformation)
- return pix
- class ExplorateurImages(QDialog):
- def __init__(self, parent=None):
- """initialisation de la fenetre"""
- super (ExplorateurImages, self).__init__(parent)
- self.createWidgets()
- self._selection = None
- self._panneaux = []
- self._panneauSelectionne = None
- def createWidgets(self):
- """construction de l'interface"""
- self.ui = Ui_exr_fenetre()
- self.ui.setupUi(self)
-
- self.connect(self.ui.exr_ok, SIGNAL("clicked()"), self.valider)
- self.connect(self.ui.exr_annuler, SIGNAL("clicked()"), self.annuler)
- self.connect(self.ui.exr_editer, SIGNAL("clicked()"), self.editer)
- self.connect(self.ui.exr_supprimer, SIGNAL("clicked()"), self.supprimer)
- self.connect(self.ui.exr_ajouter, SIGNAL("clicked()"), self.ajouter)
- self.connect(self.ui.exr_filtreSType, SIGNAL("currentIndexChanged(int)"), self.majFiltre)
- self.connect(self.ui.exr_filtreNom, SIGNAL("textEdited(QString)"), self.majFiltre)
- self.majLayout()
- def charger(self):
- """charge les images disponibles dans l'explorateur"""
- fichiers = []
- for attributsFichier in os.walk(rep("rsc")):
- for f in attributsFichier[2]:
- if os.path.splitext(f)[1] == ".rsc":
- r = charger(os.path.join(rep("rsc"), f))
- if r.estValide():
- fichiers.append(r)
- for f in fichiers:
- self.ajouterImage(f)
-
- def ajouterImage(self, img):
- """ajoute un panneau image a la liste deroulante"""
- panneau = PanneauImage(self)
- panneau.chargerImage(img)
- self._panneaux.append(panneau)
- self.ui.exr_layout.addWidget(panneau)
- def selection(self):
- return self._selection
- def majSelection(self, panneau):
- if self._panneauSelectionne:
- self._panneauSelectionne.selectionner(False)
- self._panneauSelectionne = panneau
- self.majAffichage()
- def majAffichage(self):
- self.ui.exr_editer.setEnabled(self._panneauSelectionne != None)
- self.ui.exr_supprimer.setEnabled(self._panneauSelectionne != None)
- self.ui.exr_ok.setEnabled(self._panneauSelectionne != None)
- def majFiltre(self):
- filtreNom = self.ui.exr_filtreNom.texte()
- filtreSType = (self.ui.exr_filtreSType.currentIndex() - 1)
- for panneau in self._panneaux:
- panneau.appliquerFiltre(filtreNom, filtreSType)
- def majLayout(self):
- self.ui.exr_layout.setColumnMinimumWidth(0, 140)
- self.ui.exr_layout.setColumnStretch(0, 1)
- self.ui.exr_layout.setColumnMinimumWidth(1, 140)
- self.ui.exr_layout.setColumnStretch(1, 1)
- self.ui.exr_layout.setColumnMinimumWidth(2, 140)
- self.ui.exr_layout.setColumnStretch(2, 1)
- self.ui.exr_layout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
- def editer(self):
- self._panneauSelectionne.editer()
- def supprimer(self):
- msg = "Êtes vous sûr de vouloir supprimer l'image: \n{}".format(self._panneauSelectionne.image().nom())
- if not dmConfirmer(msg): return
- self._panneauSelectionne.image().supprimer()
- self._panneauSelectionne.setVisible(False)
- self._panneaux.remove(self._panneauSelectionne)
- self._panneauSelectionne = None
- self.majAffichage()
- self.ui.exr_layout.update()
-
- def ajouter(self):
- """permet de choisir des fichiers a importer"""
- fichiers = QFileDialog.getOpenFileNames(self,
- QString.fromUtf8("Sélectionnez un/des fichier(s) à importer"),
- "c:\\",
- "Images (*.png *.gif *.jpg)")
- for chemin in list(fichiers):
- img = RImage()
- img.importer(str(chemin.toUtf8()))
- self.ajouterImage(img)
-
- self.ui.exr_layout.update()
-
- def valider(self):
- self._selection = self._panneauSelectionne.image()
- self.done(1)
- def annuler(self):
- self.done(0)
- class PanneauImage(QFrame):
- def __init__(self, fenetre, parent=None):
- """initialisation de la fenetre"""
- self.fenetre = fenetre
- super (PanneauImage, self).__init__(parent)
- self.createWidgets()
- self._image = None
- self._selectionnee = False
- def createWidgets(self):
- """construction de l'interface"""
- self.ui = Ui_exi_panneau()
- self.ui.setupUi(self)
- self.connect(self.ui.exi_image, SIGNAL("clicked()"), self.clic)
- self.connect(self.ui.exi_nom, SIGNAL("clicked()"), self.clic)
- self.connect(self.ui.exi_details, SIGNAL("clicked()"), self.clic)
- self.connect(self.ui.exi_sType, SIGNAL("clicked()"), self.clic)
- self.connect(self.ui.exi_image, SIGNAL("doubleClicked()"), self.doubleClic)
- self.connect(self.ui.exi_nom, SIGNAL("doubleClicked()"), self.doubleClic)
- self.connect(self.ui.exi_details, SIGNAL("doubleClicked()"), self.doubleClic)
- self.connect(self.ui.exi_sType, SIGNAL("doubleClicked()"), self.doubleClic)
-
- def selectionner(self, actif):
- if actif:
- couleur = QColor(250, 250, 250, 250)
- else:
- couleur = QColor(240, 240, 240, 240)
- palette = QPalette()
- palette.setColor(QPalette.Window, couleur)
- self.setPalette(palette)
-
- def chargerImage(self, image):
- self._image = image
- self.maj()
- def maj(self):
- self.ui.exi_image.chargerImage(self._image)
- self.ui.exi_nom.majTexte(self._image.nom())
- self.ui.exi_details.majTexte((self._image.extension().replace(".", "")).upper())
- self.ui.exi_sType.majTexte(self._image.libelleSType())
-
- def image(self):
- return self._image
- def editer(self):
- fen = Editerimage()
- fen.charger(self.image())
- fen.show()
- r = fen.exec_()
- if r == 1:
- self._image = fen.rimage()
- self._image.enregistrer()
- del fen
- self.maj()
-
- def appliquerFiltre(self, filtreNom, filtreSType = -1):
- self.setVisible(filtreNom in self._image.nom() and \
- (filtreSType == -1 or self._image.sType() == filtreSType))
- def clic(self):
- if not self._selectionnee:
- self.fenetre.majSelection(self)
- self.selectionner(True)
- def doubleClic(self):
- self.fenetre.valider()
-
- def mousePressEvent(self, event):
- if event.button() == 1:
- self.clic()
- def mouseDoubleClickEvent(self, event):
- if event.button() == 1:
- self.doubleClic()
- class Editerimage(QDialog):
- def __init__(self, parent=None):
- """initialisation de la fenetre"""
- super (Editerimage, self).__init__(parent)
- self.createWidgets()
- self._rimage = None
- def createWidgets(self):
- """construction de l'interface"""
- self.ui = Ui_edi_fenetre()
- self.ui.setupUi(self)
- self.connect(self.ui.edi_enregistrer, SIGNAL("clicked()"), self.enregistrer)
- self.connect(self.ui.edi_annuler, SIGNAL("clicked()"), self.annuler)
-
- def charger(self, rimage):
- """charge la rimage en parametre et affiche ses caracteristiques"""
- self._rimage = rimage
- self.ui.edi_nom.majTexte(self._rimage.nom())
- self.ui.edi_sType.setCurrentIndex((self._rimage.sType() - 1))
-
- def rimage(self):
- """retourne l'image modifiee"""
- return self._rimage
- def enregistrer(self):
- self._rimage.majNom(self.ui.edi_nom.texte())
- self._rimage.majSType((self.ui.edi_sType.currentIndex() + 1))
- self.done(1)
-
- def annuler(self):
- self.done(0)
-
-
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- # img = RImage()
- # img.importer("C:\\python_tmp\\dm\\DMonde\\rsc\\colonne.png")
- # img = RImage()
- # img.importer("C:\\python_tmp\\dm\\DMonde\\rsc\\orc.png")
- # img = RImage()
- # img.importer("C:\\python_tmp\\dm\\DMonde\\rsc\\dragon.png")
-
- img = selectionImage()
- if img:
- print img.nom()
- exit()
|