]> git.sesse.net Git - kdenlive/blob - src/renderwidget.cpp
Fix button icon
[kdenlive] / src / renderwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include <QDomDocument>
22 #include <QItemDelegate>
23 #include <QTreeWidgetItem>
24 #include <QListWidgetItem>
25 #include <QHeaderView>
26 #include <QMenu>
27 #include <QProcess>
28 #include <QInputDialog>
29
30 #include <KStandardDirs>
31 #include <KDebug>
32 #include <KMessageBox>
33 #include <KComboBox>
34 #include <KRun>
35 #include <KIO/NetAccess>
36
37 #include "kdenlivesettings.h"
38 #include "renderwidget.h"
39 #include "ui_saveprofile_ui.h"
40
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;
49
50 RenderWidget::RenderWidget(const QString &projectfolder, QWidget * parent): QDialog(parent), m_projectFolder(projectfolder) {
51     m_view.setupUi(this);
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);
56
57     m_view.buttonEdit->setIcon(KIcon("document-properties"));
58     m_view.buttonEdit->setToolTip(i18n("Edit profile"));
59     m_view.buttonEdit->setEnabled(false);
60
61     m_view.buttonSave->setIcon(KIcon("document-new"));
62     m_view.buttonSave->setToolTip(i18n("Create new profile"));
63
64     m_view.buttonInfo->setIcon(KIcon("help-about"));
65
66     if (KdenliveSettings::showrenderparams()) {
67         m_view.buttonInfo->setDown(true);
68     } else m_view.advanced_params->hide();
69
70     m_view.rescale_size->setInputMask("0099\\x0099");
71     m_view.rescale_size->setText("320x240");
72
73
74     QMenu *renderMenu = new QMenu(i18n("Start Rendering"), this);
75     QAction *renderAction = renderMenu->addAction(KIcon("video-x-generic"), i18n("Render to File"));
76     connect(renderAction, SIGNAL(triggered()), this, SLOT(slotExport()));
77     QAction *scriptAction = renderMenu->addAction(KIcon("application-x-shellscript"), i18n("Generate Script"));
78     connect(scriptAction, SIGNAL(triggered()), this, SLOT(slotGenerateScript()));
79
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);
87
88
89     parseProfiles();
90     parseScriptFiles();
91
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()));
96
97     connect(m_view.buttonInfo, SIGNAL(clicked()), this, SLOT(showInfoPanel()));
98
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()));
112
113     connect(m_view.size_list, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(slotEditItem(QListWidgetItem *)));
114
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()));
118
119     connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
120     connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));
121
122     connect(m_view.format_selection, SIGNAL(activated(int)), this, SLOT(refreshView()));
123
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);
130
131     m_view.splitter->setStretchFactor(1, 5);
132     m_view.splitter->setStretchFactor(0, 2);
133
134     m_view.out_file->setMode(KFile::File);
135
136     m_view.running_jobs->setHeaderLabels(QStringList() << QString() << i18n("File") << i18n("Progress"));
137     m_view.running_jobs->setItemDelegate(new RenderViewDelegate(this));
138
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);
150
151     m_view.scripts_list->setHeaderLabels(QStringList() << i18n("Script Files"));
152     m_view.scripts_list->setItemDelegate(new RenderScriptDelegate(this));
153
154
155     focusFirstVisibleItem();
156 }
157
158 void RenderWidget::slotEditItem(QListWidgetItem *item) {
159     if (item->data(EditableRole).toString().isEmpty()) slotSaveProfile();
160     else slotEditProfile();
161 }
162
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);
168     } else {
169         m_view.advanced_params->setVisible(true);
170         m_view.buttonInfo->setDown(true);
171         KdenliveSettings::setShowrenderparams(true);
172     }
173 }
174
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));
179     parseScriptFiles();
180 }
181
182 void RenderWidget::slotUpdateGuideBox() {
183     m_view.guides_box->setVisible(m_view.render_guide->isChecked());
184 }
185
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());
189 }
190
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());
194 }
195
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();
206         if (!e.isNull()) {
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());
209         }
210     }
211     if (nodes.count() > 0)
212         m_view.guide_end->addItem(i18n("End"), QString::number(duration));
213 }
214
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);
220     if (url != 0) {
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);
225     }
226 }
227
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);
233 }
234
235 void RenderWidget::slotSaveProfile() {
236     //TODO: update to correctly use metagroups
237     Ui::SaveProfile_UI ui;
238     QDialog *d = new QDialog(this);
239     ui.setupUi(d);
240
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));
243
244     ui.destination_list->setCurrentIndex(m_view.destination_list->currentIndex());
245     QString dest = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
246
247     QString customGroup = m_view.format_list->currentItem()->text();
248     if (customGroup.isEmpty()) customGroup = i18n("Custom");
249     ui.group_name->setText(customGroup);
250
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");
256         QDomDocument doc;
257         QFile file(exportFile);
258         doc.setContent(&file, false);
259         file.close();
260         QDomElement documentElement;
261         QDomElement profiles = doc.documentElement();
262         if (profiles.isNull() || profiles.tagName() != "profiles") {
263             doc.clear();
264             profiles = doc.createElement("profiles");
265             profiles.setAttribute("version", 1);
266             doc.appendChild(profiles);
267         }
268         int version = profiles.attribute("version", 0).toInt();
269         if (version < 1) {
270             kDebug() << "// OLD profile version";
271             doc.clear();
272             profiles = doc.createElement("profiles");
273             profiles.setAttribute("version", 1);
274             doc.appendChild(profiles);
275         }
276
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");
282         int i = 0;
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
289                 bool ok;
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);
291                 if (!ok) return;
292                 if (profileName == newProfileName) {
293                     profiles.removeChild(profilelist.item(i));
294                     break;
295                 }
296             }
297             i++;
298         }
299
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);
307
308         //QCString save = doc.toString().utf8();
309
310         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
311             KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
312             delete d;
313             return;
314         }
315         QTextStream out(&file);
316         out << doc.toString();
317         file.close();
318         parseProfiles(newMetaGroupId, newGroupName, newProfileName);
319     }
320     delete d;
321 }
322
323 void RenderWidget::slotEditProfile() {
324     QListWidgetItem *item = m_view.size_list->currentItem();
325     if (!item) return;
326     QString currentGroup = m_view.format_list->currentItem()->text();
327
328     QString params = item->data(ParamsRole).toString();
329     QString extension = item->data(ExtensionRole).toString();
330     QString currentProfile = item->text();
331
332     Ui::SaveProfile_UI ui;
333     QDialog *d = new QDialog(this);
334     ui.setupUi(d);
335
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));
338
339     ui.destination_list->setCurrentIndex(m_view.destination_list->currentIndex());
340     QString dest = ui.destination_list->itemData(ui.destination_list->currentIndex(), Qt::UserRole).toString();
341
342     QString customGroup = m_view.format_list->currentItem()->text();
343     if (customGroup.isEmpty()) customGroup = i18n("Custom");
344     ui.group_name->setText(customGroup);
345
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");
354         QDomDocument doc;
355         QFile file(exportFile);
356         doc.setContent(&file, false);
357         file.close();
358         QDomElement documentElement;
359         QDomElement profiles = doc.documentElement();
360
361         if (profiles.isNull() || profiles.tagName() != "profiles") {
362             doc.clear();
363             profiles = doc.createElement("profiles");
364             profiles.setAttribute("version", 1);
365             doc.appendChild(profiles);
366         }
367
368         int version = profiles.attribute("version", 0).toInt();
369         if (version < 1) {
370             kDebug() << "// OLD profile version";
371             doc.clear();
372             profiles = doc.createElement("profiles");
373             profiles.setAttribute("version", 1);
374             doc.appendChild(profiles);
375         }
376
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");
382         int i = 0;
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
389                 bool ok;
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);
391                 if (!ok) return;
392                 if (profileName == newProfileName) {
393                     profiles.removeChild(profilelist.item(i));
394                     break;
395                 }
396             }
397             i++;
398         }
399
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);
407
408         //QCString save = doc.toString().utf8();
409
410         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
411             KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
412             delete d;
413             return;
414         }
415         QTextStream out(&file);
416         out << doc.toString();
417         file.close();
418         parseProfiles(newMetaGroupId, newGroupName, newProfileName);
419     }
420     delete d;
421 }
422
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();
427
428     QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
429     QDomDocument doc;
430     QFile file(exportFile);
431     doc.setContent(&file, false);
432     file.close();
433
434     QDomElement documentElement;
435     QDomNodeList profiles = doc.elementsByTagName("profile");
436     int i = 0;
437     QString groupName;
438     QString profileName;
439     QString destination;
440
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");
446
447         if (profileName == currentProfile && groupName == currentGroup && destination == metaGroupId) {
448             kDebug() << "// GOT it: " << profileName;
449             doc.documentElement().removeChild(profiles.item(i));
450             break;
451         }
452         i++;
453     }
454
455     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
456         KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
457         return;
458     }
459     QTextStream out(&file);
460     out << doc.toString();
461     file.close();
462     if (refresh) {
463         parseProfiles(metaGroupId, currentGroup);
464         focusFirstVisibleItem();
465     }
466 }
467
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);
473         m_view.buttonStart->setEnabled(false);
474     } else {
475         m_view.buttonSave->setEnabled(true);
476         kDebug() << "BUTT: " << m_view.size_list->currentItem()->flags();
477         m_view.buttonStart->setEnabled(m_view.size_list->currentItem()->flags() & Qt::ItemIsEnabled);
478         if (m_view.size_list->currentItem()->data(EditableRole).toString().isEmpty()) {
479             m_view.buttonDelete->setEnabled(false);
480             m_view.buttonEdit->setEnabled(false);
481         } else {
482             m_view.buttonDelete->setEnabled(true);
483             m_view.buttonEdit->setEnabled(true);
484         }
485     }
486 }
487
488
489 void RenderWidget::focusFirstVisibleItem() {
490     if (m_view.size_list->currentItem() && !m_view.size_list->currentItem()->isHidden()) {
491         updateButtons();
492         return;
493     }
494     for (uint ix = 0; ix < m_view.size_list->count(); ix++) {
495         QListWidgetItem *item = m_view.size_list->item(ix);
496         if (item && !item->isHidden()) {
497             m_view.size_list->setCurrentRow(ix);
498             break;
499         }
500     }
501     if (!m_view.size_list->currentItem()) m_view.size_list->setCurrentRow(0);
502     updateButtons();
503 }
504
505 void RenderWidget::slotExport(bool scriptExport) {
506     QListWidgetItem *item = m_view.size_list->currentItem();
507     if (!item) return;
508
509     const QString dest = m_view.out_file->url().path();
510     if (dest.isEmpty()) return;
511     QFile f(dest);
512     if (f.exists()) {
513         if (KMessageBox::warningYesNo(this, i18n("Output file already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
514             return;
515     }
516
517     QString scriptName;
518     if (scriptExport) {
519         bool ok;
520         int ix = 0;
521         QString scriptsFolder = m_projectFolder + "/scripts/";
522         KStandardDirs::makeDir(scriptsFolder);
523         QString path = scriptsFolder + i18n("script") + QString::number(ix).rightJustified(3, '0', false) + ".sh";
524         while (QFile::exists(path)) {
525             ix++;
526             path = scriptsFolder + i18n("script") + QString::number(ix).rightJustified(3, '0', false) + ".sh";
527         }
528         scriptName = QInputDialog::getText(this, i18n("Create Render Script"), i18n("Script name (will be saved in: %1)", scriptsFolder), QLineEdit::Normal, KUrl(path).fileName(), &ok);
529         if (!ok || scriptName.isEmpty()) return;
530         scriptName.prepend(scriptsFolder);
531         QFile f(scriptName);
532         if (f.exists()) {
533             if (KMessageBox::warningYesNo(this, i18n("Script file already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
534                 return;
535         }
536     }
537
538     QStringList overlayargs;
539     if (m_view.tc_overlay->isChecked()) {
540         QString filterFile = KStandardDirs::locate("appdata", "metadata.properties");
541         overlayargs << "meta.attr.timecode=1" << "meta.attr.timecode.markup=#timecode";
542         overlayargs << "-attach" << "data_feed:attr_check" << "-attach";
543         overlayargs << "data_show:" + filterFile << "_fezzik=1" << "dynamic=1";
544     }
545     double startPos = -1;
546     double endPos = -1;
547     if (m_view.render_guide->isChecked()) {
548         startPos = m_view.guide_start->itemData(m_view.guide_start->currentIndex()).toDouble();
549         endPos = m_view.guide_end->itemData(m_view.guide_end->currentIndex()).toDouble();
550     }
551     QString renderArgs = m_view.advanced_params->toPlainText();
552
553     // Adjust frame scale
554     int width;
555     int height;
556     if (m_view.rescale->isChecked() && m_view.rescale->isEnabled()) {
557         width = m_view.rescale_size->text().section('x', 0, 0).toInt();
558         height = m_view.rescale_size->text().section('x', 1, 1).toInt();
559     } else {
560         width = m_profile.width;
561         height = m_profile.height;
562     }
563     renderArgs.replace("%dar", "@" + QString::number(m_profile.display_aspect_num) + "/" + QString::number(m_profile.display_aspect_den));
564
565     // Adjust scanning
566     if (m_view.scanning_list->currentIndex() == 1) renderArgs.append(" progressive=1");
567     else if (m_view.scanning_list->currentIndex() == 2) renderArgs.append(" progressive=0");
568
569     // disable audio if requested
570     if (!m_view.export_audio->isChecked())
571         renderArgs.append(" an=1 ");
572
573     // Check if the rendering profile is different from project profile,
574     // in which case we need to use the producer_comsumer from MLT
575     bool resizeProfile = false;
576
577     QString std = renderArgs;
578     QString destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
579     if (std.contains(" s=")) {
580         QString subsize = std.section(" s=", 1, 1);
581         subsize = subsize.section(' ', 0, 0).toLower();
582         const QString currentSize = QString::number(width) + 'x' + QString::number(height);
583         if (subsize != currentSize) resizeProfile = true;
584     } else if (destination != "audioonly") {
585         // Add current size parametrer
586         renderArgs.append(QString(" s=%1x%2").arg(width).arg(height));
587     }
588
589     // insert item in running jobs list
590     QTreeWidgetItem *renderItem;
591     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
592     if (!existing.isEmpty()) renderItem = existing.at(0);
593     else renderItem = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
594     renderItem->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
595     renderItem->setData(1, Qt::UserRole + 1, QTime::currentTime());
596
597     // Set rendering type
598     QString group = m_view.size_list->currentItem()->data(MetaGroupRole).toString();
599     if (group == "dvd" && m_view.open_dvd->isChecked()) {
600         renderItem->setData(0, Qt::UserRole, group);
601         if (renderArgs.contains("profile=")) {
602             // rendering profile contains an MLT profile, so pass it to the running jog item, usefull for dvd
603             QString prof = renderArgs.section("profile=", 1, 1);
604             prof = prof.section(' ', 0, 0);
605             kDebug() << "// render profile: " << prof;
606             renderItem->setData(0, Qt::UserRole + 1, prof);
607         }
608     } else if (group == "websites" && m_view.open_browser->isChecked()) {
609         renderItem->setData(0, Qt::UserRole, group);
610         // pass the url
611         QString url = m_view.size_list->currentItem()->data(ExtraRole).toString();
612         renderItem->setData(0, Qt::UserRole + 1, url);
613     }
614
615     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);
616     if (scriptName.isEmpty()) m_view.tabWidget->setCurrentIndex(1);
617     else {
618         QTimer::singleShot(400, this, SLOT(parseScriptFiles()));
619         m_view.tabWidget->setCurrentIndex(2);
620     }
621 }
622
623 void RenderWidget::setProfile(MltVideoProfile profile) {
624     m_profile = profile;
625     //WARNING: this way to tell the video standard is a bit hackish...
626     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);
627     else m_view.format_selection->setCurrentIndex(1);
628     m_view.scanning_list->setCurrentIndex(0);
629     refreshView();
630 }
631
632 void RenderWidget::refreshView() {
633     m_view.size_list->blockSignals(true);
634     QListWidgetItem *sizeItem;
635
636     QString destination;
637     KIcon brokenIcon("dialog-close");
638     if (m_view.destination_list->currentIndex() > 0)
639         destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
640
641
642     if (destination == "dvd") m_view.open_dvd->setVisible(true);
643     else m_view.open_dvd->setVisible(false);
644     if (destination == "websites") m_view.open_browser->setVisible(true);
645     else m_view.open_browser->setVisible(false);
646     if (!destination.isEmpty() && QString("dvd websites audioonly").contains(destination))
647         m_view.rescale->setEnabled(false);
648     else m_view.rescale->setEnabled(true);
649     // hide groups that are not in the correct destination
650     for (int i = 0; i < m_view.format_list->count(); i++) {
651         sizeItem = m_view.format_list->item(i);
652         if (sizeItem->data(MetaGroupRole).toString() == destination) {
653             sizeItem->setHidden(false);
654             //kDebug() << "// SET GRP:: " << sizeItem->text() << ", METY:" << sizeItem->data(MetaGroupRole).toString();
655         } else sizeItem->setHidden(true);
656     }
657
658     // activate first visible item
659     QListWidgetItem * item = m_view.format_list->currentItem();
660     if (!item || item->isHidden()) {
661         for (int i = 0; i < m_view.format_list->count(); i++) {
662             if (!m_view.format_list->item(i)->isHidden()) {
663                 m_view.format_list->setCurrentRow(i);
664                 break;
665             }
666         }
667         item = m_view.format_list->currentItem();
668     }
669     if (!item || item->isHidden()) {
670         m_view.format_list->setEnabled(false);
671         m_view.size_list->setEnabled(false);
672         return;
673     } else {
674         m_view.format_list->setEnabled(true);
675         m_view.size_list->setEnabled(true);
676     }
677     int count = 0;
678     for (int i = 0; i < m_view.format_list->count() && count < 2; i++) {
679         if (!m_view.format_list->isRowHidden(i)) count++;
680     }
681     if (count > 1) m_view.format_list->setVisible(true);
682     else m_view.format_list->setVisible(false);
683     QString std;
684     QString group = item->text();
685     bool firstSelected = false;
686     const QStringList formatsList = KdenliveSettings::supportedformats();
687     const QStringList vcodecsList = KdenliveSettings::videocodecs();
688     const QStringList acodecsList = KdenliveSettings::audiocodecs();
689
690     for (int i = 0; i < m_view.size_list->count(); i++) {
691         sizeItem = m_view.size_list->item(i);
692         if ((sizeItem->data(GroupRole) == group || sizeItem->data(GroupRole).toString().isEmpty()) && sizeItem->data(MetaGroupRole) == destination) {
693             std = sizeItem->data(StandardRole).toString();
694             if (!std.isEmpty()) {
695                 if (std.contains("PAL", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 0);
696                 else if (std.contains("NTSC", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 1);
697             } else {
698                 sizeItem->setHidden(false);
699                 if (!firstSelected) m_view.size_list->setCurrentItem(sizeItem);
700                 firstSelected = true;
701             }
702
703             if (!sizeItem->isHidden()) {
704                 // Make sure the selected profile uses an installed avformat codec / format
705                 std = sizeItem->data(ParamsRole).toString();
706
707                 if (!formatsList.isEmpty()) {
708                     QString format;
709                     if (std.startsWith("f=")) format = std.section("f=", 1, 1);
710                     else if (std.contains(" f=")) format = std.section(" f=", 1, 1);
711                     if (!format.isEmpty()) {
712                         format = format.section(' ', 0, 0).toLower();
713                         if (!formatsList.contains(format)) {
714                             kDebug() << "***** UNSUPPORTED F: " << format;
715                             //sizeItem->setHidden(true);
716                             sizeItem->setFlags(Qt::ItemIsSelectable);
717                             sizeItem->setToolTip(i18n("Unsupported video format: %1", format));
718                             sizeItem->setIcon(brokenIcon);
719                         }
720                     }
721                 }
722                 if (!acodecsList.isEmpty() && !sizeItem->isHidden()) {
723                     QString format;
724                     if (std.startsWith("acodec=")) format = std.section("acodec=", 1, 1);
725                     else if (std.contains(" acodec=")) format = std.section(" acodec=", 1, 1);
726                     if (!format.isEmpty()) {
727                         format = format.section(' ', 0, 0).toLower();
728                         if (!acodecsList.contains(format)) {
729                             kDebug() << "*****  UNSUPPORTED ACODEC: " << format;
730                             //sizeItem->setHidden(true);
731                             sizeItem->setFlags(Qt::ItemIsSelectable);
732                             sizeItem->setToolTip(i18n("Unsupported audio codec: %1", format));
733                             sizeItem->setIcon(brokenIcon);
734                         }
735                     }
736                 }
737                 if (!vcodecsList.isEmpty() && !sizeItem->isHidden()) {
738                     QString format;
739                     if (std.startsWith("vcodec=")) format = std.section("vcodec=", 1, 1);
740                     else if (std.contains(" vcodec=")) format = std.section(" vcodec=", 1, 1);
741                     if (!format.isEmpty()) {
742                         format = format.section(' ', 0, 0).toLower();
743                         if (!vcodecsList.contains(format)) {
744                             kDebug() << "*****  UNSUPPORTED VCODEC: " << format;
745                             //sizeItem->setHidden(true);
746                             sizeItem->setFlags(Qt::ItemIsSelectable);
747                             sizeItem->setToolTip(i18n("Unsupported video codec: %1", format));
748                             sizeItem->setIcon(brokenIcon);
749                         }
750                     }
751                 }
752             }
753         } else sizeItem->setHidden(true);
754     }
755     focusFirstVisibleItem();
756     m_view.size_list->blockSignals(false);
757     refreshParams();
758 }
759
760 KUrl RenderWidget::filenameWithExtension(KUrl url, QString extension) {
761     QString path;
762     if (!url.isEmpty()) {
763         path = url.path();
764         int pos = path.lastIndexOf('.') + 1;
765         if (pos == 0) path.append('.' + extension);
766         else path = path.left(pos) + extension;
767
768     } else {
769         path = m_projectFolder + "/untitled." + extension;
770     }
771     return KUrl(path);
772 }
773
774
775 void RenderWidget::refreshParams() {
776     QListWidgetItem *item = m_view.size_list->currentItem();
777     if (!item || item->isHidden()) {
778         m_view.advanced_params->clear();
779         m_view.buttonStart->setEnabled(false);
780         return;
781     }
782     QString params = item->data(ParamsRole).toString();
783     QString extension = item->data(ExtensionRole).toString();
784     m_view.advanced_params->setPlainText(params);
785     QString destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
786     if (params.contains(" s=") || destination == "audioonly") {
787         // profile has a fixed size, do not allow resize
788         m_view.rescale->setEnabled(false);
789         m_view.rescale_size->setEnabled(false);
790     } else {
791         m_view.rescale->setEnabled(true);
792         m_view.rescale_size->setEnabled(true);
793     }
794     KUrl url = filenameWithExtension(m_view.out_file->url(), extension);
795     m_view.out_file->setUrl(url);
796 //     if (!url.isEmpty()) {
797 //         QString path = url.path();
798 //         int pos = path.lastIndexOf('.') + 1;
799 //  if (pos == 0) path.append('.' + extension);
800 //         else path = path.left(pos) + extension;
801 //         m_view.out_file->setUrl(KUrl(path));
802 //     } else {
803 //         m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
804 //     }
805     m_view.out_file->setFilter("*." + extension);
806
807     if (item->data(EditableRole).toString().isEmpty()) {
808         m_view.buttonDelete->setEnabled(false);
809         m_view.buttonEdit->setEnabled(false);
810     } else {
811         m_view.buttonDelete->setEnabled(true);
812         m_view.buttonEdit->setEnabled(true);
813     }
814
815     m_view.buttonStart->setEnabled(m_view.size_list->currentItem()->flags() & Qt::ItemIsEnabled);
816 }
817
818 void RenderWidget::reloadProfiles() {
819     parseProfiles();
820 }
821
822 void RenderWidget::parseProfiles(QString meta, QString group, QString profile) {
823     m_view.size_list->clear();
824     m_view.format_list->clear();
825     m_view.destination_list->clear();
826     m_view.destination_list->addItem(KIcon("video-x-generic"), i18n("File rendering"));
827     m_view.destination_list->addItem(KIcon("media-optical"), i18n("DVD"), "dvd");
828     m_view.destination_list->addItem(KIcon("audio-x-generic"), i18n("Audio only"), "audioonly");
829     m_view.destination_list->addItem(KIcon("applications-internet"), i18n("Web sites"), "websites");
830     m_view.destination_list->addItem(KIcon("applications-multimedia"), i18n("Media players"), "mediaplayers");
831     m_view.destination_list->addItem(KIcon("drive-harddisk"), i18n("Lossless / HQ"), "lossless");
832     m_view.destination_list->addItem(KIcon("pda"), i18n("Mobile devices"), "mobile");
833
834     QString exportFile = KStandardDirs::locate("appdata", "export/profiles.xml");
835     parseFile(exportFile, false);
836
837
838     QString exportFolder = KStandardDirs::locateLocal("appdata", "export/");
839     QDir directory = QDir(exportFolder);
840     QStringList filter;
841     filter << "*.xml";
842     const QStringList fileList = directory.entryList(filter, QDir::Files);
843     foreach(const QString filename, fileList)
844     parseFile(exportFolder + '/' + filename, filename == "customprofiles.xml");
845
846     if (!meta.isEmpty()) {
847         m_view.destination_list->blockSignals(true);
848         m_view.destination_list->setCurrentIndex(m_view.destination_list->findData(meta));
849         m_view.destination_list->blockSignals(false);
850     }
851     refreshView();
852     QList<QListWidgetItem *> child;
853     if (!group.isEmpty()) child = m_view.format_list->findItems(group, Qt::MatchExactly);
854     if (!child.isEmpty()) {
855         for (int i = 0; i < child.count(); i++) {
856             if (child.at(i)->data(MetaGroupRole).toString() == meta) {
857                 m_view.format_list->setCurrentItem(child.at(i));
858                 break;
859             }
860         }
861     }
862     child.clear();
863     if (!profile.isEmpty()) child = m_view.size_list->findItems(profile, Qt::MatchExactly);
864     if (!child.isEmpty()) m_view.size_list->setCurrentItem(child.at(0));
865 }
866
867 void RenderWidget::parseFile(QString exportFile, bool editable) {
868     kDebug() << "// Parsing file: " << exportFile;
869     kDebug() << "------------------------------";
870     QDomDocument doc;
871     QFile file(exportFile);
872     doc.setContent(&file, false);
873     file.close();
874     QDomElement documentElement;
875     QDomElement profileElement;
876     QString extension;
877     QListWidgetItem *item;
878     QDomNodeList groups = doc.elementsByTagName("group");
879
880     if (editable || groups.count() == 0) {
881         QDomElement profiles = doc.documentElement();
882         if (editable && profiles.attribute("version", 0).toInt() < 1) {
883             kDebug() << "// OLD profile version";
884             // this is an old profile version, update it
885             QDomDocument newdoc;
886             QDomElement newprofiles = newdoc.createElement("profiles");
887             newprofiles.setAttribute("version", 1);
888             newdoc.appendChild(newprofiles);
889             QDomNodeList profilelist = doc.elementsByTagName("profile");
890             for (int i = 0; i < profilelist.count(); i++) {
891                 QString category = i18n("Custom");
892                 QString extension;
893                 QDomNode parent = profilelist.at(i).parentNode();
894                 if (!parent.isNull()) {
895                     QDomElement parentNode = parent.toElement();
896                     if (parentNode.hasAttribute("name")) category = parentNode.attribute("name");
897                     extension = parentNode.attribute("extension");
898                 }
899                 profilelist.at(i).toElement().setAttribute("category", category);
900                 if (!extension.isEmpty()) profilelist.at(i).toElement().setAttribute("extension", extension);
901                 QDomNode n = profilelist.at(i).cloneNode();
902                 newprofiles.appendChild(newdoc.importNode(n, true));
903             }
904             if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
905                 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
906                 return;
907             }
908             QTextStream out(&file);
909             out << newdoc.toString();
910             file.close();
911             parseFile(exportFile, editable);
912             return;
913         }
914
915         QDomNode node = doc.elementsByTagName("profile").at(0);
916         if (node.isNull()) {
917             kDebug() << "// Export file: " << exportFile << " IS BROKEN";
918             return;
919         }
920         int count = 1;
921         while (!node.isNull()) {
922             QDomElement profile = node.toElement();
923             QString profileName = profile.attribute("name");
924             QString standard = profile.attribute("standard");
925             QString params = profile.attribute("args");
926             QString category = profile.attribute("category", i18n("Custom"));
927             QString dest = profile.attribute("destinationid");
928             QString prof_extension = profile.attribute("extension");
929             if (!prof_extension.isEmpty()) extension = prof_extension;
930
931             QList <QListWidgetItem *> list = m_view.format_list->findItems(category, Qt::MatchExactly);
932             bool exists = false;
933             for (int j = 0; j < list.count(); j++) {
934                 if (list.at(j)->data(MetaGroupRole) == dest) {
935                     exists = true;
936                     break;
937                 }
938             }
939             if (!exists) {
940                 item = new QListWidgetItem(category, m_view.format_list);
941                 item->setData(MetaGroupRole, dest);
942             }
943
944             item = new QListWidgetItem(profileName, m_view.size_list);
945             //kDebug() << "// ADDINg item with name: " << profileName << ", GRP" << category << ", DEST:" << dest ;
946             item->setData(GroupRole, category);
947             item->setData(MetaGroupRole, dest);
948             item->setData(ExtensionRole, extension);
949             item->setData(RenderRole, "avformat");
950             item->setData(StandardRole, standard);
951             item->setData(ParamsRole, params);
952             if (profile.hasAttribute("url")) item->setData(ExtraRole, profile.attribute("url"));
953             if (editable) item->setData(EditableRole, "true");
954             node = doc.elementsByTagName("profile").at(count);
955             count++;
956         }
957         return;
958     }
959
960     int i = 0;
961     QString groupName;
962     QString profileName;
963
964     QString prof_extension;
965     QString renderer;
966     QString params;
967     QString standard;
968     KIcon icon;
969
970     while (!groups.item(i).isNull()) {
971         documentElement = groups.item(i).toElement();
972         QDomNode gname = documentElement.elementsByTagName("groupname").at(0);
973         QString metagroupName;
974         QString metagroupId;
975         if (!gname.isNull()) {
976             metagroupName = gname.firstChild().nodeValue();
977             metagroupId = gname.toElement().attribute("id");
978             if (!metagroupName.isEmpty() && !m_view.destination_list->contains(metagroupName)) {
979                 if (metagroupId == "dvd") icon = KIcon("media-optical");
980                 else if (metagroupId == "audioonly") icon = KIcon("audio-x-generic");
981                 else if (metagroupId == "websites") icon = KIcon("applications-internet");
982                 else if (metagroupId == "mediaplayers") icon = KIcon("applications-multimedia");
983                 else if (metagroupId == "lossless") icon = KIcon("drive-harddisk");
984                 else if (metagroupId == "mobile") icon = KIcon("pda");
985                 m_view.destination_list->addItem(icon, i18n(metagroupName.toUtf8().data()), metagroupId);
986             }
987         }
988         groupName = documentElement.attribute("name", i18n("Custom"));
989         extension = documentElement.attribute("extension", QString::null);
990         renderer = documentElement.attribute("renderer", QString::null);
991         QList <QListWidgetItem *> list = m_view.format_list->findItems(groupName, Qt::MatchExactly);
992         bool exists = false;
993         for (int j = 0; j < list.count(); j++) {
994             if (list.at(j)->data(MetaGroupRole) == metagroupId) {
995                 exists = true;
996                 break;
997             }
998         }
999         if (!exists) {
1000             item = new QListWidgetItem(groupName, m_view.format_list);
1001             item->setData(MetaGroupRole, metagroupId);
1002         }
1003
1004         QDomNode n = groups.item(i).firstChild();
1005         while (!n.isNull()) {
1006             if (n.toElement().tagName() != "profile") {
1007                 n = n.nextSibling();
1008                 continue;
1009             }
1010             profileElement = n.toElement();
1011             profileName = profileElement.attribute("name");
1012             standard = profileElement.attribute("standard");
1013             params = profileElement.attribute("args");
1014             prof_extension = profileElement.attribute("extension");
1015             if (!prof_extension.isEmpty()) extension = prof_extension;
1016             item = new QListWidgetItem(profileName, m_view.size_list);
1017             item->setData(GroupRole, groupName);
1018             item->setData(MetaGroupRole, metagroupId);
1019             item->setData(ExtensionRole, extension);
1020             item->setData(RenderRole, renderer);
1021             item->setData(StandardRole, standard);
1022             item->setData(ParamsRole, params);
1023             if (profileElement.hasAttribute("url")) item->setData(ExtraRole, profileElement.attribute("url"));
1024             if (editable) item->setData(EditableRole, "true");
1025             n = n.nextSibling();
1026         }
1027
1028         i++;
1029     }
1030 }
1031
1032 void RenderWidget::setRenderJob(const QString &dest, int progress) {
1033     QTreeWidgetItem *item;
1034     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
1035     if (!existing.isEmpty()) item = existing.at(0);
1036     else item = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
1037     item->setData(2, Qt::UserRole, progress);
1038     if (progress == 0) {
1039         item->setIcon(0, KIcon("system-run"));
1040         item->setSizeHint(1, QSize(m_view.running_jobs->columnWidth(1), fontMetrics().height() * 2));
1041         item->setData(1, Qt::UserRole + 1, QTime::currentTime());
1042         slotCheckJob();
1043     } else {
1044         QTime startTime = item->data(1, Qt::UserRole + 1).toTime();
1045         int seconds = startTime.secsTo(QTime::currentTime());;
1046         const QString t = i18n("Estimated time %1", QTime().addSecs(seconds * (100 - progress) / progress).toString("hh:mm:ss"));
1047         item->setData(1, Qt::UserRole, t);
1048     }
1049 }
1050
1051 void RenderWidget::setRenderStatus(const QString &dest, int status, const QString &error) {
1052     QTreeWidgetItem *item;
1053     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly, 1);
1054     if (!existing.isEmpty()) item = existing.at(0);
1055     else item = new QTreeWidgetItem(m_view.running_jobs, QStringList() << QString() << dest << QString());
1056     if (status == -1) {
1057         // Job finished successfully
1058         item->setIcon(0, KIcon("dialog-ok"));
1059         item->setData(2, Qt::UserRole, 100);
1060         QTime startTime = item->data(1, Qt::UserRole + 1).toTime();
1061         int seconds = startTime.secsTo(QTime::currentTime());
1062         const QTime tm = QTime().addSecs(seconds);
1063         const QString t = i18n("Rendering finished in %1", tm.toString("hh:mm:ss"));
1064         item->setData(1, Qt::UserRole, t);
1065         QString itemGroup = item->data(0, Qt::UserRole).toString();
1066         if (itemGroup == "dvd") {
1067             emit openDvdWizard(item->text(1), item->data(0, Qt::UserRole + 1).toString());
1068         } else if (itemGroup == "websites") {
1069             QString url = item->data(0, Qt::UserRole + 1).toString();
1070             if (!url.isEmpty()) KRun *openBrowser = new KRun(url, this);
1071         }
1072     } else if (status == -2) {
1073         // Rendering crashed
1074         item->setData(1, Qt::UserRole, i18n("Rendering crashed"));
1075         item->setIcon(0, KIcon("dialog-close"));
1076         item->setData(2, Qt::UserRole, 100);
1077         m_view.error_log->append(i18n("<strong>Rendering of %1 crashed</strong><br />", dest));
1078         m_view.error_log->append(error);
1079         m_view.error_log->append("<hr />");
1080         m_view.error_box->setVisible(true);
1081     } else if (status == -3) {
1082         // User aborted job
1083         item->setData(1, Qt::UserRole, i18n("Rendering aborted"));
1084         item->setIcon(0, KIcon("dialog-cancel"));
1085         item->setData(2, Qt::UserRole, 100);
1086     }
1087     slotCheckJob();
1088 }
1089
1090 void RenderWidget::slotAbortCurrentJob() {
1091     QTreeWidgetItem *current = m_view.running_jobs->currentItem();
1092     if (current) emit abortProcess(current->text(1));
1093 }
1094
1095 void RenderWidget::slotCheckJob() {
1096     bool activate = false;
1097     QTreeWidgetItem *current = m_view.running_jobs->currentItem();
1098     if (current) {
1099         int percent = current->data(2, Qt::UserRole).toInt();
1100         if (percent < 100) activate = true;
1101     }
1102     m_view.abort_job->setEnabled(activate);
1103 }
1104
1105 void RenderWidget::parseScriptFiles() {
1106     QStringList scriptsFilter;
1107     scriptsFilter << "*.sh";
1108     m_view.scripts_list->clear();
1109
1110     QTreeWidgetItem *item;
1111     // List the project scripts
1112     QStringList scriptFiles = QDir(m_projectFolder + "/scripts").entryList(scriptsFilter, QDir::Files);
1113     for (int i = 0; i < scriptFiles.size(); ++i) {
1114         KUrl scriptpath(m_projectFolder + "/scripts/" + scriptFiles.at(i));
1115         item = new QTreeWidgetItem(m_view.scripts_list, QStringList() << scriptpath.fileName());
1116         QString target;
1117         QFile file(scriptpath.path());
1118         if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
1119             while (!file.atEnd()) {
1120                 QByteArray line = file.readLine();
1121                 if (line.startsWith("TARGET=")) {
1122                     target = QString(line).section("TARGET=", 1);
1123                     target.remove(QChar('"'));
1124                     break;
1125                 }
1126             }
1127             file.close();
1128         }
1129         item->setSizeHint(0, QSize(m_view.scripts_list->columnWidth(0), fontMetrics().height() * 2));
1130         item->setData(0, Qt::UserRole, target);
1131         item->setData(0, Qt::UserRole + 1, scriptpath.path());
1132     }
1133     bool activate = false;
1134     QTreeWidgetItemIterator it(m_view.scripts_list);
1135     if (*it) {
1136         kDebug() << "// FOUND SCRIPT ITEM:" << (*it)->text(0);
1137         // Selecting item does not work, why ???
1138         m_view.scripts_list->setCurrentItem(*it);
1139         (*it)->setSelected(true);
1140         activate = true;
1141     }
1142     kDebug() << "SELECTED SCRIPTS: " << m_view.scripts_list->selectedItems().count();
1143     m_view.start_script->setEnabled(activate);
1144     m_view.delete_script->setEnabled(activate);
1145 }
1146
1147 void RenderWidget::slotCheckScript() {
1148     bool activate = false;
1149     QTreeWidgetItemIterator it(m_view.scripts_list);
1150     if (*it) activate = true;
1151     m_view.start_script->setEnabled(activate);
1152     m_view.delete_script->setEnabled(activate);
1153 }
1154
1155 void RenderWidget::slotStartScript() {
1156     QTreeWidgetItem *item = m_view.scripts_list->currentItem();
1157     if (item) {
1158         QString path = item->data(0, Qt::UserRole + 1).toString();
1159         QProcess::startDetached(path);
1160         m_view.tabWidget->setCurrentIndex(1);
1161     }
1162 }
1163
1164 void RenderWidget::slotDeleteScript() {
1165     QTreeWidgetItem *item = m_view.scripts_list->currentItem();
1166     if (item) {
1167         QString path = item->data(0, Qt::UserRole + 1).toString();
1168         KIO::NetAccess::del(path + ".westley", this);
1169         KIO::NetAccess::del(path, this);
1170         parseScriptFiles();
1171     }
1172 }
1173
1174 void RenderWidget::slotGenerateScript() {
1175     slotExport(true);
1176 }