1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
21 #include <QDomDocument>
23 #include <KStandardDirs>
25 #include <KMessageBox>
28 #include "kdenlivesettings.h"
29 #include "renderwidget.h"
30 #include "ui_saveprofile_ui.h"
32 const int GroupRole = Qt::UserRole;
33 const int ExtensionRole = GroupRole + 1;
34 const int StandardRole = GroupRole + 2;
35 const int RenderRole = GroupRole + 3;
36 const int ParamsRole = GroupRole + 4;
37 const int EditableRole = GroupRole + 5;
39 RenderWidget::RenderWidget(QWidget * parent): QDialog(parent) {
41 m_view.buttonDelete->setIcon(KIcon("trash-empty"));
42 m_view.buttonDelete->setToolTip(i18n("Delete profile"));
43 m_view.buttonDelete->setEnabled(false);
45 m_view.buttonEdit->setIcon(KIcon("document-properties"));
46 m_view.buttonEdit->setToolTip(i18n("Edit profile"));
47 m_view.buttonEdit->setEnabled(false);
49 m_view.buttonSave->setIcon(KIcon("document-new"));
50 m_view.buttonSave->setToolTip(i18n("Create new profile"));
52 m_view.buttonInfo->setIcon(KIcon("help-about"));
54 if (KdenliveSettings::showrenderparams()) {
55 m_view.buttonInfo->setDown(true);
56 } else m_view.advanced_params->hide();
58 m_view.experimentalrender->setChecked(KdenliveSettings::experimentalrender());
60 m_view.experimentalrender->setToolTip(i18n("Changing the size of video when rendering\nis not fully supported, you may have problems\nwith some effects or title clips, so the export\nprofiles that resize your video are marked as\nexperimental"));
62 connect(m_view.buttonInfo, SIGNAL(clicked()), this, SLOT(showInfoPanel()));
64 connect(m_view.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
65 connect(m_view.buttonEdit, SIGNAL(clicked()), this, SLOT(slotEditProfile()));
66 connect(m_view.buttonDelete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
67 connect(m_view.buttonStart, SIGNAL(clicked()), this, SLOT(slotExport()));
68 connect(m_view.out_file, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButtons()));
69 connect(m_view.format_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshView()));
70 connect(m_view.size_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshParams()));
72 connect(m_view.render_guide, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
73 connect(m_view.render_zone, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
74 connect(m_view.render_full, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
76 connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
77 connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));
79 connect(m_view.format_selection, SIGNAL(activated(int)), this, SLOT(refreshView()));
80 connect(m_view.experimentalrender, SIGNAL(stateChanged(int)), this, SLOT(slotUpdateExperimentalRendering()));
82 m_view.buttonStart->setEnabled(false);
83 m_view.guides_box->setVisible(false);
85 m_view.splitter->setStretchFactor(1, 5);
86 m_view.splitter->setStretchFactor(0, 2);
88 m_view.out_file->setMode(KFile::File);
89 focusFirstVisibleItem();
92 void RenderWidget::slotUpdateExperimentalRendering() {
93 KdenliveSettings::setExperimentalrender(m_view.experimentalrender->isChecked());
98 void RenderWidget::showInfoPanel() {
99 if (m_view.advanced_params->isVisible()) {
100 m_view.advanced_params->setVisible(false);
101 m_view.buttonInfo->setDown(false);
102 KdenliveSettings::setShowrenderparams(false);
104 m_view.advanced_params->setVisible(true);
105 m_view.buttonInfo->setDown(true);
106 KdenliveSettings::setShowrenderparams(true);
110 void RenderWidget::slotUpdateGuideBox() {
111 m_view.guides_box->setVisible(m_view.render_guide->isChecked());
114 void RenderWidget::slotCheckStartGuidePosition() {
115 if (m_view.guide_start->currentIndex() > m_view.guide_end->currentIndex())
116 m_view.guide_start->setCurrentIndex(m_view.guide_end->currentIndex());
119 void RenderWidget::slotCheckEndGuidePosition() {
120 if (m_view.guide_end->currentIndex() < m_view.guide_start->currentIndex())
121 m_view.guide_end->setCurrentIndex(m_view.guide_start->currentIndex());
124 void RenderWidget::setGuides(QDomElement guidesxml, double duration) {
125 m_view.guide_start->clear();
126 m_view.guide_end->clear();
127 QDomNodeList nodes = guidesxml.elementsByTagName("guide");
128 if (nodes.count() > 0) {
129 m_view.guide_start->addItem(i18n("Render"), "0");
130 m_view.render_guide->setEnabled(true);
131 } else m_view.render_guide->setEnabled(false);
132 for (int i = 0; i < nodes.count(); i++) {
133 QDomElement e = nodes.item(i).toElement();
135 m_view.guide_start->addItem(e.attribute("comment"), e.attribute("time").toDouble());
136 m_view.guide_end->addItem(e.attribute("comment"), e.attribute("time").toDouble());
139 if (nodes.count() > 0)
140 m_view.guide_end->addItem(i18n("End"), QString::number(duration));
143 void RenderWidget::slotUpdateButtons() {
144 if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
145 else m_view.buttonStart->setEnabled(true);
148 void RenderWidget::slotSaveProfile() {
149 Ui::SaveProfile_UI ui;
150 QDialog *d = new QDialog(this);
152 QString customGroup = i18n("Custom");
153 QStringList groupNames;
154 for (int i = 0; i < m_view.format_list->count(); i++)
155 groupNames.append(m_view.format_list->item(i)->text());
156 if (!groupNames.contains(customGroup)) groupNames.prepend(customGroup);
157 ui.group_name->addItems(groupNames);
158 int pos = ui.group_name->findText(customGroup);
159 ui.group_name->setCurrentIndex(pos);
161 ui.parameters->setText(m_view.advanced_params->toPlainText());
162 ui.extension->setText(m_view.size_list->currentItem()->data(ExtensionRole).toString());
163 ui.profile_name->setFocus();
164 if (d->exec() == QDialog::Accepted && !ui.profile_name->text().simplified().isEmpty()) {
165 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
167 QFile file(exportFile);
168 doc.setContent(&file, false);
171 QDomElement documentElement;
172 bool groupExists = false;
174 QString newProfileName = ui.profile_name->text().simplified();
175 QString newGroupName = ui.group_name->currentText();
176 QDomNodeList groups = doc.elementsByTagName("group");
178 if (groups.count() == 0) {
179 QDomElement profiles = doc.createElement("profiles");
180 doc.appendChild(profiles);
181 } else while (!groups.item(i).isNull()) {
182 documentElement = groups.item(i).toElement();
183 groupName = documentElement.attribute("name");
184 kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
185 if (groupName == newGroupName) {
192 documentElement = doc.createElement("group");
193 documentElement.setAttribute("name", ui.group_name->currentText());
194 documentElement.setAttribute("renderer", "avformat");
195 doc.documentElement().appendChild(documentElement);
197 QDomElement profileElement = doc.createElement("profile");
198 profileElement.setAttribute("name", newProfileName);
199 profileElement.setAttribute("extension", ui.extension->text().simplified());
200 profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
201 documentElement.appendChild(profileElement);
203 //QCString save = doc.toString().utf8();
205 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
206 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
210 QTextStream out(&file);
211 out << doc.toString();
213 parseProfiles(newGroupName, newProfileName);
218 void RenderWidget::slotEditProfile() {
219 QListWidgetItem *item = m_view.size_list->currentItem();
221 QString currentGroup = m_view.format_list->currentItem()->text();
223 QString params = item->data(ParamsRole).toString();
224 QString extension = item->data(ExtensionRole).toString();
225 QString currentProfile = item->text();
227 Ui::SaveProfile_UI ui;
228 QDialog *d = new QDialog(this);
230 QStringList groupNames;
231 for (int i = 0; i < m_view.format_list->count(); i++)
232 groupNames.append(m_view.format_list->item(i)->text());
233 if (!groupNames.contains(currentGroup)) groupNames.prepend(currentGroup);
234 ui.group_name->addItems(groupNames);
235 int pos = ui.group_name->findText(currentGroup);
236 ui.group_name->setCurrentIndex(pos);
237 ui.profile_name->setText(currentProfile);
238 ui.extension->setText(extension);
239 ui.parameters->setText(params);
240 ui.profile_name->setFocus();
242 if (d->exec() == QDialog::Accepted) {
244 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
246 QFile file(exportFile);
247 doc.setContent(&file, false);
250 QDomElement documentElement;
251 bool groupExists = false;
253 QString newProfileName = ui.profile_name->text();
254 QString newGroupName = ui.group_name->currentText();
255 QDomNodeList groups = doc.elementsByTagName("group");
257 if (groups.count() == 0) {
258 QDomElement profiles = doc.createElement("profiles");
259 doc.appendChild(profiles);
260 } else while (!groups.item(i).isNull()) {
261 documentElement = groups.item(i).toElement();
262 groupName = documentElement.attribute("name");
263 kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
264 if (groupName == newGroupName) {
271 documentElement = doc.createElement("group");
272 documentElement.setAttribute("name", ui.group_name->currentText());
273 documentElement.setAttribute("renderer", "avformat");
274 doc.documentElement().appendChild(documentElement);
276 QDomElement profileElement = doc.createElement("profile");
277 profileElement.setAttribute("name", newProfileName);
278 profileElement.setAttribute("extension", ui.extension->text().simplified());
279 profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
280 documentElement.appendChild(profileElement);
282 //QCString save = doc.toString().utf8();
284 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
285 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
289 QTextStream out(&file);
290 out << doc.toString();
292 parseProfiles(newGroupName, newProfileName);
297 void RenderWidget::slotDeleteProfile() {
298 QString currentGroup = m_view.format_list->currentItem()->text();
299 QString currentProfile = m_view.size_list->currentItem()->text();
301 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
303 QFile file(exportFile);
304 doc.setContent(&file, false);
307 QDomElement documentElement;
308 bool groupExists = false;
310 QDomNodeList groups = doc.elementsByTagName("group");
313 while (!groups.item(i).isNull()) {
314 documentElement = groups.item(i).toElement();
315 groupName = documentElement.attribute("name");
316 if (groupName == currentGroup) {
317 QDomNodeList children = documentElement.childNodes();
318 for (int j = 0; j < children.count(); j++) {
319 QDomElement pro = children.at(j).toElement();
320 if (pro.attribute("name") == currentProfile) {
321 groups.item(i).removeChild(children.at(j));
322 if (groups.item(i).childNodes().isEmpty())
323 doc.documentElement().removeChild(groups.item(i));
332 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
333 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
336 QTextStream out(&file);
337 out << doc.toString();
339 parseProfiles(currentGroup);
340 focusFirstVisibleItem();
343 void RenderWidget::updateButtons() {
344 if (!m_view.size_list->currentItem() || m_view.size_list->currentItem()->isHidden()) {
345 m_view.buttonSave->setEnabled(false);
346 m_view.buttonDelete->setEnabled(false);
347 m_view.buttonEdit->setEnabled(false);
349 m_view.buttonSave->setEnabled(true);
350 if (m_view.size_list->currentItem()->data(EditableRole).toString().isEmpty()) {
351 m_view.buttonDelete->setEnabled(false);
352 m_view.buttonEdit->setEnabled(false);
354 m_view.buttonDelete->setEnabled(true);
355 m_view.buttonEdit->setEnabled(true);
361 void RenderWidget::focusFirstVisibleItem() {
362 if (m_view.size_list->currentItem() && !m_view.size_list->currentItem()->isHidden()) {
366 for (uint ix = 0; ix < m_view.size_list->count(); ix++) {
367 QListWidgetItem *item = m_view.size_list->item(ix);
368 if (item && !item->isHidden()) {
369 m_view.size_list->setCurrentRow(ix);
373 if (!m_view.size_list->currentItem()) m_view.size_list->setCurrentRow(0);
377 void RenderWidget::slotExport() {
378 QListWidgetItem *item = m_view.size_list->currentItem();
380 QFile f(m_view.out_file->url().path());
382 if (KMessageBox::warningYesNo(this, i18n("File already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
385 QStringList overlayargs;
386 if (m_view.tc_overlay->isChecked()) {
387 QString filterFile = KStandardDirs::locate("appdata", "metadata.properties");
388 overlayargs << "meta.attr.timecode=1" << "meta.attr.timecode.markup=#timecode";
389 overlayargs << "-attach" << "data_feed:attr_check" << "-attach";
390 overlayargs << "data_show:" + filterFile << "_fezzik=1" << "dynamic=1";
392 double startPos = -1;
394 if (m_view.render_guide->isChecked()) {
395 startPos = m_view.guide_start->itemData(m_view.guide_start->currentIndex()).toDouble();
396 endPos = m_view.guide_end->itemData(m_view.guide_end->currentIndex()).toDouble();
398 QString renderArgs = m_view.advanced_params->toPlainText();
399 renderArgs.replace("%width", QString::number(m_profile.width));
400 renderArgs.replace("%height", QString::number(m_profile.height));
401 renderArgs.replace("%dar", "@" + QString::number(m_profile.display_aspect_num) + "/" + QString::number(m_profile.display_aspect_den));
402 if (m_view.force_progressive->checkState() == Qt::Checked) renderArgs.append(" progressive=1");
403 else if (m_view.force_progressive->checkState() == Qt::Unchecked) renderArgs.append(" progressive=0");
405 // Check if the rendering profile is different from project profile,
406 // in which case we need to use the producer_comsumer from MLT
407 bool resizeProfile = false;
409 QString std = item->data(ParamsRole).toString();
410 if (resizeProfile == false && std.contains(" s=")) {
411 QString subsize = std.section(" s=", 1, 1);
412 subsize = subsize.section(' ', 0, 0).toLower();
413 if (subsize != "%widthx%height") {
414 const QString currentSize = QString::number(m_profile.width) + 'x' + QString::number(m_profile.height);
415 if (subsize != currentSize) resizeProfile = true;
419 emit doRender(m_view.out_file->url().path(), item->data(RenderRole).toString(), overlayargs, renderArgs.simplified().split(' '), m_view.render_zone->isChecked(), m_view.play_after->isChecked(), startPos, endPos, resizeProfile);
422 void RenderWidget::setProfile(MltVideoProfile profile) {
424 //WARNING: this way to tell the video standard is a bit hackish...
425 if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) m_view.format_selection->setCurrentIndex(0);
426 else m_view.format_selection->setCurrentIndex(1);
427 m_view.force_progressive->setCheckState(Qt::PartiallyChecked);
431 void RenderWidget::refreshView() {
432 m_view.size_list->blockSignals(true);
433 QListWidgetItem *item = m_view.format_list->currentItem();
435 m_view.format_list->setCurrentRow(0);
436 item = m_view.format_list->currentItem();
440 QString group = item->text();
441 QListWidgetItem *sizeItem;
442 bool firstSelected = false;
443 const QStringList formatsList = KdenliveSettings::supportedformats();
444 const QStringList vcodecsList = KdenliveSettings::videocodecs();
445 const QStringList acodecsList = KdenliveSettings::audiocodecs();
446 for (int i = 0; i < m_view.size_list->count(); i++) {
447 sizeItem = m_view.size_list->item(i);
448 if (sizeItem->data(GroupRole) == group) {
449 std = sizeItem->data(StandardRole).toString();
450 if (!std.isEmpty()) {
451 if (std.contains("PAL", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 0);
452 else if (std.contains("NTSC", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 1);
454 sizeItem->setHidden(false);
455 if (!firstSelected) m_view.size_list->setCurrentItem(sizeItem);
456 firstSelected = true;
458 if (!KdenliveSettings::experimentalrender() && !sizeItem->isHidden()) {
459 // hide experimental codecs (which do resize the video)
460 std = sizeItem->data(ParamsRole).toString();
461 if (std.contains(" s=")) {
462 QString subsize = std.section(" s=", 1, 1);
463 subsize = subsize.section(' ', 0, 0).toLower();
464 if (subsize != "%widthx%height") {
465 const QString currentSize = QString::number(m_profile.width) + 'x' + QString::number(m_profile.height);
466 if (subsize != currentSize) sizeItem->setHidden(true);
470 if (!sizeItem->isHidden()) {
471 // Make sure the selected profile uses an installed avformat codec / format
472 std = sizeItem->data(ParamsRole).toString();
474 if (!formatsList.isEmpty()) {
476 if (std.startsWith("f=")) format = std.section("f=", 1, 1);
477 else if (std.contains(" f=")) format = std.section(" f=", 1, 1);
478 if (!format.isEmpty()) {
479 format = format.section(' ', 0, 0).toLower();
480 if (!formatsList.contains(format)) {
481 kDebug() << "***** UNSUPPORTED F: " << format;
482 sizeItem->setHidden(true);
486 if (!acodecsList.isEmpty() && !sizeItem->isHidden()) {
488 if (std.startsWith("acodec=")) format = std.section("acodec=", 1, 1);
489 else if (std.contains(" acodec=")) format = std.section(" acodec=", 1, 1);
490 if (!format.isEmpty()) {
491 format = format.section(' ', 0, 0).toLower();
492 if (!acodecsList.contains(format)) {
493 kDebug() << "***** UNSUPPORTED ACODEC: " << format;
494 sizeItem->setHidden(true);
498 if (!vcodecsList.isEmpty() && !sizeItem->isHidden()) {
500 if (std.startsWith("vcodec=")) format = std.section("vcodec=", 1, 1);
501 else if (std.contains(" vcodec=")) format = std.section(" vcodec=", 1, 1);
502 if (!format.isEmpty()) {
503 format = format.section(' ', 0, 0).toLower();
504 if (!vcodecsList.contains(format)) {
505 kDebug() << "***** UNSUPPORTED VCODEC: " << format;
506 sizeItem->setHidden(true);
511 } else sizeItem->setHidden(true);
513 focusFirstVisibleItem();
514 m_view.size_list->blockSignals(false);
518 void RenderWidget::refreshParams() {
519 QListWidgetItem *item = m_view.size_list->currentItem();
520 if (!item || item->isHidden()) {
521 m_view.advanced_params->clear();
522 m_view.buttonStart->setEnabled(false);
525 QString params = item->data(ParamsRole).toString();
526 QString extension = item->data(ExtensionRole).toString();
527 m_view.advanced_params->setPlainText(params);
528 m_view.advanced_params->setToolTip(params);
529 KUrl url = m_view.out_file->url();
530 if (!url.isEmpty()) {
531 QString path = url.path();
532 int pos = path.lastIndexOf('.') + 1;
533 if (pos == 0) path.append('.') + extension;
534 else path = path.left(pos) + extension;
535 m_view.out_file->setUrl(KUrl(path));
537 m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
539 m_view.out_file->setFilter("*." + extension);
541 if (item->data(EditableRole).toString().isEmpty()) {
542 m_view.buttonDelete->setEnabled(false);
543 m_view.buttonEdit->setEnabled(false);
545 m_view.buttonDelete->setEnabled(true);
546 m_view.buttonEdit->setEnabled(true);
548 m_view.buttonStart->setEnabled(true);
551 void RenderWidget::parseProfiles(QString group, QString profile) {
552 m_view.size_list->clear();
553 m_view.format_list->clear();
554 QString exportFile = KStandardDirs::locate("appdata", "export/profiles.xml");
555 parseFile(exportFile, false);
556 exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
557 if (QFile::exists(exportFile)) parseFile(exportFile, true);
559 QList<QListWidgetItem *> child;
560 child = m_view.format_list->findItems(group, Qt::MatchExactly);
561 if (!child.isEmpty()) m_view.format_list->setCurrentItem(child.at(0));
562 child = m_view.size_list->findItems(profile, Qt::MatchExactly);
563 if (!child.isEmpty()) m_view.size_list->setCurrentItem(child.at(0));
566 void RenderWidget::parseFile(QString exportFile, bool editable) {
568 QFile file(exportFile);
569 doc.setContent(&file, false);
571 QDomElement documentElement;
572 QDomElement profileElement;
573 QDomNodeList groups = doc.elementsByTagName("group");
575 if (groups.count() == 0) {
576 kDebug() << "// Export file: " << exportFile << " IS BROKEN";
584 QString prof_extension;
588 QListWidgetItem *item;
589 while (!groups.item(i).isNull()) {
590 documentElement = groups.item(i).toElement();
591 groupName = documentElement.attribute("name", QString::null);
592 extension = documentElement.attribute("extension", QString::null);
593 renderer = documentElement.attribute("renderer", QString::null);
594 if (m_view.format_list->findItems(groupName, Qt::MatchExactly).isEmpty())
595 new QListWidgetItem(groupName, m_view.format_list);
597 QDomNode n = groups.item(i).firstChild();
598 while (!n.isNull()) {
599 profileElement = n.toElement();
600 profileName = profileElement.attribute("name");
601 standard = profileElement.attribute("standard");
602 params = profileElement.attribute("args");
603 prof_extension = profileElement.attribute("extension");
604 if (!prof_extension.isEmpty()) extension = prof_extension;
605 item = new QListWidgetItem(profileName, m_view.size_list);
606 item->setData(GroupRole, groupName);
607 item->setData(ExtensionRole, extension);
608 item->setData(RenderRole, renderer);
609 item->setData(StandardRole, standard);
610 item->setData(ParamsRole, params);
611 if (editable) item->setData(EditableRole, "true");
621 #include "renderwidget.moc"