コード例 #1
0
    def __init__(self, parent, controller_widget, control_name):

        super(ListValuesEditor, self).__init__(parent)

        self.controller_widget = controller_widget
        self.control_name = control_name
        self.format = 'JSON'
        self.separator = ','
        self.modified = False

        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)
        textedit = QtGui.QTextEdit()
        layout.addWidget(textedit)
        hlayout2 = QtGui.QHBoxLayout()
        layout.addLayout(hlayout2)

        hlayout2.addWidget(QtGui.QLabel('Format:'))
        format_c = QtGui.QComboBox()
        hlayout2.addWidget(format_c)
        hlayout2.addWidget(QtGui.QLabel('Separator:'))
        sep_c = QtGui.QComboBox()
        hlayout2.addWidget(sep_c)

        format_c.addItem('JSON')
        format_c.addItem('CSV')

        sep_c.addItem(',')
        sep_c.addItem(';')
        sep_c.addItem(' ')

        hlayout2.addStretch(1)
        ok = QtGui.QPushButton('OK')
        cancel = QtGui.QPushButton('Cancel')
        hlayout2.addWidget(ok)
        hlayout2.addWidget(cancel)

        ok.pressed.connect(self.accept)
        cancel.pressed.connect(self.reject)

        parent_controller = controller_widget.controller
        value = getattr(parent_controller, control_name)

        text = json.dumps(value)
        textedit.setText(text)

        self.textedit = textedit
        self.format_c = format_c
        self.separator_c = sep_c
        self.internal_change = False

        textedit.textChanged.connect(self.set_modified)
        format_c.currentIndexChanged.connect(self.format_changed)
        sep_c.currentIndexChanged.connect(self.separator_changed)
コード例 #2
0
ファイル: Enum.py プロジェクト: Danilodcn/Python
    def create_widget(parent, control_name, control_value, trait,
                      label_class=None):
        """ Method to create the widget.

        Parameters
        ----------
        parent: QWidget (mandatory)
            the parent widget
        control_name: str (mandatory)
            the name of the control we want to create
        control_value: str (mandatory)
            the default control value, here the enum values
        trait: Tait (mandatory)
            the trait associated to the control
        label_class: Qt widget class (optional, default: None)
            the label widget will be an instance of this class. Its constructor
            will be called using 2 arguments: the label string and the parent
            widget.

        Returns
        -------
        out: 2-uplet
            a two element tuple of the form (control widget: QComboBox,
            associated label: QLabel)
        """
        # Create the widget that will be used to select a value
        widget = QtGui.QComboBox(parent)

        # Save the possible choices
        widget._choices = trait.handler.values

        # Add a parameter to tell us if the widget is optional
        widget.optional = trait.optional

        # Set the enum list items to the widget
        for item in widget._choices:
            widget.addItem(unicode(item))

        # Select the default value
        # If the default value is not in the enum list, pick the first item
        # of the enum list
        if control_value not in widget._choices:
            widget.setCurrentIndex(0)
        else:
            widget.setCurrentIndex(widget._choices.index(control_value))

        # Create the label associated with the enum widget
        control_label = trait.label
        if control_label is None:
            control_label = control_name
        if label_class is None:
            label_class = QtGui.QLabel
        if control_label is not None:
            label = label_class(control_label, parent)
        else:
            label = None

        return (widget, label)