SingleReadWidget.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # -*- coding: utf-8 -*-
  2. """
  3. This Module is the SingleRead window.
  4. """
  5. from PyQt4 import QtGui
  6. from ..base import kcgwidget as kcgw
  7. from ..base import backendinterface as bif
  8. from ..base.groupedelements import Elements
  9. from ..base.backend import board
  10. from ..base.backend.board import available_boards
  11. from ..base.storage import storage
  12. from ..base.globals import glob as global_objects
  13. from .. import config
  14. import time
  15. tr = kcgw.tr
  16. __widget_id__ = None
  17. class SingleReadWidget(kcgw.KCGWidgets):
  18. """
  19. The actual single read window.
  20. """
  21. def __init__(self, unique_id, parent=None):
  22. """
  23. Initialise the single read widget
  24. :param unique_id: the id for this widget
  25. :param parent: parent object
  26. :return: -
  27. """
  28. super(SingleReadWidget, self).__init__()
  29. self.id = unique_id
  30. self.par = parent
  31. self.layout = QtGui.QGridLayout()
  32. self.outerLayout = QtGui.QVBoxLayout()
  33. self.setLayout(self.outerLayout)
  34. # ---------[ Create Labels and corresponding Fields ]---------
  35. self.single_read_button = self.createButton(tr("Button", "Single Acquisition"), connect=self.on_single_read)
  36. self.continuous_read_button = self.createButton(tr("Button", "Continuous Acquisition"),
  37. connect=lambda: self.on_continuous_read(available_boards[0]))
  38. self.single_read_input = self.createInput()
  39. self.interval_spinbox = self.createSpinbox(0, 100000, start_value=1000)
  40. self.interval_spinbox.valueChanged.connect(self.set_interval)
  41. self.board_ticks = {board_id: self.createCheckbox(str(board_id), checked=True if board_id==0 else False) for board_id in available_boards}
  42. self.board_ticks_continuous_read = {
  43. board_id: self.createCheckbox(str(board_id),
  44. connect=lambda state, b_id=board_id:
  45. self.on_continuous_read(state, board_id=b_id))
  46. for board_id in available_boards
  47. }
  48. for board_id, tick in list(self.board_ticks_continuous_read.items()):
  49. Elements.addItem('acquireTrigger_{}'.format(board_id), tick)
  50. Elements.addItem(["acquire_{}".format(board_id), "no_board_{}".format(board_id)], tick)
  51. Elements.addItem('continuous_read_{}'.format(board_id), tick)
  52. for board_id, tick in self.board_ticks.items():
  53. Elements.addItem('acquireTrigger_{}'.format(board_id), tick)
  54. Elements.addItem(["acquire_{}".format(board_id), "no_board_{}".format(board_id)], tick)
  55. board_id = available_boards[0]
  56. Elements.addItem(
  57. [
  58. "acquire_{}".format(board_id),
  59. "no_board_{}".format(board_id)
  60. ],
  61. [
  62. self.single_read_button,
  63. self.continuous_read_button
  64. ])
  65. Elements.addItem('continuous_read_{}'.format(board_id), self.single_read_button)
  66. # --------[ Fill Grid ]----------------
  67. self.layout.addWidget(self.continuous_read_button, 0, 1)
  68. self.layout.addWidget(self.single_read_button, 1, 1)
  69. self.layout.addWidget(self.createLabel(tr("Label", "info:")), 2, 0)
  70. self.layout.addWidget(self.single_read_input, 2, 1)
  71. self.layout.addWidget(self.createLabel(tr("Label", "Interval:") + " (ms)"), 3, 0)
  72. self.layout.addWidget(self.interval_spinbox, 3, 1)
  73. self.setWindowTitle(tr("Heading", "Single and Continuous Read"))
  74. #01.04.2021 by Timo
  75. #We currently only support a single board.
  76. #However, this might change again in the future, so we only hide
  77. #the board selection checkbox until then, to avoid confusion
  78. '''
  79. self.tickLayout = QtGui.QHBoxLayout()
  80. self.tickLayout.addWidget(self.createLabel(
  81. tr("Label", "Boards"), tooltip=tr("Tooltip", "Check the board you want to select for single read"))
  82. )
  83. self.tickLayout.addStretch(1)
  84. for tick in list(self.board_ticks.values()):
  85. self.tickLayout.addWidget(tick)
  86. self.tickLayoutContinuousRead = QtGui.QHBoxLayout()
  87. self.tickLayoutContinuousRead.addWidget(self.createLabel(
  88. tr("Label", "Continuous Read"),
  89. tooltip=tr("Tooltip",
  90. "Tick the board you want to continuously read."
  91. "\nThe selected boards will continue reading until the checkbox is unticked.")
  92. )
  93. )
  94. self.tickLayoutContinuousRead.addStretch(1)
  95. for tick in list(self.board_ticks_continuous_read.values()):
  96. self.tickLayoutContinuousRead.addWidget(tick)
  97. self.outerLayout.addLayout(self.tickLayoutContinuousRead)
  98. self.outerLayout.addLayout(self.tickLayout)
  99. '''
  100. self.outerLayout.addLayout(self.layout)
  101. self.set_interval()
  102. def on_single_read(self):
  103. """
  104. Perform a single read on all boards which ticks are set
  105. :return:
  106. """
  107. for board_id, tick in self.board_ticks.items():
  108. if tick.isChecked() and tick.isEnabled():
  109. print(time.time(), 'on_single_read start')
  110. bif.bk_single_read(board_id, str(self.single_read_input.text()))
  111. print(time.time(), 'on_single_read stop')
  112. def on_continuous_read(self, board_id):
  113. """
  114. Toggle function to toggle continuous read when the corresponding button is pressed.
  115. :param bool state: True to activate and False to deactivate continuous read for board_id
  116. :param int? board_id: the id of the board to take action with
  117. :return: -
  118. """
  119. # if self.board_ticks_continuous_read[id].isChecked():
  120. self.set_interval()
  121. if not board.get_board_status(board_id).continuous_read:
  122. self.continuous_read_button.setText(tr("Button", "Stop Acquisition"))
  123. self.single_read_button.setEnabled(False)
  124. #self.continuous_read_button.setStyleSheet("QPushButton {background-color: green; color: black;}")
  125. else:
  126. self.continuous_read_button.setText(tr("Button", "Continuous Acquisition"))
  127. #self.continuous_read_button.setStyleSheet("")
  128. self.single_read_button.setEnabled(True)
  129. #Note:
  130. #bk_continuous_read is a toggle function.
  131. #Each call switches from off to on, and vice versa
  132. bif.bk_continuous_read(board_id) # interval=self.interval_spinbox.value())
  133. def set_interval(self):
  134. """
  135. Set the interval between reads
  136. """
  137. storage.continuous_interval = self.interval_spinbox.value()
  138. def closeEvent(self, event):
  139. """
  140. Event handler to handle the event of closing this window and gracefully delete ids and such
  141. :param event: the event to process (gets passed by PyQt)
  142. """
  143. global __widget_id__
  144. __widget_id__ = None
  145. del self.par.widgets[self.id]
  146. Elements.removeItem(
  147. [
  148. #"acquireTrigger_{}".format(board_id),
  149. "acquire_{}".format(board_id),
  150. "no_board_{}".format(board_id)
  151. ],
  152. [
  153. self.single_read_button,
  154. #self.continuous_read_button
  155. ])
  156. Elements.removeItem('continuous_read_{}'.format(board_id), self.single_read_button)
  157. for board_id, tick in list(self.board_ticks_continuous_read.items()):
  158. Elements.removeItem(
  159. [
  160. "continuous_read_{}".format(board_id),
  161. "acquireTrigger_{}".format(board_id),
  162. "acquire_{}".format(board_id),
  163. "no_board_{}".format(board_id)
  164. ], tick)
  165. for board_id, tick in self.board_ticks.items():
  166. Elements.removeItem(
  167. [
  168. "acquireTrigger_{}".format(board_id),
  169. "acquire_{}".format(board_id),
  170. "no_board_{}".format(board_id)
  171. ], tick)
  172. super(SingleReadWidget, self).closeEvent(event)
  173. def addSingleReadWidget():
  174. """
  175. Add this widget to the gui.
  176. This function will actually open the subwindow.
  177. :return: -
  178. """
  179. global __widget_id__
  180. if __widget_id__:
  181. global_objects.get_global('area').widgets[__widget_id__].setFocus()
  182. else:
  183. nid = kcgw.idg.genid()
  184. __widget_id__ = nid
  185. w = SingleReadWidget(nid, global_objects.get_global('area'))
  186. global_objects.get_global('area').newWidget(w, "Single and Continuous Read", nid, widget_type=4, minSize=True) # TODO: proper type
  187. kcgw.register_widget(QtGui.QIcon(config.icon_path(config.singleReadIcon)),
  188. tr("Heading", "Single Read"),
  189. addSingleReadWidget,
  190. "Ctrl+i"
  191. )