GUIElements.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ############################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://caram.cl/software/flatcam #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 2/5/2014 #
  6. # MIT Licence #
  7. ############################################################
  8. from gi.repository import Gtk
  9. import re
  10. from copy import copy
  11. import FlatCAMApp
  12. class RadioSet(Gtk.Box):
  13. def __init__(self, choices):
  14. """
  15. The choices are specified as a list of dictionaries containing:
  16. * 'label': Shown in the UI
  17. * 'value': The value returned is selected
  18. :param choices: List of choices. See description.
  19. :type choices: list
  20. """
  21. Gtk.Box.__init__(self)
  22. self.choices = copy(choices)
  23. self.group = None
  24. for choice in self.choices:
  25. if self.group is None:
  26. choice['radio'] = Gtk.RadioButton.new_with_label(None, choice['label'])
  27. self.group = choice['radio']
  28. else:
  29. choice['radio'] = Gtk.RadioButton.new_with_label_from_widget(self.group, choice['label'])
  30. self.pack_start(choice['radio'], expand=True, fill=False, padding=2)
  31. choice['radio'].connect('toggled', self.on_toggle)
  32. self.group_toggle_fn = lambda x, y: None
  33. def on_toggle(self, btn):
  34. if btn.get_active():
  35. self.group_toggle_fn(btn, self.get_value)
  36. return
  37. def get_value(self):
  38. for choice in self.choices:
  39. if choice['radio'].get_active():
  40. return choice['value']
  41. FlatCAMApp.App.log.error("No button was toggled in RadioSet.")
  42. return None
  43. def set_value(self, val):
  44. for choice in self.choices:
  45. if choice['value'] == val:
  46. choice['radio'].set_active(True)
  47. return
  48. FlatCAMApp.App.log.error("Value given is not part of this RadioSet: %s" % str(val))
  49. class LengthEntry(Gtk.Entry):
  50. def __init__(self, output_units='IN'):
  51. Gtk.Entry.__init__(self)
  52. self.output_units = output_units
  53. self.format_re = re.compile(r"^([^\s]+)(?:\s([a-zA-Z]+))?$")
  54. # Unit conversion table OUTPUT-INPUT
  55. self.scales = {
  56. 'IN': {'MM': 1/25.4},
  57. 'MM': {'IN': 25.4}
  58. }
  59. self.connect('activate', self.on_activate)
  60. def on_activate(self, *args):
  61. val = self.get_value()
  62. if val is not None:
  63. self.set_text(str(val))
  64. else:
  65. FlatCAMApp.App.log.warning("Could not interpret entry: %s" % self.get_text())
  66. def get_value(self):
  67. raw = self.get_text().strip(' ')
  68. match = self.format_re.search(raw)
  69. if not match:
  70. return None
  71. try:
  72. if match.group(2) is not None and match.group(2).upper() in self.scales:
  73. return float(match.group(1))*self.scales[self.output_units][match.group(2).upper()]
  74. else:
  75. return float(match.group(1))
  76. except:
  77. FlatCAMApp.App.log.error("Could not parse value in entry: %s" % str(raw))
  78. return None
  79. def set_value(self, val):
  80. self.set_text(str(val))
  81. class FloatEntry(Gtk.Entry):
  82. def __init__(self):
  83. Gtk.Entry.__init__(self)
  84. self.connect('activate', self.on_activate)
  85. def on_activate(self, *args):
  86. val = self.get_value()
  87. if val is not None:
  88. self.set_text(str(val))
  89. else:
  90. FlatCAMApp.App.log.warning("Could not interpret entry: %s" % self.get_text())
  91. def get_value(self):
  92. raw = self.get_text().strip(' ')
  93. try:
  94. evaled = eval(raw)
  95. except:
  96. FlatCAMApp.App.log.error("Could not evaluate: %s" % str(raw))
  97. return None
  98. return float(evaled)
  99. def set_value(self, val):
  100. self.set_text(str(val))
  101. class IntEntry(Gtk.Entry):
  102. def __init__(self):
  103. Gtk.Entry.__init__(self)
  104. def get_value(self):
  105. return int(self.get_text())
  106. def set_value(self, val):
  107. self.set_text(str(val))
  108. class FCEntry(Gtk.Entry):
  109. def __init__(self):
  110. Gtk.Entry.__init__(self)
  111. def get_value(self):
  112. return self.get_text()
  113. def set_value(self, val):
  114. self.set_text(str(val))
  115. class EvalEntry(Gtk.Entry):
  116. def __init__(self):
  117. Gtk.Entry.__init__(self)
  118. def on_activate(self, *args):
  119. val = self.get_value()
  120. if val is not None:
  121. self.set_text(str(val))
  122. else:
  123. FlatCAMApp.App.log.warning("Could not interpret entry: %s" % self.get_text())
  124. def get_value(self):
  125. raw = self.get_text().strip(' ')
  126. try:
  127. return eval(raw)
  128. except:
  129. FlatCAMApp.App.log.error("Could not evaluate: %s" % str(raw))
  130. return None
  131. def set_value(self, val):
  132. self.set_text(str(val))
  133. class FCCheckBox(Gtk.CheckButton):
  134. def __init__(self, label=''):
  135. Gtk.CheckButton.__init__(self, label=label)
  136. def get_value(self):
  137. return self.get_active()
  138. def set_value(self, val):
  139. self.set_active(val)