ressources.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 PyQt4.QtGui import QPixmap
  6. #le repertoire de l'application
  7. ppal = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
  8. def repApp():
  9. return ppal
  10. def repRessources():
  11. return ["ressources\\commun", \
  12. "ressources\\dd35", \
  13. "parties\\Partie1\\ressources"]
  14. class Ressource(object):
  15. """classe de base des ressources utilisees"""
  16. def __init__(self, cheminR):
  17. self._cheminR = cheminR
  18. def chemin(self):
  19. """renvoie le chemin absolu vers le fichier"""
  20. return os.path.join(repApp(), self._cheminR)
  21. def nom(self):
  22. #on vire l'extension
  23. nom = ""
  24. for car in reversed(self._cheminR):
  25. if car in ["\\", "/"]:
  26. break
  27. else:
  28. nom = car + nom
  29. nom = os.path.splitext(nom)[0]
  30. return nom
  31. def repR(self):
  32. """renvoie le repertoire, presente de maniere relative"""
  33. return os.path.dirname(self._cheminR)
  34. def extension(self):
  35. return os.path.splitext(self._cheminR)[1]
  36. def definition(self):
  37. return self._cheminR
  38. def existe(self):
  39. return os.path.isfile(self.chemin())
  40. def estValide(self):
  41. return self.existe()
  42. class Image(Ressource):
  43. """classe de base des ressources utilisees"""
  44. def __init__(self, cheminR):
  45. super(Image, self).__init__(cheminR)
  46. def pix(self, l = 0, h = 0):
  47. pix = QPixmap(self.chemin())
  48. if not pix.isNull():
  49. if l > 0 and h > 0:
  50. pix.scaled(l, h)
  51. elif l > 0 and h == 0:
  52. pix.scaledToWidth(l)
  53. elif l == 0 and h > 0:
  54. pix.scaledToHeigth(l)
  55. return pix
  56. def estValide(self):
  57. return self.existe() and \
  58. self.extension() in [".png", ".jpg", ".gif"]
  59. r = Image("ressources\\commun\\orc.png")
  60. print r.repR()