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), m_standard("PAL") {
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 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()));
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()));
64 connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
65 connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));
68 m_view.buttonStart->setEnabled(false);
69 m_view.guides_box->setVisible(false);
73 void RenderWidget::slotUpdateGuideBox() {
74 m_view.guides_box->setVisible(m_view.render_guide->isChecked());
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());
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());
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();
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());
102 if (nodes.count() > 0)
103 m_view.guide_end->addItem(i18n("End"), QString::number(duration));
106 void RenderWidget::slotUpdateButtons() {
107 if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
108 else m_view.buttonStart->setEnabled(true);
111 void RenderWidget::slotSaveProfile() {
112 Ui::SaveProfile_UI ui;
113 QDialog *d = new QDialog(this);
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);
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");
130 QFile file(exportFile);
131 doc.setContent(&file, false);
134 QDomElement documentElement;
135 bool groupExists = false;
137 QString newProfileName = ui.profile_name->text();
138 QString newGroupName = ui.group_name->currentText();
139 QDomNodeList groups = doc.elementsByTagName("group");
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) {
155 documentElement = doc.createElement("group");
156 documentElement.setAttribute("name", ui.group_name->currentText());
157 documentElement.setAttribute("renderer", "avformat");
158 doc.documentElement().appendChild(documentElement);
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);
166 //QCString save = doc.toString().utf8();
168 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
169 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
173 QTextStream out(&file);
174 out << doc.toString();
176 parseProfiles(newGroupName, newProfileName);
181 void RenderWidget::slotEditProfile() {
182 QListWidgetItem *item = m_view.size_list->currentItem();
184 QString currentGroup = m_view.format_list->currentItem()->text();
186 QString params = item->data(ParamsRole).toString();
187 QString extension = item->data(ExtensionRole).toString();
188 QString currentProfile = item->text();
190 Ui::SaveProfile_UI ui;
191 QDialog *d = new QDialog(this);
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();
205 if (d->exec() == QDialog::Accepted) {
207 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
209 QFile file(exportFile);
210 doc.setContent(&file, false);
213 QDomElement documentElement;
214 bool groupExists = false;
216 QString newProfileName = ui.profile_name->text();
217 QString newGroupName = ui.group_name->currentText();
218 QDomNodeList groups = doc.elementsByTagName("group");
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) {
234 documentElement = doc.createElement("group");
235 documentElement.setAttribute("name", ui.group_name->currentText());
236 documentElement.setAttribute("renderer", "avformat");
237 doc.documentElement().appendChild(documentElement);
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);
245 //QCString save = doc.toString().utf8();
247 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
248 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
252 QTextStream out(&file);
253 out << doc.toString();
255 parseProfiles(newGroupName, newProfileName);
260 void RenderWidget::slotDeleteProfile() {
261 QString currentGroup = m_view.format_list->currentItem()->text();
262 QString currentProfile = m_view.size_list->currentItem()->text();
264 QString exportFile = KStandardDirs::locateLocal("appdata", "export/customprofiles.xml");
266 QFile file(exportFile);
267 doc.setContent(&file, false);
270 QDomElement documentElement;
271 bool groupExists = false;
273 QDomNodeList groups = doc.elementsByTagName("group");
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));
295 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
296 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
299 QTextStream out(&file);
300 out << doc.toString();
302 parseProfiles(currentGroup);
305 void RenderWidget::slotExport() {
306 QListWidgetItem *item = m_view.size_list->currentItem();
308 QFile f(m_view.out_file->url().path());
310 if (KMessageBox::warningYesNo(this, i18n("File already exists. Do you want to overwrite it ?")) != KMessageBox::Yes)
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";
320 double startPos = -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();
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);
329 void RenderWidget::setDocumentStandard(QString std) {
334 void RenderWidget::refreshView() {
335 QListWidgetItem *item = m_view.format_list->currentItem();
337 m_view.format_list->setCurrentRow(0);
338 item = m_view.format_list->currentItem();
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);
358 void RenderWidget::refreshParams() {
359 QListWidgetItem *item = m_view.size_list->currentItem();
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));
373 m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
376 if (item->data(EditableRole).toString().isEmpty()) {
377 m_view.buttonDelete->setEnabled(false);
378 m_view.buttonEdit->setEnabled(false);
380 m_view.buttonDelete->setEnabled(true);
381 m_view.buttonEdit->setEnabled(true);
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 if (QFile::exists(exportFile)) parseFile(exportFile, true);
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));
400 void RenderWidget::parseFile(QString exportFile, bool editable) {
402 QFile file(exportFile);
403 doc.setContent(&file, false);
405 QDomElement documentElement;
406 QDomElement profileElement;
407 QDomNodeList groups = doc.elementsByTagName("group");
409 if (groups.count() == 0) {
410 kDebug() << "// Export file: " << exportFile << " IS BROKEN";
418 QString prof_extension;
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);
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");
455 #include "renderwidget.moc"