|
| 1 | +import sys |
| 2 | +from easycoder import Handler, FatalError, RuntimeError |
| 3 | +from PySide6.QtCore import QTimer |
| 4 | +from PySide6.QtWidgets import ( |
| 5 | + QApplication, |
| 6 | + QCheckBox, |
| 7 | + QComboBox, |
| 8 | + QDateEdit, |
| 9 | + QDateTimeEdit, |
| 10 | + QDial, |
| 11 | + QDoubleSpinBox, |
| 12 | + QFontComboBox, |
| 13 | + QLabel, |
| 14 | + QLCDNumber, |
| 15 | + QLineEdit, |
| 16 | + QMainWindow, |
| 17 | + QProgressBar, |
| 18 | + QPushButton, |
| 19 | + QRadioButton, |
| 20 | + QSlider, |
| 21 | + QSpinBox, |
| 22 | + QTimeEdit, |
| 23 | + QVBoxLayout, |
| 24 | + QHBoxLayout, |
| 25 | + QGridLayout, |
| 26 | + QStackedLayout, |
| 27 | + QWidget |
| 28 | +) |
| 29 | + |
| 30 | +class Graphics(Handler): |
| 31 | + |
| 32 | + class MainWindow(QMainWindow): |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + super().__init__() |
| 36 | + |
| 37 | + def __init__(self, compiler): |
| 38 | + Handler.__init__(self, compiler) |
| 39 | + |
| 40 | + def getName(self): |
| 41 | + return 'pyside6' |
| 42 | + |
| 43 | + def closeEvent(self): |
| 44 | + print('window closed') |
| 45 | + |
| 46 | + ############################################################################# |
| 47 | + # Keyword handlers |
| 48 | + |
| 49 | + # Add a widget to a layout |
| 50 | + def k_add(self, command): |
| 51 | + if self.nextIsSymbol(): |
| 52 | + record = self.getSymbolRecord() |
| 53 | + command['widget'] = record['name'] |
| 54 | + if self.nextIs('to'): |
| 55 | + if self.nextIsSymbol(): |
| 56 | + record = self.getSymbolRecord() |
| 57 | + command['layout'] = record['name'] |
| 58 | + self.add(command) |
| 59 | + return True |
| 60 | + return False |
| 61 | + |
| 62 | + def r_add(self, command): |
| 63 | + widgetRecord = self.getVariable(command['widget']) |
| 64 | + layoutRecord = self.getVariable(command['layout']) |
| 65 | + if widgetRecord['keyword'] == 'layout': |
| 66 | + layoutRecord['widget'].addLayout(widgetRecord['widget']) |
| 67 | + else: |
| 68 | + layoutRecord['widget'].addWidget(widgetRecord['widget']) |
| 69 | + return self.nextPC() |
| 70 | + |
| 71 | + # Create a window |
| 72 | + def k_createWindow(self, command): |
| 73 | + command['title'] = 'Default' |
| 74 | + command['x'] = 100 |
| 75 | + command['y'] = 100 |
| 76 | + command['w'] = 640 |
| 77 | + command['h'] = 480 |
| 78 | + while True: |
| 79 | + token = self.peek() |
| 80 | + if token in ['title', 'at', 'size']: |
| 81 | + self.nextToken() |
| 82 | + if token == 'title': command['title'] = self.nextValue() |
| 83 | + elif token == 'at': |
| 84 | + command['x'] = self.nextValue() |
| 85 | + command['y'] = self.nextValue() |
| 86 | + elif token == 'size': |
| 87 | + command['w'] = self.nextValue() |
| 88 | + command['h'] = self.nextValue() |
| 89 | + else: break |
| 90 | + self.add(command) |
| 91 | + return True |
| 92 | + |
| 93 | + # Create a widget |
| 94 | + def k_createLayout(self, command): |
| 95 | + if self.nextIs('type'): |
| 96 | + command['type'] = self.nextToken() |
| 97 | + self.add(command) |
| 98 | + return True |
| 99 | + return False |
| 100 | + |
| 101 | + def k_createLabel(self, command): |
| 102 | + if self.peek() == 'text': |
| 103 | + self.nextToken() |
| 104 | + text = self.nextValue() |
| 105 | + else: text = '' |
| 106 | + command['text'] = text |
| 107 | + self.add(command) |
| 108 | + return True |
| 109 | + |
| 110 | + def k_createPushbutton(self, command): |
| 111 | + if self.peek() == 'text': |
| 112 | + self.nextToken() |
| 113 | + text = self.nextValue() |
| 114 | + else: text = '' |
| 115 | + command['text'] = text |
| 116 | + self.add(command) |
| 117 | + return True |
| 118 | + |
| 119 | + def k_create(self, command): |
| 120 | + if self.nextIsSymbol(): |
| 121 | + record = self.getSymbolRecord() |
| 122 | + command['name'] = record['name'] |
| 123 | + keyword = record['keyword'] |
| 124 | + if keyword == 'window': return self.k_createWindow(command) |
| 125 | + elif keyword == 'layout': return self.k_createLayout(command) |
| 126 | + elif keyword == 'label': return self.k_createLabel(command) |
| 127 | + elif keyword == 'pushbutton': return self.k_createPushbutton(command) |
| 128 | + return False |
| 129 | + |
| 130 | + def r_createWindow(self, command, record): |
| 131 | + window = self.MainWindow() |
| 132 | + window.setWindowTitle(self.getRuntimeValue(command['title'])) |
| 133 | + x = self.getRuntimeValue(command['x']) |
| 134 | + y = self.getRuntimeValue(command['y']) |
| 135 | + w = self.getRuntimeValue(command['w']) |
| 136 | + h = self.getRuntimeValue(command['h']) |
| 137 | + if x != None and y != None and w != None and h != None: |
| 138 | + window.setGeometry(x, y, w, h) |
| 139 | + record['window'] = window |
| 140 | + return self.nextPC() |
| 141 | + |
| 142 | + def r_createLayout(self, command, record): |
| 143 | + type = command['type'] |
| 144 | + if type == 'QHBoxLayout': layout = QHBoxLayout() |
| 145 | + elif type == 'QGridLayout': layout = QGridLayout() |
| 146 | + elif type == 'QStackedLayout': layout = QStackedLayout() |
| 147 | + else: layout = QVBoxLayout() |
| 148 | + record['widget'] = layout |
| 149 | + return self.nextPC() |
| 150 | + |
| 151 | + def r_createLabel(self, command, record): |
| 152 | + label = QLabel(self.getRuntimeValue(command['text'])) |
| 153 | + record['widget'] = label |
| 154 | + return self.nextPC() |
| 155 | + |
| 156 | + def r_createPushbutton(self, command, record): |
| 157 | + pushbutton = QPushButton(self.getRuntimeValue(command['text'])) |
| 158 | + record['widget'] = pushbutton |
| 159 | + return self.nextPC() |
| 160 | + |
| 161 | + def r_create(self, command): |
| 162 | + record = self.getVariable(command['name']) |
| 163 | + keyword = record['keyword'] |
| 164 | + if keyword == 'window': return self.r_createWindow(command, record) |
| 165 | + elif keyword == 'layout': return self.r_createLayout(command, record) |
| 166 | + elif keyword == 'label': return self.r_createLabel(command, record) |
| 167 | + elif keyword == 'pushbutton': return self.r_createPushbutton(command, record) |
| 168 | + return None |
| 169 | + |
| 170 | + # Initialize the graphics environment |
| 171 | + def k_init(self, command): |
| 172 | + if self.nextIs('graphics'): |
| 173 | + self.add(command) |
| 174 | + return True |
| 175 | + return False |
| 176 | + |
| 177 | + def r_init(self, command): |
| 178 | + self.app = QApplication(sys.argv) |
| 179 | + return self.nextPC() |
| 180 | + |
| 181 | + # Declare a label variable |
| 182 | + def k_label(self, command): |
| 183 | + return self.compileVariable(command, False) |
| 184 | + |
| 185 | + def r_label(self, command): |
| 186 | + return self.nextPC() |
| 187 | + |
| 188 | + # Declare a layout variable |
| 189 | + def k_layout(self, command): |
| 190 | + return self.compileVariable(command, False) |
| 191 | + |
| 192 | + def r_layout(self, command): |
| 193 | + return self.nextPC() |
| 194 | + |
| 195 | + # Declare a pushbutton variable |
| 196 | + def k_pushbutton(self, command): |
| 197 | + return self.compileVariable(command, False) |
| 198 | + |
| 199 | + def r_pushbutton(self, command): |
| 200 | + return self.nextPC() |
| 201 | + |
| 202 | + # Clean exit |
| 203 | + def on_last_window_closed(self): |
| 204 | + print("Last window closed! Performing cleanup...") |
| 205 | + self.program.kill() |
| 206 | + |
| 207 | + # This is called every 10ms to keep the main application running |
| 208 | + def flush(self): |
| 209 | + self.program.flushCB() |
| 210 | + |
| 211 | + # Resume execution at the line following 'start graphics' |
| 212 | + def resume(self): |
| 213 | + self.program.flush(self.nextPC()) |
| 214 | + |
| 215 | + # Show a window with a specified layout |
| 216 | + # show {name} in {window}} |
| 217 | + def k_show(self, command): |
| 218 | + if self.nextIsSymbol(): |
| 219 | + record = self.getSymbolRecord() |
| 220 | + if record['keyword'] == 'layout': |
| 221 | + command['layout'] = record['name'] |
| 222 | + if self.nextIs('in'): |
| 223 | + if self.nextIsSymbol(): |
| 224 | + record = self.getSymbolRecord() |
| 225 | + if record['keyword'] == 'window': |
| 226 | + command['window'] = record['name'] |
| 227 | + self.add(command) |
| 228 | + return True |
| 229 | + return False |
| 230 | + |
| 231 | + def r_show(self, command): |
| 232 | + layoutRecord = self.getVariable(command['layout']) |
| 233 | + windowRecord = self.getVariable(command['window']) |
| 234 | + window = windowRecord['window'] |
| 235 | + container = QWidget() |
| 236 | + container.setLayout(layoutRecord['widget']) |
| 237 | + window.setCentralWidget(container) |
| 238 | + window.show() |
| 239 | + return self.nextPC() |
| 240 | + |
| 241 | + # Start the graphics |
| 242 | + def k_start(self, command): |
| 243 | + if self.nextIs('graphics'): |
| 244 | + self.add(command) |
| 245 | + return True |
| 246 | + return False |
| 247 | + |
| 248 | + def r_start(self, command): |
| 249 | + timer = QTimer() |
| 250 | + timer.timeout.connect(self.flush) |
| 251 | + timer.start(10) |
| 252 | + QTimer.singleShot(500, self.resume) |
| 253 | + self.app.lastWindowClosed.connect(self.on_last_window_closed) |
| 254 | + self.app.exec() |
| 255 | + |
| 256 | + # Declare a window variable |
| 257 | + def k_window(self, command): |
| 258 | + return self.compileVariable(command, False) |
| 259 | + |
| 260 | + def r_window(self, command): |
| 261 | + return self.nextPC() |
| 262 | + |
| 263 | + ############################################################################# |
| 264 | + # Compile a value in this domain |
| 265 | + def compileValue(self): |
| 266 | + value = {} |
| 267 | + value['domain'] = 'rbr' |
| 268 | + if self.tokenIs('the'): |
| 269 | + self.nextToken() |
| 270 | + token = self.getToken() |
| 271 | + if token == 'xxxxx': |
| 272 | + return value |
| 273 | + |
| 274 | + return None |
| 275 | + |
| 276 | + ############################################################################# |
| 277 | + # Modify a value or leave it unchanged. |
| 278 | + def modifyValue(self, value): |
| 279 | + return value |
| 280 | + |
| 281 | + ############################################################################# |
| 282 | + # Value handlers |
| 283 | + |
| 284 | + def v_xxxxx(self, v): |
| 285 | + value = {} |
| 286 | + return value |
| 287 | + |
| 288 | + ############################################################################# |
| 289 | + # Compile a condition |
| 290 | + def compileCondition(self): |
| 291 | + condition = {} |
| 292 | + return condition |
| 293 | + |
| 294 | + ############################################################################# |
| 295 | + # Condition handlers |
0 commit comments