added prompt that either uses cli or, if available qt

This commit is contained in:
Ulrich Mohr 2022-05-22 21:28:34 +02:00
parent 084a51ee67
commit 2cb801dc97

112
pus_tc/prompt_parameters.py Normal file
View File

@ -0,0 +1,112 @@
from PyQt5.QtWidgets import (
QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QGroupBox, QGridLayout, QLineEdit
)
from PyQt5 import QtCore
from tmtccmd.core.globals_manager import get_global
from tmtccmd.config.definitions import CoreGlobalIds
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)
def reset(self):
self.value = self.defaultValue
self.widget.setPlaceholderText(self.defaultValue)
self.widget.setText("")
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)
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
"""
def prompt_parameters(parameterList):
gui = get_global(CoreGlobalIds.GUI)
if gui:
return _gui_prompt(parameterList)
else:
return _cli_prompt(parameterList)
def _gui_prompt(parameterList):
dialog = ParameterDialog()
for parameter in parameterList:
dialog.addParameter(parameter["name"], parameter["defaultValue"])
dialog.exec_()
return dialog.getParameters()
def _cli_prompt(parameterList):
result = {}
for parameter in parameterList:
userInput = input("Specify {} [{}]: ".format(parameter["name"], parameter["defaultValue"]))
if userInput == "":
userInput = parameter["defaultValue"]
result[parameter["name"]] = userInput
return result