]> git.sesse.net Git - kdenlive/blob - src/renderwidget.cpp
Start of Kdenlive's d-bus interface, bringing a new tab in the render dialog showing...
[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 <QHeaderView>
25
26 #include <KStandardDirs>
27 #include <KDebug>
28 #include <KMessageBox>
29 #include <KComboBox>
30
31 #include "kdenlivesettings.h"
32 #include "renderwidget.h"
33 #include "ui_saveprofile_ui.h"
34
35 const int GroupRole = Qt::UserRole;
36 const int ExtensionRole = GroupRole + 1;
37 const int StandardRole = GroupRole + 2;
38 const int RenderRole = GroupRole + 3;
39 const int ParamsRole = GroupRole + 4;
40 const int EditableRole = GroupRole + 5;
41
42 RenderWidget::RenderWidget(QWidget * parent): QDialog(parent) {
43     m_view.setupUi(this);
44     m_view.buttonDelete->setIcon(KIcon("trash-empty"));
45     m_view.buttonDelete->setToolTip(i18n("Delete profile"));
46     m_view.buttonDelete->setEnabled(false);
47
48     m_view.buttonEdit->setIcon(KIcon("document-properties"));
49     m_view.buttonEdit->setToolTip(i18n("Edit profile"));
50     m_view.buttonEdit->setEnabled(false);
51
52     m_view.buttonSave->setIcon(KIcon("document-new"));
53     m_view.buttonSave->setToolTip(i18n("Create new profile"));
54
55     m_view.buttonInfo->setIcon(KIcon("help-about"));
56
57     if (KdenliveSettings::showrenderparams()) {
58         m_view.buttonInfo->setDown(true);
59     } else m_view.advanced_params->hide();
60
61     m_view.experimentalrender->setChecked(KdenliveSettings::experimentalrender());
62
63     m_view.experimentalrender->setToolTip(i18n("Changing the size of video when rendering\nis not fully supported, you may have problems\nwith some effects or title clips, so the export\nprofiles that resize your video are marked as\nexperimental"));
64
65     connect(m_view.buttonInfo, SIGNAL(clicked()), this, SLOT(showInfoPanel()));
66
67     connect(m_view.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
68     connect(m_view.buttonEdit, SIGNAL(clicked()), this, SLOT(slotEditProfile()));
69     connect(m_view.buttonDelete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
70     connect(m_view.buttonStart, SIGNAL(clicked()), this, SLOT(slotExport()));
71     connect(m_view.buttonClose, SIGNAL(clicked()), this, SLOT(hide()));
72     connect(m_view.buttonClose2, SIGNAL(clicked()), this, SLOT(hide()));
73     connect(m_view.out_file, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButtons()));
74     connect(m_view.format_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshView()));
75     connect(m_view.size_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshParams()));
76
77     connect(m_view.render_guide, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
78     connect(m_view.render_zone, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
79     connect(m_view.render_full, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
80
81     connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
82     connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));
83
84     connect(m_view.format_selection, SIGNAL(activated(int)), this, SLOT(refreshView()));
85     connect(m_view.experimentalrender, SIGNAL(stateChanged(int)), this, SLOT(slotUpdateExperimentalRendering()));
86
87     m_view.buttonStart->setEnabled(false);
88     m_view.guides_box->setVisible(false);
89     parseProfiles();
90     m_view.splitter->setStretchFactor(1, 5);
91     m_view.splitter->setStretchFactor(0, 2);
92
93     m_view.out_file->setMode(KFile::File);
94
95     m_view.running_jobs->setItemDelegate(new RenderViewDelegate(this));
96     QHeaderView *header = m_view.running_jobs->header();
97     QFontMetrics fm = fontMetrics();
98     //header->resizeSection(0, fm.width("typical-name-for-a-torrent.torrent"));
99     header->setResizeMode(0, QHeaderView::Interactive);
100     header->resizeSection(0, fm.width("typical-name-for-a-file.torrent"));
101     header->setResizeMode(1, QHeaderView::Fixed);
102     header->resizeSection(0, width() * 2 / 3);
103     header->setResizeMode(1, QHeaderView::Interactive);
104     //header->setResizeMode(1, QHeaderView::Fixed);
105
106     focusFirstVisibleItem();
107 }
108
109 void RenderWidget::slotUpdateExperimentalRendering() {
110     KdenliveSettings::setExperimentalrender(m_view.experimentalrender->isChecked());
111     refreshView();
112 }
113
114
115 void RenderWidget::showInfoPanel() {
116     if (m_view.advanced_params->isVisible()) {
117         m_view.advanced_params->setVisible(false);
118         m_view.buttonInfo->setDown(false);
119         KdenliveSettings::setShowrenderparams(false);
120     } else {
121         m_view.advanced_params->setVisible(true);
122         m_view.buttonInfo->setDown(true);
123         KdenliveSettings::setShowrenderparams(true);
124     }
125 }
126
127 void RenderWidget::slotUpdateGuideBox() {
128     m_view.guides_box->setVisible(m_view.render_guide->isChecked());
129 }
130
131 void RenderWidget::slotCheckStartGuidePosition() {
132     if (m_view.guide_start->currentIndex() > m_view.guide_end->currentIndex())
133         m_view.guide_start->setCurrentIndex(m_view.guide_end->currentIndex());
134 }
135
136 void RenderWidget::slotCheckEndGuidePosition() {
137     if (m_view.guide_end->currentIndex() < m_view.guide_start->currentIndex())
138         m_view.guide_end->setCurrentIndex(m_view.guide_start->currentIndex());
139 }
140
141 void RenderWidget::setGuides(QDomElement guidesxml, double duration) {
142     m_view.guide_start->clear();
143     m_view.guide_end->clear();
144     QDomNodeList nodes = guidesxml.elementsByTagName("guide");
145     if (nodes.count() > 0) {
146         m_view.guide_start->addItem(i18n("Render"), "0");
147         m_view.render_guide->setEnabled(true);
148     } else m_view.render_guide->setEnabled(false);
149     for (int i = 0; i < nodes.count(); i++) {
150         QDomElement e = nodes.item(i).toElement();
151         if (!e.isNull()) {
152             m_view.guide_start->addItem(e.attribute("comment"), e.attribute("time").toDouble());
153             m_view.guide_end->addItem(e.attribute("comment"), e.attribute("time").toDouble());
154         }
155     }
156     if (nodes.count() > 0)
157         m_view.guide_end->addItem(i18n("End"), QString::number(duration));
158 }
159
160 void RenderWidget::slotUpdateButtons() {
161     if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
162     else m_view.buttonStart->setEnabled(true);
163 }
164
165 void RenderWidget::slotSaveProfile() {
166     Ui::SaveProfile_UI ui;
167     QDialog *d = new QDialog(this);
168     ui.setupUi(d);
169     QString customGroup = i18n("Custom");
170     QStringList groupNames;
171     for (int i = 0; i < m_view.format_list->count(); i++)
172         groupNames.append(m_view.format_list->item(i)->text());
173     if (!groupNames.contains(customGroup)) groupNames.prepend(customGroup);
174     ui.group_name->addItems(groupNames);
175     int pos = ui.group_name->findText(customGroup);
176     ui.group_name->setCurrentIndex(pos);
177
178     ui.parameters->setText(m_view.advanced_params->toPlainText());
179     ui.extension->setText(m_view.size_list->currentItem()->data(ExtensionRole).toString());
180     ui.profile_name->setFocus();
181     if (d->exec() == QDialog::Accepted && !ui.profile_name->text().simplified().isEmpty()) {
182         QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
183         QDomDocument doc;
184         QFile file(exportFile);
185         doc.setContent(&file, false);
186         file.close();
187
188         QDomElement documentElement;
189         bool groupExists = false;
190         QString groupName;
191         QString newProfileName = ui.profile_name->text().simplified();
192         QString newGroupName = ui.group_name->currentText();
193         QDomNodeList groups = doc.elementsByTagName("group");
194         int i = 0;
195         if (groups.count() == 0) {
196             QDomElement profiles = doc.createElement("profiles");
197             doc.appendChild(profiles);
198         } else while (!groups.item(i).isNull()) {
199                 documentElement = groups.item(i).toElement();
200                 groupName = documentElement.attribute("name");
201                 kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
202                 if (groupName == newGroupName) {
203                     groupExists = true;
204                     break;
205                 }
206                 i++;
207             }
208         if (!groupExists) {
209             documentElement = doc.createElement("group");
210             documentElement.setAttribute("name", ui.group_name->currentText());
211             documentElement.setAttribute("renderer", "avformat");
212             doc.documentElement().appendChild(documentElement);
213         }
214         QDomElement profileElement = doc.createElement("profile");
215         profileElement.setAttribute("name", newProfileName);
216         profileElement.setAttribute("extension", ui.extension->text().simplified());
217         profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
218         documentElement.appendChild(profileElement);
219
220         //QCString save = doc.toString().utf8();
221
222         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
223             KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
224             delete d;
225             return;
226         }
227         QTextStream out(&file);
228         out << doc.toString();
229         file.close();
230         parseProfiles(newGroupName, newProfileName);
231     }
232     delete d;
233 }
234
235 void RenderWidget::slotEditProfile() {
236     QListWidgetItem *item = m_view.size_list->currentItem();
237     if (!item) return;
238     QString currentGroup = m_view.format_list->currentItem()->text();
239
240     QString params = item->data(ParamsRole).toString();
241     QString extension = item->data(ExtensionRole).toString();
242     QString currentProfile = item->text();
243
244     Ui::SaveProfile_UI ui;
245     QDialog *d = new QDialog(this);
246     ui.setupUi(d);
247     QStringList groupNames;
248     for (int i = 0; i < m_view.format_list->count(); i++)
249         groupNames.append(m_view.format_list->item(i)->text());
250     if (!groupNames.contains(currentGroup)) groupNames.prepend(currentGroup);
251     ui.group_name->addItems(groupNames);
252     int pos = ui.group_name->findText(currentGroup);
253     ui.group_name->setCurrentIndex(pos);
254     ui.profile_name->setText(currentProfile);
255     ui.extension->setText(extension);
256     ui.parameters->setText(params);
257     ui.profile_name->setFocus();
258
259     if (d->exec() == QDialog::Accepted) {
260         slotDeleteProfile();
261         QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
262         QDomDocument doc;
263         QFile file(exportFile);
264         doc.setContent(&file, false);
265         file.close();
266
267         QDomElement documentElement;
268         bool groupExists = false;
269         QString groupName;
270         QString newProfileName = ui.profile_name->text();
271         QString newGroupName = ui.group_name->currentText();
272         QDomNodeList groups = doc.elementsByTagName("group");
273         int i = 0;
274         if (groups.count() == 0) {
275             QDomElement profiles = doc.createElement("profiles");
276             doc.appendChild(profiles);
277         } else while (!groups.item(i).isNull()) {
278                 documentElement = groups.item(i).toElement();
279                 groupName = documentElement.attribute("name");
280                 kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
281                 if (groupName == newGroupName) {
282                     groupExists = true;
283                     break;
284                 }
285                 i++;
286             }
287         if (!groupExists) {
288             documentElement = doc.createElement("group");
289             documentElement.setAttribute("name", ui.group_name->currentText());
290             documentElement.setAttribute("renderer", "avformat");
291             doc.documentElement().appendChild(documentElement);
292         }
293         QDomElement profileElement = doc.createElement("profile");
294         profileElement.setAttribute("name", newProfileName);
295         profileElement.setAttribute("extension", ui.extension->text().simplified());
296         profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
297         documentElement.appendChild(profileElement);
298
299         //QCString save = doc.toString().utf8();
300
301         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
302             KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
303             delete d;
304             return;
305         }
306         QTextStream out(&file);
307         out << doc.toString();
308         file.close();
309         parseProfiles(newGroupName, newProfileName);
310     }
311     delete d;
312 }
313
314 void RenderWidget::slotDeleteProfile() {
315     QString currentGroup = m_view.format_list->currentItem()->text();
316     QString currentProfile = m_view.size_list->currentItem()->text();
317
318     QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
319     QDomDocument doc;
320     QFile file(exportFile);
321     doc.setContent(&file, false);
322     file.close();
323
324     QDomElement documentElement;
325     bool groupExists = false;
326     QString groupName;
327     QDomNodeList groups = doc.elementsByTagName("group");
328     int i = 0;
329
330     while (!groups.item(i).isNull()) {
331         documentElement = groups.item(i).toElement();
332         groupName = documentElement.attribute("name");
333         if (groupName == currentGroup) {
334             QDomNodeList children = documentElement.childNodes();
335             for (int j = 0; j < children.count(); j++) {
336                 QDomElement pro = children.at(j).toElement();
337                 if (pro.attribute("name") == currentProfile) {
338                     groups.item(i).removeChild(children.at(j));
339                     if (groups.item(i).childNodes().isEmpty())
340                         doc.documentElement().removeChild(groups.item(i));
341                     break;
342                 }
343             }
344             break;
345         }
346         i++;
347     }
348
349     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
350         KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
351         return;
352     }
353     QTextStream out(&file);
354     out << doc.toString();
355     file.close();
356     parseProfiles(currentGroup);
357     focusFirstVisibleItem();
358 }
359
360 void RenderWidget::updateButtons() {
361     if (!m_view.size_list->currentItem() || m_view.size_list->currentItem()->isHidden()) {
362         m_view.buttonSave->setEnabled(false);
363         m_view.buttonDelete->setEnabled(false);
364         m_view.buttonEdit->setEnabled(false);
365     } else {
366         m_view.buttonSave->setEnabled(true);
367         if (m_view.size_list->currentItem()->data(EditableRole).toString().isEmpty()) {
368             m_view.buttonDelete->setEnabled(false);
369             m_view.buttonEdit->setEnabled(false);
370         } else {
371             m_view.buttonDelete->setEnabled(true);
372             m_view.buttonEdit->setEnabled(true);
373         }
374     }
375 }
376
377
378 void RenderWidget::focusFirstVisibleItem() {
379     if (m_view.size_list->currentItem() && !m_view.size_list->currentItem()->isHidden()) {
380         updateButtons();
381         return;
382     }
383     for (uint ix = 0; ix < m_view.size_list->count(); ix++) {
384         QListWidgetItem *item = m_view.size_list->item(ix);
385         if (item && !item->isHidden()) {
386             m_view.size_list->setCurrentRow(ix);
387             break;
388         }
389     }
390     if (!m_view.size_list->currentItem()) m_view.size_list->setCurrentRow(0);
391     updateButtons();
392 }
393
394 void RenderWidget::slotExport() {
395     QListWidgetItem *item = m_view.size_list->currentItem();
396     if (!item) return;
397     QFile f(m_view.out_file->url().path());
398     if (f.exists()) {
399         if (KMessageBox::warningYesNo(this, i18n("File already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
400             return;
401     }
402     QStringList overlayargs;
403     if (m_view.tc_overlay->isChecked()) {
404         QString filterFile = KStandardDirs::locate("appdata", "metadata.properties");
405         overlayargs << "meta.attr.timecode=1" << "meta.attr.timecode.markup=#timecode";
406         overlayargs << "-attach" << "data_feed:attr_check" << "-attach";
407         overlayargs << "data_show:" + filterFile << "_fezzik=1" << "dynamic=1";
408     }
409     double startPos = -1;
410     double endPos = -1;
411     if (m_view.render_guide->isChecked()) {
412         startPos = m_view.guide_start->itemData(m_view.guide_start->currentIndex()).toDouble();
413         endPos = m_view.guide_end->itemData(m_view.guide_end->currentIndex()).toDouble();
414     }
415     QString renderArgs = m_view.advanced_params->toPlainText();
416     renderArgs.replace("%width", QString::number(m_profile.width));
417     renderArgs.replace("%height", QString::number(m_profile.height));
418     renderArgs.replace("%dar", "@" + QString::number(m_profile.display_aspect_num) + "/" + QString::number(m_profile.display_aspect_den));
419     if (m_view.force_progressive->checkState() == Qt::Checked) renderArgs.append(" progressive=1");
420     else if (m_view.force_progressive->checkState() == Qt::Unchecked) renderArgs.append(" progressive=0");
421
422     // Check if the rendering profile is different from project profile,
423     // in which case we need to use the producer_comsumer from MLT
424     bool resizeProfile = false;
425
426     QString std = item->data(ParamsRole).toString();
427     if (resizeProfile == false && std.contains(" s=")) {
428         QString subsize = std.section(" s=", 1, 1);
429         subsize = subsize.section(' ', 0, 0).toLower();
430         if (subsize != "%widthx%height") {
431             const QString currentSize = QString::number(m_profile.width) + 'x' + QString::number(m_profile.height);
432             if (subsize != currentSize) resizeProfile = true;
433         }
434     }
435
436     emit doRender(m_view.out_file->url().path(), item->data(RenderRole).toString(), overlayargs, renderArgs.simplified().split(' '), m_view.render_zone->isChecked(), m_view.play_after->isChecked(), startPos, endPos, resizeProfile);
437 }
438
439 void RenderWidget::setProfile(MltVideoProfile profile) {
440     m_profile = profile;
441     //WARNING: this way to tell the video standard is a bit hackish...
442     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);
443     else m_view.format_selection->setCurrentIndex(1);
444     m_view.force_progressive->setCheckState(Qt::PartiallyChecked);
445     refreshView();
446 }
447
448 void RenderWidget::refreshView() {
449     m_view.size_list->blockSignals(true);
450     QListWidgetItem *item = m_view.format_list->currentItem();
451     if (!item) {
452         m_view.format_list->setCurrentRow(0);
453         item = m_view.format_list->currentItem();
454     }
455     if (!item) return;
456     QString std;
457     QString group = item->text();
458     QListWidgetItem *sizeItem;
459     bool firstSelected = false;
460     const QStringList formatsList = KdenliveSettings::supportedformats();
461     const QStringList vcodecsList = KdenliveSettings::videocodecs();
462     const QStringList acodecsList = KdenliveSettings::audiocodecs();
463     for (int i = 0; i < m_view.size_list->count(); i++) {
464         sizeItem = m_view.size_list->item(i);
465         if (sizeItem->data(GroupRole) == group) {
466             std = sizeItem->data(StandardRole).toString();
467             if (!std.isEmpty()) {
468                 if (std.contains("PAL", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 0);
469                 else if (std.contains("NTSC", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 1);
470             } else {
471                 sizeItem->setHidden(false);
472                 if (!firstSelected) m_view.size_list->setCurrentItem(sizeItem);
473                 firstSelected = true;
474             }
475             if (!KdenliveSettings::experimentalrender() && !sizeItem->isHidden()) {
476                 // hide experimental codecs (which do resize the video)
477                 std = sizeItem->data(ParamsRole).toString();
478                 if (std.contains(" s=")) {
479                     QString subsize = std.section(" s=", 1, 1);
480                     subsize = subsize.section(' ', 0, 0).toLower();
481                     if (subsize != "%widthx%height") {
482                         const QString currentSize = QString::number(m_profile.width) + 'x' + QString::number(m_profile.height);
483                         if (subsize != currentSize) sizeItem->setHidden(true);
484                     }
485                 }
486             }
487             if (!sizeItem->isHidden()) {
488                 // Make sure the selected profile uses an installed avformat codec / format
489                 std = sizeItem->data(ParamsRole).toString();
490
491                 if (!formatsList.isEmpty()) {
492                     QString format;
493                     if (std.startsWith("f=")) format = std.section("f=", 1, 1);
494                     else if (std.contains(" f=")) format = std.section(" f=", 1, 1);
495                     if (!format.isEmpty()) {
496                         format = format.section(' ', 0, 0).toLower();
497                         if (!formatsList.contains(format)) {
498                             kDebug() << "*****  UNSUPPORTED F: " << format;
499                             sizeItem->setHidden(true);
500                         }
501                     }
502                 }
503                 if (!acodecsList.isEmpty() && !sizeItem->isHidden()) {
504                     QString format;
505                     if (std.startsWith("acodec=")) format = std.section("acodec=", 1, 1);
506                     else if (std.contains(" acodec=")) format = std.section(" acodec=", 1, 1);
507                     if (!format.isEmpty()) {
508                         format = format.section(' ', 0, 0).toLower();
509                         if (!acodecsList.contains(format)) {
510                             kDebug() << "*****  UNSUPPORTED ACODEC: " << format;
511                             sizeItem->setHidden(true);
512                         }
513                     }
514                 }
515                 if (!vcodecsList.isEmpty() && !sizeItem->isHidden()) {
516                     QString format;
517                     if (std.startsWith("vcodec=")) format = std.section("vcodec=", 1, 1);
518                     else if (std.contains(" vcodec=")) format = std.section(" vcodec=", 1, 1);
519                     if (!format.isEmpty()) {
520                         format = format.section(' ', 0, 0).toLower();
521                         if (!vcodecsList.contains(format)) {
522                             kDebug() << "*****  UNSUPPORTED VCODEC: " << format;
523                             sizeItem->setHidden(true);
524                         }
525                     }
526                 }
527             }
528         } else sizeItem->setHidden(true);
529     }
530     focusFirstVisibleItem();
531     m_view.size_list->blockSignals(false);
532     refreshParams();
533 }
534
535 void RenderWidget::refreshParams() {
536     QListWidgetItem *item = m_view.size_list->currentItem();
537     if (!item || item->isHidden()) {
538         m_view.advanced_params->clear();
539         m_view.buttonStart->setEnabled(false);
540         return;
541     }
542     QString params = item->data(ParamsRole).toString();
543     QString extension = item->data(ExtensionRole).toString();
544     m_view.advanced_params->setPlainText(params);
545     m_view.advanced_params->setToolTip(params);
546     KUrl url = m_view.out_file->url();
547     if (!url.isEmpty()) {
548         QString path = url.path();
549         int pos = path.lastIndexOf('.') + 1;
550         if (pos == 0) path.append('.') + extension;
551         else path = path.left(pos) + extension;
552         m_view.out_file->setUrl(KUrl(path));
553     } else {
554         m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
555     }
556     m_view.out_file->setFilter("*." + extension);
557
558     if (item->data(EditableRole).toString().isEmpty()) {
559         m_view.buttonDelete->setEnabled(false);
560         m_view.buttonEdit->setEnabled(false);
561     } else {
562         m_view.buttonDelete->setEnabled(true);
563         m_view.buttonEdit->setEnabled(true);
564     }
565     m_view.buttonStart->setEnabled(true);
566 }
567
568 void RenderWidget::parseProfiles(QString group, QString profile) {
569     m_view.size_list->clear();
570     m_view.format_list->clear();
571     QString exportFile = KStandardDirs::locate("appdata", "export/profiles.xml");
572     parseFile(exportFile, false);
573     exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
574     if (QFile::exists(exportFile)) parseFile(exportFile, true);
575     refreshView();
576     QList<QListWidgetItem *> child;
577     child = m_view.format_list->findItems(group, Qt::MatchExactly);
578     if (!child.isEmpty()) m_view.format_list->setCurrentItem(child.at(0));
579     child = m_view.size_list->findItems(profile, Qt::MatchExactly);
580     if (!child.isEmpty()) m_view.size_list->setCurrentItem(child.at(0));
581 }
582
583 void RenderWidget::parseFile(QString exportFile, bool editable) {
584     QDomDocument doc;
585     QFile file(exportFile);
586     doc.setContent(&file, false);
587     file.close();
588     QDomElement documentElement;
589     QDomElement profileElement;
590     QDomNodeList groups = doc.elementsByTagName("group");
591
592     if (groups.count() == 0) {
593         kDebug() << "// Export file: " << exportFile << " IS BROKEN";
594         return;
595     }
596
597     int i = 0;
598     QString groupName;
599     QString profileName;
600     QString extension;
601     QString prof_extension;
602     QString renderer;
603     QString params;
604     QString standard;
605     QListWidgetItem *item;
606     while (!groups.item(i).isNull()) {
607         documentElement = groups.item(i).toElement();
608         groupName = documentElement.attribute("name", QString::null);
609         extension = documentElement.attribute("extension", QString::null);
610         renderer = documentElement.attribute("renderer", QString::null);
611         if (m_view.format_list->findItems(groupName, Qt::MatchExactly).isEmpty())
612             new QListWidgetItem(groupName, m_view.format_list);
613
614         QDomNode n = groups.item(i).firstChild();
615         while (!n.isNull()) {
616             profileElement = n.toElement();
617             profileName = profileElement.attribute("name");
618             standard = profileElement.attribute("standard");
619             params = profileElement.attribute("args");
620             prof_extension = profileElement.attribute("extension");
621             if (!prof_extension.isEmpty()) extension = prof_extension;
622             item = new QListWidgetItem(profileName, m_view.size_list);
623             item->setData(GroupRole, groupName);
624             item->setData(ExtensionRole, extension);
625             item->setData(RenderRole, renderer);
626             item->setData(StandardRole, standard);
627             item->setData(ParamsRole, params);
628             if (editable) item->setData(EditableRole, "true");
629             n = n.nextSibling();
630         }
631
632         i++;
633     }
634 }
635
636 void RenderWidget::setRenderJob(const QString &dest, int progress) {
637     QList<QTreeWidgetItem *> existing = m_view.running_jobs->findItems(dest, Qt::MatchExactly);
638     if (!existing.isEmpty()) {
639         if (progress == -1) {
640             // Job finished successfully
641             existing.at(0)->setIcon(0, KIcon("dialog-ok"));
642             existing.at(0)->setData(1, Qt::UserRole, 100);
643         } else if (progress == -2) {
644             // Rendering crashed
645             existing.at(0)->setIcon(0, KIcon("dialog-close"));
646             existing.at(0)->setData(1, Qt::UserRole, 0);
647         } else existing.at(0)->setData(1, Qt::UserRole, progress);
648         return;
649     }
650     QTreeWidgetItem *item = new QTreeWidgetItem(m_view.running_jobs, QStringList() << dest << QString());
651     if (progress == -1) {
652         // Job finished successfully
653         item->setIcon(0, KIcon("dialog-ok"));
654         item->setData(1, Qt::UserRole, 100);
655     } else if (progress == -2) {
656         // Rendering crashed
657         item->setIcon(0, KIcon("dialog-close"));
658         item->setData(1, Qt::UserRole, 0);
659     } else item->setData(1, Qt::UserRole, progress);
660
661 }
662
663 #include "renderwidget.moc"