]> git.sesse.net Git - kdenlive/blob - src/renderwidget.cpp
Fix version number (now 0.7), add myself as author
[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.buttonSave->setIcon(KIcon("document-new"));
46     m_view.buttonSave->setToolTip(i18n("Create new profile"));
47
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()));
54
55     m_view.buttonStart->setEnabled(false);
56     parseProfiles();
57 }
58
59 void RenderWidget::slotUpdateButtons() {
60     if (m_view.out_file->url().isEmpty()) m_view.buttonStart->setEnabled(false);
61     else m_view.buttonStart->setEnabled(true);
62 }
63
64 void RenderWidget::slotSaveProfile() {
65     Ui::SaveProfile_UI ui;
66     QDialog *d = new QDialog(this);
67     ui.setupUi(d);
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);
76
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");
82         QDomDocument doc;
83         QFile file(exportFile);
84         doc.setContent(&file, false);
85         file.close();
86
87         QDomElement documentElement;
88         bool groupExists = false;
89         QString groupName;
90         QString newProfileName = ui.profile_name->text();
91         QString newGroupName = ui.group_name->currentText();
92         QDomNodeList groups = doc.elementsByTagName("group");
93         int i = 0;
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) {
102                     groupExists = true;
103                     break;
104                 }
105                 i++;
106             }
107         if (!groupExists) {
108             documentElement = doc.createElement("group");
109             documentElement.setAttribute("name", ui.group_name->currentText());
110             documentElement.setAttribute("renderer", "avformat");
111             doc.documentElement().appendChild(documentElement);
112         }
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);
118
119         //QCString save = doc.toString().utf8();
120
121         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
122             KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
123             delete d;
124             return;
125         }
126         QTextStream out(&file);
127         out << doc.toString();
128         file.close();
129         parseProfiles(newGroupName, newProfileName);
130     }
131     delete d;
132 }
133
134 void RenderWidget::slotDeleteProfile() {
135     QString currentGroup = m_view.format_list->currentItem()->text();
136     QString currentProfile = m_view.size_list->currentItem()->text();
137
138     QString exportFile = KStandardDirs::locateLocal("data", "kdenlive/export/customprofiles.xml");
139     QDomDocument doc;
140     QFile file(exportFile);
141     doc.setContent(&file, false);
142     file.close();
143
144     QDomElement documentElement;
145     bool groupExists = false;
146     QString groupName;
147     QDomNodeList groups = doc.elementsByTagName("group");
148     int i = 0;
149
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));
161                     break;
162                 }
163             }
164             break;
165         }
166         i++;
167     }
168
169     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
170         KMessageBox::sorry(this, i18n("Unable to write to file %1", exportFile));
171         return;
172     }
173     QTextStream out(&file);
174     out << doc.toString();
175     file.close();
176     parseProfiles(currentGroup);
177 }
178
179 void RenderWidget::slotExport() {
180     QListWidgetItem *item = m_view.size_list->currentItem();
181     if (!item) return;
182     QFile f(m_view.out_file->url().path());
183     if (f.exists()) {
184         if (KMessageBox::warningYesNo(this, i18n("File already exists. Doy you want to overwrite it ?")) != KMessageBox::Yes)
185             return;
186     }
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());
188 }
189
190 void RenderWidget::setDocumentStandard(QString std) {
191     m_standard = std;
192     refreshView();
193 }
194
195 void RenderWidget::refreshView() {
196     QListWidgetItem *item = m_view.format_list->currentItem();
197     if (!item) {
198         m_view.format_list->setCurrentRow(0);
199         item = m_view.format_list->currentItem();
200     }
201     if (!item) return;
202     QString std;
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);
215     }
216
217 }
218
219 void RenderWidget::refreshParams() {
220     QListWidgetItem *item = m_view.size_list->currentItem();
221     if (!item) return;
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));
233     } else {
234         m_view.out_file->setUrl(KUrl(QDir::homePath() + "/untitled." + extension));
235     }
236
237     if (item->data(EditableRole).toString().isEmpty()) m_view.buttonDelete->setEnabled(false);
238     else m_view.buttonDelete->setEnabled(true);
239 }
240
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);
248     refreshView();
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));
254 }
255
256 void RenderWidget::parseFile(QString exportFile, bool editable) {
257     QDomDocument doc;
258     QFile file(exportFile);
259     doc.setContent(&file, false);
260     file.close();
261     QDomElement documentElement;
262     QDomElement profileElement;
263     QDomNodeList groups = doc.elementsByTagName("group");
264
265     if (groups.count() == 0) {
266         kDebug() << "// Export file: " << exportFile << " IS BROKEN";
267         return;
268     }
269
270     int i = 0;
271     QString groupName;
272     QString profileName;
273     QString extension;
274     QString prof_extension;
275     QString renderer;
276     QString params;
277     QString standard;
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);
286
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");
302             n = n.nextSibling();
303         }
304
305         i++;
306     }
307 }
308
309
310
311 #include "renderwidget.moc"
312
313