icon.py 3.2 KB

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