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();
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();
179 void RenderWidget::slotExport() {
180 QFile f(m_view.out_file->url().path());
182 if (KMessageBox::warningYesNo(this, i18n("File already exists. Doy you want to overwrite it ?")) != KMessageBox::Yes)
185 emit doRender(m_view.out_file->url().path(), m_view.advanced_params->text().split(' '), m_view.zone_only->isChecked(), m_view.play_after->isChecked());
188 void RenderWidget::setDocumentStandard(QString std) {
193 void RenderWidget::refreshView() {
194 QListWidgetItem *item = m_view.format_list->currentItem();
196 m_view.format_list->setCurrentRow(0);
197 item = m_view.format_list->currentItem();
201 QString group = item->text();
202 QListWidgetItem *sizeItem;
203 bool firstSelected = false;
204 for (int i = 0; i < m_view.size_list->count(); i++) {
205 sizeItem = m_view.size_list->item(i);
206 std = sizeItem->data(StandardRole).toString();
207 if (!std.isEmpty() && !std.contains(m_standard, Qt::CaseInsensitive)) sizeItem->setHidden(true);
208 else if (sizeItem->data(GroupRole) == group) {
209 sizeItem->setHidden(false);
210 if (!firstSelected) m_view.size_list->setCurrentItem(sizeItem);
211 firstSelected = true;
212 } else sizeItem->setHidden(true);
217 void RenderWidget::refreshParams() {
218 QListWidgetItem *item = m_view.size_list->currentItem();
220 QString params = item->data(ParamsRole).toString();
221 QString extension = item->data(ExtensionRole).toString();
222 m_view.advanced_params->setText(params);
223 m_view.advanced_params->setToolTip(params);
224 KUrl url = m_view.out_file->url();
225 if (!url.isEmpty()) {
226 QString path = url.path();
227 int pos = path.lastIndexOf('.') + 1;
228 if (pos == 0) path.append('.') + extension;
229 else path = path.left(pos) + extension;
230 m_view.out_file->setUrl(KUrl(path));
232 m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
235 if (item->data(EditableRole).toString().isEmpty()) m_view.buttonDelete->setEnabled(false);
236 else m_view.buttonDelete->setEnabled(true);
239 void RenderWidget::parseProfiles() {
240 m_view.size_list->clear();
241 m_view.format_list->clear();
242 QString exportFile = KStandardDirs::locate("data", "kdenlive/export/profiles.xml");
243 parseFile(exportFile, false);
244 exportFile = KStandardDirs::locateLocal("data", "kdenlive/export/customprofiles.xml");
245 parseFile(exportFile, true);
249 void RenderWidget::parseFile(QString exportFile, bool editable) {
251 QFile file(exportFile);
252 doc.setContent(&file, false);
254 QDomElement documentElement;
255 QDomElement profileElement;
256 QDomNodeList groups = doc.elementsByTagName("group");
258 if (groups.count() == 0) {
259 kDebug() << "// Export file: " << exportFile << " IS BROKEN";
267 QString prof_extension;
271 QListWidgetItem *item;
272 while (!groups.item(i).isNull()) {
273 documentElement = groups.item(i).toElement();
274 groupName = documentElement.attribute("name", QString::null);
275 extension = documentElement.attribute("extension", QString::null);
276 renderer = documentElement.attribute("renderer", QString::null);
277 if (m_view.format_list->findItems(groupName, Qt::MatchExactly).isEmpty())
278 new QListWidgetItem(groupName, m_view.format_list);
280 QDomNode n = groups.item(i).firstChild();
281 while (!n.isNull()) {
282 profileElement = n.toElement();
283 profileName = profileElement.attribute("name");
284 standard = profileElement.attribute("standard");
285 params = profileElement.attribute("args");
286 prof_extension = profileElement.attribute("extension");
287 if (!prof_extension.isEmpty()) extension = prof_extension;
288 item = new QListWidgetItem(profileName, m_view.size_list);
289 item->setData(GroupRole, groupName);
290 item->setData(ExtensionRole, extension);
291 item->setData(RenderRole, renderer);
292 item->setData(StandardRole, standard);
293 item->setData(ParamsRole, params);
294 if (editable) item->setData(EditableRole, "true");
304 #include "renderwidget.moc"