GUIElements.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. from PyQt4 import QtGui, QtCore
  2. from copy import copy
  3. import FlatCAMApp
  4. import re
  5. class RadioSet(QtGui.QWidget):
  6. def __init__(self, choices, orientation='horizontal', parent=None):
  7. """
  8. The choices are specified as a list of dictionaries containing:
  9. * 'label': Shown in the UI
  10. * 'value': The value returned is selected
  11. :param choices: List of choices. See description.
  12. :type choices: list
  13. """
  14. super(RadioSet, self).__init__(parent)
  15. self.choices = copy(choices)
  16. if orientation == 'horizontal':
  17. layout = QtGui.QHBoxLayout()
  18. else:
  19. layout = QtGui.QVBoxLayout()
  20. group = QtGui.QButtonGroup(self)
  21. for choice in self.choices:
  22. choice['radio'] = QtGui.QRadioButton(choice['label'])
  23. group.addButton(choice['radio'])
  24. layout.addWidget(choice['radio'], stretch=0)
  25. choice['radio'].toggled.connect(self.on_toggle)
  26. layout.addStretch()
  27. self.setLayout(layout)
  28. self.group_toggle_fn = lambda: None
  29. def on_toggle(self):
  30. FlatCAMApp.App.log.debug("Radio toggled")
  31. radio = self.sender()
  32. if radio.isChecked():
  33. self.group_toggle_fn()
  34. return
  35. def get_value(self):
  36. for choice in self.choices:
  37. if choice['radio'].isChecked():
  38. return choice['value']
  39. FlatCAMApp.App.log.error("No button was toggled in RadioSet.")
  40. return None
  41. def set_value(self, val):
  42. for choice in self.choices:
  43. if choice['value'] == val:
  44. choice['radio'].setChecked(True)
  45. return
  46. FlatCAMApp.App.log.error("Value given is not part of this RadioSet: %s" % str(val))
  47. class LengthEntry(QtGui.QLineEdit):
  48. def __init__(self, output_units='IN', parent=None):
  49. super(LengthEntry, self).__init__(parent)
  50. self.output_units = output_units
  51. self.format_re = re.compile(r"^([^\s]+)(?:\s([a-zA-Z]+))?$")
  52. # Unit conversion table OUTPUT-INPUT
  53. self.scales = {
  54. 'IN': {'IN': 1.0,
  55. 'MM': 1/25.4},
  56. 'MM': {'IN': 25.4,
  57. 'MM': 1.0}
  58. }
  59. def returnPressed(self, *args, **kwargs):
  60. val = self.get_value()
  61. if val is not None:
  62. self.set_text(QtCore.QString(str(val)))
  63. else:
  64. FlatCAMApp.App.log.warning("Could not interpret entry: %s" % self.get_text())
  65. def get_value(self):
  66. raw = str(self.text()).strip(' ')
  67. match = self.format_re.search(raw)
  68. if not match:
  69. return None
  70. try:
  71. if match.group(2) is not None and match.group(2).upper() in self.scales:
  72. return float(eval(match.group(1)))*float(self.scales[self.output_units][match.group(2).upper()])
  73. else:
  74. return float(eval(match.group(1)))
  75. except:
  76. FlatCAMApp.App.log.warning("Could not parse value in entry: %s" % str(raw))
  77. return None
  78. def set_value(self, val):
  79. self.setText(QtCore.QString(str(val)))
  80. class FloatEntry(QtGui.QLineEdit):
  81. def __init__(self, parent=None):
  82. super(FloatEntry, self).__init__(parent)
  83. def returnPressed(self, *args, **kwargs):
  84. val = self.get_value()
  85. if val is not None:
  86. self.set_text(QtCore.QString(str(val)))
  87. else:
  88. FlatCAMApp.App.log.warning("Could not interpret entry: %s" % self.text())
  89. def get_value(self):
  90. raw = str(self.text()).strip(' ')
  91. try:
  92. evaled = eval(raw)
  93. except:
  94. FlatCAMApp.App.log.error("Could not evaluate: %s" % str(raw))
  95. return None
  96. return float(evaled)
  97. def set_value(self, val):
  98. self.setText("%.6f" % val)
  99. class IntEntry(QtGui.QLineEdit):
  100. def __init__(self, parent=None, allow_empty=False, empty_val=None):
  101. super(IntEntry, self).__init__(parent)
  102. self.allow_empty = allow_empty
  103. self.empty_val = empty_val
  104. def get_value(self):
  105. if self.allow_empty:
  106. if str(self.text()) == "":
  107. return self.empty_val
  108. return int(self.text())
  109. def set_value(self, val):
  110. if val == self.empty_val and self.allow_empty:
  111. self.setText(QtCore.QString(""))
  112. return
  113. self.setText(QtCore.QString(str(val)))
  114. class FCEntry(QtGui.QLineEdit):
  115. def __init__(self, parent=None):
  116. super(FCEntry, self).__init__(parent)
  117. def get_value(self):
  118. return str(self.text())
  119. def set_value(self, val):
  120. self.setText(QtCore.QString(str(val)))
  121. class EvalEntry(QtGui.QLineEdit):
  122. def __init__(self, parent=None):
  123. super(EvalEntry, self).__init__(parent)
  124. def returnPressed(self, *args, **kwargs):
  125. val = self.get_value()
  126. if val is not None:
  127. self.setText(QtCore.QString(str(val)))
  128. else:
  129. FlatCAMApp.App.log.warning("Could not interpret entry: %s" % self.get_text())
  130. def get_value(self):
  131. raw = str(self.text()).strip(' ')
  132. try:
  133. return eval(raw)
  134. except:
  135. FlatCAMApp.App.log.error("Could not evaluate: %s" % str(raw))
  136. return None
  137. def set_value(self, val):
  138. self.setText(QtCore.QString(str(val)))
  139. class FCCheckBox(QtGui.QCheckBox):
  140. def __init__(self, label='', parent=None):
  141. super(FCCheckBox, self).__init__(QtCore.QString(label), parent)
  142. def get_value(self):
  143. return self.isChecked()
  144. def set_value(self, val):
  145. self.setChecked(val)
  146. class FCTextArea(QtGui.QPlainTextEdit):
  147. def __init__(self, parent=None):
  148. super(FCTextArea, self).__init__(parent)
  149. def set_value(self, val):
  150. self.setPlainText(val)
  151. def get_value(self):
  152. return str(self.toPlainText())
  153. class VerticalScrollArea(QtGui.QScrollArea):
  154. """
  155. This widget extends QtGui.QScrollArea to make a vertical-only
  156. scroll area that also expands horizontally to accomodate
  157. its contents.
  158. """
  159. def __init__(self, parent=None):
  160. QtGui.QScrollArea.__init__(self, parent=parent)
  161. self.setWidgetResizable(True)
  162. self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  163. self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
  164. def eventFilter(self, source, event):
  165. """
  166. The event filter gets automatically installed when setWidget()
  167. is called.
  168. :param source:
  169. :param event:
  170. :return:
  171. """
  172. if event.type() == QtCore.QEvent.Resize and source == self.widget():
  173. # FlatCAMApp.App.log.debug("VerticalScrollArea: Widget resized:")
  174. # FlatCAMApp.App.log.debug(" minimumSizeHint().width() = %d" % self.widget().minimumSizeHint().width())
  175. # FlatCAMApp.App.log.debug(" verticalScrollBar().width() = %d" % self.verticalScrollBar().width())
  176. self.setMinimumWidth(self.widget().sizeHint().width() +
  177. self.verticalScrollBar().sizeHint().width())
  178. # if self.verticalScrollBar().isVisible():
  179. # FlatCAMApp.App.log.debug(" Scroll bar visible")
  180. # self.setMinimumWidth(self.widget().minimumSizeHint().width() +
  181. # self.verticalScrollBar().width())
  182. # else:
  183. # FlatCAMApp.App.log.debug(" Scroll bar hidden")
  184. # self.setMinimumWidth(self.widget().minimumSizeHint().width())
  185. return QtGui.QWidget.eventFilter(self, source, event)
  186. class OptionalInputSection():
  187. def __init__(self, cb, optinputs):
  188. assert isinstance(cb, FCCheckBox)
  189. self.cb = cb
  190. self.optinputs = optinputs
  191. self.on_cb_change()
  192. self.cb.stateChanged.connect(self.on_cb_change)
  193. def on_cb_change(self):
  194. if self.cb.checkState():
  195. for widget in self.optinputs:
  196. widget.setEnabled(True)
  197. else:
  198. for widget in self.optinputs:
  199. widget.setEnabled(False)