singleread.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.board import available_boards
  10. from ..base.storage import storage
  11. from ..base.globals import glob as global_objects
  12. from .. import config
  13. tr = kcgw.tr
  14. __widget_id__ = None
  15. class SingleReadWidget(kcgw.KCGWidgets):
  16. """
  17. The actual single read window.
  18. """
  19. def __init__(self, unique_id, parent=None):
  20. """
  21. Initialise the single read widget
  22. :param unique_id: the id for this widget
  23. :param parent: parent object
  24. :return: -
  25. """
  26. super(SingleReadWidget, self).__init__()
  27. self.id = unique_id
  28. self.par = parent
  29. self.layout = QtGui.QGridLayout()
  30. self.outerLayout = QtGui.QVBoxLayout()
  31. self.setLayout(self.outerLayout)
  32. # ---------[ Create Labels and corresponding Fields ]---------
  33. self.single_read_button = self.createButton(tr("Button", "Single Read"), connect=self.on_single_read)
  34. self.interval_spinbox = self.createSpinbox(0, 100000, start_value=1000)
  35. self.interval_spinbox.valueChanged.connect(self.set_interval)
  36. self.board_ticks = {board_id: self.createCheckbox(str(board_id), checked=True if board_id==0 else False) for board_id in available_boards}
  37. self.board_ticks_continuous_read = {
  38. board_id: self.createCheckbox(str(board_id),
  39. connect=lambda state, b_id=board_id:
  40. self.on_continuous_read(state, board_id=b_id))
  41. for board_id in available_boards
  42. }
  43. for board_id, tick in self.board_ticks_continuous_read.items():
  44. Elements.addItem('acquireTrigger_{}'.format(board_id), tick)
  45. Elements.addItem(["acquire_{}".format(board_id), "no_board_{}".format(board_id)], tick)
  46. Elements.addItem('continuous_read_{}'.format(board_id), tick)
  47. for board_id, tick in self.board_ticks.iteritems():
  48. Elements.addItem('acquireTrigger_{}'.format(board_id), tick)
  49. Elements.addItem(["acquire_{}".format(board_id), "no_board_{}".format(board_id)], tick)
  50. # --------[ Fill Grid ]----------------
  51. self.layout.addWidget(self.single_read_button, 0, 1)
  52. # self.layout.addWidget(self.continuous_read_button, 0, 1)
  53. self.layout.addWidget(self.createLabel(tr("Label", "Interval:") + " (ms)"), 1, 0)
  54. self.layout.addWidget(self.interval_spinbox, 1, 1)
  55. self.setWindowTitle(tr("Heading", "Single and Continuous Read"))
  56. self.tickLayout = QtGui.QHBoxLayout()
  57. self.tickLayout.addWidget(self.createLabel(
  58. tr("Label", "Boards"), tooltip=tr("Tooltip", "Check the board you want to select for single read"))
  59. )
  60. self.tickLayout.addStretch(1)
  61. for tick in self.board_ticks.values():
  62. self.tickLayout.addWidget(tick)
  63. self.tickLayoutContinuousRead = QtGui.QHBoxLayout()
  64. self.tickLayoutContinuousRead.addWidget(self.createLabel(
  65. tr("Label", "Continuous Read"),
  66. tooltip=tr("Tooltip",
  67. "Tick the board you want to continuously read."
  68. "\nThe selected boards will continue reading until the checkbox is unticked.")
  69. )
  70. )
  71. self.tickLayoutContinuousRead.addStretch(1)
  72. for tick in self.board_ticks_continuous_read.values():
  73. self.tickLayoutContinuousRead.addWidget(tick)
  74. self.outerLayout.addLayout(self.tickLayoutContinuousRead)
  75. self.outerLayout.addLayout(self.tickLayout)
  76. self.outerLayout.addLayout(self.layout)
  77. self.outerLayout.addStretch(1)
  78. self.set_interval()
  79. def on_single_read(self):
  80. """
  81. Perform a single read on all boards which ticks are set
  82. :return:
  83. """
  84. for board_id, tick in self.board_ticks.iteritems():
  85. if tick.isChecked() and tick.isEnabled():
  86. bif.bk_single_read(board_id)
  87. def on_continuous_read(self, state, board_id):
  88. """
  89. Toggle function to toggle continuous read when the corresponding button is pressed.
  90. :param bool state: True to activate and False to deactivate continuous read for board_id
  91. :param int? board_id: the id of the board to take action with
  92. :return: -
  93. """
  94. # if self.board_ticks_continuous_read[id].isChecked():
  95. self.set_interval()
  96. if state:
  97. # self.continuous_read_button.setText(tr("Button", "Stop Continuous Read"))
  98. # self.continuous_read[id] = True
  99. self.board_ticks_continuous_read[board_id].setStyleSheet("border-color: green; border-width: 3px;")
  100. else:
  101. self.board_ticks_continuous_read[board_id].setStyleSheet("")
  102. bif.bk_continuous_read(board_id) # interval=self.interval_spinbox.value())
  103. def set_interval(self):
  104. """
  105. Set the interval between reads
  106. """
  107. storage.continuous_interval = self.interval_spinbox.value()
  108. def closeEvent(self, event):
  109. """
  110. Event handler to handle the event of closing this window and gracefully delete ids and such
  111. :param event: the event to process (gets passed by PyQt)
  112. """
  113. global __widget_id__
  114. __widget_id__ = None
  115. del self.par.widgets[self.id]
  116. Elements.removeItem(['no_board', 'acquire'],
  117. [
  118. self.single_read_button,
  119. # self.continuous_read_button
  120. ])
  121. for board_id, tick in self.board_ticks_continuous_read.items():
  122. Elements.removeItem(
  123. [
  124. "continuous_read_{}".format(board_id),
  125. "acquireTrigger_{}".format(board_id),
  126. "acquire_{}".format(board_id),
  127. "no_board_{}".format(board_id)
  128. ], tick)
  129. for board_id, tick in self.board_ticks.iteritems():
  130. Elements.removeItem(
  131. [
  132. "acquireTrigger_{}".format(board_id),
  133. "acquire_{}".format(board_id),
  134. "no_board_{}".format(board_id)
  135. ], tick)
  136. super(SingleReadWidget, self).closeEvent(event)
  137. def add_single_read_widget():
  138. """
  139. Add this widget to the gui.
  140. This function will actually open the subwindow.
  141. :return: -
  142. """
  143. global __widget_id__
  144. if __widget_id__:
  145. global_objects.get_global('area').widgets[__widget_id__].setFocus()
  146. else:
  147. nid = kcgw.idg.genid()
  148. __widget_id__ = nid
  149. w = SingleReadWidget(nid, global_objects.get_global('area'))
  150. global_objects.get_global('area').newWidget(w, "Single and Continuous Read", nid, widget_type=4, minSize=True) # TODO: proper type
  151. kcgw.register_widget(QtGui.QIcon(config.install_path + config.singleReadIcon),
  152. tr("Heading", "Single Read"),
  153. add_single_read_widget,
  154. "Ctrl+i"
  155. )