def line(xml: QXmlStreamWriter, pen: QPen, tag: str = "line"): xml.writeStartElement(tag) xml.writeAttribute("color", pen.color().name()) xml.writeAttribute("width", str(pen.width())) if pen.style() == Qt.DashLine: xml.writeAttribute("style", "dash") elif pen.style() == Qt.DotLine: xml.writeAttribute("style", "dot") else: xml.writeAttribute("style", "solid") xml.writeEndElement() #fecha line
def setValues(self, pen: QPen): for i in range(self.lstPenStyles.count()): item = self.lstPenStyles.item(i) if item.data(PenParametersDialog.PenStyleRole) == pen.style(): item.setSelected(True) break for i in range(self.lstPenColors.count()): item = self.lstPenColors.item(i) if QColor(item.data(PenParametersDialog.ColorRole)).name( ) == pen.color().name(): item.setSelected(True) break self.spinPenSize.setValue(pen.width())
class PFSNode(PFSElement): def __init__(self, id: str, x: int, y: int): PFSElement.__init__(self, id) self.setPos(x / 2, y / 2) self._width = 0 self._height = 0 self._pen = QPen(Qt.black) self._brush = QBrush(Qt.white, Qt.SolidPattern) self.emitter = PFSSenderSignal() self.changed = self.emitter.changed self.deleted = self.emitter.deleted self.penEdited = self.emitter.penEdited def move(self, x, y): self.moveBy(x / 2, y / 2) self.changed.emit() for it in self.scene().items(): print(str(it.__class__) + " " + str(it.shape().boundingRect())) def setPenColor(self, color: QColor): self._pen.setColor(color) self.scene().update() def setPenStyle(self, style: Qt): self._pen.setStyle(style) self.scene().update() self.penEdited.emit(self) def setPenWidth(self, width: str): self._pen.setWidth(float(width)) self.scene().update() def setBrushColor(self, color: QColor): self._brush.setColor(color) self.scene().update() def changeElementPosX(self, prop): x = PFSUndoPropertyText(prop, self.moveX) self.scene()._page._net.undoStack.push(x) def changeElementPosY(self, prop): x = PFSUndoPropertyText(prop, self.moveY) self.scene()._page._net.undoStack.push(x) def changeElementWidth(self, prop): x = PFSUndoPropertyText(prop, self.resizeWidth) self.scene()._page._net.undoStack.push(x) def changeElementHeight(self, prop): x = PFSUndoPropertyText(prop, self.resizeHeight) self.scene()._page._net.undoStack.push(x) def changeLineColor(self): color = QColorDialog.getColor(self._pen.color(), self.scene()._page._net, "Escolha a cor do contorno") if color.isValid() and color != self._pen.color(): x = PFSUndoPropertyButton(color, self._pen.color(), self.setPenColor) self.scene()._page._net.undoStack.push(x) def changeLineStyle(self, text): if text in self.PEN_LIST: x = PFSUndoPropertyCombo(self.PEN_LIST[text], self._pen.style(), self.setPenStyle) self.scene()._page._net.undoStack.push(x) def changeLineWidth(self, prop): x = PFSUndoPropertyText(prop, self.setPenWidth) self.scene()._page._net.undoStack.push(x) def changeFillColor(self): color = QColorDialog.getColor(self._brush.color(), self.scene()._page._net, "Escolha a cor do preenchimento") if color.isValid() and color != self._brush.color(): x = PFSUndoPropertyButton(color, self._brush.color(), self.setBrushColor) self.scene()._page._net.undoStack.push(x) def moveX(self, txt, update=True): self.moveBy(float(txt) / 2, 0) if update: self.scene().update() def moveY(self, txt, update=True): self.moveBy(0, float(txt) / 2) if update: self.scene().update() def resizeWidth(self, txt): self._width = float(txt) self.changed.emit() self.scene().update() def resizeHeight(self, txt): self._height = float(txt) self.changed.emit() self.scene().update()
from PyQt5.QtCore import Qt from PyQt5.QtGui import QPen WIDTH = 5 COLOR = Qt.gray STYLE = Qt.SolidLine ACTIVE_WIDTH = 7 ACTIVE_COLOR = Qt.darkGray ACTIVE_STYLE = STYLE line = QPen(COLOR, WIDTH, STYLE) point = QPen(line.color(), WIDTH * 2, line.style()) activeLine = QPen(ACTIVE_COLOR, ACTIVE_WIDTH, ACTIVE_STYLE) activePoint = QPen(ACTIVE_COLOR, ACTIVE_WIDTH * 2, ACTIVE_STYLE)
class PenFormation(InstrumentFormation): ''' Specialize to Qt <QPen> Redefine: - applyTo() ''' # TODO: a QPen has a QBrush also, and it needs to be scaled also def __init__(self, parentSelector, role=""): InstrumentFormation.__init__(self, name="Pen", parentSelector=parentSelector, role=role) self.instrument = QPen() self.styleProperties=[BaseStyleProperty("Color", self.instrument.setColor, self.selector, default = self.instrument.color(), resettableValueFactory=ResettableColorValue, layoutFactory=ColorStylePropertyLayout), BaseStyleProperty("Width", self.instrument.setWidth, self.selector, default=self.instrument.width(), layoutFactory=IntStylePropertyLayout, minimum=0, maximum=10, singleStep=1), BaseStyleProperty("Style", self.instrument.setStyle, self.selector, default=self.instrument.style(), layoutFactory=ComboBoxStylePropertyLayout, domainModel = config.PenModel) ] def applyTo(self, morph): ''' Assert this formation's values have already been applied to instrument via editing (which calls Instrument.setters()) What remains is to set the instrument to the morph. Also, scale instrument correlated with scale of morph. ''' # Callback morph API: only morph knows its scale, and how to inversely scale drawing instrument morph.scaleInstrument(self.instrument, baseValue=self.styleProperties[1].resettableValue.value) morph.setPen(self.instrument) """ def scaledPropagateToInstrument(self, morph): ''' Propagate my values that are transformed, after unscaling by the local (item) transform. Where a DocumentElement has a transform that is used to size it (e.g. when the DocumentElement comprises a unit shape, scaled to size.) TODO when item transform is 2D and not uniform in x, y??? This is not undoing viewing transform, only local transform. For a Pen in Qt, width property. Note that in Qt, QPen.setWidth() also affects the QPen's QBrush. ''' unscaledWidth = self.styleProperties[1].resettableValue.value() itemScale = morph.scale() scaledWidthF = 1.0/itemScale * unscaledWidth # !!! Note float value and setWidthF is float setter self.instrument.setWidthF(scaledWidthF) #print "PenFormation.applyTo width: item scale, unscaled width, scaled width", itemScale, unscaledWidth, scaledWidthF, " on morph", morph """