1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
21 #include <QDomDocument>
23 #include <KStandardDirs>
25 #include <KMessageBox>
28 #include "kdenlivesettings.h"
29 #include "renderwidget.h"
30 #include "ui_saveprofile_ui.h"
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;
39 RenderWidget::RenderWidget(QWidget * parent): QDialog(parent) {
41 m_view.buttonDelete->setIcon(KIcon("trash-empty"));
42 m_view.buttonDelete->setToolTip(i18n("Delete profile"));
43 m_view.buttonDelete->setEnabled(false);
45 m_view.buttonEdit->setIcon(KIcon("document-properties"));
46 m_view.buttonEdit->setToolTip(i18n("Edit profile"));
47 m_view.buttonEdit->setEnabled(false);
49 m_view.buttonSave->setIcon(KIcon("document-new"));
50 m_view.buttonSave->setToolTip(i18n("Create new profile"));
52 m_view.buttonInfo->setIcon(KIcon("help-about"));
54 if (KdenliveSettings::showrenderparams()) {
55 m_view.buttonInfo->setDown(true);
56 } else m_view.advanced_params->hide();
58 connect(m_view.buttonInfo, SIGNAL(clicked()), this, SLOT(showInfoPanel()));
60 connect(m_view.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
61 connect(m_view.buttonEdit, SIGNAL(clicked()), this, SLOT(slotEditProfile()));
62 connect(m_view.buttonDelete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
63 connect(m_view.buttonStart, SIGNAL(clicked()), this, SLOT(slotExport()));
64 connect(m_view.out_file, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButtons()));
65 connect(m_view.format_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshView()));
66 connect(m_view.size_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshParams()));
68 connect(m_view.render_guide, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
69 connect(m_view.render_zone, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
70 connect(m_view.render_full, SIGNAL(clicked(bool)), this, SLOT(slotUpdateGuideBox()));
72 connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
73 connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));
75 connect(m_view.format_selection, SIGNAL(activated(int)), this, SLOT(refreshView()));
77 m_view.buttonStart->setEnabled(false);
78 m_view.guides_box->setVisible(false);
80 m_view.splitter->setStretchFactor(1, 5);
81 m_view.splitter->setStretchFactor(0, 2);
84 void RenderWidget::showInfoPanel() {
85 if (m_view.advanced_params->isVisible()) {
86 m_view.advanced_params->setVisible(false);
87 m_view.buttonInfo->setDown(false);
88 KdenliveSettings::setShowrenderparams(false);
90 m_view.advanced_params->setVisible(true);
91 m_view.buttonInfo->setDown(true);
92 KdenliveSettings::setShowrenderparams(true);
96 void RenderWidget::slotUpdateGuideBox() {
97 m_view.guides_box->setVisible(m_view.render_guide->isChecked());
100 void RenderWidget::slotCheckStartGuidePosition() {
101 if (m_view.guide_start->currentIndex() > m_view.guide_end->currentIndex())
102 m_view.guide_start->setCurrentIndex(m_view.guide_end->currentIndex());
105 void RenderWidget::slotCheckEndGuidePosition() {
106 if (m_view.guide_end->currentIndex() < m_view.guide_start->currentIndex())
107 m_view.guide_end->setCurrentIndex(m_view.guide_start->currentIndex());
110 void RenderWidget::setGuides(QDomElement guidesxml, double duration) {
111 m_view.guide_start->clear();
112 m_view.guide_end->clear();
113 QDomNodeList nodes = guidesxml.elementsByTagName("guide");
114 if (nodes.count() > 0) {
115 m_view.guide_start->addItem(i18n("Render"), "0");
116 m_view.render_guide->setEnabled(true);
117 } else m_view.render_guide->setEnabled(false);
118 for (int i = 0; i < nodes.count(); i++) {
119 QDomElement e = nodes.item(i).toElement();
121 m_view.guide_start->addItem(e.attribute("comment"), e.attribute("time").toDouble());
122 m_view.guide_end->addItem(e.attribute("comment"), e.attribute("time").toDouble());
125 if (nodes.count() > 0)
126 m_view.guide_end->addItem(i18n("End"), QString::number(duration));
129 void RenderWidget::slotUpdateButtons() {
130 if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
131 else m_view.buttonStart->setEnabled(true);
134 void RenderWidget::slotSaveProfile() {
135 Ui::SaveProfile_UI ui;
136 QDialog *d = new QDialog(this);
138 QString customGroup = i18n("Custom");
139 QStringList groupNames;
140 for (int i = 0; i < m_view.format_list->count(); i++)
141 groupNames.append(m_view.format_list->item(i)->text());
142 if (!groupNames.contains(customGroup)) groupNames.prepend(customGroup);
143 ui.group_name->addItems(groupNames);
144 int pos = ui.group_name->findText(customGroup);
145 ui.group_name->setCurrentIndex(pos);
147 ui.parameters->setText(m_view.advanced_params->toPlainText());
148 ui.extension->setText(m_view.size_list->currentItem()->data(ExtensionRole).toString());
149 ui.profile_name->setFocus();
150 if (d->exec() == QDialog::Accepted) {
151 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
153 QFile file(exportFile);
154 doc.setContent(&file, false);
157 QDomElement documentElement;
158 bool groupExists = false;
160 QString newProfileName = ui.profile_name->text();
161 QString newGroupName = ui.group_name->currentText();
162 QDomNodeList groups = doc.elementsByTagName("group");
164 if (groups.count() == 0) {
165 QDomElement profiles = doc.createElement("profiles");
166 doc.appendChild(profiles);
167 } else while (!groups.item(i).isNull()) {
168 documentElement = groups.item(i).toElement();
169 groupName = documentElement.attribute("name");
170 kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
171 if (groupName == newGroupName) {
178 documentElement = doc.createElement("group");
179 documentElement.setAttribute("name", ui.group_name->currentText());
180 documentElement.setAttribute("renderer", "avformat");
181 doc.documentElement().appendChild(documentElement);
183 QDomElement profileElement = doc.createElement("profile");
184 profileElement.setAttribute("name", newProfileName);
185 profileElement.setAttribute("extension", ui.extension->text().simplified());
186 profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
187 documentElement.appendChild(profileElement);
189 //QCString save = doc.toString().utf8();
191 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
192 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
196 QTextStream out(&file);
197 out << doc.toString();
199 parseProfiles(newGroupName, newProfileName);
204 void RenderWidget::slotEditProfile() {
205 QListWidgetItem *item = m_view.size_list->currentItem();
207 QString currentGroup = m_view.format_list->currentItem()->text();
209 QString params = item->data(ParamsRole).toString();
210 QString extension = item->data(ExtensionRole).toString();
211 QString currentProfile = item->text();
213 Ui::SaveProfile_UI ui;
214 QDialog *d = new QDialog(this);
216 QStringList groupNames;
217 for (int i = 0; i < m_view.format_list->count(); i++)
218 groupNames.append(m_view.format_list->item(i)->text());
219 if (!groupNames.contains(currentGroup)) groupNames.prepend(currentGroup);
220 ui.group_name->addItems(groupNames);
221 int pos = ui.group_name->findText(currentGroup);
222 ui.group_name->setCurrentIndex(pos);
223 ui.profile_name->setText(currentProfile);
224 ui.extension->setText(extension);
225 ui.parameters->setText(params);
226 ui.profile_name->setFocus();
228 if (d->exec() == QDialog::Accepted) {
230 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
232 QFile file(exportFile);
233 doc.setContent(&file, false);
236 QDomElement documentElement;
237 bool groupExists = false;
239 QString newProfileName = ui.profile_name->text();
240 QString newGroupName = ui.group_name->currentText();
241 QDomNodeList groups = doc.elementsByTagName("group");
243 if (groups.count() == 0) {
244 QDomElement profiles = doc.createElement("profiles");
245 doc.appendChild(profiles);
246 } else while (!groups.item(i).isNull()) {
247 documentElement = groups.item(i).toElement();
248 groupName = documentElement.attribute("name");
249 kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
250 if (groupName == newGroupName) {
257 documentElement = doc.createElement("group");
258 documentElement.setAttribute("name", ui.group_name->currentText());
259 documentElement.setAttribute("renderer", "avformat");
260 doc.documentElement().appendChild(documentElement);
262 QDomElement profileElement = doc.createElement("profile");
263 profileElement.setAttribute("name", newProfileName);
264 profileElement.setAttribute("extension", ui.extension->text().simplified());
265 profileElement.setAttribute("args", ui.parameters->toPlainText().simplified());
266 documentElement.appendChild(profileElement);
268 //QCString save = doc.toString().utf8();
270 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
271 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
275 QTextStream out(&file);
276 out << doc.toString();
278 parseProfiles(newGroupName, newProfileName);
283 void RenderWidget::slotDeleteProfile() {
284 QString currentGroup = m_view.format_list->currentItem()->text();
285 QString currentProfile = m_view.size_list->currentItem()->text();
287 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
289 QFile file(exportFile);
290 doc.setContent(&file, false);
293 QDomElement documentElement;
294 bool groupExists = false;
296 QDomNodeList groups = doc.elementsByTagName("group");
299 while (!groups.item(i).isNull()) {
300 documentElement = groups.item(i).toElement();
301 groupName = documentElement.attribute("name");
302 if (groupName == currentGroup) {
303 QDomNodeList children = documentElement.childNodes();
304 for (int j = 0; j < children.count(); j++) {
305 QDomElement pro = children.at(j).toElement();
306 if (pro.attribute("name") == currentProfile) {
307 groups.item(i).removeChild(children.at(j));
308 if (groups.item(i).childNodes().isEmpty())
309 doc.documentElement().removeChild(groups.item(i));
318 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
319 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
322 QTextStream out(&file);
323 out << doc.toString();
325 parseProfiles(currentGroup);
328 void RenderWidget::slotExport() {
329 QListWidgetItem *item = m_view.size_list->currentItem();
331 QFile f(m_view.out_file->url().path());
333 if (KMessageBox::warningYesNo(this, i18n("File already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
336 QStringList overlayargs;
337 if (m_view.tc_overlay->isChecked()) {
338 QString filterFile = KStandardDirs::locate("appdata", "metadata.properties");
339 overlayargs << "meta.attr.timecode=1" << "meta.attr.timecode.markup=#timecode";
340 overlayargs << "-attach" << "data_feed:attr_check" << "-attach";
341 overlayargs << "data_show:" + filterFile << "_fezzik=1" << "dynamic=1";
343 double startPos = -1;
345 if (m_view.render_guide->isChecked()) {
346 startPos = m_view.guide_start->itemData(m_view.guide_start->currentIndex()).toDouble();
347 endPos = m_view.guide_end->itemData(m_view.guide_end->currentIndex()).toDouble();
349 emit doRender(m_view.out_file->url().path(), item->data(RenderRole).toString(), overlayargs, m_view.advanced_params->toPlainText().split(' '), m_view.render_zone->isChecked(), m_view.play_after->isChecked(), startPos, endPos);
352 void RenderWidget::setDocumentStandard(QString std) {
353 if (std == "PAL") m_view.format_selection->setCurrentIndex(0);
354 else m_view.format_selection->setCurrentIndex(1);
359 void RenderWidget::refreshView() {
360 QListWidgetItem *item = m_view.format_list->currentItem();
362 m_view.format_list->setCurrentRow(0);
363 item = m_view.format_list->currentItem();
367 QString group = item->text();
368 QListWidgetItem *sizeItem;
369 bool firstSelected = false;
370 for (int i = 0; i < m_view.size_list->count(); i++) {
371 sizeItem = m_view.size_list->item(i);
372 std = sizeItem->data(StandardRole).toString();
373 if (sizeItem->data(GroupRole) == group) {
374 if (!std.isEmpty()) {
375 if (std.contains("PAL", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 0);
376 else if (std.contains("NTSC", Qt::CaseInsensitive)) sizeItem->setHidden(m_view.format_selection->currentIndex() != 1);
378 sizeItem->setHidden(false);
379 if (!firstSelected) m_view.size_list->setCurrentItem(sizeItem);
380 firstSelected = true;
382 } else sizeItem->setHidden(true);
387 void RenderWidget::refreshParams() {
388 QListWidgetItem *item = m_view.size_list->currentItem();
390 QString params = item->data(ParamsRole).toString();
391 QString extension = item->data(ExtensionRole).toString();
392 m_view.advanced_params->setPlainText(params);
393 m_view.advanced_params->setToolTip(params);
394 KUrl url = m_view.out_file->url();
395 if (!url.isEmpty()) {
396 QString path = url.path();
397 int pos = path.lastIndexOf('.') + 1;
398 if (pos == 0) path.append('.') + extension;
399 else path = path.left(pos) + extension;
400 m_view.out_file->setUrl(KUrl(path));
402 m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
405 if (item->data(EditableRole).toString().isEmpty()) {
406 m_view.buttonDelete->setEnabled(false);
407 m_view.buttonEdit->setEnabled(false);
409 m_view.buttonDelete->setEnabled(true);
410 m_view.buttonEdit->setEnabled(true);
414 void RenderWidget::parseProfiles(QString group, QString profile) {
415 m_view.size_list->clear();
416 m_view.format_list->clear();
417 QString exportFile = KStandardDirs::locate("appdata", "export/profiles.xml");
418 parseFile(exportFile, false);
419 exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
420 if (QFile::exists(exportFile)) parseFile(exportFile, true);
422 QList<QListWidgetItem *> child;
423 child = m_view.format_list->findItems(group, Qt::MatchExactly);
424 if (!child.isEmpty()) m_view.format_list->setCurrentItem(child.at(0));
425 child = m_view.size_list->findItems(profile, Qt::MatchExactly);
426 if (!child.isEmpty()) m_view.size_list->setCurrentItem(child.at(0));
429 void RenderWidget::parseFile(QString exportFile, bool editable) {
431 QFile file(exportFile);
432 doc.setContent(&file, false);
434 QDomElement documentElement;
435 QDomElement profileElement;
436 QDomNodeList groups = doc.elementsByTagName("group");
438 if (groups.count() == 0) {
439 kDebug() << "// Export file: " << exportFile << " IS BROKEN";
447 QString prof_extension;
451 QListWidgetItem *item;
452 while (!groups.item(i).isNull()) {
453 documentElement = groups.item(i).toElement();
454 groupName = documentElement.attribute("name", QString::null);
455 extension = documentElement.attribute("extension", QString::null);
456 renderer = documentElement.attribute("renderer", QString::null);
457 if (m_view.format_list->findItems(groupName, Qt::MatchExactly).isEmpty())
458 new QListWidgetItem(groupName, m_view.format_list);
460 QDomNode n = groups.item(i).firstChild();
461 while (!n.isNull()) {
462 profileElement = n.toElement();
463 profileName = profileElement.attribute("name");
464 standard = profileElement.attribute("standard");
465 params = profileElement.attribute("args");
466 prof_extension = profileElement.attribute("extension");
467 if (!prof_extension.isEmpty()) extension = prof_extension;
468 item = new QListWidgetItem(profileName, m_view.size_list);
469 item->setData(GroupRole, groupName);
470 item->setData(ExtensionRole, extension);
471 item->setData(RenderRole, renderer);
472 item->setData(StandardRole, standard);
473 item->setData(ParamsRole, params);
474 if (editable) item->setData(EditableRole, "true");
484 #include "renderwidget.moc"