vial/src/main/python/qmk_settings.py

133 lines
4.1 KiB
Python
Raw Normal View History

2021-06-29 19:43:21 -04:00
# SPDX-License-Identifier: GPL-2.0-or-later
2021-06-30 23:20:18 -04:00
import json
2021-06-29 19:43:21 -04:00
import struct
from PyQt5 import QtCore
2021-06-30 23:20:18 -04:00
from PyQt5.QtWidgets import QVBoxLayout, QCheckBox, QGridLayout, QLabel, QWidget, QSizePolicy, QTabWidget, QSpinBox, \
QHBoxLayout, QPushButton
2021-06-29 19:43:21 -04:00
from basic_editor import BasicEditor
2021-06-30 23:42:56 -04:00
from util import tr
2021-06-29 19:43:21 -04:00
from vial_device import VialKeyboard
2021-06-30 23:20:18 -04:00
class GenericOption:
def __init__(self, option, container):
self.row = container.rowCount()
self.option = option
2021-06-30 23:42:56 -04:00
self.qsid = self.option["qsid"]
2021-06-30 23:20:18 -04:00
self.container = container
self.container.addWidget(QLabel(option["title"]), self.row, 0)
2021-06-30 23:42:56 -04:00
def reload(self, keyboard):
data = keyboard.qmk_settings_get(self.qsid)
if not data:
raise RuntimeError("failed to retrieve setting {} from keyboard".format(self.option))
return data
2021-06-30 23:20:18 -04:00
class BooleanOption(GenericOption):
def __init__(self, option, container):
super().__init__(option, container)
2021-06-30 23:42:56 -04:00
self.qsid_bit = self.option["bit"]
2021-06-30 23:20:18 -04:00
self.checkbox = QCheckBox()
2021-06-30 23:42:56 -04:00
self.checkbox.stateChanged.connect(self.on_change)
2021-06-30 23:20:18 -04:00
self.container.addWidget(self.checkbox, self.row, 1)
2021-06-30 23:42:56 -04:00
def on_change(self):
print(self.option, self.checkbox.isChecked())
def reload(self, keyboard):
data = super().reload(keyboard)
checked = data[0] & (1 << self.qsid_bit)
self.checkbox.blockSignals(True)
self.checkbox.setChecked(checked != 0)
self.checkbox.blockSignals(False)
2021-06-30 23:20:18 -04:00
class IntegerOption(GenericOption):
2021-06-30 23:42:56 -04:00
def __init__(self, option, container):
2021-06-30 23:20:18 -04:00
super().__init__(option, container)
self.spinbox = QSpinBox()
2021-06-30 23:42:56 -04:00
self.spinbox.setMinimum(option["min"])
self.spinbox.setMaximum(option["max"])
2021-06-30 23:20:18 -04:00
self.container.addWidget(self.spinbox, self.row, 1)
2021-06-30 23:42:56 -04:00
def reload(self, keyboard):
data = super().reload(keyboard)[0:self.option["width"]]
self.spinbox.setValue(int.from_bytes(data, byteorder="little"))
2021-06-30 23:20:18 -04:00
2021-06-29 19:43:21 -04:00
class QmkSettings(BasicEditor):
2021-06-30 23:20:18 -04:00
def __init__(self, appctx):
2021-06-29 19:43:21 -04:00
super().__init__()
2021-06-30 23:20:18 -04:00
self.appctx = appctx
self.keyboard = None
2021-06-29 19:43:21 -04:00
2021-06-30 23:20:18 -04:00
self.tabs_widget = QTabWidget()
self.addWidget(self.tabs_widget)
buttons = QHBoxLayout()
buttons.addStretch()
2021-06-30 23:42:56 -04:00
buttons.addWidget(QPushButton(tr("QmkSettings", "Save")))
btn_undo = QPushButton(tr("QmkSettings", "Undo"))
btn_undo.clicked.connect(self.reload_settings)
buttons.addWidget(btn_undo)
buttons.addWidget(QPushButton(tr("QmkSettings", "Reset")))
2021-06-30 23:20:18 -04:00
self.addLayout(buttons)
2021-06-29 19:43:21 -04:00
2021-06-30 23:20:18 -04:00
self.tabs = []
self.create_gui()
@staticmethod
def populate_tab(tab, container):
options = []
for field in tab["fields"]:
if field["type"] == "boolean":
options.append(BooleanOption(field, container))
elif field["type"] == "integer":
options.append(IntegerOption(field, container))
else:
raise RuntimeError("unsupported field type: {}".format(field))
return options
def create_gui(self):
with open(self.appctx.get_resource("qmk_settings.json"), "r") as inf:
settings = json.load(inf)
for tab in settings["tabs"]:
w = QWidget()
w.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
container = QGridLayout()
w.setLayout(container)
l = QVBoxLayout()
l.addWidget(w)
l.setAlignment(w, QtCore.Qt.AlignHCenter)
w2 = QWidget()
w2.setLayout(l)
self.tabs_widget.addTab(w2, tab["name"])
self.tabs.append(self.populate_tab(tab, container))
2021-06-29 19:43:21 -04:00
def reload_settings(self):
2021-06-30 23:42:56 -04:00
for tab in self.tabs:
for field in tab:
field.reload(self.keyboard)
2021-06-29 19:43:21 -04:00
def rebuild(self, device):
super().rebuild(device)
if self.valid():
self.keyboard = device.keyboard
self.reload_settings()
def valid(self):
return isinstance(self.device, VialKeyboard) and \
(self.device.keyboard and self.device.keyboard.vial_protocol >= 3) # TODO(xyz): protocol bump