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
54 import simplejson
as json
62 ui_path = os.path.join(info.PATH,
'windows',
'ui',
'title-editor.ui')
67 QDialog.__init__(self)
86 imp = minidom.getDOMImplementation()
87 self.
xmldoc = imp.createDocument(
None,
"any",
None)
111 temp_svg_path = os.path.join(info.TITLE_PATH,
"temp.svg")
112 if os.path.exists(temp_svg_path):
113 os.remove(temp_svg_path)
116 self.cmbTemplate.addItem(_(
"<Select a template>"))
119 for file
in sorted(os.listdir(info.TITLE_PATH)):
121 if fnmatch.fnmatch(file,
'*.svg'):
122 (fileName, fileExtension) = os.path.splitext(file)
123 self.cmbTemplate.addItem(fileName.replace(
"_",
" "), os.path.join(info.TITLE_PATH, file))
129 if fnmatch.fnmatch(file,
'*.svg'):
130 (fileName, fileExtension) = os.path.splitext(file)
131 self.cmbTemplate.addItem(fileName.replace(
"_",
" "), os.path.join(self.
template_dir, file))
139 self.txtLine1.textChanged.connect(functools.partial(self.
txtLine_changed))
140 self.txtLine2.textChanged.connect(functools.partial(self.
txtLine_changed))
141 self.txtLine3.textChanged.connect(functools.partial(self.
txtLine_changed))
142 self.txtLine4.textChanged.connect(functools.partial(self.
txtLine_changed))
143 self.txtLine5.textChanged.connect(functools.partial(self.
txtLine_changed))
144 self.txtLine6.textChanged.connect(functools.partial(self.
txtLine_changed))
150 text_list.append(self.txtLine1.toPlainText())
151 text_list.append(self.txtLine2.toPlainText())
152 text_list.append(self.txtLine3.toPlainText())
153 text_list.append(self.txtLine4.toPlainText())
154 text_list.append(self.txtLine5.toPlainText())
155 text_list.append(self.txtLine6.toPlainText())
157 if len(self.
tspan_node[i].childNodes) > 0
and i <= (len(text_list) - 1):
158 new_text_node = self.xmldoc.createTextNode(text_list[i])
159 old_text_node = self.
tspan_node[i].childNodes[0]
173 self.txtLine1.setVisible(
False)
174 self.lblLine1.setVisible(
False)
175 self.txtLine2.setVisible(
False)
176 self.lblLine2.setVisible(
False)
177 self.txtLine3.setVisible(
False)
178 self.lblLine3.setVisible(
False)
179 self.txtLine4.setVisible(
False)
180 self.lblLine4.setVisible(
False)
181 self.txtLine5.setVisible(
False)
182 self.lblLine5.setVisible(
False)
183 self.txtLine6.setVisible(
False)
184 self.lblLine6.setVisible(
False)
191 self.txtLine1.setEnabled(
True)
192 self.txtLine1.setVisible(
True)
193 self.lblLine1.setVisible(
True)
195 self.txtLine2.setEnabled(
True)
196 self.txtLine2.setVisible(
True)
197 self.lblLine2.setVisible(
True)
199 self.txtLine3.setEnabled(
True)
200 self.txtLine3.setVisible(
True)
201 self.lblLine3.setVisible(
True)
203 self.txtLine4.setEnabled(
True)
204 self.txtLine4.setVisible(
True)
205 self.lblLine4.setVisible(
True)
207 self.txtLine5.setEnabled(
True)
208 self.txtLine5.setVisible(
True)
209 self.lblLine5.setVisible(
True)
211 self.txtLine6.setEnabled(
True)
212 self.txtLine6.setVisible(
True)
213 self.lblLine6.setVisible(
True)
216 scene = QGraphicsScene(self)
217 view = self.graphicsView
219 svg_scaled = svg.scaled(svg.width() / 4, svg.height() / 4, Qt.KeepAspectRatio)
220 scene.addPixmap(svg_scaled)
226 if self.cmbTemplate.currentIndex() > 0:
228 template = self.cmbTemplate.currentText()
229 template_path = self.cmbTemplate.itemData(self.cmbTemplate.currentIndex())
243 self.
filename = os.path.join(info.TITLE_PATH,
"temp.svg")
246 shutil.copy(template_path, self.
filename)
258 self.
tspan_node = self.xmldoc.getElementsByTagName(
'tspan')
267 self.
text_node = self.xmldoc.getElementsByTagName(
'text')
268 self.
rect_node = self.xmldoc.getElementsByTagName(
'rect')
274 title_text.append(self.
tspan_node[i].childNodes[0].data)
277 num_fields = len(title_text)
279 self.txtLine1.setText(
"")
280 self.txtLine1.setText(title_text[0])
282 self.txtLine2.setText(
"")
283 self.txtLine2.setText(title_text[1])
285 self.txtLine3.setText(
"")
286 self.txtLine3.setText(title_text[2])
288 self.txtLine4.setText(
"")
289 self.txtLine4.setText(title_text[3])
291 self.txtLine5.setText(
"")
292 self.txtLine5.setText(title_text[4])
294 self.txtLine6.setText(
"")
295 self.txtLine6.setText(title_text[5])
303 self.btnFont.setEnabled(
True)
304 self.btnFontColor.setEnabled(
True)
305 self.btnBackgroundColor.setEnabled(
True)
306 self.btnAdvanced.setEnabled(
True)
308 self.btnFont.setEnabled(
False)
309 self.btnFontColor.setEnabled(
False)
315 if not self.filename.endswith(
"svg"):
318 file = open(self.filename.encode(
'UTF-8'),
"wb")
319 file.write(bytes(xmldoc.toxml(),
'UTF-8'))
321 except IOError
as inst:
322 log.error(
"Error writing SVG title")
329 col = QColorDialog.getColor(Qt.white, self, _(
"Select a Color"),
330 QColorDialog.DontUseNativeDialog | QColorDialog.ShowAlphaChannel)
334 self.btnFontColor.setStyleSheet(
"background-color: %s" % col.name())
348 col = QColorDialog.getColor(Qt.white, self, _(
"Select a Color"),
349 QColorDialog.DontUseNativeDialog | QColorDialog.ShowAlphaChannel)
353 self.btnBackgroundColor.setStyleSheet(
"background-color: %s" % col.name())
367 font, ok = QFontDialog.getFont(QFont(), caption=_(
"Change Font"))
371 fontinfo = QtGui.QFontInfo(font)
387 if item.startswith(value):
398 s = node.attributes[
"style"].value
417 opacity = float(txt[8:])
429 color = QtGui.QColor(color)
431 alpha = int(opacity * 65535.0)
432 self.btnFontColor.setStyleSheet(
"background-color: %s; opacity %s" % (color.name(), alpha))
441 s = self.
rect_node[0].attributes[
"style"].value
460 opacity = float(txt[8:])
474 color = QtGui.QColor(color)
476 alpha = int(opacity * 65535.0)
478 self.btnBackgroundColor.setStyleSheet(
"background-color: %s; opacity %s" % (color.name(), alpha))
487 s = text_child.attributes[
"style"].value
508 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))
597 project_path =
get_app().project.current_filepath
598 default_folder = info.HOME_PATH
600 default_folder = os.path.dirname(project_path)
603 title_path = os.path.join(default_folder,
"%s.svg" % _(
"New Title"))
606 file_path, file_type = QFileDialog.getSaveFileName(self, _(
"Save Title As..."), title_path, _(
"Scalable Vector Graphics (*.svg)"))
610 if not file_path.endswith(
"svg"):
611 file_path = file_path +
".svg" 623 super(TitleEditor, self).
accept()
626 path, filename = os.path.split(filepath)
633 file = File.get(path=filepath)
640 clip = openshot.Clip(filepath)
644 reader = clip.Reader()
645 file_data = json.loads(reader.Json())
648 file_data[
"media_type"] =
"image" 652 file.data = file_data
659 msg.setText(_(
"{} is not a valid video, audio, or image file.".
format(filename)))
671 prog = s.get(
"title_editor")
675 log.info(
"Advanced title editor command: {} {} ".
format(prog, self.
filename))
677 p = subprocess.Popen([prog, self.
filename])
688 msg.setText(_(
"Please install {} to use this function").
format(prog.capitalize()))
def btnAdvanced_clicked(self)
def cmbTemplate_activated(self)
def get_app()
Returns the current QApplication instance of OpenShot.
def update_background_color_button(self)
Updates the color shown on the background color button.
def create_temp_title(self, template_path)
def load_svg_template(self)
Load an SVG title and init all textboxes and controls.
def btnBackgroundColor_clicked(self)
def btnFontColor_clicked(self)
def hide_textboxes(self)
Hide all text inputs.
def set_font_color_elements(self, color, alpha)
def find_in_list(self, l, value)
when passed a partial value, function will return the list index
def writeToFile(self, xmldoc)
writes a new svg file containing the user edited data
def get_settings()
Get the current QApplication's settings instance.
def btnFont_clicked(self)
def init_ui(window)
Initialize all child widgets and action of a window or dialog.
def set_bg_style(self, color, alpha)
sets the background color
def track_metric_screen(screen_name)
Track a GUI screen being shown.
def txtLine_changed(self)
def load_ui(window, path)
Load a Qt *.ui file, and also load an XML parsed version.
def show_textboxes(self, num_fields)
Only show a certain number of text inputs.
def update_font_color_button(self)
Updates the color shown on the font color button.
def add_file(self, filepath)
def set_font_style(self)
sets the font properties