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 <QHeaderView>
27 #include <QInputDialog>
29 #include <KStandardDirs>
31 #include <KMessageBox>
34 #include <KIO/NetAccess>
36 #include "kdenlivesettings.h"
37 #include "renderwidget.h"
38 #include "ui_saveprofile_ui.h"
40 const int GroupRole = Qt::UserRole;
41 const int ExtensionRole = GroupRole + 1;
42 const int StandardRole = GroupRole + 2;
43 const int RenderRole = GroupRole + 3;
44 const int ParamsRole = GroupRole + 4;
45 const int EditableRole = GroupRole + 5;
46 const int MetaGroupRole = GroupRole + 6;
47 const int ExtraRole = GroupRole + 7;
49 RenderWidget::RenderWidget(const QString &projectfolder, QWidget * parent): QDialog(parent), m_projectFolder(projectfolder) {
51 setWindowTitle(i18n("Rendering"));
52 m_view.buttonDelete->setIcon(KIcon("trash-empty"));
53 m_view.buttonDelete->setToolTip(i18n("Delete profile"));
54 m_view.buttonDelete->setEnabled(false);
56 m_view.buttonEdit->setIcon(KIcon("document-properties"));
57 m_view.buttonEdit->setToolTip(i18n("Edit profile"));
58 m_view.buttonEdit->setEnabled(false);
60 m_view.buttonSave->setIcon(KIcon("document-new"));
61 m_view.buttonSave->setToolTip(i18n("Create new profile"));
63 m_view.buttonInfo->setIcon(KIcon("help-about"));
65 if (KdenliveSettings::showrenderparams()) {
66 m_view.buttonInfo->setDown(true);
67 } else m_view.advanced_params->hide();
69 m_view.rescale_size->setInputMask("0099\\x0099");
70 m_view.rescale_size->setText("320x240");
73 QMenu *renderMenu = new QMenu(i18n("Start Rendering"), this);
74 QAction *renderAction = renderMenu->addAction(KIcon("file-new"), i18n("Render to File"));
75 connect(renderAction, SIGNAL(triggered()), this, SLOT(slotExport()));
76 QAction *scriptAction = renderMenu->addAction(KIcon("file-new"), i18n("Generate Script"));
77 connect(scriptAction, SIGNAL(triggered()), this, SLOT(slotGenerateScript()));
79 m_view.buttonStart->setMenu(renderMenu);
80 m_view.buttonStart->setPopupMode(QToolButton::MenuButtonPopup);
81 m_view.buttonStart->setDefaultAction(renderAction);
82 m_view.buttonStart->setToolButtonStyle(Qt::ToolButtonTextOnly);
83 m_view.abort_job->setEnabled(false);
84 m_view.start_script->setEnabled(false);
85 m_view.delete_script->setEnabled(false);
91 connect(m_view.start_script, SIGNAL(clicked()), this, SLOT(slotStartScript()));
92 connect(m_view.delete_script, SIGNAL(clicked()), this, SLOT(slotDeleteScript()));
93 connect(m_view.scripts_list, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(slotCheckScript()));
94 connect(m_view.running_jobs, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(slotCheckJob()));
96 connect(m_view.buttonInfo, SIGNAL(clicked()), this, SLOT(showInfoPanel()));
98 connect(m_view.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
99 connect(m_view.buttonEdit, SIGNAL(clicked()), this, SLOT(slotEditProfile()));
100 connect(m_view.buttonDelete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
101 connect(m_view.abort_job, SIGNAL(clicked()), this, SLOT(slotAbortCurrentJob()));
102 connect(m_view.buttonClose, SIGNAL(clicked()), this, SLOT(hide()));
103 connect(m_view.buttonClose2, SIGNAL(clicked()), this, SLOT(hide()));
104 connect(m_view.buttonClose3, SIGNAL(clicked()), this, SLOT(hide()));
105 connect(m_view.rescale, SIGNAL(toggled(bool)), m_view.rescale_size, SLOT(setEnabled(bool)));
106 connect(m_view.destination_list, SIGNAL(activated(int)), this, SLOT(refreshView()));
107 connect(m_view.out_file, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButtons()));
108 connect(m_view.out_file, SIGNAL(urlSelected(const KUrl &)), this, SLOT(slotUpdateButtons(const KUrl &)));
109 connect(m_view.format_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshView()));
110 connect(m_view.size_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshParams()));
112 connect(m_view.render_guide, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
113 connect(m_view.render_zone, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
114 connect(m_view.render_full, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
116 connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
117 connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));
119 connect(m_view.format_selection, SIGNAL(activated(int)), this, SLOT(refreshView()));
121 m_view.buttonStart->setEnabled(false);
122 m_view.rescale_size->setEnabled(false);
123 m_view.guides_box->setVisible(false);
124 m_view.open_dvd->setVisible(false);
125 m_view.open_browser->setVisible(false);
126 m_view.error_box->setVisible(false);
128 m_view.splitter->setStretchFactor(1, 5);
129 m_view.splitter->setStretchFactor(0, 2);
131 m_view.out_file->setMode(KFile::File);
133 m_view.running_jobs->setHeaderLabels(QStringList() << QString() << i18n("File") << i18n("Progress"));
134 m_view.running_jobs->setItemDelegate(new RenderViewDelegate(this));
136 QHeaderView *header = m_view.running_jobs->header();
137 QFontMetrics fm = fontMetrics();
138 //header->resizeSection(0, fm.width("typical-name-for-a-torrent.torrent"));
139 header->setResizeMode(0, QHeaderView::Fixed);
140 header->resizeSection(0, 30);
141 header->setResizeMode(1, QHeaderView::Interactive);
142 header->resizeSection(1, fm.width("typical-name-for-a-file.torrent"));
143 header->setResizeMode(2, QHeaderView::Fixed);
144 header->resizeSection(1, width() * 2 / 3);
145 header->setResizeMode(2, QHeaderView::Interactive);
146 //header->setResizeMode(1, QHeaderView::Fixed);
148 m_view.scripts_list->setHeaderLabels(QStringList() << i18n("Script Files"));
149 m_view.scripts_list->setItemDelegate(new RenderScriptDelegate(this));
152 focusFirstVisibleItem();
155 void RenderWidget::showInfoPanel() {
156 if (m_view.advanced_params->isVisible()) {
157 m_view.advanced_params->setVisible(false);
158 m_view.buttonInfo->setDown(false);
159 KdenliveSettings::setShowrenderparams(false);
161 m_view.advanced_params->setVisible(true);
162 m_view.buttonInfo->setDown(true);
163 KdenliveSettings::setShowrenderparams(true);
167 void RenderWidget::setDocumentPath(const QString path) {
168 m_projectFolder = path;
169 const QString fileName = m_view.out_file->url().fileName();
170 m_view.out_file->setUrl(KUrl(m_projectFolder + '/' + fileName));
174 void RenderWidget::slotUpdateGuideBox() {
175 m_view.guides_box->setVisible(m_view.render_guide->isChecked());
178 void RenderWidget::slotCheckStartGuidePosition() {
179 if (m_view.guide_start->currentIndex() > m_view.guide_end->currentIndex())
180 m_view.guide_start->setCurrentIndex(m_view.guide_end->currentIndex());
183 void RenderWidget::slotCheckEndGuidePosition() {
184 if (m_view.guide_end->currentIndex() < m_view.guide_start->currentIndex())
185 m_view.guide_end->setCurrentIndex(m_view.guide_start->currentIndex());
188 void RenderWidget::setGuides(QDomElement guidesxml, double duration) {
189 m_view.guide_start->clear();
190 m_view.guide_end->clear();
191 QDomNodeList nodes = guidesxml.elementsByTagName("guide");
192 if (nodes.count() > 0) {
193 m_view.guide_start->addItem(i18n("Render"), "0");
194 m_view.render_guide->setEnabled(true);
195 } else m_view.render_guide->setEnabled(false);
196 for (int i = 0; i < nodes.count(); i++) {
197 QDomElement e = nodes.item(i).toElement();
199 m_view.guide_start->addItem(e.attribute("comment"), e.attribute("time").toDouble());
200 m_view.guide_end->addItem(e.attribute("comment"), e.attribute("time").toDouble());
203 if (nodes.count() > 0)
204 m_view.guide_end->addItem(i18n("End"), QString::number(duration));
207 // Will be called when the user selects an output file via the file dialog.
208 // File extension will be added automatically.
209 void RenderWidget::slotUpdateButtons(KUrl url) {
210 if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
211 else m_view.buttonStart->setEnabled(true);
213 QListWidgetItem *item = m_view.size_list->currentItem();
214 QString extension = item->data(ExtensionRole).toString();
215 url = filenameWithExtension(url, extension);
216 m_view.out_file->setUrl(url);
220 // Will be called when the user changes the output file path in the text line.
221 // File extension must NOT be added, would make editing impossible!
222 void RenderWidget::slotUpdateButtons() {
223 if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
224 else m_view.buttonStart->setEnabled(true);
227 void RenderWidget::slotSaveProfile() {
228 Ui::SaveProfile_UI ui;
229 QDialog *d = new QDialog(this);
231 QString customGroup = i18n("Custom");
232 QStringList groupNames;
233 for (int i = 0; i < m_view.format_list->count(); i++)
234 groupNames.append(m_view.format_list->item(i)->text());
235 if (!groupNames.contains(customGroup)) groupNames.prepend(customGroup);
236 ui.group_name->addItems(groupNames);
237 int pos = ui.group_name->findText(customGroup);
238 ui.group_name->setCurrentIndex(pos);
240 ui.parameters->setText(m_view.advanced_params->toPlainText());
241 ui.extension->setText(m_view.size_list->currentItem()->data(ExtensionRole).toString());
242 ui.profile_name->setFocus();
243 if (d->exec() == QDialog::Accepted && !ui.profile_name->text().simplified().isEmpty()) {
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().simplified();
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::slotEditProfile() {
298 QListWidgetItem *item = m_view.size_list->currentItem();
300 QString currentGroup = m_view.format_list->currentItem()->text();
302 QString params = item->data(ParamsRole).toString();
303 QString extension = item->data(ExtensionRole).toString();
304 QString currentProfile = item->text();
306 Ui::SaveProfile_UI ui;
307 QDialog *d = new QDialog(this);
309 QStringList groupNames;
310 for (int i = 0; i < m_view.format_list->count(); i++)
311 groupNames.append(m_view.format_list->item(i)->text());
312 if (!groupNames.contains(currentGroup)) groupNames.prepend(currentGroup);
313 ui.group_name->addItems(groupNames);
314 int pos = ui.group_name->findText(currentGroup);
315 ui.group_name->setCurrentIndex(pos);
316 ui.profile_name->setText(currentProfile);
317 ui.extension->setText(extension);
318 ui.parameters->setText(params);
319 ui.profile_name->setFocus();
321 if (d->exec() == QDialog::Accepted) {
323 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
325 QFile file(exportFile);
326 doc.setContent(&file, false);
329 QDomElement documentElement;
330 bool groupExists = false;
332 QString newProfileName = ui.profile_name->text();
333 QString newGroupName = ui.group_name->currentText();
334 QDomNodeList groups = doc.elementsByTagName("group");
336 if (groups.count() == 0) {
337 QDomElement profiles = doc.createElement("profiles");
338 doc.appendChild(profiles);
339 } else while (!groups.item(i).isNull()) {
340 documentElement = groups.item(i).toElement();
341 groupName = documentElement.attribute("name");
342 kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
343 if (groupName == newGroupName) {
350 documentElement = doc.createElement("group");
351 documentElement.setAttribute("name", ui.group_name->currentText());
352 documentElement.setAttribute("renderer", "avformat");
353 doc.documentElement().appendChild(documentElement);
355 QDomElement profileElement = doc.createElement("profile");
356 profileElement.setAttribute("name", newProfileName);
357 profileElement.setAttribute("extension", ui.extension->text().simplified());
358 profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
359 documentElement.appendChild(profileElement);
361 //QCString save = doc.toString().utf8();
363 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
364 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
368 QTextStream out(&file);
369 out << doc.toString();
371 parseProfiles(newGroupName, newProfileName);
376 void RenderWidget::slotDeleteProfile() {
377 QString currentGroup = m_view.format_list->currentItem()->text();
378 QString currentProfile = m_view.size_list->currentItem()->text();
380 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
382 QFile file(exportFile);
383 doc.setContent(&file, false);
386 QDomElement documentElement;
387 bool groupExists = false;
389 QDomNodeList groups = doc.elementsByTagName("group");
392 while (!groups.item(i).isNull()) {
393 documentElement = groups.item(i).toElement();
394 groupName = documentElement.attribute("name");
395 if (groupName == currentGroup) {
396 QDomNodeList children = documentElement.childNodes();
397 for (int j = 0; j < children.count(); j++) {
398 QDomElement pro = children.at(j).toElement();
399 if (pro.attribute("name") == currentProfile) {
400 groups.item(i).removeChild(children.at(j));
401 if (groups.item(i).childNodes().isEmpty())
402 doc.documentElement().removeChild(groups.item(i));
411 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
412 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
415 QTextStream out(&file);
416 out << doc.toString();
418 parseProfiles(currentGroup);
419 focusFirstVisibleItem();
422 void RenderWidget::updateButtons() {
423 if (!m_view.size_list->currentItem() || m_view.size_list->currentItem()->isHidden()) {
424 m_view.buttonSave->setEnabled(false);
425 m_view.buttonDelete->setEnabled(false);
426 m_view.buttonEdit->setEnabled(false);
428 m_view.buttonSave->setEnabled(true);
429 if (m_view.size_list->currentItem()->data(EditableRole).toString().isEmpty()) {
430 m_view.buttonDelete->setEnabled(false);
431 m_view.buttonEdit->setEnabled(false);
433 m_view.buttonDelete->setEnabled(true);
434 m_view.buttonEdit->setEnabled(true);
440 void RenderWidget::focusFirstVisibleItem() {
441 if (m_view.size_list->currentItem() && !m_view.size_list->currentItem()->isHidden()) {
445 for (uint ix = 0; ix < m_view.size_list->count(); ix++) {
446 QListWidgetItem *item = m_view.size_list->item(ix);
447 if (item && !item->isHidden()) {
448 m_view.size_list->setCurrentRow(ix);
452 if (!m_view.size_list->currentItem()) m_view.size_list->setCurrentRow(0);
456 void RenderWidget::slotExport(bool scriptExport) {
457 QListWidgetItem *item = m_view.size_list->currentItem();
460 const QString dest = m_view.out_file->url().path();
461 if (dest.isEmpty()) return;
464 if (KMessageBox::warningYesNo(this, i18n("Output file already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
472 QString scriptsFolder = m_projectFolder + "/scripts/";
473 KStandardDirs::makeDir(scriptsFolder);
474 QString path = scriptsFolder + i18n("script") + QString::number(ix).rightJustified(3, '0', false) + ".sh";
475 while (QFile::exists(path)) {
477 path = scriptsFolder + i18n("script") + QString::number(ix).rightJustified(3, '0', false) + ".sh";
479 scriptName = QInputDialog::getText(this, i18n("Create Render Script"), i18n("Script name (will be saved in: %1)", scriptsFolder), QLineEdit::Normal, KUrl(path).fileName(), &ok);
480 if (!ok || scriptName.isEmpty()) return;
481 scriptName.prepend(scriptsFolder);
484 if (KMessageBox::warningYesNo(this, i18n("Script file already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
489 QStringList overlayargs;
490 if (m_view.tc_overlay->isChecked()) {
491 QString filterFile = KStandardDirs::locate("appdata", "metadata.properties");
492 overlayargs << "meta.attr.timecode=1" << "meta.attr.timecode.markup=#timecode";
493 overlayargs << "-attach" << "data_feed:attr_check" << "-attach";
494 overlayargs << "data_show:" + filterFile << "_fezzik=1" << "dynamic=1";
496 double startPos = -1;
498 if (m_view.render_guide->isChecked()) {
499 startPos = m_view.guide_start->itemData(m_view.guide_start->currentIndex()).toDouble();
500 endPos = m_view.guide_end->itemData(m_view.guide_end->currentIndex()).toDouble();
502 QString renderArgs = m_view.advanced_params->toPlainText();
504 // Adjust frame scale
507 if (m_view.rescale->isChecked() && m_view.rescale->isEnabled()) {
508 width = m_view.rescale_size->text().section('x', 0, 0).toInt();
509 height = m_view.rescale_size->text().section('x', 1, 1).toInt();
511 width = m_profile.width;
512 height = m_profile.height;
514 renderArgs.replace("%dar", "@" + QString::number(m_profile.display_aspect_num) + "/" + QString::number(m_profile.display_aspect_den));
517 if (m_view.scanning_list->currentIndex() == 1) renderArgs.append(" progressive=1");
518 else if (m_view.scanning_list->currentIndex() == 2) renderArgs.append(" progressive=0");
520 // disable audio if requested
521 if (!m_view.export_audio->isChecked())
522 renderArgs.append(" an=1 ");
524 // Check if the rendering profile is different from project profile,
525 // in which case we need to use the producer_comsumer from MLT
526 bool resizeProfile = false;
528 QString std = renderArgs;
529 QString destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
530 if (std.contains(" s=")) {
531 QString subsize = std.section(" s=", 1, 1);
532 subsize = subsize.section(' ', 0, 0).toLower();
533 const QString currentSize = QString::number(width) + 'x' + QString::number(height);
534 if (subsize != currentSize) resizeProfile = true;
535 } else if (destination != "audioonly") {
536 // Add current size parametrer
537 renderArgs.append(QString(" s=%1x%2").arg(width).arg(height));
540 // insert item in running jobs list
541 QTreeWidgetItem *renderItem;
542 QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
543 if (!existing.isEmpty()) renderItem = existing.at(0);
544 else renderItem = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
545 renderItem->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
546 renderItem->setData(1, Qt::UserRole + 1, QTime::currentTime());
548 // Set rendering type
549 QString group = m_view.size_list->currentItem()->data(MetaGroupRole).toString();
550 if (group == "dvd" && m_view.open_dvd->isChecked()) {
551 renderItem->setData(0, Qt::UserRole, group);
552 if (renderArgs.contains("profile=")) {
553 // rendering profile contains an MLT profile, so pass it to the running jog item, usefull for dvd
554 QString prof = renderArgs.section("profile=", 1, 1);
555 prof = prof.section(' ', 0, 0);
556 kDebug() << "// render profile: " << prof;
557 renderItem->setData(0, Qt::UserRole + 1, prof);
559 } else if (group == "websites" && m_view.open_browser->isChecked()) {
560 renderItem->setData(0, Qt::UserRole, group);
562 QString url = m_view.size_list->currentItem()->data(ExtraRole).toString();
563 renderItem->setData(0, Qt::UserRole + 1, url);
566 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);
567 if (scriptName.isEmpty()) m_view.tabWidget->setCurrentIndex(1);
569 QTimer::singleShot(400, this, SLOT(parseScriptFiles()));
570 m_view.tabWidget->setCurrentIndex(2);
574 void RenderWidget::setProfile(MltVideoProfile profile) {
576 //WARNING: this way to tell the video standard is a bit hackish...
577 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);
578 else m_view.format_selection->setCurrentIndex(1);
579 m_view.scanning_list->setCurrentIndex(0);
583 void RenderWidget::refreshView() {
584 m_view.size_list->blockSignals(true);
585 QListWidgetItem *sizeItem;
588 if (m_view.destination_list->currentIndex() > 0)
589 destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
591 if (destination == "dvd") m_view.open_dvd->setVisible(true);
592 else m_view.open_dvd->setVisible(false);
593 if (destination == "websites") m_view.open_browser->setVisible(true);
594 else m_view.open_browser->setVisible(false);
595 if (!destination.isEmpty() && QString("dvd websites audioonly").contains(destination))
596 m_view.rescale->setEnabled(false);
597 else m_view.rescale->setEnabled(true);
598 // hide groups that are not in the correct destination
599 for (int i = 0; i < m_view.format_list->count(); i++) {
600 sizeItem = m_view.format_list->item(i);
601 if (sizeItem->data(MetaGroupRole).toString() == destination)
602 sizeItem->setHidden(false);
603 else sizeItem->setHidden(true);
606 // activate first visible item
607 QListWidgetItem * item = m_view.format_list->currentItem();
608 if (!item || item->isHidden()) {
609 for (int i = 0; i < m_view.format_list->count(); i++) {
610 if (!m_view.format_list->item(i)->isHidden()) {
611 m_view.format_list->setCurrentRow(i);
615 item = m_view.format_list->currentItem();
619 for (int i = 0; i < m_view.format_list->count() && count < 2; i++) {
620 if (!m_view.format_list->isRowHidden(i)) count++;
622 if (count > 1) m_view.format_list->setVisible(true);
623 else m_view.format_list->setVisible(false);
625 QString group = item->text();
626 bool firstSelected = false;
627 const QStringList formatsList = KdenliveSettings::supportedformats();
628 const QStringList vcodecsList = KdenliveSettings::videocodecs();
629 const QStringList acodecsList = KdenliveSettings::audiocodecs();
631 for (int i = 0; i < m_view.size_list->count(); i++) {
632 sizeItem = m_view.size_list->item(i);
633 if (sizeItem->data(GroupRole) == group) {
634 std = sizeItem->data(StandardRole).toString();
635 if (!std.isEmpty()) {
636 if (std.contains("PAL", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 0);
637 else if (std.contains("NTSC", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 1);
639 sizeItem->setHidden(false);
640 if (!firstSelected) m_view.size_list->setCurrentItem(sizeItem);
641 firstSelected = true;
644 if (!sizeItem->isHidden()) {
645 // Make sure the selected profile uses an installed avformat codec / format
646 std = sizeItem->data(ParamsRole).toString();
648 if (!formatsList.isEmpty()) {
650 if (std.startsWith("f=")) format = std.section("f=", 1, 1);
651 else if (std.contains(" f=")) format = std.section(" f=", 1, 1);
652 if (!format.isEmpty()) {
653 format = format.section(' ', 0, 0).toLower();
654 if (!formatsList.contains(format)) {
655 kDebug() << "***** UNSUPPORTED F: " << format;
656 sizeItem->setHidden(true);
660 if (!acodecsList.isEmpty() && !sizeItem->isHidden()) {
662 if (std.startsWith("acodec=")) format = std.section("acodec=", 1, 1);
663 else if (std.contains(" acodec=")) format = std.section(" acodec=", 1, 1);
664 if (!format.isEmpty()) {
665 format = format.section(' ', 0, 0).toLower();
666 if (!acodecsList.contains(format)) {
667 kDebug() << "***** UNSUPPORTED ACODEC: " << format;
668 sizeItem->setHidden(true);
672 if (!vcodecsList.isEmpty() && !sizeItem->isHidden()) {
674 if (std.startsWith("vcodec=")) format = std.section("vcodec=", 1, 1);
675 else if (std.contains(" vcodec=")) format = std.section(" vcodec=", 1, 1);
676 if (!format.isEmpty()) {
677 format = format.section(' ', 0, 0).toLower();
678 if (!vcodecsList.contains(format)) {
679 kDebug() << "***** UNSUPPORTED VCODEC: " << format;
680 sizeItem->setHidden(true);
685 } else sizeItem->setHidden(true);
687 focusFirstVisibleItem();
688 m_view.size_list->blockSignals(false);
692 KUrl RenderWidget::filenameWithExtension(KUrl url, QString extension) {
694 if (!url.isEmpty()) {
696 int pos = path.lastIndexOf('.') + 1;
697 if (pos == 0) path.append('.' + extension);
698 else path = path.left(pos) + extension;
701 path = m_projectFolder + "/untitled." + extension;
707 void RenderWidget::refreshParams() {
708 QListWidgetItem *item = m_view.size_list->currentItem();
709 if (!item || item->isHidden()) {
710 m_view.advanced_params->clear();
711 m_view.buttonStart->setEnabled(false);
714 QString params = item->data(ParamsRole).toString();
715 QString extension = item->data(ExtensionRole).toString();
716 m_view.advanced_params->setPlainText(params);
717 QString destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
718 if (params.contains(" s=") || destination == "audioonly") {
719 // profile has a fixed size, do not allow resize
720 m_view.rescale->setEnabled(false);
721 m_view.rescale_size->setEnabled(false);
723 m_view.rescale->setEnabled(true);
724 m_view.rescale_size->setEnabled(true);
726 KUrl url = filenameWithExtension(m_view.out_file->url(), extension);
727 m_view.out_file->setUrl(url);
728 // if (!url.isEmpty()) {
729 // QString path = url.path();
730 // int pos = path.lastIndexOf('.') + 1;
731 // if (pos == 0) path.append('.' + extension);
732 // else path = path.left(pos) + extension;
733 // m_view.out_file->setUrl(KUrl(path));
735 // m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
737 m_view.out_file->setFilter("*." + extension);
739 if (item->data(EditableRole).toString().isEmpty()) {
740 m_view.buttonDelete->setEnabled(false);
741 m_view.buttonEdit->setEnabled(false);
743 m_view.buttonDelete->setEnabled(true);
744 m_view.buttonEdit->setEnabled(true);
746 m_view.buttonStart->setEnabled(true);
749 void RenderWidget::parseProfiles(QString group, QString profile) {
750 m_view.size_list->clear();
751 m_view.format_list->clear();
752 m_view.destination_list->clear();
753 m_view.destination_list->addItem(KIcon("video-x-generic"), i18n("File rendering"));
754 QString exportFile = KStandardDirs::locate("appdata", "export/profiles.xml");
755 parseFile(exportFile, false);
756 exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
757 if (QFile::exists(exportFile)) parseFile(exportFile, true);
759 QList<QListWidgetItem *> child;
760 child = m_view.format_list->findItems(group, Qt::MatchExactly);
761 if (!child.isEmpty()) m_view.format_list->setCurrentItem(child.at(0));
762 child = m_view.size_list->findItems(profile, Qt::MatchExactly);
763 if (!child.isEmpty()) m_view.size_list->setCurrentItem(child.at(0));
766 void RenderWidget::parseFile(QString exportFile, bool editable) {
768 QFile file(exportFile);
769 doc.setContent(&file, false);
771 QDomElement documentElement;
772 QDomElement profileElement;
773 QDomNodeList groups = doc.elementsByTagName("group");
775 if (groups.count() == 0) {
776 kDebug() << "// Export file: " << exportFile << " IS BROKEN";
784 QString prof_extension;
789 QListWidgetItem *item;
790 while (!groups.item(i).isNull()) {
791 documentElement = groups.item(i).toElement();
792 QDomNode gname = documentElement.elementsByTagName("groupname").at(0);
793 QString metagroupName;
795 if (!gname.isNull()) {
796 metagroupName = gname.firstChild().nodeValue();
797 metagroupId = gname.toElement().attribute("id");
798 if (!metagroupName.isEmpty() && !m_view.destination_list->contains(metagroupName)) {
799 if (metagroupId == "dvd") icon = KIcon("media-optical");
800 else if (metagroupId == "audioonly") icon = KIcon("audio-x-generic");
801 else if (metagroupId == "websites") icon = KIcon("applications-internet");
802 else if (metagroupId == "mediaplayers") icon = KIcon("applications-multimedia");
803 else if (metagroupId == "lossless") icon = KIcon("drive-harddisk");
804 m_view.destination_list->addItem(icon, i18n(metagroupName.toUtf8().data()), metagroupId);
807 groupName = documentElement.attribute("name", QString::null);
808 extension = documentElement.attribute("extension", QString::null);
809 renderer = documentElement.attribute("renderer", QString::null);
810 if (m_view.format_list->findItems(groupName, Qt::MatchExactly).isEmpty()) {
811 item = new QListWidgetItem(groupName, m_view.format_list);
812 item->setData(MetaGroupRole, metagroupId);
815 QDomNode n = groups.item(i).firstChild();
816 while (!n.isNull()) {
817 if (n.toElement().tagName() != "profile") {
821 profileElement = n.toElement();
822 profileName = profileElement.attribute("name");
823 standard = profileElement.attribute("standard");
824 params = profileElement.attribute("args");
825 prof_extension = profileElement.attribute("extension");
826 if (!prof_extension.isEmpty()) extension = prof_extension;
827 item = new QListWidgetItem(profileName, m_view.size_list);
828 item->setData(GroupRole, groupName);
829 item->setData(MetaGroupRole, metagroupId);
830 item->setData(ExtensionRole, extension);
831 item->setData(RenderRole, renderer);
832 item->setData(StandardRole, standard);
833 item->setData(ParamsRole, params);
834 if (profileElement.hasAttribute("url")) item->setData(ExtraRole, profileElement.attribute("url"));
835 if (editable) item->setData(EditableRole, "true");
843 void RenderWidget::setRenderJob(const QString &dest, int progress) {
844 QTreeWidgetItem *item;
845 QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
846 if (!existing.isEmpty()) item = existing.at(0);
847 else item = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
848 item->setData(2, Qt::UserRole, progress);
850 item->setIcon(0, KIcon("system-run"));
851 item->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
852 item->setData(1, Qt::UserRole + 1, QTime::currentTime());
855 QTime startTime = item->data(1, Qt::UserRole + 1).toTime();
856 int seconds = startTime.secsTo(QTime::currentTime());;
857 const QString t = i18n("Estimated time %1", QTime().addSecs(seconds * (100 - progress) / progress).toString("hh:mm:ss"));
858 item->setData(1, Qt::UserRole, t);
862 void RenderWidget::setRenderStatus(const QString &dest, int status, const QString &error) {
863 QTreeWidgetItem *item;
864 QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
865 if (!existing.isEmpty()) item = existing.at(0);
866 else item = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
868 // Job finished successfully
869 item->setIcon(0, KIcon("dialog-ok"));
870 item->setData(2, Qt::UserRole, 100);
871 QTime startTime = item->data(1, Qt::UserRole + 1).toTime();
872 int seconds = startTime.secsTo(QTime::currentTime());
873 const QTime tm = QTime().addSecs(seconds);
874 const QString t = i18n("Rendering finished in %1", tm.toString("hh:mm:ss"));
875 item->setData(1, Qt::UserRole, t);
876 QString itemGroup = item->data(0, Qt::UserRole).toString();
877 if (itemGroup == "dvd") {
878 emit openDvdWizard(item->text(1), item->data(0, Qt::UserRole + 1).toString());
879 } else if (itemGroup == "websites") {
880 QString url = item->data(0, Qt::UserRole + 1).toString();
881 if (!url.isEmpty()) KRun *openBrowser = new KRun(url, this);
883 } else if (status == -2) {
885 item->setData(1, Qt::UserRole, i18n("Rendering crashed"));
886 item->setIcon(0, KIcon("dialog-close"));
887 item->setData(2, Qt::UserRole, 100);
888 m_view.error_log->append(i18n("<strong>Rendering of %1 crashed</strong><br />", dest));
889 m_view.error_log->append(error);
890 m_view.error_log->append("<hr />");
891 m_view.error_box->setVisible(true);
892 } else if (status == -3) {
894 item->setData(1, Qt::UserRole, i18n("Rendering aborted"));
895 item->setIcon(0, KIcon("dialog-cancel"));
896 item->setData(2, Qt::UserRole, 100);
901 void RenderWidget::slotAbortCurrentJob() {
902 QTreeWidgetItem *current = m_view.running_jobs->currentItem();
903 if (current) emit abortProcess(current->text(1));
906 void RenderWidget::slotCheckJob() {
907 bool activate = false;
908 QTreeWidgetItem *current = m_view.running_jobs->currentItem();
910 int percent = current->data(2, Qt::UserRole).toInt();
911 if (percent < 100) activate = true;
913 m_view.abort_job->setEnabled(activate);
916 void RenderWidget::parseScriptFiles() {
917 QStringList scriptsFilter;
918 scriptsFilter << "*.sh";
919 m_view.scripts_list->clear();
921 QTreeWidgetItem *item;
922 // List the project scripts
923 QStringList scriptFiles = QDir(m_projectFolder + "/scripts").entryList(scriptsFilter, QDir::Files);
924 for (int i = 0; i < scriptFiles.size(); ++i) {
925 KUrl scriptpath(m_projectFolder + "/scripts/" + scriptFiles.at(i));
926 item = new QTreeWidgetItem(m_view.scripts_list, QStringList() << scriptpath.fileName());
928 QFile file(scriptpath.path());
929 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
930 while (!file.atEnd()) {
931 QByteArray line = file.readLine();
932 if (line.startsWith("TARGET=")) {
933 target = QString(line).section("TARGET=", 1);
934 target.remove(QChar('"'));
940 item->setSizeHint(0, QSize(m_view.scripts_list->columnWidth(0), fontMetrics().height() * 2));
941 item->setData(0, Qt::UserRole, target);
942 item->setData(0, Qt::UserRole + 1, scriptpath.path());
944 bool activate = false;
945 QTreeWidgetItemIterator it(m_view.scripts_list);
947 kDebug() << "// FOUND SCRIPT ITEM:" << (*it)->text(0);
948 // Selecting item does not work, why ???
949 m_view.scripts_list->setCurrentItem(*it);
950 (*it)->setSelected(true);
953 kDebug() << "SELECTED SCRIPTS: " << m_view.scripts_list->selectedItems().count();
954 m_view.start_script->setEnabled(activate);
955 m_view.delete_script->setEnabled(activate);
958 void RenderWidget::slotCheckScript() {
959 bool activate = false;
960 QTreeWidgetItemIterator it(m_view.scripts_list);
961 if (*it) activate = true;
962 m_view.start_script->setEnabled(activate);
963 m_view.delete_script->setEnabled(activate);
966 void RenderWidget::slotStartScript() {
967 QTreeWidgetItem *item = m_view.scripts_list->currentItem();
969 QString path = item->data(0, Qt::UserRole + 1).toString();
970 QProcess::startDetached(path);
971 m_view.tabWidget->setCurrentIndex(1);
975 void RenderWidget::slotDeleteScript() {
976 QTreeWidgetItem *item = m_view.scripts_list->currentItem();
978 QString path = item->data(0, Qt::UserRole + 1).toString();
979 KIO::NetAccess::del(path + ".westley", this);
980 KIO::NetAccess::del(path, this);
985 void RenderWidget::slotGenerateScript() {