icon.py 3.1 KB

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