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))
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)
def Activated(self): """Execute when the command is called. The document attribute is set here by the parent class. """ super(AnnotationStyleEditor, self).Activated() # reset rename table self.renamed = {} # load dialog ui_file = ":/ui/dialog_AnnotationStyleEditor.ui" self.form = Gui.PySideUic.loadUi(ui_file) # restore stored size w = param.GetInt("AnnotationStyleEditorWidth", 450) h = param.GetInt("AnnotationStyleEditorHeight", 450) self.form.resize(w, h) # center the dialog over FreeCAD window mw = Gui.getMainWindow() self.form.move(mw.frameGeometry().topLeft() + mw.rect().center() - self.form.rect().center()) # set icons self.form.setWindowIcon(QtGui.QIcon(":/icons/Draft_Annotation_Style.svg")) self.form.pushButtonDelete.setIcon(QtGui.QIcon(":/icons/edit_Cancel.svg")) self.form.pushButtonRename.setIcon(QtGui.QIcon(":/icons/accessories-text-editor.svg")) self.form.pushButtonDelete.resize(self.form.pushButtonDelete.sizeHint()) self.form.pushButtonRename.resize(self.form.pushButtonRename.sizeHint()) # fill the styles combo self.styles = self.read_meta() for style in self.styles.keys(): self.form.comboBoxStyles.addItem(style) # connect signals/slots self.form.comboBoxStyles.currentIndexChanged.connect(self.on_style_changed) self.form.pushButtonDelete.clicked.connect(self.on_delete) self.form.pushButtonRename.clicked.connect(self.on_rename) for attr in DEFAULT.keys(): control = getattr(self.form, attr) for signal in ("clicked", "textChanged", "valueChanged", "stateChanged", "currentIndexChanged"): if hasattr(control, signal): getattr(control, signal).connect(self.update_style) break # show editor dialog result = self.form.exec_() # process if OK was clicked if result: self.save_meta(self.styles) # store dialog size param.SetInt("AnnotationStyleEditorWidth", self.form.width()) param.SetInt("AnnotationStyleEditorHeight", self.form.height())
def update_style(self, arg=None): """Update the current style with the values from the editor.""" index = self.form.comboBoxStyles.currentIndex() if index > 1: values = {} style = self.form.comboBoxStyles.itemText(index) for key in DEFAULT.keys(): control = getattr(self.form, key) if DEFAULT[key][0] == "str": values[key] = control.text() elif DEFAULT[key][0] == "font": values[key] = control.currentFont().family() elif DEFAULT[key][0] == "color": values[key] = control.property("color").rgb() << 8 elif DEFAULT[key][0] in ["int", "float"]: values[key] = control.value() elif DEFAULT[key][0] == "bool": values[key] = control.isChecked() elif DEFAULT[key][0] == "index": values[key] = control.currentIndex() self.styles[style] = values