Ejemplo n.º 1
0
    def fill_editor(self, style):
        """Fill the editor fields with the contents of a style."""
        if style is None:
            style = {}
            for key, val in DEFAULT.items():
                style[key] = val[1]

        if not isinstance(style, dict):
            if style in self.styles:
                style = self.styles[style]
            else:
                print("debug: unable to fill dialog from style", style)

        for key, value in style.items():
            control = getattr(self.form, key)
            if DEFAULT[key][0] == "str":
                control.setText(value)
            elif DEFAULT[key][0] == "font":
                control.setCurrentFont(QtGui.QFont(value))
            elif DEFAULT[key][0] == "color":
                r = ((value >> 24) & 0xFF) / 255.0
                g = ((value >> 16) & 0xFF) / 255.0
                b = ((value >> 8) & 0xFF) / 255.0
                color = QtGui.QColor.fromRgbF(r, g, b)
                control.setProperty("color", color)
            elif DEFAULT[key][0] in ["int", "float"]:
                control.setValue(value)
            elif DEFAULT[key][0] == "bool":
                control.setChecked(value)
            elif DEFAULT[key][0] == "index":
                control.setCurrentIndex(value)
Ejemplo n.º 2
0
 def on_style_changed(self, index):
     """Execute as a callback when the styles combobox changes."""
     if index <= 1:
         # nothing happens
         self.form.pushButtonDelete.setEnabled(False)
         self.form.pushButtonRename.setEnabled(False)
         self.fill_editor(None)
     if index == 1:
         # Add new... entry
         reply = QtGui.QInputDialog.getText(None, "Create new style",
                                            "Style name:")
         if reply[1]:
             # OK or Enter pressed
             name = reply[0]
             if name in self.styles:
                 reply = QtGui.QMessageBox.information(
                     None, "Style exists", "This style name already exists")
             else:
                 # create new default style
                 self.styles[name] = {}
                 for key, val in DEFAULT.items():
                     self.styles[name][key] = val[1]
                 self.form.comboBoxStyles.addItem(name)
                 self.form.comboBoxStyles.setCurrentIndex(
                     self.form.comboBoxStyles.count() - 1)
     elif index > 1:
         # Existing style
         self.form.pushButtonDelete.setEnabled(True)
         self.form.pushButtonRename.setEnabled(True)
         self.fill_editor(self.form.comboBoxStyles.itemText(index))