matrix_test: simplify the UI

main
Ilya Zhuravlev 2021-05-22 16:50:35 -04:00
parent c109143397
commit 4c5174729e
5 changed files with 66 additions and 31 deletions

View File

@ -16,3 +16,9 @@ class BasicEditor(QVBoxLayout):
def on_container_clicked(self): def on_container_clicked(self):
pass pass
def activate(self):
pass
def deactivate(self):
pass

View File

@ -9,6 +9,8 @@ class EditorContainer(QWidget):
def __init__(self, editor): def __init__(self, editor):
super().__init__() super().__init__()
self.editor = editor
self.setLayout(editor) self.setLayout(editor)
self.clicked.connect(editor.on_container_clicked) self.clicked.connect(editor.on_container_clicked)

View File

@ -542,12 +542,13 @@ class Keyboard:
keyboard_id = data[4:12] keyboard_id = data[4:12]
return keyboard_id return keyboard_id
def get_unlock_status(self): def get_unlock_status(self, retries=20):
# VIA keyboards are always unlocked # VIA keyboards are always unlocked
if self.vial_protocol < 0: if self.vial_protocol < 0:
return 1 return 1
data = self.usb_send(self.dev, struct.pack("BB", CMD_VIA_VIAL_PREFIX, CMD_VIAL_GET_UNLOCK_STATUS), retries=20) data = self.usb_send(self.dev, struct.pack("BB", CMD_VIA_VIAL_PREFIX, CMD_VIAL_GET_UNLOCK_STATUS),
retries=retries)
return data[0] return data[0]
def get_unlock_in_progress(self): def get_unlock_in_progress(self):

View File

