]> git.sesse.net Git - kdenlive/blob - src/renderwidget.cpp
Add render zone between guides
[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
23 #include <KStandardDirs>
24 #include <KDebug>
25 #include <KMessageBox>
26 #include <KComboBox>
27
28 #include "kdenlivesettings.h"
29 #include "renderwidget.h"
30 #include "ui_saveprofile_ui.h"
31
32 const int GroupRole = Qt::UserRole;
33 const int ExtensionRole = GroupRole + 1;
34 const int StandardRole = GroupRole + 2;
35 const int RenderRole = GroupRole + 3;
36 const int ParamsRole = GroupRole + 4;
37 const int EditableRole = GroupRole + 5;
38
39 RenderWidget::RenderWidget(QWidget * parent): QDialog(parent), m_standard("PAL") {
40     m_view.setupUi(this);
41     m_view.buttonDelete->setIcon(KIcon("trash-empty"));
42     m_view.buttonDelete->setToolTip(i18n("Delete profile"));
43     m_view.buttonDelete->setEnabled(false);
44
45     m_view.buttonEdit->setIcon(KIcon("document-properties"));
46     m_view.buttonEdit->setToolTip(i18n("Edit profile"));
47     m_view.buttonEdit->setEnabled(false);
48
49     m_view.buttonSave->setIcon(KIcon("document-new"));
50     m_view.buttonSave->setToolTip(i18n("Create new profile"));
51
52     connect(m_view.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
53     connect(m_view.buttonEdit, SIGNAL(clicked()), this, SLOT(slotEditProfile()));
54     connect(m_view.buttonDelete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
55     connect(m_view.buttonStart, SIGNAL(clicked()), this, SLOT(slotExport()));
56     connect(m_view.out_file, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButtons()));
57     connect(m_view.format_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshView()));
58     connect(m_view.size_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshParams()));
59
60     connect(m_view.render_guide, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
61     connect(m_view.render_zone, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
62     connect(m_view.render_full, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
63
64     connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
65     connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));
66
67
68     m_view.buttonStart->setEnabled(false);
69     m_view.guides_box->setVisible(false);
70     parseProfiles();
71 }
72
73 void RenderWidget::slotUpdateGuideBox() {
74     m_view.guides_box->setVisible(m_view.render_guide->isChecked());
75 }
76
77 void RenderWidget::slotCheckStartGuidePosition() {
78     if (m_view.guide_start->currentIndex() > m_view.guide_end->currentIndex())
79         m_view.guide_start->setCurrentIndex(m_view.guide_end->currentIndex());
80 }
81
82 void RenderWidget::slotCheckEndGuidePosition() {
83     if (m_view.guide_end->currentIndex() < m_view.guide_start->currentIndex())
84         m_view.guide_end->setCurrentIndex(m_view.guide_start->currentIndex());
85 }
86
87 void RenderWidget::setGuides(QDomElement guidesxml, double duration) {
88     m_view.guide_start->clear();
89     m_view.guide_end->clear();
90     QDomNodeList nodes = guidesxml.elementsByTagName("guide");
91     if (nodes.count() > 0) {
92         m_view.guide_start->addItem(i18n("Start"), "0");
93         m_view.render_guide->setEnabled(true);
94     } else m_view.render_guide->setEnabled(false);
95     for (int i = 0; i < nodes.count(); i++) {
96         QDomElement e = nodes.item(i).toElement();
97         if (!e.isNull()) {
98             m_view.guide_start->addItem(e.attribute("comment"), e.attribute("time").toDouble());
99             m_view.guide_end->addItem(e.attribute("comment"), e.attribute("time").toDouble());
100         }
101     }
102     if (nodes.count() > 0)
103         m_view.guide_end->addItem(i18n("End"), QString::number(duration));
104 }
105
106 void RenderWidget::slotUpdateButtons() {
107     if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
108     else m_view.buttonStart->setEnabled(true);
109 }
110
111 void RenderWidget::slotSaveProfile() {
112     Ui::SaveProfile_UI ui;
113     QDialog *d = new QDialog(this);
114     ui.setupUi(d);
115     QString customGroup = i18n("Custom");
116     QStringList groupNames;
117     for (int i = 0; i < m_view.format_list->count(); i++)
118         groupNames.append(m_view.format_list->item(i)->text());
119     if (!groupNames.contains(customGroup)) groupNames.prepend(customGroup);
120     ui.group_name->addItems(groupNames);
121     int pos = ui.group_name->findText(customGroup);
122     ui.group_name->setCurrentIndex(pos);
123
124     ui.parameters->setText(m_view.advanced_params->text());
125     ui.extension->setText(m_view.size_list->currentItem()->data(ExtensionRole).toString());
126     ui.profile_name->setFocus();
127     if (d->exec() == QDialog::Accepted) {
128         QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
129         QDomDocument doc;
130         QFile file(exportFile);
131         doc.setContent(&file, false);
132         file.close();
133
134         QDomElement documentElement;
135         bool groupExists = false;
136         QString groupName;
137         QString newProfileName = ui.profile_name->text();
138         QString newGroupName = ui.group_name->currentText();
139         QDomNodeList groups = doc.elementsByTagName("group");
140         int i = 0;
141         if (groups.count() == 0) {
142             QDomElement profiles = doc.createElement("profiles");
143             doc.appendChild(profiles);
144         } else while (!groups.item(i).isNull()) {
145                 documentElement = groups.item(i).toElement();
146                 groupName = documentElement.attribute("name");
147                 kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
148                 if (groupName == newGroupName) {
149                     groupExists = true;
150                     break;
151                 }
152                 i++;
153             }
154         if (!groupExists) {
155             documentElement = doc.createElement("group");
156             documentElement.setAttribute("name", ui.group_name->currentText());
157             documentElement.setAttribute("renderer", "avformat");
158             doc.documentElement().appendChild(documentElement);
159         }
160         QDomElement profileElement = doc.createElement("profile");
161         profileElement.setAttribute("name", newProfileName);
162         profileElement.setAttribute("extension", ui.extension->text().simplified());
163         profileElement.setAttribute("args", ui.parameters->text().simplified());
164         documentElement.appendChild(profileElement);
165
166         //QCString save = doc.toString().utf8();
167
168         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
169             KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
170             delete d;
171             return;
172         }
173         QTextStream out(&file);
174         out << doc.toString();
175         file.close();
176         parseProfiles(newGroupName, newProfileName);
177     }
178     delete d;
179 }
180
181 void RenderWidget::slotEditProfile() {
182     QListWidgetItem *item = m_view.size_list->currentItem();
183     if (!item) return;
184     QString currentGroup = m_view.format_list->currentItem()->text();
185
186     QString params = item->data(ParamsRole).toString();
187     QString extension = item->data(ExtensionRole).toString();
188     QString currentProfile = item->text();
189
190     Ui::SaveProfile_UI ui;
191     QDialog *d = new QDialog(this);
192     ui.setupUi(d);
193     QStringList groupNames;
194     for (int i = 0; i < m_view.format_list->count(); i++)
195         groupNames.append(m_view.format_list->item(i)->text());
196     if (!groupNames.contains(currentGroup)) groupNames.prepend(currentGroup);
197     ui.group_name->addItems(groupNames);
198     int pos = ui.group_name->findText(currentGroup);
199     ui.group_name->setCurrentIndex(pos);
200     ui.profile_name->setText(currentProfile);
201     ui.extension->setText(extension);
202     ui.parameters->setText(params);
203     ui.profile_name->setFocus();
204
205     if (d->exec() == QDialog::Accepted) {
206         slotDeleteProfile();
207         QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
208         QDomDocument doc;
209         QFile file(exportFile);
210         doc.setContent(&file, false);
211         file.close();
212
213         QDomElement documentElement;
214         bool groupExists = false;
215         QString groupName;
216         QString newProfileName = ui.profile_name->text();
217         QString newGroupName = ui.group_name->currentText();
218         QDomNodeList groups = doc.elementsByTagName("group");
219         int i = 0;
220         if (groups.count() == 0) {
221             QDomElement profiles = doc.createElement("profiles");
222             doc.appendChild(profiles);
223         } else while (!groups.item(i).isNull()) {
224                 documentElement = groups.item(i).toElement();
225                 groupName = documentElement.attribute("name");
226                 kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
227                 if (groupName == newGroupName) {
228                     groupExists = true;
229                     break;
230                 }
231                 i++;
232             }
233         if (!groupExists) {
234             documentElement = doc.createElement("group");
235             documentElement.setAttribute("name", ui.group_name->currentText());
236             documentElement.setAttribute("renderer", "avformat");
237             doc.documentElement().appendChild(documentElement);
238         }
239         QDomElement profileElement = doc.createElement("profile");
240         profileElement.setAttribute("name", newProfileName);
241         profileElement.setAttribute("extension", ui.extension->text().simplified());
242         profileElement.setAttribute("args", ui.parameters->text().simplified());
243         documentElement.appendChild(profileElement);
244
245         //QCString save = doc.toString().utf8();
246
247         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
248             KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
249             delete d;
250             return;
251         }
252         QTextStream out(&file);
253         out << doc.toString();
254         file.close();
255         parseProfiles(newGroupName, newProfileName);
256     }
257     delete d;
258 }
259
260 void RenderWidget::slotDeleteProfile() {
261     QString currentGroup = m_view.format_list->currentItem()->text();
262     QString currentProfile = m_view.size_list->currentItem()->text();
263
264     QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
265     QDomDocument doc;
266     QFile file(exportFile);
267     doc.setContent(&file, false);
268     file.close();
269
270     QDomElement documentElement;
271     bool groupExists = false;
272     QString groupName;
273     QDomNodeList groups = doc.elementsByTagName("group");
274     int i = 0;
275
276     while (!groups.item(i).isNull()) {
277         documentElement = groups.item(i).toElement();
278         groupName = documentElement.attribute("name");
279         if (groupName == currentGroup) {
280             QDomNodeList children = documentElement.childNodes();
281             for (int j = 0; j < children.count(); j++) {
282                 QDomElement pro = children.at(j).toElement();
283                 if (pro.attribute("name") == currentProfile) {
284                     groups.item(i).removeChild(children.at(j));
285                     if (groups.item(i).childNodes().isEmpty())
286                         doc.documentElement().removeChild(groups.item(i));
287                     break;
288                 }
289             }
290             break;
291         }
292         i++;
293     }
294
295     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
296         KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
297         return;
298     }
299     QTextStream out(&file);
300     out << doc.toString();
301     file.close();
302     parseProfiles(currentGroup);
303 }
304
305 void RenderWidget::slotExport() {
306     QListWidgetItem *item = m_view.size_list->currentItem();
307     if (!item) return;
308     QFile f(m_view.out_file->url().path());
309     if (f.exists()) {
310         if (KMessageBox::warningYesNo(this, i18n("File already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
311             return;
312     }
313     QStringList overlayargs;
314     if (m_view.tc_overlay->isChecked()) {
315         QString filterFile = KStandardDirs::locate("appdata", "metadata.properties");
316         overlayargs << "meta.attr.timecode=1" << "meta.attr.timecode.markup=#timecode";
317         overlayargs << "-attach" << "data_feed:attr_check" << "-attach";
318         overlayargs << "data_show:" + filterFile << "_fezzik=1" << "dynamic=1";
319     }
320     double startPos = -1;
321     double endPos = -1;
322     if (m_view.render_guide->isChecked()) {
323         startPos = m_view.guide_start->itemData(m_view.guide_start->currentIndex()).toDouble();
324         endPos = m_view.guide_end->itemData(m_view.guide_end->currentIndex()).toDouble();
325     }
326     emit doRender(m_view.out_file->url().path(), item->data(RenderRole).toString(), overlayargs, m_view.advanced_params->text().split(' '), m_view.render_zone->isChecked(), m_view.play_after->isChecked(), startPos, endPos);
327 }
328
329 void RenderWidget::setDocumentStandard(QString std) {
330     m_standard = std;
331     refreshView();
332 }
333
334 void RenderWidget::refreshView() {
335     QListWidgetItem *item = m_view.format_list->currentItem();
336     if (!item) {
337         m_view.format_list->setCurrentRow(0);
338         item = m_view.format_list->currentItem();
339     }
340     if (!item) return;
341     QString std;
342     QString group = item->text();
343     QListWidgetItem *sizeItem;
344     bool firstSelected = false;
345     for (int i = 0; i < m_view.size_list->count(); i++) {
346         sizeItem = m_view.size_list->item(i);
347         std = sizeItem->data(StandardRole).toString();
348         if (!std.isEmpty() && !std.contains(m_standard, Qt::CaseInsensitive)) sizeItem->setHidden(true);
349         else if (sizeItem->data(GroupRole) == group) {
350             sizeItem->setHidden(false);
351             if (!firstSelected) m_view.size_list->setCurrentItem(sizeItem);
352             firstSelected = true;
353         } else sizeItem->setHidden(true);
354     }
355
356 }
357
358 void RenderWidget::refreshParams() {
359     QListWidgetItem *item = m_view.size_list->currentItem();
360     if (!item) return;
361     QString params = item->data(ParamsRole).toString();
362     QString extension = item->data(ExtensionRole).toString();
363     m_view.advanced_params->setText(params);
364     m_view.advanced_params->setToolTip(params);
365     KUrl url = m_view.out_file->url();
366     if (!url.isEmpty()) {
367         QString path = url.path();
368         int pos = path.lastIndexOf('.') + 1;
369         if (pos == 0) path.append('.') + extension;
370         else path = path.left(pos) + extension;
371         m_view.out_file->setUrl(KUrl(path));
372     } else {
373         m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
374     }
375
376     if (item->data(EditableRole).toString().isEmpty()) {
377         m_view.buttonDelete->setEnabled(false);
378         m_view.buttonEdit->setEnabled(false);
379     } else {
380         m_view.buttonDelete->setEnabled(true);
381         m_view.buttonEdit->setEnabled(true);
382     }
383 }
384
385 void RenderWidget::parseProfiles(QString group, QString profile) {
386     m_view.size_list->clear();
387     m_view.format_list->clear();
388     QString exportFile = KStandardDirs::locate("appdata", "export/profiles.xml");
389     parseFile(exportFile, false);
390     exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
391     parseFile(exportFile, true);
392     refreshView();
393     QList<QListWidgetItem *> child;
394     child = m_view.format_list->findItems(group, Qt::MatchExactly);
395     if (!child.isEmpty()) m_view.format_list->setCurrentItem(child.at(0));
396     child = m_view.size_list->findItems(profile, Qt::MatchExactly);
397     if (!child.isEmpty()) m_view.size_list->setCurrentItem(child.at(0));
398 }
399
400 void RenderWidget::parseFile(QString exportFile, bool editable) {
401     QDomDocument doc;
402     QFile file(exportFile);
403     doc.setContent(&file, false);
404     file.close();
405     QDomElement documentElement;
406     QDomElement profileElement;
407     QDomNodeList groups = doc.elementsByTagName("group");
408
409     if (groups.count() == 0) {
410         kDebug() << "// Export file: " << exportFile << " IS BROKEN";
411         return;
412     }
413
414     int i = 0;
415     QString groupName;
416     QString profileName;
417     QString extension;
418     QString prof_extension;
419     QString renderer;
420     QString params;
421     QString standard;
422     QListWidgetItem *item;
423     while (!groups.item(i).isNull()) {
424         documentElement = groups.item(i).toElement();
425         groupName = documentElement.attribute("name", QString::null);
426         extension = documentElement.attribute("extension", QString::null);
427         renderer = documentElement.attribute("renderer", QString::null);
428         if (m_view.format_list->findItems(groupName, Qt::MatchExactly).isEmpty())
429             new QListWidgetItem(groupName, m_view.format_list);
430
431         QDomNode n = groups.item(i).firstChild();
432         while (!n.isNull()) {
433             profileElement = n.toElement();
434             profileName = profileElement.attribute("name");
435             standard = profileElement.attribute("standard");
436             params = profileElement.attribute("args");
437             prof_extension = profileElement.attribute("extension");
438             if (!prof_extension.isEmpty()) extension = prof_extension;
439             item = new QListWidgetItem(profileName, m_view.size_list);
440             item->setData(GroupRole, groupName);
441             item->setData(ExtensionRole, extension);
442             item->setData(RenderRole, renderer);
443             item->setData(StandardRole, standard);
444             item->setData(ParamsRole, params);
445             if (editable) item->setData(EditableRole, "true");
446             n = n.nextSibling();
447         }
448
449         i++;
450     }
451 }
452
453
454
455 #include "renderwidget.moc"
456
457