37 from xml.dom
import minidom
40 from PyQt5.QtGui import QIcon, QStandardItemModel, QStandardItem, QFont
42 from PyQt5
import uic, QtSvg, QtGui
45 from classes
import info, ui_util, settings, qt_types, updates
46 from classes.logger
import log
47 from classes.app
import get_app
48 from classes.query
import File
50 from windows.views.titles_listview
import TitlesListView
55 import simplejson
as json
63 ui_path = os.path.join(info.PATH,
'windows',
'ui',
'title-editor.ui')
65 def __init__(self, edit_file_path=None, duplicate=False):
68 QDialog.__init__(self)
89 imp = minidom.getDOMImplementation()
90 self.
xmldoc = imp.createDocument(
None,
"any",
None)
115 self.buttonBox.button(self.buttonBox.Save).setEnabled(
False)
120 self.widget.setVisible(
False)
135 for child
in self.settingsContainer.children():
136 if type(child) == QTextEdit
and child.objectName() !=
"txtFileName":
137 text_list.append(child.toPlainText())
141 if len(self.
tspan_node[i].childNodes) > 0
and i <= (len(text_list) - 1):
142 new_text_node = self.xmldoc.createTextNode(text_list[i])
143 old_text_node = self.
tspan_node[i].childNodes[0]
156 new_file, tmp_filename = tempfile.mkstemp()
157 tmp_filename =
"%s.png" % tmp_filename
161 reader = clip.Reader()
167 reader.GetFrame(1).Thumbnail(tmp_filename, self.graphicsView.width(), self.graphicsView.height(),
"",
"",
"#000",
False,
"png", 100, 0.0)
172 scene = QGraphicsScene(self)
173 view = self.graphicsView
174 svg = QtGui.QPixmap(tmp_filename)
175 svg_scaled = svg.scaled(self.graphicsView.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
176 scene.addPixmap(svg_scaled)
183 self.
filename = os.path.join(info.TITLE_PATH,
"temp.svg")
186 shutil.copy(template_path, self.
filename)
201 self.
tspan_node = self.xmldoc.getElementsByTagName(
'tspan')
205 for child
in self.settingsContainer.children():
207 self.settingsContainer.layout().removeWidget(child)
213 self.
text_node = self.xmldoc.getElementsByTagName(
'text')
214 self.
rect_node = self.xmldoc.getElementsByTagName(
'rect')
218 label_line_text = _(
"File Name:")
219 label.setText(label_line_text)
220 label.setToolTip(label_line_text)
224 self.txtFileName.setObjectName(
"txtFileName")
230 self.txtFileName.setEnabled(
False)
232 name = _(
"TitleFileName-%d")
238 match = re.match(
r"^(.*?)(-?)([0-9]*)(\.svg)?$", name)
240 name = match.group(1) +
"-%d"
243 offset = int(match.group(3))
245 name = match.group(1) + match.group(2) +
"%d"
247 for i
in range(1, 1000):
248 curname = name % (offset + i)
249 possible_path = os.path.join(info.ASSETS_PATH,
"%s.svg" % curname)
250 if not os.path.exists(possible_path):
251 self.txtFileName.setText(curname)
253 self.txtFileName.setFixedHeight(28)
254 self.settingsContainer.layout().addRow(label, self.
txtFileName)
261 title_text.append(text)
265 label_line_text = _(
"Line %s:") % str(i + 1)
266 label.setText(label_line_text)
267 label.setToolTip(label_line_text)
271 widget.setText(_(text))
272 widget.setFixedHeight(28)
273 widget.textChanged.connect(functools.partial(self.
txtLine_changed, widget))
274 self.settingsContainer.layout().addRow(label, widget)
279 label.setText(_(
"Font:"))
280 label.setToolTip(_(
"Font:"))
282 self.btnFont.setText(_(
"Change Font"))
283 self.settingsContainer.layout().addRow(label, self.
btnFont)
288 label.setText(_(
"Text:"))
289 label.setToolTip(_(
"Text:"))
291 self.btnFontColor.setText(_(
"Text Color"))
292 self.settingsContainer.layout().addRow(label, self.
btnFontColor)
297 label.setText(_(
"Background:"))
298 label.setToolTip(_(
"Background:"))
300 self.btnBackgroundColor.setText(_(
"Background Color"))
306 label.setText(_(
"Advanced:"))
307 label.setToolTip(_(
"Advanced:"))
309 self.btnAdvanced.setText(_(
"Use Advanced Editor"))
310 self.settingsContainer.layout().addRow(label, self.
btnAdvanced)
318 if len(title_text) >= 1:
319 self.btnFont.setEnabled(
True)
320 self.btnFontColor.setEnabled(
True)
321 self.btnBackgroundColor.setEnabled(
True)
322 self.btnAdvanced.setEnabled(
True)
324 self.btnFont.setEnabled(
False)
325 self.btnFontColor.setEnabled(
False)
328 self.buttonBox.button(self.buttonBox.Save).setEnabled(
True)
334 if not self.filename.endswith(
"svg"):
337 file = open(self.filename.encode(
'UTF-8'),
"wb")
338 file.write(bytes(xmldoc.toxml(),
'UTF-8'))
340 except IOError
as inst:
341 log.error(
"Error writing SVG title")
348 col = QColorDialog.getColor(self.
font_color_code, self, _(
"Select a Color"),
349 QColorDialog.DontUseNativeDialog | QColorDialog.ShowAlphaChannel)
368 col = QColorDialog.getColor(self.
bg_color_code, self, _(
"Select a Color"),
369 QColorDialog.DontUseNativeDialog | QColorDialog.ShowAlphaChannel)
388 font, ok = QFontDialog.getFont(QFont(), caption=_(
"Change Font"))
392 fontinfo = QtGui.QFontInfo(font)
408 if item.startswith(value):
419 s = node.attributes[
"style"].value
438 opacity = float(txt[8:])
450 color = QtGui.QColor(color)
453 colrgb = color.getRgbF()
454 lum = (0.299 * colrgb[0] + 0.587 * colrgb[1] + 0.114 * colrgb[2])
456 text_color = QtGui.QColor(Qt.white)
458 text_color = QtGui.QColor(Qt.black)
461 alpha = int(opacity * 65535.0)
464 self.btnFontColor.setStyleSheet(
465 "background-color: %s; opacity: %s; color: %s;"
466 % (color.name(), alpha, text_color.name()))
476 s = self.
rect_node[0].attributes[
"style"].value
495 opacity = float(txt[8:])
509 color = QtGui.QColor(color)
512 colrgb = color.getRgbF()
513 lum = (0.299 * colrgb[0] + 0.587 * colrgb[1] + 0.114 * colrgb[2])
515 text_color = QtGui.QColor(Qt.white)
517 text_color = QtGui.QColor(Qt.black)
520 alpha = int(opacity * 65535.0)
523 self.btnBackgroundColor.setStyleSheet(
524 "background-color: %s; opacity: %s; color: %s;"
525 % (color.name(), alpha, text_color.name()))
535 s = text_child.attributes[
"style"].value
558 s = tspan_child.attributes[
"style"].value
584 s = self.
rect_node[0].attributes[
"style"].value
588 ar.append(
"fill:" + color)
590 ar[fill] =
"fill:" + color
594 ar.append(
"opacity:" + str(alpha))
596 ar[opacity] =
"opacity:" + str(alpha)
610 s = text_child.attributes[
"style"].value
615 ar.append(
"fill:" + color)
617 ar[fill] =
"fill:" + color
621 ar.append(
"opacity:" + str(alpha))
623 ar[opacity] =
"opacity:" + str(alpha)
626 text_child.setAttribute(
"style", t.join(ar))
633 s = tspan_child.attributes[
"style"].value
638 ar.append(
"fill:" + color)
640 ar[fill] =
"fill:" + color
642 tspan_child.setAttribute(
"style", t.join(ar))
658 file_name =
"%s.svg" % self.txtFileName.toPlainText().strip()
659 file_path = os.path.join(info.ASSETS_PATH, file_name)
661 if self.txtFileName.toPlainText().strip():
664 ret = QMessageBox.question(self, _(
"Title Editor"), _(
"%s already exists.\nDo you want to replace it?") % file_name,
665 QMessageBox.No | QMessageBox.Yes)
666 if ret == QMessageBox.No:
680 super(TitleEditor, self).
accept()
683 path, filename = os.path.split(filepath)
690 file = File.get(path=filepath)
697 clip = openshot.Clip(filepath)
701 reader = clip.Reader()
702 file_data = json.loads(reader.Json())
705 file_data[
"media_type"] =
"image"
709 file.data = file_data
716 msg.setText(_(
"{} is not a valid video, audio, or image file.".format(filename)))
728 prog = s.get(
"title_editor")
732 log.info(
"Advanced title editor command: {} {} ".format(prog, self.
filename))
734 p = subprocess.Popen([prog, self.
filename])
745 msg.setText(_(
"Please install {} to use this function").format(prog.capitalize()))
def set_bg_style
sets the background color
def track_metric_screen
Track a GUI screen being shown.
def get_app
Returns the current QApplication instance of OpenShot.
def set_font_color_elements
def set_font_style
sets the font properties
def load_svg_template
Load an SVG title and init all textboxes and controls.
def find_in_list
when passed a partial value, function will return the list index
def update_background_color_button
Updates the color shown on the background color button.
def load_ui
Load a Qt *.ui file, and also load an XML parsed version.
def update_font_color_button
Updates the color shown on the font color button.
def init_ui
Initialize all child widgets and action of a window or dialog.
def writeToFile
writes a new svg file containing the user edited data
def btnBackgroundColor_clicked
def get_settings
Get the current QApplication's settings instance.