eive-tmtc/eive_tmtc/pus_tc/prompt_parameters.py

118 lines
3.3 KiB
Python
Raw Normal View History

from PyQt5.QtWidgets import (
2022-05-23 18:25:25 +02:00
QDialog,
QDialogButtonBox,
QVBoxLayout,
QLabel,
QGroupBox,
QGridLayout,
QLineEdit,
)
from PyQt5 import QtCore
class Parameter:
2023-06-10 14:42:28 +02:00
def __init__(self, name: str, default_value: str, widget: QLineEdit):
self.name = name
2023-06-10 14:42:28 +02:00
self.defaultValue = default_value
self.widget = widget
self.value = self.defaultValue
self.widget.setPlaceholderText(self.defaultValue)
2022-05-23 18:25:25 +02:00
def reset(self):
self.value = self.defaultValue
self.widget.setPlaceholderText(self.defaultValue)
self.widget.setText("")
2022-05-23 18:25:25 +02:00
class ParameterDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Enter Parameters")
2023-06-10 14:42:28 +02:00
buttons = QDialogButtonBox.Ok | QDialogButtonBox.Cancel | QDialogButtonBox.Reset
2023-06-10 14:42:28 +02:00
self.buttonBox = QDialogButtonBox(buttons)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
self.buttonBox.button(QDialogButtonBox.Reset).clicked.connect(self._reset)
self.data = 1
self.rootLayout = QVBoxLayout()
self.group = QGroupBox("Parameters")
self.rootLayout.addWidget(self.group)
self.rootLayout.addWidget(self.buttonBox)
self.setLayout(self.rootLayout)
2022-05-23 18:25:25 +02:00
self.groupLayout = QGridLayout()
self.group.setLayout(self.groupLayout)
self.parameters = {}
2023-06-10 14:42:28 +02:00
def add_parameter(self, name: str, default_value: str):
row = self.groupLayout.rowCount() + 1
description = QLabel(name)
self.groupLayout.addWidget(description, row, 0)
2023-06-10 14:42:28 +02:00
value_widget = QLineEdit()
self.groupLayout.addWidget(value_widget, row, 1)
2023-06-10 14:42:28 +02:00
parameter = Parameter(name, default_value, value_widget)
self.parameters[name] = parameter
def _reset(self):
for value in self.parameters.values():
value.reset()
2023-06-10 14:42:28 +02:00
def get_parameters(self):
output = {}
for key, parameter in self.parameters.items():
2023-06-10 14:42:28 +02:00
if parameter.widget is not None:
if parameter.widget.text() != "":
parameter.value = parameter.widget.text()
output[key] = parameter.value
return output
@QtCore.pyqtSlot()
def reject(self):
print("reject")
self._reset()
super().reject()
"""Prompt the user to specify additional Parameters
:param parameterList: array of dictionaries with name and defaultValue attributes
:return: dict with all names as key and user supplied input as value string
"""
2022-05-23 18:25:25 +02:00
2022-07-05 02:12:54 +02:00
def prompt_parameters_gui(param_list) -> dict:
return _gui_prompt(param_list)
2022-07-05 02:12:54 +02:00
def prompt_parameters_cli(param_list) -> dict:
return _cli_prompt(param_list)
2022-05-23 18:25:25 +02:00
2022-07-05 02:12:54 +02:00
def _gui_prompt(param_list) -> dict:
dialog = ParameterDialog()
2022-07-05 02:12:54 +02:00
for parameter in param_list:
2023-06-10 14:42:28 +02:00
dialog.add_parameter(parameter["name"], parameter["defaultValue"])
dialog.exec_()
2023-06-10 14:42:28 +02:00
return dialog.get_parameters()
2022-05-23 18:25:25 +02:00
2022-07-05 02:12:54 +02:00
def _cli_prompt(param_list) -> dict:
result = {}
2022-07-05 02:12:54 +02:00
for parameter in param_list:
user_input = input(
2022-05-23 18:25:25 +02:00
"Specify {} [{}]: ".format(parameter["name"], parameter["defaultValue"])
)
2022-07-05 02:12:54 +02:00
if user_input == "":
user_input = parameter["defaultValue"]
result[parameter["name"]] = user_input
2022-05-23 18:25:25 +02:00
return result