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.buttonSave->setIcon(KIcon("document-new"));
46 m_view.buttonSave->setToolTip(i18n("Create new profile"));
48 connect(m_view.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
49 connect(m_view.buttonDelete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
50 connect(m_view.buttonStart, SIGNAL(clicked()), this, SLOT(slotExport()));
51 connect(m_view.out_file, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButtons()));
52 connect(m_view.format_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshView()));
53 connect(m_view.size_list, SIGNAL(currentRowChanged(int)), this, SLOT(refreshParams()));
55 m_view.buttonStart->setEnabled(false);
59 void RenderWidget::slotUpdateButtons() {
60 if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
61 else m_view.buttonStart->setEnabled(true);
64 void RenderWidget::slotSaveProfile() {
65 Ui::SaveProfile_UI ui;
66 QDialog *d = new QDialog(this);
68 QString customGroup = i18n("Custom");
69 QStringList groupNames;
70 for (int i = 0; i < m_view.format_list->count(); i++)
71 groupNames.append(m_view.format_list->item(i)->text());
72 if (!groupNames.contains(customGroup)) groupNames.prepend(customGroup);
73 ui.group_name->addItems(groupNames);
74 int pos = ui.group_name->findText(customGroup);
75 ui.group_name->setCurrentIndex(pos);
77 ui.parameters->setText(m_view.advanced_params->text());
78 ui.extension->setText(m_view.size_list->currentItem()->data(ExtensionRole).toString());
79 ui.profile_name->setFocus();
80 if (d->exec() == QDialog::Accepted) {
81 QString exportFile = KStandardDirs::locateLocal("data", "kdenlive/export/customprofiles.xml");
83 QFile file(exportFile);
84 doc.setContent(&file, false);
87 QDomElement documentElement;
88 bool groupExists = false;
90 QString newProfileName = ui.profile_name->text();
91 QString newGroupName = ui.group_name->currentText();
92 QDomNodeList groups = doc.elementsByTagName("group");
94 if (groups.count() == 0) {
95 QDomElement profiles = doc.createElement("profiles");
96 doc.appendChild(profiles);
97 } else while (!groups.item(i).isNull()) {
98 documentElement = groups.item(i).toElement();
99 groupName = documentElement.attribute("name");
100 kDebug() << "// SAVE, PARSING FROUP: " << i << ", name: " << groupName << ", LOOK FR: " << newGroupName;
101 if (groupName == newGroupName) {
108 documentElement = doc.createElement("group");
109 documentElement.setAttribute("name", ui.group_name->currentText());
110 documentElement.setAttribute("renderer", "avformat");
111 doc.documentElement().appendChild(documentElement);
113 QDomElement profileElement = doc.createElement("profile");
114 profileElement.setAttribute("name", newProfileName);
115 profileElement.setAttribute("extension", ui.extension->text().simplified());
116 profileElement.setAttribute("args", ui.parameters->text().simplified());
117 documentElement.appendChild(profileElement);
119 //QCString save = doc.toString().utf8();
121 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
122 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
126 QTextStream out(&file);
127 out << doc.toString();
129 parseProfiles(newGroupName, newProfileName);
134 void RenderWidget::slotDeleteProfile() {
135 QString currentGroup = m_view.format_list->currentItem()->text();
136 QString currentProfile = m_view.size_list->currentItem()->text();
138 QString exportFile = KStandardDirs::locateLocal("data", "kdenlive/export/customprofiles.xml");
140 QFile file(exportFile);
141 doc.setContent(&file, false);
144 QDomElement documentElement;
145 bool groupExists = false;
147 QDomNodeList groups = doc.elementsByTagName("group");
150 while (!groups.item(i).isNull()) {
151 documentElement = groups.item(i).toElement();
152 groupName = documentElement.attribute("name");
153 if (groupName == currentGroup) {
154 QDomNodeList children = documentElement.childNodes();
155 for (int j = 0; j < children.count(); j++) {
156 QDomElement pro = children.at(j).toElement();
157 if (pro.attribute("name") == currentProfile) {
158 groups.item(i).removeChild(children.at(j));
159 if (groups.item(i).childNodes().isEmpty())
160 doc.documentElement().removeChild(groups.item(i));
169 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
170 KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
173 QTextStream out(&file);
174 out << doc.toString();
176 parseProfiles(currentGroup);
179 void RenderWidget::slotExport() {
180 QListWidgetItem *item = m_view.size_list->currentItem();
182 QFile f(m_view.out_file->url().path());
184 if (KMessageBox::warningYesNo(this, i18n("File already exists. Doy you want to overwrite it ?")) != KMessageBox::Yes)
187 emit doRender(m_view.out_file->url().path(), item->data(RenderRole).toString(), m_view.advanced_params->text().split(' '), m_view.zone_only->isChecked(), m_view.play_after->isChecked());
190 void RenderWidget::setDocumentStandard(QString std) {
195 void RenderWidget::refreshView() {
196 QListWidgetItem *item = m_view.format_list->currentItem();
198 m_view.format_list->setCurrentRow(0);
199 item = m_view.format_list->currentItem();
203 QString group = item->text();
204 QListWidgetItem *sizeItem;
205 bool firstSelected = false;
206 for (int i = 0; i < m_view.size_list->count(); i++) {
207 sizeItem = m_view.size_list->item(i);
208 std = sizeItem->data(StandardRole).toString();
209 if (!std.isEmpty() && !std.contains(m_standard, Qt::CaseInsensitive)) sizeItem->setHidden(true);
210 else if (sizeItem->data(GroupRole) == group) {
211 sizeItem->setHidden(false);
212 if (!firstSelected) m_view.size_list->setCurrentItem(sizeItem);
213 firstSelected = true;
214 } else sizeItem->setHidden(true);
219 void RenderWidget::refreshParams() {
220 QListWidgetItem *item = m_view.size_list->currentItem();
222 QString params = item->data(ParamsRole).toString();
223 QString extension = item->data(ExtensionRole).toString();
224 m_view.advanced_params->setText(params);
225 m_view.advanced_params->setToolTip(params);
226 KUrl url = m_view.out_file->url();
227 if (!url.isEmpty()) {
228 QString path = url.path();
229 int pos = path.lastIndexOf('.') + 1;
230 if (pos == 0) path.append('.') + extension;
231 else path = path.left(pos) + extension;
232 m_view.out_file->setUrl(KUrl(path));
234 m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
237 if (item->data(EditableRole).toString().isEmpty()) m_view.buttonDelete->setEnabled(false);
238 else m_view.buttonDelete->setEnabled(true);
241 void RenderWidget::parseProfiles(QString group, QString profile) {
242 m_view.size_list->clear();
243 m_view.format_list->clear();
244 QString exportFile = KStandardDirs::locate("data", "kdenlive/export/profiles.xml");
245 parseFile(exportFile, false);
246 exportFile = KStandardDirs::locateLocal("data", "kdenlive/export/customprofiles.xml");
247 parseFile(exportFile, true);
249 QList<QListWidgetItem *> child;
250 child = m_view.format_list->findItems(group, Qt::MatchExactly);
251 if (!child.isEmpty()) m_view.format_list->setCurrentItem(child.at(0));
252 child = m_view.size_list->findItems(profile, Qt::MatchExactly);
253 if (!child.isEmpty()) m_view.size_list->setCurrentItem(child.at(0));
256 void RenderWidget::parseFile(QString exportFile, bool editable) {
258 QFile file(exportFile);
259 doc.setContent(&file, false);
261 QDomElement documentElement;
262 QDomElement profileElement;
263 QDomNodeList groups = doc.elementsByTagName("group");
265 if (groups.count() == 0) {
266 kDebug() << "// Export file: " << exportFile << " IS BROKEN";
274 QString prof_extension;
278 QListWidgetItem *item;
279 while (!groups.item(i).isNull()) {
280 documentElement = groups.item(i).toElement();
281 groupName = documentElement.attribute("name", QString::null);
282 extension = documentElement.attribute("extension", QString::null);
283 renderer = documentElement.attribute("renderer", QString::null);
284 if (m_view.format_list->findItems(groupName, Qt::MatchExactly).isEmpty())
285 new QListWidgetItem(groupName, m_view.format_list);
287 QDomNode n = groups.item(i).firstChild();
288 while (!n.isNull()) {
289 profileElement = n.toElement();
290 profileName = profileElement.attribute("name");
291 standard = profileElement.attribute("standard");
292 params = profileElement.attribute("args");
293 prof_extension = profileElement.attribute("extension");
294 if (!prof_extension.isEmpty()) extension = prof_extension;
295 item = new QListWidgetItem(profileName, m_view.size_list);
296 item->setData(GroupRole, groupName);
297 item->setData(ExtensionRole, extension);
298 item->setData(RenderRole, renderer);
299 item->setData(StandardRole, standard);
300 item->setData(ParamsRole, params);
301 if (editable) item->setData(EditableRole, "true");
311 #include "renderwidget.moc"