GUIElements.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from gi.repository import Gtk
  2. import re
  3. from copy import copy
  4. class RadioSet(Gtk.Box):
  5. def __init__(self, choices):
  6. """
  7. The choices are specified as a list of dictionaries containing:
  8. * 'label': Shown in the UI
  9. * 'value': The value returned is selected
  10. :param choices: List of choices. See description.
  11. :type choices: list
  12. """
  13. Gtk.Box.__init__(self)
  14. self.choices = copy(choices)
  15. self.group = None
  16. for choice in self.choices:
  17. if self.group is None:
  18. choice['radio'] = Gtk.RadioButton.new_with_label(None, choice['label'])
  19. self.group = choice['radio']
  20. else:
  21. choice['radio'] = Gtk.RadioButton.new_with_label_from_widget(self.group, choice['label'])
  22. self.pack_start(choice['radio'], expand=True, fill=False, padding=2)
  23. # choice['radio'].connect('toggled', self.on_toggle)
  24. # def on_toggle(self, *args):
  25. # return
  26. def get_value(self):
  27. for choice in self.choices:
  28. if choice['radio'].get_active():
  29. return choice['value']
  30. print "ERROR: No button was toggled in RadioSet."
  31. return None
  32. def set_value(self, val):
  33. for choice in self.choices:
  34. if choice['value'] == val:
  35. choice['radio'].set_active(True)
  36. return
  37. print "ERROR: Value given is not part of this RadioSet:", val
  38. class LengthEntry(Gtk.Entry):
  39. def __init__(self, output_units='IN'):
  40. Gtk.Entry.__init__(self)
  41. self.output_units = output_units
  42. self.format_re = re.compile(r"^([^\s]+)(?:\s([a-zA-Z]+))?$")
  43. # Unit conversion table OUTPUT-INPUT
  44. self.scales = {
  45. 'IN': {'MM': 1/25.4},
  46. 'MM': {'IN': 25.4}
  47. }
  48. self.connect('activate', self.on_activate)
  49. def on_activate(self, *args):
  50. val = self.get_value()
  51. if val is not None:
  52. self.set_text(str(val))
  53. else:
  54. print "WARNING: Could not interpret entry:", self.get_text()
  55. def get_value(self):
  56. raw = self.get_text().strip(' ')
  57. match = self.format_re.search(raw)
  58. if not match:
  59. return None
  60. try:
  61. if match.group(2) is not None and match.group(2).upper() in self.scales:
  62. return float(match.group(1))*self.scales[self.output_units][match.group(2).upper()]
  63. else:
  64. return float(match.group(1))
  65. except:
  66. print "ERROR: Could not parse value in entry:", raw
  67. return None
  68. def set_value(self, val):
  69. self.set_text(str(val))
  70. class FloatEntry(Gtk.Entry):
  71. def __init__(self):
  72. Gtk.Entry.__init__(self)
  73. self.connect('activate', self.on_activate)
  74. def on_activate(self, *args):
  75. val = self.get_value()
  76. if val is not None:
  77. self.set_text(str(val))
  78. else:
  79. print "WARNING: Could not interpret entry:", self.get_text()
  80. def get_value(self):
  81. raw = self.get_text().strip(' ')
  82. try:
  83. evaled = eval(raw)
  84. except:
  85. print "ERROR: Could not evaluate:", raw
  86. return None
  87. return float(evaled)
  88. def set_value(self, val):
  89. self.set_text(str(val))
  90. class IntEntry(Gtk.Entry):
  91. def __init__(self):
  92. Gtk.Entry.__init__(self)
  93. def get_value(self):
  94. return int(self.get_text())
  95. def set_value(self, val):
  96. self.set_text(str(val))
  97. class FCEntry(Gtk.Entry):
  98. def __init__(self):
  99. Gtk.Entry.__init__(self)
  100. def get_value(self):
  101. return self.get_text()
  102. def set_value(self, val):
  103. self.set_text(str(val))
  104. class FCCheckBox(Gtk.CheckButton):
  105. def __init__(self, label=''):
  106. Gtk.CheckButton.__init__(self, label=label)
  107. def get_value(self):
  108. return self.get_active()
  109. def set_value(self, val):
  110. self.set_active(val)