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>
22 #include <QItemDelegate>
23 #include <QTreeWidgetItem>
24 #include <QListWidgetItem>
25 #include <QHeaderView>
28 #include <QInputDialog>
30 #include <KStandardDirs>
32 #include <KMessageBox>
35 #include <KIO/NetAccess>
37 #include "kdenlivesettings.h"
38 #include "renderwidget.h"
39 #include "ui_saveprofile_ui.h"
41 const int GroupRole = Qt::UserRole;
42 const int ExtensionRole = GroupRole + 1;
43 const int StandardRole = GroupRole + 2;
44 const int RenderRole = GroupRole + 3;
45 const int ParamsRole = GroupRole + 4;
46 const int EditableRole = GroupRole + 5;
47 const int MetaGroupRole = GroupRole + 6;
48 const int ExtraRole = GroupRole + 7;
50 RenderWidget::RenderWidget(const QString &projectfolder, QWidget * parent): QDialog(parent), m_projectFolder(projectfolder) {
52 setWindowTitle(i18n("Rendering"));
53 m_view.buttonDelete->setIcon(KIcon("trash-empty"));
54 m_view.buttonDelete->setToolTip(i18n("Delete profile"));
55 m_view.buttonDelete->setEnabled(false);
57 m_view.buttonEdit->setIcon(KIcon("document-properties"));
58 m_view.buttonEdit->setToolTip(i18n("Edit profile"));
59 m_view.buttonEdit->setEnabled(false);
61 m_view.buttonSave->setIcon(KIcon("document-new"));
62 m_view.buttonSave->setToolTip(i18n("Create new profile"));
64 m_view.buttonInfo->setIcon(KIcon("help-about"));
66 if (KdenliveSettings::showrenderparams()) {
67 m_view.buttonInfo->setDown(true);
68 } else m_view.advanced_params->hide();
70 m_view.rescale_size->setInputMask("0099\\x0099");
71 m_view.rescale_size->setText("320x240");
74 QMenu *renderMenu = new QMenu(i18n("Start Rendering"), this);
75 QAction *renderAction = renderMenu->addAction(KIcon("file-new"), i18n("Render to File"));
76 connect(renderAction, SIGNAL(triggered()), this, SLOT(slotExport()));
77 QAction *scriptAction = renderMenu->addAction(KIcon("file-new"), i18n("Generate Script"));
78 connect(scriptAction, SIGNAL(triggered()), this, SLOT(slotGenerateScript()));
80 m_view.buttonStart->setMenu(renderMenu);
81 m_view.buttonStart->setPopupMode(QToolButton::MenuButtonPopup);
82 m_view.buttonStart->setDefaultAction(renderAction);
83 m_view.buttonStart->setToolButtonStyle(Qt::ToolButtonTextOnly);
84 m_view.abort_job->setEnabled(false);
85 m_view.start_script->setEnabled(false);
86 m_view.delete_script->setEnabled(false);
92 connect(m_view.start_script, SIGNAL(clicked()), this, SLOT(slotStartScript()));
93 connect(m_view.delete_script, SIGNAL(clicked()), this, SLOT(slotDeleteScript()));
94 connect(m_view.scripts_list, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(slotCheckScript()));
95 connect(m_view.running_jobs, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(slotCheckJob()));
97 connect(m_view.buttonInfo, SIGNAL(clicked()), this, SLOT(showInfoPanel()));
99 connect(m_view.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
100 connect(m_view.buttonEdit, SIGNAL(clicked()), this, SLOT(slotEditProfile()));
101 connect(m_view.buttonDelete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
102 connect(m_view.abort_job, SIGNAL(clicked()), this, SLOT(slotAbortCurrentJob()));
103 connect(m_view.buttonClose, SIGNAL(clicked()), this, SLOT(hide()));
104 connect(m_view.buttonClose2, SIGNAL(clicked()), this, SLOT(hide()));
105 connect(m_view.buttonClose3, SIGNAL(clicked()), this, SLOT(hide()));
106 connect(m_view.rescale, SIGNAL(toggled(bool)), m_view.rescale_size, SLOT(setEnabled(bool)));
107 connect(m_view.destination_list, SIGNAL(activated(int)), this, SLOT(refreshView()));
108 connect(m_view.out_file, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButtons()));
109 connect(m_view.out_file, SIGNAL(urlSelected(const KUrl &)), this, SLOT(slotUpdateButtons(const KUrl &)));
110 connect(m_view.format_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshView()));
111 connect(m_view.size_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshParams()));
113 connect(m_view.size_list, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(slotEditItem(QListWidgetItem *)));
115 connect(m_view.render_guide, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
116 connect(m_view.render_zone, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
117 connect(m_view.render_full, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
119 connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
120 connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));
122 connect(m_view.format_selection, SIGNAL(activated(int)), this, SLOT(refreshView()));
124 m_view.buttonStart->setEnabled(false);
125 m_view.rescale_size->setEnabled(false);
126 m_view.guides_box->setVisible(false);
127 m_view.open_dvd->setVisible(false);
128 m_view.open_browser->setVisible(false);
129 m_view.error_box->setVisible(false);
131 m_view.splitter->setStretchFactor(1, 5);
132 m_view.splitter->setStretchFactor(0, 2);
134 m_view.out_file->setMode(KFile::File);
136 m_view.running_jobs->setHeaderLabels(QStringList() << QString() << i18n("File") << i18n("Progress"));
137 m_view.running_jobs->setItemDelegate(new RenderViewDelegate(this));
139 QHeaderView *header = m_view.running_jobs->header();
140 QFontMetrics fm = fontMetrics();
141 //header->resizeSection(0, fm.width("typical-name-for-a-torrent.torrent"));
142 header->setResizeMode(0, QHeaderView::Fixed);
143 header->resizeSection(0, 30);
144 header->setResizeMode(1, QHeaderView::Interactive);
145 header->resizeSection(1, fm.width("typical-name-for-a-file.torrent"));
146 header->setResizeMode(2, QHeaderView::Fixed);
147 header->resizeSection(1, width() * 2 / 3);
148 header->setResizeMode(2, QHeaderView::Interactive);
149 //header->setResizeMode(1, QHeaderView::Fixed);
151 m_view.scripts_list->setHeaderLabels(QStringList() << i18n("Script Files"));
152 m_view.scripts_list->setItemDelegate(new RenderScriptDelegate(this));
155 focusFirstVisibleItem();
158 void RenderWidget::slotEditItem(QListWidgetItem *item) {
159 if (item->data(EditableRole).toString().isEmpty()) slotSaveProfile();
160 else slotEditProfile();
163 void RenderWidget::showInfoPanel() {
164 if (m_view.advanced_params->isVisible()) {
165 m_view.advanced_params->setVisible(false);
166 m_view.buttonInfo->setDown(false);
167 KdenliveSettings::setShowrenderparams(false);
169 m_view.advanced_params->setVisible(true);
170 m_view.buttonInfo->setDown(true);
171 KdenliveSettings::setShowrenderparams(true);
175 void RenderWidget::setDocumentPath(const QString path) {
176 m_projectFolder = path;
177 const QString fileName = m_view.out_file->url().fileName();
178 m_view.out_file->setUrl(KUrl(m_projectFolder + '/' + fileName));
182 void RenderWidget::slotUpdateGuideBox() {
183 m_view.guides_box->setVisible(m_view.render_guide->isChecked());
186 void RenderWidget::slotCheckStartGuidePosition() {
187 if (m_view.guide_start->currentIndex() > m_view.guide_end->currentIndex())
188 m_view.guide_start->setCurrentIndex(m_view.guide_end->currentIndex());
191 void RenderWidget::slotCheckEndGuidePosition() {
192 if (m_view.guide_end->currentIndex() < m_view.guide_start->currentIndex())
193 m_view.guide_end->setCurrentIndex(m_view.guide_start->currentIndex());
196 void RenderWidget::setGuides(QDomElement guidesxml, double duration) {
197 m_view.guide_start->clear();
198 m_view.guide_end->clear();
199 QDomNodeList nodes = guidesxml.elementsByTagName("guide");
200 if (nodes.count() > 0) {
201 m_view.guide_start->addItem(i18n("Render"), "0");
202 m_view.render_guide->setEnabled(true);
203 } else m_view.render_guide->setEnabled(false);
204 for (int i = 0; i < nodes.count(); i++) {
205 QDomElement e = nodes.item(i).toElement();
207 m_view.guide_start->addItem(e.attribute("comment"), e.attribute("time").toDouble());
208 m_view.guide_end->addItem(e.attribute("comment"), e.attribute("time").toDouble());
211 if (nodes.count() > 0)
212 m_view.guide_end->addItem(i18n("End"), QString::number(duration));
215 // Will be called when the user selects an output file via the file dialog.
216 // File extension will be added automatically.
217 void RenderWidget::slotUpdateButtons(KUrl url) {
218 if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
219 else m_view.buttonStart->setEnabled(true);
221 QListWidgetItem *item = m_view.size_list->currentItem();
222 QString extension = item->data(ExtensionRole).toString();
223 url = filenameWithExtension(url, extension);
224 m_view.out_file->setUrl(url);
228 // Will be called when the user changes the output file path in the text line.
229 // File extension must NOT be added, would make editing impossible!
230 void RenderWidget::slotUpdateButtons() {
231 if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
232 else m_view.buttonStart->setEnabled(true);
235 void RenderWidget::slotSaveProfile() {
236 //TODO: update to correctly use metagroups
237 Ui::SaveProfile_UI ui;
238 QDialog *d = new QDialog(this);
241 for (int i = 0; i < m_view.destination_list->count(); i++)
242 ui.destination_list->addItem(m_view.destination_list->itemIcon(i), m_view.destination_list->itemText(i), m_view.destination_list->itemData(i, Qt::UserRole));
244 ui.destination_list->setCurrentIndex(m_view.destination_list->currentIndex());
245 QString dest = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
247 QString customGroup = m_view.format_list->currentItem()->text();
248 if (customGroup.isEmpty()) customGroup = i18n("Custom");
249 ui.group_name->setText(customGroup);
251 ui.parameters->setText(m_view.advanced_params->toPlainText());
252 ui.extension->setText(m_view.size_list->currentItem()->data(ExtensionRole).toString());
253 ui.profile_name->setFocus();
254 if (d->exec() == QDialog::Accepted && !ui.profile_name->text().simplified().isEmpty()) {
255 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
257 QFile file(exportFile);
258 doc.setContent(&file, false);
260 QDomElement documentElement;
261 QDomElement profiles = doc.documentElement();
262 if (profiles.isNull() || profiles.tagName() != "profiles") {
264 profiles = doc.createElement("profiles");
265 profiles.setAttribute("version", 1);
266 doc.appendChild(profiles);
268 int version = profiles.attribute("version", 0).toInt();
270 kDebug() << "// OLD profile version";
272 profiles = doc.createElement("profiles");
273 profiles.setAttribute("version", 1);
274 doc.appendChild(profiles);
277 QString newProfileName = ui.profile_name->text().simplified();
278 QString newGroupName = ui.group_name->text().simplified();
279 if (newGroupName.isEmpty()) newGroupName = i18n("Custom");
280 QString newMetaGroupId = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
281 QDomNodeList profilelist = doc.elementsByTagName("profile");
283 while (!profilelist.item(i).isNull()) {
284 // make sure a profile with same name doesn't exist
285 documentElement = profilelist.item(i).toElement();
286 QString profileName = documentElement.attribute("name");
287 if (profileName == newProfileName) {
288 // a profile with that same name already exists
290 newProfileName = QInputDialog::getText(this, i18n("Profile already exists"), i18n("This profile name already exists. Change the name if you don't want to overwrite it."), QLineEdit::Normal, newProfileName, &ok);
292 if (profileName == newProfileName) {
293 profiles.removeChild(profilelist.item(i));
300 QDomElement profileElement = doc.createElement("profile");
301 profileElement.setAttribute("name", newProfileName);
302 profileElement.setAttribute("category", newGroupName);
303 profileElement.setAttribute("destinationid", newMetaGroupId);
304 profileElement.setAttribute("extension", ui.extension->text().simplified());
305 profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
306 profiles.appendChild(profileElement);
308 //QCString save = doc.toString().utf8();
310 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
311 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
315 QTextStream out(&file);
316 out << doc.toString();
318 parseProfiles(newMetaGroupId, newGroupName, newProfileName);
323 void RenderWidget::slotEditProfile() {
324 QListWidgetItem *item = m_view.size_list->currentItem();
326 QString currentGroup = m_view.format_list->currentItem()->text();
328 QString params = item->data(ParamsRole).toString();
329 QString extension = item->data(ExtensionRole).toString();
330 QString currentProfile = item->text();
332 Ui::SaveProfile_UI ui;
333 QDialog *d = new QDialog(this);
336 for (int i = 0; i < m_view.destination_list->count(); i++)
337 ui.destination_list->addItem(m_view.destination_list->itemIcon(i), m_view.destination_list->itemText(i), m_view.destination_list->itemData(i, Qt::UserRole));
339 ui.destination_list->setCurrentIndex(m_view.destination_list->currentIndex());
340 QString dest = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
342 QString customGroup = m_view.format_list->currentItem()->text();
343 if (customGroup.isEmpty()) customGroup = i18n("Custom");
344 ui.group_name->setText(customGroup);
346 ui.profile_name->setText(currentProfile);
347 ui.extension->setText(extension);
348 ui.parameters->setText(params);
349 ui.profile_name->setFocus();
350 d->setWindowTitle(i18n("Edit Profile"));
351 if (d->exec() == QDialog::Accepted) {
352 slotDeleteProfile(false);
353 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
355 QFile file(exportFile);
356 doc.setContent(&file, false);
358 QDomElement documentElement;
359 QDomElement profiles = doc.documentElement();
361 if (profiles.isNull() || profiles.tagName() != "profiles") {
363 profiles = doc.createElement("profiles");
364 profiles.setAttribute("version", 1);
365 doc.appendChild(profiles);
368 int version = profiles.attribute("version", 0).toInt();
370 kDebug() << "// OLD profile version";
372 profiles = doc.createElement("profiles");
373 profiles.setAttribute("version", 1);
374 doc.appendChild(profiles);
377 QString newProfileName = ui.profile_name->text().simplified();
378 QString newGroupName = ui.group_name->text().simplified();
379 if (newGroupName.isEmpty()) newGroupName = i18n("Custom");
380 QString newMetaGroupId = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
381 QDomNodeList profilelist = doc.elementsByTagName("profile");
383 while (!profilelist.item(i).isNull()) {
384 // make sure a profile with same name doesn't exist
385 documentElement = profilelist.item(i).toElement();
386 QString profileName = documentElement.attribute("name");
387 if (profileName == newProfileName) {
388 // a profile with that same name already exists
390 newProfileName = QInputDialog::getText(this, i18n("Profile already exists"), i18n("This profile name already exists. Change the name if you don't want to overwrite it."), QLineEdit::Normal, newProfileName, &ok);
392 if (profileName == newProfileName) {
393 profiles.removeChild(profilelist.item(i));
400 QDomElement profileElement = doc.createElement("profile");
401 profileElement.setAttribute("name", newProfileName);
402 profileElement.setAttribute("category", newGroupName);
403 profileElement.setAttribute("destinationid", newMetaGroupId);
404 profileElement.setAttribute("extension", ui.extension->text().simplified());
405 profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
406 profiles.appendChild(profileElement);
408 //QCString save = doc.toString().utf8();
410 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
411 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
415 QTextStream out(&file);
416 out << doc.toString();
418 parseProfiles(newMetaGroupId, newGroupName, newProfileName);
423 void RenderWidget::slotDeleteProfile(bool refresh) {
424 QString currentGroup = m_view.format_list->currentItem()->text();
425 QString currentProfile = m_view.size_list->currentItem()->text();
426 QString metaGroupId = m_view.destination_list->itemData(m_view.destination_list->currentIndex(), Qt::UserRole).toString();
428 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
430 QFile file(exportFile);
431 doc.setContent(&file, false);
434 QDomElement documentElement;
435 QDomNodeList profiles = doc.elementsByTagName("profile");
441 while (!profiles.item(i).isNull()) {
442 documentElement = profiles.item(i).toElement();
443 profileName = documentElement.attribute("name");
444 groupName = documentElement.attribute("category");
445 destination = documentElement.attribute("destinationid");
447 if (profileName == currentProfile && groupName == currentGroup && destination == metaGroupId) {
448 kDebug() << "// GOT it: " << profileName;
449 doc.documentElement().removeChild(profiles.item(i));
455 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
456 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
459 QTextStream out(&file);
460 out << doc.toString();
463 parseProfiles(metaGroupId, currentGroup);
464 focusFirstVisibleItem();
468 void RenderWidget::updateButtons() {
469 if (!m_view.size_list->currentItem() || m_view.size_list->currentItem()->isHidden()) {
470 m_view.buttonSave->setEnabled(false);
471 m_view.buttonDelete->setEnabled(false);
472 m_view.buttonEdit->setEnabled(false);
474 m_view.buttonSave->setEnabled(true);
475 if (m_view.size_list->currentItem()->data(EditableRole).toString().isEmpty()) {
476 m_view.buttonDelete->setEnabled(false);
477 m_view.buttonEdit->setEnabled(false);
479 m_view.buttonDelete->setEnabled(true);
480 m_view.buttonEdit->setEnabled(true);
486 void RenderWidget::focusFirstVisibleItem() {
487 if (m_view.size_list->currentItem() && !m_view.size_list->currentItem()->isHidden()) {
491 for (uint ix = 0; ix < m_view.size_list->count(); ix++) {
492 QListWidgetItem *item = m_view.size_list->item(ix);
493 if (item && !item->isHidden()) {
494 m_view.size_list->setCurrentRow(ix);
498 if (!m_view.size_list->currentItem()) m_view.size_list->setCurrentRow(0);
502 void RenderWidget::slotExport(bool scriptExport) {
503 QListWidgetItem *item = m_view.size_list->currentItem();
506 const QString dest = m_view.out_file->url().path();
507 if (dest.isEmpty()) return;
510 if (KMessageBox::warningYesNo(this, i18n("Output file already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
518 QString scriptsFolder = m_projectFolder + "/scripts/";
519 KStandardDirs::makeDir(scriptsFolder);
520 QString path = scriptsFolder + i18n("script") + QString::number(ix).rightJustified(3, '0', false) + ".sh";
521 while (QFile::exists(path)) {
523 path = scriptsFolder + i18n("script") + QString::number(ix).rightJustified(3, '0', false) + ".sh";
525 scriptName = QInputDialog::getText(this, i18n("Create Render Script"), i18n("Script name (will be saved in: %1)", scriptsFolder), QLineEdit::Normal, KUrl(path).fileName(), &ok);
526 if (!ok || scriptName.isEmpty()) return;
527 scriptName.prepend(scriptsFolder);
530 if (KMessageBox::warningYesNo(this, i18n("Script file already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
535 QStringList overlayargs;
536 if (m_view.tc_overlay->isChecked()) {
537 QString filterFile = KStandardDirs::locate("appdata", "metadata.properties");
538 overlayargs << "meta.attr.timecode=1" << "meta.attr.timecode.markup=#timecode";
539 overlayargs << "-attach" << "data_feed:attr_check" << "-attach";
540 overlayargs << "data_show:" + filterFile << "_fezzik=1" << "dynamic=1";
542 double startPos = -1;
544 if (m_view.render_guide->isChecked()) {
545 startPos = m_view.guide_start->itemData(m_view.guide_start->currentIndex()).toDouble();
546 endPos = m_view.guide_end->itemData(m_view.guide_end->currentIndex()).toDouble();
548 QString renderArgs = m_view.advanced_params->toPlainText();
550 // Adjust frame scale
553 if (m_view.rescale->isChecked() && m_view.rescale->isEnabled()) {
554 width = m_view.rescale_size->text().section('x', 0, 0).toInt();
555 height = m_view.rescale_size->text().section('x', 1, 1).toInt();
557 width = m_profile.width;
558 height = m_profile.height;
560 renderArgs.replace("%dar", "@" + QString::number(m_profile.display_aspect_num) + "/" + QString::number(m_profile.display_aspect_den));
563 if (m_view.scanning_list->currentIndex() == 1) renderArgs.append(" progressive=1");
564 else if (m_view.scanning_list->currentIndex() == 2) renderArgs.append(" progressive=0");
566 // disable audio if requested
567 if (!m_view.export_audio->isChecked())
568 renderArgs.append(" an=1 ");
570 // Check if the rendering profile is different from project profile,
571 // in which case we need to use the producer_comsumer from MLT
572 bool resizeProfile = false;
574 QString std = renderArgs;
575 QString destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
576 if (std.contains(" s=")) {
577 QString subsize = std.section(" s=", 1, 1);
578 subsize = subsize.section(' ', 0, 0).toLower();
579 const QString currentSize = QString::number(width) + 'x' + QString::number(height);
580 if (subsize != currentSize) resizeProfile = true;
581 } else if (destination != "audioonly") {
582 // Add current size parametrer
583 renderArgs.append(QString(" s=%1x%2").arg(width).arg(height));
586 // insert item in running jobs list
587 QTreeWidgetItem *renderItem;
588 QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
589 if (!existing.isEmpty()) renderItem = existing.at(0);
590 else renderItem = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
591 renderItem->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
592 renderItem->setData(1, Qt::UserRole + 1, QTime::currentTime());
594 // Set rendering type
595 QString group = m_view.size_list->currentItem()->data(MetaGroupRole).toString();
596 if (group == "dvd" && m_view.open_dvd->isChecked()) {
597 renderItem->setData(0, Qt::UserRole, group);
598 if (renderArgs.contains("profile=")) {
599 // rendering profile contains an MLT profile, so pass it to the running jog item, usefull for dvd
600 QString prof = renderArgs.section("profile=", 1, 1);
601 prof = prof.section(' ', 0, 0);
602 kDebug() << "// render profile: " << prof;
603 renderItem->setData(0, Qt::UserRole + 1, prof);
605 } else if (group == "websites" && m_view.open_browser->isChecked()) {
606 renderItem->setData(0, Qt::UserRole, group);
608 QString url = m_view.size_list->currentItem()->data(ExtraRole).toString();
609 renderItem->setData(0, Qt::UserRole + 1, url);
612 emit doRender(dest, item->data(RenderRole).toString(), overlayargs, renderArgs.simplified().split(' '), m_view.render_zone->isChecked(), m_view.play_after->isChecked(), startPos, endPos, resizeProfile, scriptName);
613 if (scriptName.isEmpty()) m_view.tabWidget->setCurrentIndex(1);
615 QTimer::singleShot(400, this, SLOT(parseScriptFiles()));
616 m_view.tabWidget->setCurrentIndex(2);
620 void RenderWidget::setProfile(MltVideoProfile profile) {
622 //WARNING: this way to tell the video standard is a bit hackish...
623 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);
624 else m_view.format_selection->setCurrentIndex(1);
625 m_view.scanning_list->setCurrentIndex(0);
629 void RenderWidget::refreshView() {
630 m_view.size_list->blockSignals(true);
631 QListWidgetItem *sizeItem;
634 KIcon brokenIcon("dialog-close");
635 if (m_view.destination_list->currentIndex() > 0)
636 destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
639 if (destination == "dvd") m_view.open_dvd->setVisible(true);
640 else m_view.open_dvd->setVisible(false);
641 if (destination == "websites") m_view.open_browser->setVisible(true);
642 else m_view.open_browser->setVisible(false);
643 if (!destination.isEmpty() && QString("dvd websites audioonly").contains(destination))
644 m_view.rescale->setEnabled(false);
645 else m_view.rescale->setEnabled(true);
646 // hide groups that are not in the correct destination
647 for (int i = 0; i < m_view.format_list->count(); i++) {
648 sizeItem = m_view.format_list->item(i);
649 if (sizeItem->data(MetaGroupRole).toString() == destination) {
650 sizeItem->setHidden(false);
651 //kDebug() << "// SET GRP:: " << sizeItem->text() << ", METY:" << sizeItem->data(MetaGroupRole).toString();
652 } else sizeItem->setHidden(true);
655 // activate first visible item
656 QListWidgetItem * item = m_view.format_list->currentItem();
657 if (!item || item->isHidden()) {
658 for (int i = 0; i < m_view.format_list->count(); i++) {
659 if (!m_view.format_list->item(i)->isHidden()) {
660 m_view.format_list->setCurrentRow(i);
664 item = m_view.format_list->currentItem();
666 if (!item || item->isHidden()) {
667 m_view.format_list->setEnabled(false);
668 m_view.size_list->setEnabled(false);
671 m_view.format_list->setEnabled(true);
672 m_view.size_list->setEnabled(true);
675 for (int i = 0; i < m_view.format_list->count() && count < 2; i++) {
676 if (!m_view.format_list->isRowHidden(i)) count++;
678 if (count > 1) m_view.format_list->setVisible(true);
679 else m_view.format_list->setVisible(false);
681 QString group = item->text();
682 bool firstSelected = false;
683 const QStringList formatsList = KdenliveSettings::supportedformats();
684 const QStringList vcodecsList = KdenliveSettings::videocodecs();
685 const QStringList acodecsList = KdenliveSettings::audiocodecs();
687 for (int i = 0; i < m_view.size_list->count(); i++) {
688 sizeItem = m_view.size_list->item(i);
689 if ((sizeItem->data(GroupRole) == group || sizeItem->data(GroupRole).toString().isEmpty()) && sizeItem->data(MetaGroupRole) == destination) {
690 std = sizeItem->data(StandardRole).toString();
691 if (!std.isEmpty()) {
692 if (std.contains("PAL", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 0);
693 else if (std.contains("NTSC", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 1);
695 sizeItem->setHidden(false);
696 if (!firstSelected) m_view.size_list->setCurrentItem(sizeItem);
697 firstSelected = true;
700 if (!sizeItem->isHidden()) {
701 // Make sure the selected profile uses an installed avformat codec / format
702 std = sizeItem->data(ParamsRole).toString();
704 if (!formatsList.isEmpty()) {
706 if (std.startsWith("f=")) format = std.section("f=", 1, 1);
707 else if (std.contains(" f=")) format = std.section(" f=", 1, 1);
708 if (!format.isEmpty()) {
709 format = format.section(' ', 0, 0).toLower();
710 if (!formatsList.contains(format)) {
711 kDebug() << "***** UNSUPPORTED F: " << format;
712 //sizeItem->setHidden(true);
713 sizeItem->setFlags(Qt::NoItemFlags);
714 sizeItem->setToolTip(i18n("Unsupported video format: %1", format));
715 sizeItem->setIcon(brokenIcon);
719 if (!acodecsList.isEmpty() && !sizeItem->isHidden()) {
721 if (std.startsWith("acodec=")) format = std.section("acodec=", 1, 1);
722 else if (std.contains(" acodec=")) format = std.section(" acodec=", 1, 1);
723 if (!format.isEmpty()) {
724 format = format.section(' ', 0, 0).toLower();
725 if (!acodecsList.contains(format)) {
726 kDebug() << "***** UNSUPPORTED ACODEC: " << format;
727 //sizeItem->setHidden(true);
728 sizeItem->setFlags(Qt::NoItemFlags);
729 sizeItem->setToolTip(i18n("Unsupported audio codec: %1", format));
730 sizeItem->setIcon(brokenIcon);
734 if (!vcodecsList.isEmpty() && !sizeItem->isHidden()) {
736 if (std.startsWith("vcodec=")) format = std.section("vcodec=", 1, 1);
737 else if (std.contains(" vcodec=")) format = std.section(" vcodec=", 1, 1);
738 if (!format.isEmpty()) {
739 format = format.section(' ', 0, 0).toLower();
740 if (!vcodecsList.contains(format)) {
741 kDebug() << "***** UNSUPPORTED VCODEC: " << format;
742 //sizeItem->setHidden(true);
743 sizeItem->setFlags(Qt::NoItemFlags);
744 sizeItem->setToolTip(i18n("Unsupported video codec: %1", format));
745 sizeItem->setIcon(brokenIcon);
750 } else sizeItem->setHidden(true);
752 focusFirstVisibleItem();
753 m_view.size_list->blockSignals(false);
757 KUrl RenderWidget::filenameWithExtension(KUrl url, QString extension) {
759 if (!url.isEmpty()) {
761 int pos = path.lastIndexOf('.') + 1;
762 if (pos == 0) path.append('.' + extension);
763 else path = path.left(pos) + extension;
766 path = m_projectFolder + "/untitled." + extension;
772 void RenderWidget::refreshParams() {
773 QListWidgetItem *item = m_view.size_list->currentItem();
774 if (!item || item->isHidden()) {
775 m_view.advanced_params->clear();
776 m_view.buttonStart->setEnabled(false);
779 QString params = item->data(ParamsRole).toString();
780 QString extension = item->data(ExtensionRole).toString();
781 m_view.advanced_params->setPlainText(params);
782 QString destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
783 if (params.contains(" s=") || destination == "audioonly") {
784 // profile has a fixed size, do not allow resize
785 m_view.rescale->setEnabled(false);
786 m_view.rescale_size->setEnabled(false);
788 m_view.rescale->setEnabled(true);
789 m_view.rescale_size->setEnabled(true);
791 KUrl url = filenameWithExtension(m_view.out_file->url(), extension);
792 m_view.out_file->setUrl(url);
793 // if (!url.isEmpty()) {
794 // QString path = url.path();
795 // int pos = path.lastIndexOf('.') + 1;
796 // if (pos == 0) path.append('.' + extension);
797 // else path = path.left(pos) + extension;
798 // m_view.out_file->setUrl(KUrl(path));
800 // m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
802 m_view.out_file->setFilter("*." + extension);
804 if (item->data(EditableRole).toString().isEmpty()) {
805 m_view.buttonDelete->setEnabled(false);
806 m_view.buttonEdit->setEnabled(false);
808 m_view.buttonDelete->setEnabled(true);
809 m_view.buttonEdit->setEnabled(true);
811 m_view.buttonStart->setEnabled(true);
814 void RenderWidget::reloadProfiles() {
818 void RenderWidget::parseProfiles(QString meta, QString group, QString profile) {
819 m_view.size_list->clear();
820 m_view.format_list->clear();
821 m_view.destination_list->clear();
822 m_view.destination_list->addItem(KIcon("video-x-generic"), i18n("File rendering"));
823 m_view.destination_list->addItem(KIcon("media-optical"), i18n("DVD"), "dvd");
824 m_view.destination_list->addItem(KIcon("audio-x-generic"), i18n("Audio only"), "audioonly");
825 m_view.destination_list->addItem(KIcon("applications-internet"), i18n("Web sites"), "websites");
826 m_view.destination_list->addItem(KIcon("applications-multimedia"), i18n("Media players"), "mediaplayers");
827 m_view.destination_list->addItem(KIcon("drive-harddisk"), i18n("Lossless / HQ"), "lossless");
828 m_view.destination_list->addItem(KIcon("pda"), i18n("Mobile devices"), "mobile");
830 QString exportFile = KStandardDirs::locate("appdata", "export/profiles.xml");
831 parseFile(exportFile, false);
834 QString exportFolder = KStandardDirs::locateLocal("appdata", "export/");
835 QDir directory = QDir(exportFolder);
838 const QStringList fileList = directory.entryList(filter, QDir::Files);
839 foreach(const QString filename, fileList)
840 parseFile(exportFolder + '/' + filename, filename == "customprofiles.xml");
842 if (!meta.isEmpty()) {
843 m_view.destination_list->blockSignals(true);
844 m_view.destination_list->setCurrentIndex(m_view.destination_list->findData(meta));
845 m_view.destination_list->blockSignals(false);
848 QList<QListWidgetItem *> child;
849 if (!group.isEmpty()) child = m_view.format_list->findItems(group, Qt::MatchExactly);
850 if (!child.isEmpty()) {
851 for (int i = 0; i < child.count(); i++) {
852 if (child.at(i)->data(MetaGroupRole).toString() == meta) {
853 m_view.format_list->setCurrentItem(child.at(i));
859 if (!profile.isEmpty()) child = m_view.size_list->findItems(profile, Qt::MatchExactly);
860 if (!child.isEmpty()) m_view.size_list->setCurrentItem(child.at(0));
863 void RenderWidget::parseFile(QString exportFile, bool editable) {
864 kDebug() << "// Parsing file: " << exportFile;
865 kDebug() << "------------------------------";
867 QFile file(exportFile);
868 doc.setContent(&file, false);
870 QDomElement documentElement;
871 QDomElement profileElement;
873 QListWidgetItem *item;
874 QDomNodeList groups = doc.elementsByTagName("group");
876 if (editable || groups.count() == 0) {
877 QDomElement profiles = doc.documentElement();
878 if (editable && profiles.attribute("version", 0).toInt() < 1) {
879 kDebug() << "// OLD profile version";
880 // this is an old profile version, update it
882 QDomElement newprofiles = newdoc.createElement("profiles");
883 newprofiles.setAttribute("version", 1);
884 newdoc.appendChild(newprofiles);
885 QDomNodeList profilelist = doc.elementsByTagName("profile");
886 for (int i = 0; i < profilelist.count(); i++) {
887 QString category = i18n("Custom");
889 QDomNode parent = profilelist.at(i).parentNode();
890 if (!parent.isNull()) {
891 QDomElement parentNode = parent.toElement();
892 if (parentNode.hasAttribute("name")) category = parentNode.attribute("name");
893 extension = parentNode.attribute("extension");
895 profilelist.at(i).toElement().setAttribute("category", category);
896 if (!extension.isEmpty()) profilelist.at(i).toElement().setAttribute("extension", extension);
897 QDomNode n = profilelist.at(i).cloneNode();
898 newprofiles.appendChild(newdoc.importNode(n, true));
900 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
901 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
904 QTextStream out(&file);
905 out << newdoc.toString();
907 parseFile(exportFile, editable);
911 QDomNode node = doc.elementsByTagName("profile").at(0);
913 kDebug() << "// Export file: " << exportFile << " IS BROKEN";
917 while (!node.isNull()) {
918 QDomElement profile = node.toElement();
919 QString profileName = profile.attribute("name");
920 QString standard = profile.attribute("standard");
921 QString params = profile.attribute("args");
922 QString category = profile.attribute("category", i18n("Custom"));
923 QString dest = profile.attribute("destinationid");
924 QString prof_extension = profile.attribute("extension");
925 if (!prof_extension.isEmpty()) extension = prof_extension;
927 QList <QListWidgetItem *> list = m_view.format_list->findItems(category, Qt::MatchExactly);
929 for (int j = 0; j < list.count(); j++) {
930 if (list.at(j)->data(MetaGroupRole) == dest) {
936 item = new QListWidgetItem(category, m_view.format_list);
937 item->setData(MetaGroupRole, dest);
940 item = new QListWidgetItem(profileName, m_view.size_list);
941 //kDebug() << "// ADDINg item with name: " << profileName << ", GRP" << category << ", DEST:" << dest ;
942 item->setData(GroupRole, category);
943 item->setData(MetaGroupRole, dest);
944 item->setData(ExtensionRole, extension);
945 item->setData(RenderRole, "avformat");
946 item->setData(StandardRole, standard);
947 item->setData(ParamsRole, params);
948 if (profile.hasAttribute("url")) item->setData(ExtraRole, profile.attribute("url"));
949 if (editable) item->setData(EditableRole, "true");
950 node = doc.elementsByTagName("profile").at(count);
960 QString prof_extension;
966 while (!groups.item(i).isNull()) {
967 documentElement = groups.item(i).toElement();
968 QDomNode gname = documentElement.elementsByTagName("groupname").at(0);
969 QString metagroupName;
971 if (!gname.isNull()) {
972 metagroupName = gname.firstChild().nodeValue();
973 metagroupId = gname.toElement().attribute("id");
974 if (!metagroupName.isEmpty() && !m_view.destination_list->contains(metagroupName)) {
975 if (metagroupId == "dvd") icon = KIcon("media-optical");
976 else if (metagroupId == "audioonly") icon = KIcon("audio-x-generic");
977 else if (metagroupId == "websites") icon = KIcon("applications-internet");
978 else if (metagroupId == "mediaplayers") icon = KIcon("applications-multimedia");
979 else if (metagroupId == "lossless") icon = KIcon("drive-harddisk");
980 else if (metagroupId == "mobile") icon = KIcon("pda");
981 m_view.destination_list->addItem(icon, i18n(metagroupName.toUtf8().data()), metagroupId);
984 groupName = documentElement.attribute("name", i18n("Custom"));
985 extension = documentElement.attribute("extension", QString::null);
986 renderer = documentElement.attribute("renderer", QString::null);
987 QList <QListWidgetItem *> list = m_view.format_list->findItems(groupName, Qt::MatchExactly);
989 for (int j = 0; j < list.count(); j++) {
990 if (list.at(j)->data(MetaGroupRole) == metagroupId) {
996 item = new QListWidgetItem(groupName, m_view.format_list);
997 item->setData(MetaGroupRole, metagroupId);
1000 QDomNode n = groups.item(i).firstChild();
1001 while (!n.isNull()) {
1002 if (n.toElement().tagName() != "profile") {
1003 n = n.nextSibling();
1006 profileElement = n.toElement();
1007 profileName = profileElement.attribute("name");
1008 standard = profileElement.attribute("standard");
1009 params = profileElement.attribute("args");
1010 prof_extension = profileElement.attribute("extension");
1011 if (!prof_extension.isEmpty()) extension = prof_extension;
1012 item = new QListWidgetItem(profileName, m_view.size_list);
1013 item->setData(GroupRole, groupName);
1014 item->setData(MetaGroupRole, metagroupId);
1015 item->setData(ExtensionRole, extension);
1016 item->setData(RenderRole, renderer);
1017 item->setData(StandardRole, standard);
1018 item->setData(ParamsRole, params);
1019 if (profileElement.hasAttribute("url")) item->setData(ExtraRole, profileElement.attribute("url"));
1020 if (editable) item->setData(EditableRole, "true");
1021 n = n.nextSibling();
1028 void RenderWidget::setRenderJob(const QString &dest, int progress) {
1029 QTreeWidgetItem *item;
1030 QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
1031 if (!existing.isEmpty()) item = existing.at(0);
1032 else item = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
1033 item->setData(2, Qt::UserRole, progress);
1034 if (progress == 0) {
1035 item->setIcon(0, KIcon("system-run"));
1036 item->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
1037 item->setData(1, Qt::UserRole + 1, QTime::currentTime());
1040 QTime startTime = item->data(1, Qt::UserRole + 1).toTime();
1041 int seconds = startTime.secsTo(QTime::currentTime());;
1042 const QString t = i18n("Estimated time %1", QTime().addSecs(seconds * (100 - progress) / progress).toString("hh:mm:ss"));
1043 item->setData(1, Qt::UserRole, t);
1047 void RenderWidget::setRenderStatus(const QString &dest, int status, const QString &error) {
1048 QTreeWidgetItem *item;
1049 QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
1050 if (!existing.isEmpty()) item = existing.at(0);
1051 else item = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
1053 // Job finished successfully
1054 item->setIcon(0, KIcon("dialog-ok"));
1055 item->setData(2, Qt::UserRole, 100);
1056 QTime startTime = item->data(1, Qt::UserRole + 1).toTime();
1057 int seconds = startTime.secsTo(QTime::currentTime());
1058 const QTime tm = QTime().addSecs(seconds);
1059 const QString t = i18n("Rendering finished in %1", tm.toString("hh:mm:ss"));
1060 item->setData(1, Qt::UserRole, t);
1061 QString itemGroup = item->data(0, Qt::UserRole).toString();
1062 if (itemGroup == "dvd") {
1063 emit openDvdWizard(item->text(1), item->data(0, Qt::UserRole + 1).toString());
1064 } else if (itemGroup == "websites") {
1065 QString url = item->data(0, Qt::UserRole + 1).toString();
1066 if (!url.isEmpty()) KRun *openBrowser = new KRun(url, this);
1068 } else if (status == -2) {
1069 // Rendering crashed
1070 item->setData(1, Qt::UserRole, i18n("Rendering crashed"));
1071 item->setIcon(0, KIcon("dialog-close"));
1072 item->setData(2, Qt::UserRole, 100);
1073 m_view.error_log->append(i18n("<strong>Rendering of %1 crashed</strong><br />", dest));
1074 m_view.error_log->append(error);
1075 m_view.error_log->append("<hr />");
1076 m_view.error_box->setVisible(true);
1077 } else if (status == -3) {
1079 item->setData(1, Qt::UserRole, i18n("Rendering aborted"));
1080 item->setIcon(0, KIcon("dialog-cancel"));
1081 item->setData(2, Qt::UserRole, 100);
1086 void RenderWidget::slotAbortCurrentJob() {
1087 QTreeWidgetItem *current = m_view.running_jobs->currentItem();
1088 if (current) emit abortProcess(current->text(1));
1091 void RenderWidget::slotCheckJob() {
1092 bool activate = false;
1093 QTreeWidgetItem *current = m_view.running_jobs->currentItem();
1095 int percent = current->data(2, Qt::UserRole).toInt();
1096 if (percent < 100) activate = true;
1098 m_view.abort_job->setEnabled(activate);
1101 void RenderWidget::parseScriptFiles() {
1102 QStringList scriptsFilter;
1103 scriptsFilter << "*.sh";
1104 m_view.scripts_list->clear();
1106 QTreeWidgetItem *item;
1107 // List the project scripts
1108 QStringList scriptFiles = QDir(m_projectFolder + "/scripts").entryList(scriptsFilter, QDir::Files);
1109 for (int i = 0; i < scriptFiles.size(); ++i) {
1110 KUrl scriptpath(m_projectFolder + "/scripts/" + scriptFiles.at(i));
1111 item = new QTreeWidgetItem(m_view.scripts_list, QStringList() << scriptpath.fileName());
1113 QFile file(scriptpath.path());
1114 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
1115 while (!file.atEnd()) {
1116 QByteArray line = file.readLine();
1117 if (line.startsWith("TARGET=")) {
1118 target = QString(line).section("TARGET=", 1);
1119 target.remove(QChar('"'));
1125 item->setSizeHint(0, QSize(m_view.scripts_list->columnWidth(0), fontMetrics().height() * 2));
1126 item->setData(0, Qt::UserRole, target);
1127 item->setData(0, Qt::UserRole + 1, scriptpath.path());
1129 bool activate = false;
1130 QTreeWidgetItemIterator it(m_view.scripts_list);
1132 kDebug() << "// FOUND SCRIPT ITEM:" << (*it)->text(0);
1133 // Selecting item does not work, why ???
1134 m_view.scripts_list->setCurrentItem(*it);
1135 (*it)->setSelected(true);
1138 kDebug() << "SELECTED SCRIPTS: " << m_view.scripts_list->selectedItems().count();
1139 m_view.start_script->setEnabled(activate);
1140 m_view.delete_script->setEnabled(activate);
1143 void RenderWidget::slotCheckScript() {
1144 bool activate = false;
1145 QTreeWidgetItemIterator it(m_view.scripts_list);
1146 if (*it) activate = true;
1147 m_view.start_script->setEnabled(activate);
1148 m_view.delete_script->setEnabled(activate);
1151 void RenderWidget::slotStartScript() {
1152 QTreeWidgetItem *item = m_view.scripts_list->currentItem();
1154 QString path = item->data(0, Qt::UserRole + 1).toString();
1155 QProcess::startDetached(path);
1156 m_view.tabWidget->setCurrentIndex(1);
1160 void RenderWidget::slotDeleteScript() {
1161 QTreeWidgetItem *item = m_view.scripts_list->currentItem();
1163 QString path = item->data(0, Qt::UserRole + 1).toString();
1164 KIO::NetAccess::del(path + ".westley", this);
1165 KIO::NetAccess::del(path, this);
1170 void RenderWidget::slotGenerateScript() {