icon.py 3.2 KB

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