eive-tmtc/pus_tc/prompt_parameters.py

121 lines
3.4 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
2022-07-05 02:12:54 +02:00
from tmtccmd.config import CoreModeList
from tmtccmd.core.globals_manager import get_global
class Parameter:
def __init__(self, name: str, defaultValue: str, widget: QLineEdit):
self.name = name
self.defaultValue = defaultValue
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")
Buttons = QDialogButtonBox.Ok | QDialogButtonBox.Cancel | QDialogButtonBox.Reset
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 = {}
def addParameter(self, name: str, defaultValue: str):
row = self.groupLayout.rowCount() + 1
description = QLabel(name)
self.groupLayout.addWidget(description, row, 0)
valueWidget = QLineEdit()
self.groupLayout.addWidget(valueWidget, row, 1)
parameter = Parameter(name, defaultValue, valueWidget)
self.parameters[name] = parameter
def _reset(self):
for value in self.parameters.values():
value.reset()
def getParameters(self):
output = {}
for key, parameter in self.parameters.items():
if parameter.widget != 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:
dialog.addParameter(parameter["name"], parameter["defaultValue"])
dialog.exec_()
return dialog.getParameters()
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