icon.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """!
  2. @package icons.icon
  3. @brief Icon metadata
  4. Classes:
  5. - MetaIcon
  6. (C) 2007-2008, 2010-2011 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Martin Landa <landa.martin gmail.com>
  10. @author Anna Kratochvilova <kratochanna gmail.com>
  11. """
  12. import os
  13. import sys
  14. import copy
  15. import wx
  16. from core.settings import UserSettings
  17. # default icon set
  18. import grass_icons
  19. iconSetDefault = grass_icons.iconSet
  20. iconPathDefault = grass_icons.iconPath
  21. iconTheme = UserSettings.Get(group = 'appearance', key = 'iconTheme', subkey = 'type')
  22. if iconTheme != 'grass':
  23. sys.stderr.write(_("Unknown iconset '%s', using default 'grass'...\n") % (iconTheme))
  24. iconSet = iconSetDefault
  25. iconPath = iconPathDefault
  26. # join paths
  27. try:
  28. if iconPath and not os.path.exists(iconPath):
  29. raise OSError
  30. for key, img in iconSet.iteritems():
  31. if key not in iconSet or \
  32. iconSet[key] is None: # add key
  33. iconSet[key] = img
  34. iconSet[key] = os.path.join(iconPath, iconSet[key])
  35. except StandardError, e:
  36. sys.exit(_("Unable to load icon theme. Reason: %s. Quiting wxGUI...") % e)
  37. class MetaIcon:
  38. """!Handle icon metadata (image path, tooltip, ...)
  39. """
  40. def __init__(self, img, label = None, desc = None):
  41. self.imagepath = iconSet.get(img, wx.ART_MISSING_IMAGE)
  42. if not self.imagepath:
  43. self.type = 'unknown'
  44. else:
  45. if self.imagepath.find ('wxART_') > -1:
  46. self.type = 'wx'
  47. else:
  48. self.type = 'img'
  49. self.label = label
  50. if desc:
  51. self.description = desc
  52. else:
  53. self.description = ''
  54. def __str__(self):
  55. return "label=%s, img=%s, type=%s" % (self.label, self.imagepath, self.type)
  56. def GetBitmap(self, size = None):
  57. bmp = None
  58. if self.type == 'wx':
  59. bmp = wx.ArtProvider.GetBitmap(id = self.imagepath, client = wx.ART_TOOLBAR, size = size)
  60. elif self.type == 'img':
  61. if os.path.isfile(self.imagepath) and os.path.getsize(self.imagepath):
  62. if size and len(size) == 2:
  63. image = wx.Image(name = self.imagepath)
  64. image.Rescale(size[0], size[1])
  65. bmp = image.ConvertToBitmap()
  66. elif self.imagepath:
  67. bmp = wx.Bitmap(name = self.imagepath)
  68. return bmp
  69. def GetLabel(self):
  70. return self.label
  71. def GetDesc(self):
  72. return self.description
  73. def GetImageName(self):
  74. return os.path.basename(self.imagepath)
  75. def SetLabel(self, label = None, desc = None):
  76. """!Set label/description for icon
  77. @param label icon label (None for no change)
  78. @param desc icon description (None for no change)
  79. @return copy of original object
  80. """
  81. cobj = copy.copy(self)
  82. if label:
  83. cobj.label = label
  84. if desc:
  85. cobj.description = desc
  86. return cobj