def update_font_color_button(self): """Updates the color shown on the font color button""" # Loop through each TEXT element for node in self.text_nodes + self.tspan_nodes: # Get the value in the style attribute and turn into a dict s = node.attributes["style"].value ard = style_to_dict(s) # Get fill color or default to white color = ard.get("fill", "#FFF") # Look up color if needed # Some colors are located in a different node if color.startswith("url(#") and self.xmldoc.getElementsByTagName("defs").length == 1: color_ref_id = color[5:-1] ref_color = self.get_ref_color(color_ref_id) if ref_color: color = ref_color # Get opacity or default to opaque opacity = float(ard.get("opacity", 1.0)) color = QtGui.QColor(color) text_color = self.best_contrast(color) # Set the color of the button, ignoring alpha self.btnFontColor.setStyleSheet( "background-color: %s; opacity: %s; color: %s;" % (color.name(), 1, text_color.name())) # Store the opacity as the color's alpha level color.setAlphaF(opacity) self.font_color_code = color log.debug("Set color of font-color button to %s", color.name())
def set_font_color_elements(self, color, alpha): # Loop through each TEXT element for text_child in self.text_nodes + self.tspan_nodes: # SET TEXT PROPERTIES s = text_child.attributes["style"].value ard = style_to_dict(s) ard.update({ "fill": color, "opacity": str(alpha), }) text_child.setAttribute("style", dict_to_style(ard)) log.debug("Set text node style, fill:%s opacity:%s", color, alpha)
def set_bg_style(self, color, alpha): '''sets the background color''' if self.rect_node: # Turn the style attribute into a dict for modification s = self.rect_node[0].attributes["style"].value ard = style_to_dict(s) ard.update({ "fill": color, "opacity": str(alpha), }) # Convert back to a string and update the node in the xml doc self.bg_style_string = dict_to_style(ard) self.rect_node[0].setAttribute("style", self.bg_style_string) log.debug("Updated background style to %s", self.bg_style_string)
def set_font_style(self): '''sets the font properties''' # Loop through each TEXT element for text_child in self.text_nodes + self.tspan_nodes: # set the style elements for the main text node s = text_child.attributes["style"].value ard = style_to_dict(s) set_if_existing(ard, "font-style", self.font_style) set_if_existing(ard, "font-family", f"'{self.font_family}'") set_if_existing(ard, "font-size", f"{self.font_size_pixel}px") self.title_style_string = dict_to_style(ard) # set the text node text_child.setAttribute("style", self.title_style_string) log.debug("Updated font styles to %s", self.title_style_string)
def get_ref_color(self, id): """Get the color value from a reference id (i.e. linearGradient3267)""" for ref_node in self.xmldoc.getElementsByTagName("defs")[0].childNodes: if ref_node.attributes and "id" in ref_node.attributes: ref_node_id = ref_node.attributes["id"].value if id == ref_node_id: # Found a matching color reference if "xlink:href" in ref_node.attributes: # look up color reference again xlink_ref_id = ref_node.attributes["xlink:href"].value[1:] return self.get_ref_color(xlink_ref_id) if "href" in ref_node.attributes: # look up color reference again xlink_ref_id = ref_node.attributes["href"].value[1:] return self.get_ref_color(xlink_ref_id) elif ref_node.childNodes: for stop_node in ref_node.childNodes: if stop_node.nodeName == "stop": # get color from stop ard = style_to_dict(stop_node.attributes["style"].value) if "stop-color" in ard: return ard.get("stop-color") return ""
def update_background_color_button(self): """Updates the color shown on the background color button""" if self.rect_node: # All backgrounds should be the first (index 0) rect tag in the svg s = self.rect_node[0].attributes["style"].value ard = style_to_dict(s) # Get fill color or default to black + full opacity color = ard.get("fill", "#000") opacity = float(ard.get("opacity", 1.0)) color = QtGui.QColor(color) text_color = self.best_contrast(color) # Set the colors of the button, ignoring opacity self.btnBackgroundColor.setStyleSheet( "background-color: %s; opacity: %s; color: %s;" % (color.name(), 1, text_color.name())) # Store the opacity as the color's alpha level color.setAlphaF(opacity) self.bg_color_code = color log.debug("Set color of background-color button to %s", color.name())
def load_svg_template(self, filename_field=None): """ Load an SVG title and init all textboxes and controls """ log.debug("Loading SVG file %s as title template", self.filename) # Get translation object _ = get_app()._tr # The container for all our widgets layout = self.settingsContainer.layout() # parse the svg object self.xmldoc = minidom.parse(self.filename) # get the text elements self.tspan_nodes = self.xmldoc.getElementsByTagName('tspan') # Reset default font self.font_family = "Bitstream Vera Sans" if self.qfont: del self.qfont self.qfont = QtGui.QFont(self.font_family) # Loop through child widgets (and remove them) for child in self.settingsContainer.children(): try: if isinstance(child, QWidget): layout.removeWidget(child) child.deleteLater() except Exception as ex: log.debug('Failed to delete child settings widget: %s', ex) # Get text nodes and rect nodes self.text_nodes = self.xmldoc.getElementsByTagName('text') self.rect_node = self.xmldoc.getElementsByTagName('rect') # Create Label label = QLabel(self) label_line_text = _("File Name:") label.setText(label_line_text) label.setToolTip(label_line_text) # create text editor for file name self.txtFileName = QTextEdit(self) self.txtFileName.setObjectName("txtFileName") # If edit mode or reload, set file name if filename_field: self.txtFileName.setText(filename_field) elif self.edit_file_path and not self.duplicate: # Use existing name (and prevent editing name) self.txtFileName.setText(os.path.basename(self.edit_file_path)) self.txtFileName.setEnabled(False) else: name = _("TitleFileName (%d)") offset = 0 if self.duplicate and self.edit_file_path: # Re-use current name name = os.path.basename(self.edit_file_path) # Splits the filename into: # [base-part][optional space][([number])].svg # Match groups are: # 1: Base name ("title", "Title-2", "Title number 3000") # 2: Space(s) preceding groups 3+4, IFF 3/4 are a match # 3: The entire parenthesized number ("(1)", "(20)", "(1000)") # 4: Just the number inside the parens ("1", "20", "1000") match = re.match(r"^(.+?)(\s*)(\(([0-9]*)\))?\.svg$", name) # Make sure the new title has " (%d)" appended by default name = match.group(1) + " (%d)" if match.group(4): # Filename already contained a number -> start counting from there offset = int(match.group(4)) # -> only include space(s) if there before name = match.group(1) + match.group(2) + "(%d)" # Find an unused file name for i in range(1, 1000): curname = name % (offset + i) possible_path = os.path.join(info.TITLE_PATH, "%s.svg" % curname) if not os.path.exists(possible_path): self.txtFileName.setText(curname) break self.txtFileName.setFixedHeight(28) layout.addRow(label, self.txtFileName) # Get text values title_text = [] for i, node in enumerate(self.tspan_nodes): if len(node.childNodes) < 1: continue text = node.childNodes[0].data title_text.append(text) # Set font size (for possible font dialog) s = node.attributes["style"].value ard = style_to_dict(s) fs = ard.get("font_size") if fs and fs.endswith("px"): self.qfont.setPixelSize(float(fs[:-2])) elif fs and fs.endswith("pt"): self.qfont.setPointSizeF(float(fs[:-2])) # Create Label label_line_text = _("Line %s:") % str(i + 1) label = QLabel(label_line_text) label.setToolTip(label_line_text) # create text editor for each text element in title widget = QTextEdit(_(text)) widget.setFixedHeight(28) widget.textChanged.connect(functools.partial(self.txtLine_changed, widget)) layout.addRow(label, widget) # Add Font button label = QLabel(_("Font:")) label.setToolTip(_("Font:")) self.btnFont = QPushButton(_("Change Font")) layout.addRow(label, self.btnFont) self.btnFont.clicked.connect(self.btnFont_clicked) # Add Text color button label = QLabel(_("Text:")) label.setToolTip(_("Text:")) self.btnFontColor = QPushButton(_("Text Color")) layout.addRow(label, self.btnFontColor) self.btnFontColor.clicked.connect(self.btnFontColor_clicked) # Add Background color button label = QLabel(_("Background:")) label.setToolTip(_("Background:")) self.btnBackgroundColor = QPushButton(_("Background Color")) layout.addRow(label, self.btnBackgroundColor) self.btnBackgroundColor.clicked.connect(self.btnBackgroundColor_clicked) # Add Advanced Editor button label = QLabel(_("Advanced:")) label.setToolTip(_("Advanced:")) self.btnAdvanced = QPushButton(_("Use Advanced Editor")) layout.addRow(label, self.btnAdvanced) self.btnAdvanced.clicked.connect(self.btnAdvanced_clicked) # Update color buttons self.update_font_color_button() self.update_background_color_button() # Enable / Disable buttons based on # of text nodes if len(title_text) >= 1: self.btnFont.setEnabled(True) self.btnFontColor.setEnabled(True) self.btnBackgroundColor.setEnabled(True) self.btnAdvanced.setEnabled(True) else: self.btnFont.setEnabled(False) self.btnFontColor.setEnabled(False) # Enable Save button when a template is selected self.buttonBox.button(self.buttonBox.Save).setEnabled(True)