Exemplo n.º 1
0
    def _set_widgets(self):
        """
        Parse operation (function arguments) and create widgets according to
        the function annotations. Fill with default or current argument values
        and display the widgets.
        """
        self._clear_widgets()

        # Retreive operation (function detail), instantiate neccesary widgets
        package, fun = self._op["module"].rsplit('.', 1)
        module = import_module(package)
        self._op["function"] = getattr(module, fun)
        argspec = inspect.getfullargspec(self._op["function"])

        # Create widgets
        self._labels = []
        self._widgets = []
        self._none_checkbox_widgets = []
        for par_name, annotation in sorted(argspec.annotations.items()):
            label = QLabel(par_name)
            font = qtgui.QFont()
            font.setPointSize(10)
            label.setFont(font)

            # find associated widget and functions
            mapping = {}
            if "widget" in annotation:
                mapping["widget"] = widget = getattr(qtgui, annotation["widget"])
                mapping["display_func"] = annotation["display_func"]
                mapping["display_conversion"] = annotation["display_conversion"]
                mapping["get_func"] = annotation["get_func"]
            elif "type" in annotation:
                try:
                    mapping = dict(GenericOperationWidget.type_mapping[annotation["type"]])
                except KeyError:
                    print("Can't find wiget for type: {type} for parameter " +
                          "{par_name}".format(type=annotation["type"], par_name=par_name))
                    continue
            else:
                continue

            # instanciate widget and make display_function callable
            widget = mapping["widget"]()
            if not callable(mapping["display_func"]):
                mapping["display_func"] = widget.__getattribute__(mapping["display_func"])

            # save mapping and parameter name for later use
            widget.mapping = mapping
            widget.par_name = par_name

            # create checkbox to allow not to set this parameter
            checkbox = QCheckBox()
            checkbox.setChecked(True)
            checkbox.setToolTip("Set the \"%s\" parameter. If unchecked, the default value is used." % par_name)
            checkbox.par_name = widget.par_name

            # Set value of widget according to parameters of the operation
            try:
                if "display_conversion" in mapping:
                    display_value = mapping["display_conversion"](self._op["kwargs"][par_name])    
                else:
                    display_value = self._op["kwargs"][par_name]
                mapping["display_func"](display_value)
            except KeyError:
                checkbox.setChecked(False)
            
            self._labels.append(label)
            self._widgets.append(widget)
            self._none_checkbox_widgets.append(checkbox)

        # Add widgets to layout
        if not self._widgets:
            self.widget_layout.addWidget(QLabel("No operation parameters"))
        for i, widget in enumerate(self._widgets):
            self.widget_layout.addWidget(self._labels[i])
            self.widget_layout.addWidget(self._widgets[i])
            self.widget_layout.addWidget(self._none_checkbox_widgets[i])