|
| 1 | +# This is an example custom dialog for more advanced users. |
| 2 | +# Complex custom dialogs can be directly coded using the PyQt framework |
| 3 | + |
| 4 | +from pyqtgraph.Qt import QtGui, QtCore |
| 5 | +from gui.custom_variables_dialog import Slider_var, Spin_var |
| 6 | + |
| 7 | +# Custom Variable dialog |
| 8 | +class Custom_variables_dialog(QtGui.QDialog): |
| 9 | + # Dialog for setting and getting task variables. |
| 10 | + def __init__(self, parent, board): |
| 11 | + super(QtGui.QDialog, self).__init__(parent) |
| 12 | + self.setWindowTitle("Blink Variable GUI") |
| 13 | + self.layout = QtGui.QVBoxLayout(self) |
| 14 | + self.variables_grid = Variables_grid(self, board) |
| 15 | + self.layout.addWidget(self.variables_grid) |
| 16 | + self.layout.setContentsMargins(0, 0, 0, 0) |
| 17 | + self.setLayout(self.layout) |
| 18 | + |
| 19 | + |
| 20 | +class Variables_grid(QtGui.QWidget): |
| 21 | + def __init__(self, parent, board): |
| 22 | + super(QtGui.QWidget, self).__init__(parent) |
| 23 | + variables = board.sm_info["variables"] |
| 24 | + self.grid_layout = QtGui.QGridLayout() |
| 25 | + initial_variables_dict = {v_name: v_value_str for (v_name, v_value_str) in sorted(variables.items())} |
| 26 | + self.variables_gui = Variables_gui(self, self.grid_layout, board, initial_variables_dict) |
| 27 | + self.setLayout(self.grid_layout) |
| 28 | + |
| 29 | + |
| 30 | +class Variables_gui(QtGui.QWidget): |
| 31 | + def __init__(self, parent, grid_layout, board, init_vars): |
| 32 | + super(QtGui.QWidget, self).__init__(parent) |
| 33 | + self.board = board |
| 34 | + |
| 35 | + # create widgets |
| 36 | + widget = QtGui.QWidget() |
| 37 | + layout = QtGui.QGridLayout() |
| 38 | + row = 0 |
| 39 | + |
| 40 | + # blink rate controls |
| 41 | + self.blink_rate = Slider_var(init_vars, "⏱️ <b>Blink Rate</b>", 1, 15, 0.5, "blink_rate") |
| 42 | + self.blink_rate.setSuffix(" Hz") |
| 43 | + self.blink_rate.setHint("Frequency of alternating LED blinks") |
| 44 | + self.blink_rate.setBoard(board) |
| 45 | + self.blink_rate.add_to_grid(layout, row) |
| 46 | + row += 1 |
| 47 | + self.min_btn = QtGui.QPushButton("min") |
| 48 | + self.mid_btn = QtGui.QPushButton("50%") |
| 49 | + self.max_btn = QtGui.QPushButton("max") |
| 50 | + for col,btn in enumerate([self.min_btn,self.mid_btn,self.max_btn]): |
| 51 | + layout.addWidget(btn, row,col, QtCore.Qt.AlignCenter) |
| 52 | + btn.setFocusPolicy(QtCore.Qt.NoFocus) |
| 53 | + btn.setMaximumWidth(70) |
| 54 | + row += 1 |
| 55 | + |
| 56 | + # separator |
| 57 | + layout.addWidget(QtGui.QLabel("<hr>"), row, 0, 1, 4) |
| 58 | + row += 1 |
| 59 | + |
| 60 | + # radio buttons |
| 61 | + red_is_enabled = eval(init_vars["red_enabled"]) |
| 62 | + green_is_enabled = eval(init_vars["green_enabled"]) |
| 63 | + self.both_radio = QtGui.QRadioButton() |
| 64 | + self.both_lbl = QtGui.QLabel("🔴Both🟢") |
| 65 | + self.red_radio = QtGui.QRadioButton() |
| 66 | + self.red_lbl = QtGui.QLabel("Red") |
| 67 | + self.red_lbl.setStyleSheet("border:3px solid red;background:red;border-radius:3px;") # you can use css styling |
| 68 | + self.green_radio = QtGui.QRadioButton() |
| 69 | + self.green_lbl = QtGui.QLabel("Green") |
| 70 | + self.green_lbl.setStyleSheet("border-radius:3px;border:3px solid green;background:green;color:white") |
| 71 | + if red_is_enabled and green_is_enabled: |
| 72 | + self.both_radio.setChecked(True) |
| 73 | + self.red_radio.setChecked(False) |
| 74 | + self.green_radio.setChecked(False) |
| 75 | + else: |
| 76 | + self.both_radio.setChecked(False) |
| 77 | + if red_is_enabled: |
| 78 | + self.red_radio.setChecked(True) |
| 79 | + self.green_radio.setChecked(False) |
| 80 | + elif green_is_enabled: |
| 81 | + self.red_radio.setChecked(False) |
| 82 | + self.green_radio.setChecked(True) |
| 83 | + else: |
| 84 | + self.red_radio.setChecked(False) |
| 85 | + self.green_radio.setChecked(False) |
| 86 | + |
| 87 | + layout.addWidget(self.both_lbl, row, 0, QtCore.Qt.AlignCenter) |
| 88 | + layout.addWidget(self.red_lbl, row, 1, QtCore.Qt.AlignCenter) |
| 89 | + layout.addWidget(self.green_lbl, row, 2, QtCore.Qt.AlignCenter) |
| 90 | + row += 1 |
| 91 | + layout.addWidget(self.both_radio, row, 0, QtCore.Qt.AlignCenter) |
| 92 | + layout.addWidget(self.red_radio, row, 1, QtCore.Qt.AlignCenter) |
| 93 | + layout.addWidget(self.green_radio, row, 2, QtCore.Qt.AlignCenter) |
| 94 | + row += 1 |
| 95 | + |
| 96 | + # counts |
| 97 | + self.red_count = Spin_var(init_vars, "<b>Red count</b>", 1, 10, 1, "red_count") |
| 98 | + self.red_count.setBoard(board) |
| 99 | + self.red_count.add_to_grid(layout, row) |
| 100 | + row += 1 |
| 101 | + self.green_count = Spin_var(init_vars, "<b>Green count</b>", 1, 10, 1, "green_count") |
| 102 | + self.green_count.setBoard(board) |
| 103 | + self.green_count.add_to_grid(layout, row) |
| 104 | + row += 1 |
| 105 | + |
| 106 | + # image |
| 107 | + self.picture = QtGui.QLabel() |
| 108 | + image = QtGui.QPixmap("config/user_variable_dialogs/example_image.png") |
| 109 | + self.picture.setPixmap(image) |
| 110 | + layout.addWidget(self.picture, row, 0, 1, 4) |
| 111 | + row += 1 |
| 112 | + |
| 113 | + # gif |
| 114 | + self.gif = QtGui.QLabel() |
| 115 | + self.movie = QtGui.QMovie("config/user_variable_dialogs/example_movie.gif") |
| 116 | + self.gif.setMovie(self.movie) |
| 117 | + self.movie.start() |
| 118 | + layout.addWidget(self.gif, row, 0, 1, 4) |
| 119 | + row += 1 |
| 120 | + |
| 121 | + layout.setRowStretch(row, 1) |
| 122 | + widget.setLayout(layout) |
| 123 | + grid_layout.addWidget(widget, 0, 0, QtCore.Qt.AlignLeft) |
| 124 | + |
| 125 | + # connect some buttons to functions |
| 126 | + self.min_btn.clicked.connect(self.slide_to_min) |
| 127 | + self.mid_btn.clicked.connect(self.slide_to_mid) |
| 128 | + self.max_btn.clicked.connect(self.slide_to_max) |
| 129 | + self.both_radio.clicked.connect(self.update_count_options) |
| 130 | + self.red_radio.clicked.connect(self.update_count_options) |
| 131 | + self.green_radio.clicked.connect(self.update_count_options) |
| 132 | + |
| 133 | + # add close shortcut |
| 134 | + self.close_shortcut = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+W"), self) |
| 135 | + self.close_shortcut.activated.connect(self.close) |
| 136 | + |
| 137 | + def update_count_options(self): |
| 138 | + red_val = self.red_radio.isChecked() | self.both_radio.isChecked() |
| 139 | + green_val = self.green_radio.isChecked() | self.both_radio.isChecked() |
| 140 | + self.board.set_variable("red_enabled", red_val) # update variable |
| 141 | + self.board.set_variable("green_enabled", green_val) |
| 142 | + self.red_count.setEnabled(red_val) # enable or disable control |
| 143 | + self.green_count.setEnabled(green_val) |
| 144 | + if not self.board.framework_running: # Value returned later. |
| 145 | + msg = QtGui.QMessageBox() |
| 146 | + msg.setText("Variable Changed") |
| 147 | + msg.exec() |
| 148 | + |
| 149 | + def slide_to_min(self): |
| 150 | + self.blink_rate.slider.setValue(1) |
| 151 | + self.blink_rate.update_val_lbl() |
| 152 | + self.blink_rate.set() |
| 153 | + |
| 154 | + def slide_to_mid(self): |
| 155 | + self.blink_rate.slider.setValue(7.5) |
| 156 | + self.blink_rate.update_val_lbl() |
| 157 | + self.blink_rate.set() |
| 158 | + |
| 159 | + def slide_to_max(self): |
| 160 | + self.blink_rate.slider.setValue(15) |
| 161 | + self.blink_rate.update_val_lbl() |
| 162 | + self.blink_rate.set() |
0 commit comments