]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
Reindent the codebase using 'linux' bracket placement.
[kdenlive] / src / effectstackview.cpp
1 /***************************************************************************
2                           effecstackview.cpp  -  description
3                              -------------------
4     begin                : Feb 15 2008
5     copyright            : (C) 2008 by Marco Gittler
6     email                : g.marco@freenet.de
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18
19 #include "effectstackview.h"
20 #include "effectslist.h"
21 #include "clipitem.h"
22 #include "mainwindow.h"
23 #include "kdenlivesettings.h"
24
25 #include <KDebug>
26 #include <KLocale>
27 #include <KMessageBox>
28 #include <KStandardDirs>
29
30 #include <QMenu>
31 #include <QTextStream>
32 #include <QFile>
33 #include <QInputDialog>
34
35
36 EffectStackView::EffectStackView(QWidget *parent)
37         : QWidget(parent)
38 {
39     ui.setupUi(this);
40     effectedit = new EffectStackEdit(ui.frame);
41     //ui.effectlist->horizontalHeader()->setVisible(false);
42     //ui.effectlist->verticalHeader()->setVisible(false);
43     clipref = NULL;
44
45     ui.buttonNew->setIcon(KIcon("document-new"));
46     ui.buttonNew->setToolTip(i18n("Add new effect"));
47     ui.buttonUp->setIcon(KIcon("go-up"));
48     ui.buttonUp->setToolTip(i18n("Move effect up"));
49     ui.buttonDown->setIcon(KIcon("go-down"));
50     ui.buttonDown->setToolTip(i18n("Move effect down"));
51     ui.buttonDel->setIcon(KIcon("trash-empty"));
52     ui.buttonDel->setToolTip(i18n("Delete effect"));
53     ui.buttonSave->setIcon(KIcon("document-save"));
54     ui.buttonSave->setToolTip(i18n("Save effect"));
55     ui.buttonReset->setIcon(KIcon("view-refresh"));
56     ui.buttonReset->setToolTip(i18n("Reset effect"));
57
58
59     ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop);//use internal if drop is recognised right
60
61     connect(ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
62     connect(ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
63     connect(ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
64     connect(ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
65     connect(ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
66     connect(ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
67     connect(ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
68     connect(this, SIGNAL(transferParamDesc(const QDomElement&, int , int)), effectedit , SLOT(transferParamDesc(const QDomElement&, int , int)));
69     connect(effectedit, SIGNAL(parameterChanged(const QDomElement&, const QDomElement&)), this , SLOT(slotUpdateEffectParams(const QDomElement&, const QDomElement&)));
70     effectLists["audio"] = &MainWindow::audioEffects;
71     effectLists["video"] = &MainWindow::videoEffects;
72     effectLists["custom"] = &MainWindow::customEffects;
73     ui.splitter->setStretchFactor(1, 10);
74     ui.splitter->setStretchFactor(0, 1);
75     setEnabled(false);
76 }
77
78 void EffectStackView::setMenu(QMenu *menu)
79 {
80     ui.buttonNew->setMenu(menu);
81 }
82
83 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
84 {
85     effectedit->updateProjectFormat(profile, t);
86 }
87
88 void EffectStackView::slotSaveEffect()
89 {
90     QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
91     if (name.isEmpty()) return;
92     QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
93     path = path + name + ".xml";
94     if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
95
96     int i = ui.effectlist->currentRow();
97     QDomDocument doc;
98     QDomElement effect = clipref->effectAt(i).cloneNode().toElement();
99     doc.appendChild(doc.importNode(effect, true));
100     effect = doc.firstChild().toElement();
101     effect.removeAttribute("kdenlive_ix");
102     effect.setAttribute("id", name);
103     effect.setAttribute("type", "custom");
104     QDomElement effectname = effect.firstChildElement("name");
105     effect.removeChild(effectname);
106     effectname = doc.createElement("name");
107     QDomText nametext = doc.createTextNode(name);
108     effectname.appendChild(nametext);
109     effect.insertBefore(effectname, QDomNode());
110     QDomElement effectprops = effect.firstChildElement("properties");
111     effectprops.setAttribute("id", name);
112     effectprops.setAttribute("type", "custom");
113
114
115     QFile file(path);
116     if (file.open(QFile::WriteOnly | QFile::Truncate)) {
117         QTextStream out(&file);
118         out << doc.toString();
119     }
120     file.close();
121     emit reloadEffects();
122 }
123
124 void EffectStackView::slotUpdateEffectParams(const QDomElement& old, const QDomElement& e)
125 {
126     if (clipref)
127         emit updateClipEffect(clipref, old, e, ui.effectlist->currentRow());
128 }
129
130 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
131 {
132     if (c && c == clipref) {
133         if (ix == -1) ix = ui.effectlist->currentRow();
134     } else {
135         clipref = c;
136         if (c) ix = c->selectedEffectIndex();
137         else ix = 0;
138     }
139     if (clipref == NULL) {
140         ui.effectlist->clear();
141         effectedit->transferParamDesc(QDomElement(), 0, 0);
142         setEnabled(false);
143         return;
144     }
145     setEnabled(true);
146     setupListView(ix);
147 }
148
149 void EffectStackView::slotItemChanged(QListWidgetItem *item)
150 {
151     bool disable = true;
152     if (item->checkState() == Qt::Checked) disable = false;
153     ui.buttonReset->setEnabled(!disable);
154     int activeRow = ui.effectlist->currentRow();
155     if (activeRow >= 0) {
156         emit changeEffectState(clipref, activeRow, disable);
157     }
158 }
159
160
161 void EffectStackView::setupListView(int ix)
162 {
163     ui.effectlist->clear();
164
165     // Issue 238: Add icons for effect type in effectstack.
166     KIcon videoIcon("kdenlive-show-video");
167     KIcon audioIcon("kdenlive-show-audio");
168     QListWidgetItem* item;
169
170     for (int i = 0;i < clipref->effectsCount();i++) {
171         QDomElement d = clipref->effectAt(i);
172
173         QDomNode namenode = d.elementsByTagName("name").item(0);
174         if (!namenode.isNull()) {
175             // Issue 238: Add icons for effect type in effectstack.
176             // Logic more or less copied from initeffects.cpp
177             QString type = d.attribute("type", QString());
178             if ("audio" == type) {
179                 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
180             } else if ("custom" == type) {
181                 item = new QListWidgetItem(i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
182             } else {
183                 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
184             }
185             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
186             if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
187             else item->setCheckState(Qt::Checked);
188         }
189     }
190     if (clipref->effectsCount() == 0) {
191         emit transferParamDesc(QDomElement(), 0, 0);
192         ui.buttonDel->setEnabled(false);
193         ui.buttonSave->setEnabled(false);
194         ui.buttonReset->setEnabled(false);
195         ui.buttonUp->setEnabled(false);
196         ui.buttonDown->setEnabled(false);
197     } else {
198         if (ix < 0) ix = 0;
199         if (ix > ui.effectlist->count() - 1) ix = ui.effectlist->count() - 1;
200         ui.effectlist->setCurrentRow(ix);
201         ui.buttonDel->setEnabled(true);
202         ui.buttonSave->setEnabled(true);
203         ui.buttonReset->setEnabled(true);
204         ui.buttonUp->setEnabled(ix > 0);
205         ui.buttonDown->setEnabled(ix < clipref->effectsCount() - 1);
206     }
207 }
208
209 void EffectStackView::slotItemSelectionChanged()
210 {
211     bool hasItem = ui.effectlist->currentItem();
212     int activeRow = ui.effectlist->currentRow();
213     bool isChecked = false;
214     if (hasItem && ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
215     if (hasItem && ui.effectlist->currentItem()->isSelected()) {
216         emit transferParamDesc(clipref->effectAt(activeRow), clipref->cropStart().frames(KdenliveSettings::project_fps()), clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
217     }
218     if (clipref) clipref->setSelectedEffect(activeRow);
219     ui.buttonDel->setEnabled(hasItem);
220     ui.buttonSave->setEnabled(hasItem);
221     ui.buttonReset->setEnabled(hasItem && isChecked);
222     ui.buttonUp->setEnabled(activeRow > 0);
223     ui.buttonDown->setEnabled((activeRow < ui.effectlist->count() - 1) && hasItem);
224 }
225
226 void EffectStackView::slotItemUp()
227 {
228     int activeRow = ui.effectlist->currentRow();
229     if (activeRow <= 0) return;
230     emit changeEffectPosition(clipref, activeRow + 1, activeRow);
231 }
232
233 void EffectStackView::slotItemDown()
234 {
235     int activeRow = ui.effectlist->currentRow();
236     if (activeRow >= ui.effectlist->count() - 1) return;
237     emit changeEffectPosition(clipref, activeRow + 1, activeRow + 2);
238 }
239
240 void EffectStackView::slotItemDel()
241 {
242     int activeRow = ui.effectlist->currentRow();
243     if (activeRow >= 0) {
244         emit removeEffect(clipref, clipref->effectAt(activeRow));
245     }
246 }
247
248 void EffectStackView::slotResetEffect()
249 {
250     int activeRow = ui.effectlist->currentRow();
251     if (activeRow < 0) return;
252     QDomElement old = clipref->effectAt(activeRow).cloneNode().toElement();
253     QDomElement dom;
254     QString effectName = ui.effectlist->currentItem()->text();
255     foreach(const QString &type, effectLists.keys()) {
256         EffectsList *list = effectLists[type];
257         if (list->effectNames().contains(effectName)) {
258             dom = list->getEffectByName(effectName);
259             break;
260         }
261     }
262     if (!dom.isNull()) {
263         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
264         emit transferParamDesc(dom, clipref->cropStart().frames(KdenliveSettings::project_fps()), clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
265         emit updateClipEffect(clipref, old, dom, activeRow);
266     }
267 }
268
269
270 void EffectStackView::raiseWindow(QWidget* dock)
271 {
272     if (clipref && dock)
273         dock->raise();
274 }
275
276 void EffectStackView::clear()
277 {
278     ui.effectlist->clear();
279     ui.buttonDel->setEnabled(false);
280     ui.buttonSave->setEnabled(false);
281     ui.buttonReset->setEnabled(false);
282     ui.buttonUp->setEnabled(false);
283     ui.buttonDown->setEnabled(false);
284     effectedit->transferParamDesc(QDomElement(), 0, 0);
285 }
286
287 #include "effectstackview.moc"