@ -64,7 +64,9 @@ class MainWindow(QMainWindow):
Unlocker.global_layout_editor = self.layout_editor Unlocker.global_layout_editor = self.layout_editor
self.current_tab = None
self.tabs = QTabWidget() self.tabs = QTabWidget()
self.tabs.currentChanged.connect(self.on_tab_changed)
self.refresh_tabs() self.refresh_tabs()
self.lbl_no_devices = QLabel(tr("MainWindow", 'No devices detected. Connect a Vial-compatible device and press ' self.lbl_no_devices = QLabel(tr("MainWindow", 'No devices detected. Connect a Vial-compatible device and press '
@ -328,3 +330,16 @@ class MainWindow(QMainWindow):
msg = QMessageBox() msg = QMessageBox()
msg.setText(tr("MainWindow", "In order to fully apply the theme you should restart the application.")) msg.setText(tr("MainWindow", "In order to fully apply the theme you should restart the application."))
msg.exec_() msg.exec_()
def on_tab_changed(self, index):
old_tab = self.current_tab
new_tab = None
if index >= 0:
new_tab = self.tabs.widget(index)
if old_tab is not None:
old_tab.editor.deactivate()
if new_tab is not None:
new_tab.editor.activate()
self.current_tab = new_tab

View File

@ -1,16 +1,18 @@
# SPDX-License-Identifier: GPL-2.0-or-later # SPDX-License-Identifier: GPL-2.0-or-later
from PyQt5.QtWidgets import QVBoxLayout, QPushButton, QWidget from PyQt5.QtWidgets import QVBoxLayout, QPushButton, QWidget, QHBoxLayout, QLabel
from PyQt5.QtCore import Qt, QTimer from PyQt5.QtCore import Qt, QTimer
import math import math
from basic_editor import BasicEditor from basic_editor import BasicEditor
from keyboard_widget import KeyboardWidget from keyboard_widget import KeyboardWidget
from util import tr
from vial_device import VialKeyboard from vial_device import VialKeyboard
from unlocker import Unlocker from unlocker import Unlocker
class MatrixTest(BasicEditor): class MatrixTest(BasicEditor):
def __init__(self, layout_editor): def __init__(self, layout_editor):
super().__init__() super().__init__()
@ -19,16 +21,22 @@ class MatrixTest(BasicEditor):
self.keyboardWidget = KeyboardWidget(layout_editor) self.keyboardWidget = KeyboardWidget(layout_editor)
self.keyboardWidget.set_enabled(False) self.keyboardWidget.set_enabled(False)
self.startButtonWidget = QPushButton("Start testing") self.unlock_btn = QPushButton("Unlock")
self.resetButtonWidget = QPushButton("Reset") self.reset_btn = QPushButton("Reset")
layout = QVBoxLayout() layout = QVBoxLayout()
layout.addWidget(self.keyboardWidget) layout.addWidget(self.keyboardWidget)
layout.setAlignment(self.keyboardWidget, Qt.AlignCenter) layout.setAlignment(self.keyboardWidget, Qt.AlignCenter)
self.addLayout(layout) self.addLayout(layout)
self.addWidget(self.resetButtonWidget)
self.addWidget(self.startButtonWidget) btn_layout = QHBoxLayout()
btn_layout.addStretch()
self.unlock_lbl = QLabel(tr("MatrixTest", "Unlock the keyboard before testing:"))
btn_layout.addWidget(self.unlock_lbl)
btn_layout.addWidget(self.unlock_btn)
btn_layout.addWidget(self.reset_btn)
self.addLayout(btn_layout)
self.keyboard = None self.keyboard = None
self.device = None self.device = None
@ -37,8 +45,8 @@ class MatrixTest(BasicEditor):
self.timer = QTimer() self.timer = QTimer()
self.timer.timeout.connect(self.matrix_poller) self.timer.timeout.connect(self.matrix_poller)
self.startButtonWidget.clicked.connect(self.toggle) self.unlock_btn.clicked.connect(self.unlock)
self.resetButtonWidget.clicked.connect(self.reset_keyboard_widget) self.reset_btn.clicked.connect(self.reset_keyboard_widget)
self.grabber = QWidget() self.grabber = QWidget()
@ -52,7 +60,8 @@ class MatrixTest(BasicEditor):
def valid(self): def valid(self):
# Check if vial protocol is v3 or later # Check if vial protocol is v3 or later
return isinstance(self.device, VialKeyboard) and (self.device.keyboard and self.device.keyboard.vial_protocol >= 3) return isinstance(self.device, VialKeyboard) and \
(self.device.keyboard and self.device.keyboard.vial_protocol >= 3)
def reset_keyboard_widget(self): def reset_keyboard_widget(self):
# reset keyboard widget # reset keyboard widget
@ -66,9 +75,24 @@ class MatrixTest(BasicEditor):
def matrix_poller(self): def matrix_poller(self):
if not self.valid(): if not self.valid():
self.stop() self.timer.stop()
return return
try:
unlocked = self.keyboard.get_unlock_status(3)
except (RuntimeError, ValueError):
self.timer.stop()
return
if not unlocked:
self.unlock_btn.show()
self.unlock_lbl.show()
return
# we're unlocked, so hide unlock button and label
self.unlock_btn.hide()
self.unlock_lbl.hide()
# Get size for matrix # Get size for matrix
rows = self.keyboard.rows rows = self.keyboard.rows
cols = self.keyboard.cols cols = self.keyboard.cols
@ -79,7 +103,7 @@ class MatrixTest(BasicEditor):
try: try:
data = self.keyboard.matrix_poll() data = self.keyboard.matrix_poll()
except (RuntimeError, ValueError): except (RuntimeError, ValueError):
self.stop() self.timer.stop()
return return
# Calculate the amount of bytes belong to 1 row, each bit is 1 key, so per 8 keys in a row, # Calculate the amount of bytes belong to 1 row, each bit is 1 key, so per 8 keys in a row,
@ -116,26 +140,13 @@ class MatrixTest(BasicEditor):
self.keyboardWidget.update() self.keyboardWidget.update()
self.keyboardWidget.updateGeometry() self.keyboardWidget.updateGeometry()
def start(self): def unlock(self):
self.grabber.grabKeyboard()
Unlocker.unlock(self.keyboard) Unlocker.unlock(self.keyboard)
self.startButtonWidget.setText("Stop testing")
self.timer.start(20)
self.polling = True
def stop(self): def activate(self):
self.grabber.grabKeyboard()
self.timer.start(20)
def deactivate(self):
self.grabber.releaseKeyboard() self.grabber.releaseKeyboard()
self.timer.stop() self.timer.stop()
try:
self.keyboard.lock()
except (RuntimeError, ValueError):
# if keyboard disappeared, we can't relock it
pass
self.startButtonWidget.setText("Start testing")
self.polling = False
def toggle(self):
if not self.polling:
self.start()
else:
self.stop()