from PyQt5.QtWidgets import ( QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QGroupBox, QGridLayout, QLineEdit, ) from PyQt5 import QtCore 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) 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_gui(param_list) -> dict: return _gui_prompt(param_list) def prompt_parameters_cli(param_list) -> dict: return _cli_prompt(param_list) def _gui_prompt(param_list) -> dict: dialog = ParameterDialog() for parameter in param_list: dialog.addParameter(parameter["name"], parameter["defaultValue"]) dialog.exec_() return dialog.getParameters() def _cli_prompt(param_list) -> dict: result = {} for parameter in param_list: user_input = input( "Specify {} [{}]: ".format(parameter["name"], parameter["defaultValue"]) ) if user_input == "": user_input = parameter["defaultValue"] result[parameter["name"]] = user_input return result