36 from xml.dom
import minidom
39 from PyQt5.QtGui import QIcon, QStandardItemModel, QStandardItem, QFont
41 from PyQt5
import uic, QtSvg, QtGui
42 from PyQt5.QtWebKitWidgets
import QWebView
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)
117 self.widget.setVisible(
False)
132 for child
in self.settingsContainer.children():
133 if type(child) == QTextEdit
and child.objectName() !=
"txtFileName":
134 text_list.append(child.toPlainText())
138 if len(self.
tspan_node[i].childNodes) > 0
and i <= (len(text_list) - 1):
139 new_text_node = self.xmldoc.createTextNode(text_list[i])
140 old_text_node = self.
tspan_node[i].childNodes[0]
152 scene = QGraphicsScene(self)
153 view = self.graphicsView
155 svg_scaled = svg.scaled(self.graphicsView.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
156 scene.addPixmap(svg_scaled)
163 self.
filename = os.path.join(info.TITLE_PATH,
"temp.svg")
166 shutil.copy(template_path, self.
filename)
181 self.
tspan_node = self.xmldoc.getElementsByTagName(
'tspan')
185 for child
in self.settingsContainer.children():
187 self.settingsContainer.layout().removeWidget(child)
193 self.
text_node = self.xmldoc.getElementsByTagName(
'text')
194 self.
rect_node = self.xmldoc.getElementsByTagName(
'rect')
198 label_line_text = _(
"File Name:")
199 label.setText(label_line_text)
200 label.setToolTip(label_line_text)
204 self.txtFileName.setObjectName(
"txtFileName")
210 self.txtFileName.setEnabled(
False)
212 name = _(
"TitleFileName-%d")
218 match = re.match(
r"^(.*?)(-?)([0-9]*)(\.svg)?$", name)
220 name = match.group(1) +
"-%d"
223 offset = int(match.group(3))
225 name = match.group(1) + match.group(2) +
"%d"
227 for i
in range(1, 1000):
228 curname = name % (offset + i)
229 possible_path = os.path.join(info.ASSETS_PATH,
"%s.svg" % curname)
230 if not os.path.exists(possible_path):
231 self.txtFileName.setText(curname)
233 self.txtFileName.setFixedHeight(28)
234 self.settingsContainer.layout().addRow(label, self.
txtFileName)
241 title_text.append(text)
245 label_line_text = _(
"Line %s:") % str(i + 1)
246 label.setText(label_line_text)
247 label.setToolTip(label_line_text)
251 widget.setText(_(text))
252 widget.setFixedHeight(28)
253 widget.textChanged.connect(functools.partial(self.
txtLine_changed, widget))
254 self.settingsContainer.layout().addRow(label, widget)
259 label.setText(_(
"Font:"))
260 label.setToolTip(_(
"Font:"))
262 self.btnFont.setText(_(
"Change Font"))
263 self.settingsContainer.layout().addRow(label, self.
btnFont)
268 label.setText(_(
"Text:"))
269 label.setToolTip(_(
"Text:"))
271 self.btnFontColor.setText(_(
"Text Color"))
272 self.settingsContainer.layout().addRow(label, self.
btnFontColor)
277 label.setText(_(
"Background:"))
278 label.setToolTip(_(
"Background:"))
280 self.btnBackgroundColor.setText(_(
"Background Color"))
286 label.setText(_(
"Advanced:"))
287 label.setToolTip(_(
"Advanced:"))
289 self.btnAdvanced.setText(_(
"Use Advanced Editor"))
290 self.settingsContainer.layout().addRow(label, self.
btnAdvanced)
298 if len(title_text) >= 1:
299 self.btnFont.setEnabled(
True)
300 self.btnFontColor.setEnabled(
True)
301 self.btnBackgroundColor.setEnabled(
True)
302 self.btnAdvanced.setEnabled(
True)
304 self.btnFont.setEnabled(
False)
305 self.btnFontColor.setEnabled(
False)
311 if not self.filename.endswith(
"svg"):
314 file = open(self.filename.encode(
'UTF-8'),
"wb")
315 file.write(bytes(xmldoc.toxml(),
'UTF-8'))
317 except IOError
as inst:
318 log.error(
"Error writing SVG title")
325 col = QColorDialog.getColor(Qt.white, self, _(
"Select a Color"),
326 QColorDialog.DontUseNativeDialog | QColorDialog.ShowAlphaChannel)
330 self.btnFontColor.setStyleSheet(
"background-color: %s" % col.name())
344 col = QColorDialog.getColor(Qt.white, self, _(
"Select a Color"),
345 QColorDialog.DontUseNativeDialog | QColorDialog.ShowAlphaChannel)
349 self.btnBackgroundColor.setStyleSheet(
"background-color: %s" % col.name())
363 font, ok = QFontDialog.getFont(QFont(), caption=_(
"Change Font"))
367 fontinfo = QtGui.QFontInfo(font)
383 if item.startswith(value):
394 s = node.attributes[
"style"].value
413 opacity = float(txt[8:])
425 color = QtGui.QColor(color)
427 alpha = int(opacity * 65535.0)
428 self.btnFontColor.setStyleSheet(
"background-color: %s; opacity %s" % (color.name(), alpha))
437 s = self.
rect_node[0].attributes[
"style"].value
456 opacity = float(txt[8:])
470 color = QtGui.QColor(color)
472 alpha = int(opacity * 65535.0)
474 self.btnBackgroundColor.setStyleSheet(
"background-color: %s; opacity %s" % (color.name(), alpha))
483 s = text_child.attributes[
"style"].value
506 s = tspan_child.attributes[
"style"].value
532 s = self.
rect_node[0].attributes[
"style"].value
536 ar.append(
"fill:" + color)
538 ar[fill] =
"fill:" + color
542 ar.append(
"opacity:" + str(alpha))
544 ar[opacity] =
"opacity:" + str(alpha)
558 s = text_child.attributes[
"style"].value
563 ar.append(
"fill:" + color)
565 ar[fill] =
"fill:" + color
569 ar.append(
"opacity:" + str(alpha))
571 ar[opacity] =
"opacity:" + str(alpha)
574 text_child.setAttribute(
"style", t.join(ar))
581 s = tspan_child.attributes[
"style"].value
586 ar.append(
"fill:" + color)
588 ar[fill] =
"fill:" + color
590 tspan_child.setAttribute(
"style", t.join(ar))
606 file_name =
"%s.svg" % self.txtFileName.toPlainText().strip()
607 file_path = os.path.join(info.ASSETS_PATH, file_name)
609 if self.txtFileName.toPlainText().strip():
612 ret = QMessageBox.question(self, _(
"Title Editor"), _(
"%s already exists.\nDo you want to replace it?") % file_name,
613 QMessageBox.No | QMessageBox.Yes)
614 if ret == QMessageBox.No:
628 super(TitleEditor, self).
accept()
631 path, filename = os.path.split(filepath)
638 file = File.get(path=filepath)
645 clip = openshot.Clip(filepath)
649 reader = clip.Reader()
650 file_data = json.loads(reader.Json())
653 file_data[
"media_type"] =
"image"
657 file.data = file_data
664 msg.setText(_(
"{} is not a valid video, audio, or image file.".format(filename)))
676 prog = s.get(
"title_editor")
680 log.info(
"Advanced title editor command: {} {} ".format(prog, self.
filename))
682 p = subprocess.Popen([prog, self.
filename])
693 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.