icon.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. from core.utils import _
  18. # default icon set
  19. import grass_icons
  20. iconSetDefault = grass_icons.iconSet
  21. iconPathDefault = grass_icons.iconPath
  22. iconTheme = UserSettings.Get(group = 'appearance', key = 'iconTheme', subkey = 'type')
  23. if iconTheme != 'grass':
  24. sys.stderr.write(_("Unknown iconset '%s', using default 'grass'...\n") % (iconTheme))
  25. iconSet = iconSetDefault
  26. iconPath = iconPathDefault
  27. # join paths
  28. try:
  29. if iconPath and not os.path.exists(iconPath):
  30. raise OSError
  31. for key, img in iconSet.iteritems():
  32. if key not in iconSet or \
  33. iconSet[key] is None: # add key
  34. iconSet[key] = img
  35. iconSet[key] = os.path.join(iconPath, iconSet[key])
  36. except StandardError, e:
  37. sys.exit(_("Unable to load icon theme. Reason: %s. Quiting wxGUI...") % e)
  38. class MetaIcon:
  39. """!Handle icon metadata (image path, tooltip, ...)
  40. """
  41. def __init__(self, img, label = None, desc = None):
  42. self.imagepath = iconSet.get(img, wx.ART_MISSING_IMAGE)
  43. if not self.imagepath:
  44. self.type = 'unknown'
  45. else:
  46. if self.imagepath.find ('wxART_') > -1:
  47. self.type = 'wx'
  48. else:
  49. self.type = 'img'
  50. self.label = label
  51. if desc:
  52. self.description = desc
  53. else:
  54. self.description = ''
  55. def __str__(self):
  56. return "label=%s, img=%s, type=%s" % (self.label, self.imagepath, self.type)
  57. def GetBitmap(self, size = None):
  58. bmp = None
  59. if self.type == 'wx':
  60. bmp = wx.ArtProvider.GetBitmap(id = self.imagepath, client = wx.ART_TOOLBAR, size = size)
  61. elif self.type == 'img':
  62. if os.path.isfile(self.imagepath) and os.path.getsize(self.imagepath):
  63. if size and len(size) == 2:
  64. image = wx.Image(name = self.imagepath)
  65. image.Rescale(size[0], size[1])
  66. bmp = image.ConvertToBitmap()
  67. elif self.imagepath:
  68. bmp = wx.Bitmap(name = self.imagepath)
  69. return bmp
  70. def GetLabel(self):
  71. return self.label
  72. def GetDesc(self):
  73. return self.description
  74. def GetImageName(self):
  75. return os.path.basename(self.imagepath)
  76. def SetLabel(self, label = None, desc = None):
  77. """!Set label/description for icon
  78. @param label icon label (None for no change)
  79. @param desc icon description (None for no change)
  80. @return copy of original object
  81. """
  82. cobj = copy.copy(self)
  83. if label:
  84. cobj.label = label
  85. if desc:
  86. cobj.description = desc
  87. return cobj