]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
Add ability to enable or disable all effects in the effect stack at once:
[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 "docclipbase.h"
24 #include "kdenlivesettings.h"
25
26 #include <KDebug>
27 #include <KLocale>
28 #include <KMessageBox>
29 #include <KStandardDirs>
30
31 #include <QMenu>
32 #include <QTextStream>
33 #include <QFile>
34 #include <QInputDialog>
35
36
37 EffectStackView::EffectStackView(QWidget *parent) :
38         QWidget(parent)
39 {
40     m_ui.setupUi(this);
41     QVBoxLayout *vbox1 = new QVBoxLayout(m_ui.frame);
42     m_effectedit = new EffectStackEdit(m_ui.frame);
43     vbox1->setContentsMargins(0, 0, 0, 0);
44     vbox1->setSpacing(0);
45     vbox1->addWidget(m_effectedit);
46     m_ui.frame->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
47     //m_ui.effectlist->horizontalHeader()->setVisible(false);
48     //m_ui.effectlist->verticalHeader()->setVisible(false);
49     m_clipref = NULL;
50
51     m_ui.buttonNew->setIcon(KIcon("document-new"));
52     m_ui.buttonNew->setToolTip(i18n("Add new effect"));
53     m_ui.buttonUp->setIcon(KIcon("go-up"));
54     m_ui.buttonUp->setToolTip(i18n("Move effect up"));
55     m_ui.buttonDown->setIcon(KIcon("go-down"));
56     m_ui.buttonDown->setToolTip(i18n("Move effect down"));
57     m_ui.buttonDel->setIcon(KIcon("edit-delete"));
58     m_ui.buttonDel->setToolTip(i18n("Delete effect"));
59     m_ui.buttonSave->setIcon(KIcon("document-save"));
60     m_ui.buttonSave->setToolTip(i18n("Save effect"));
61     m_ui.buttonReset->setIcon(KIcon("view-refresh"));
62     m_ui.buttonReset->setToolTip(i18n("Reset effect"));
63     m_ui.checkAll->setToolTip(i18n("Enable/Disable all effects"));
64
65
66     m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop); //use internal if drop is recognised right
67
68     connect(m_ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
69     connect(m_ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
70     connect(m_ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
71     connect(m_ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
72     connect(m_ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
73     connect(m_ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
74     connect(m_ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
75     connect(m_ui.checkAll, SIGNAL(stateChanged(int)), this, SLOT(slotCheckAll(int)));
76     connect(m_effectedit, SIGNAL(parameterChanged(const QDomElement, const QDomElement)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement)));
77     connect(m_effectedit, SIGNAL(seekTimeline(int)), this , SLOT(slotSeekTimeline(int)));
78     m_effectLists["audio"] = &MainWindow::audioEffects;
79     m_effectLists["video"] = &MainWindow::videoEffects;
80     m_effectLists["custom"] = &MainWindow::customEffects;
81     m_ui.splitter->setStretchFactor(1, 10);
82     m_ui.splitter->setStretchFactor(0, 1);
83     setEnabled(false);
84 }
85
86 EffectStackView::~EffectStackView()
87 {
88     m_effectLists.clear();
89     delete m_effectedit;
90 }
91
92 void EffectStackView::updateTimecodeFormat()
93 {
94     m_effectedit->updateTimecodeFormat();
95 }
96
97 void EffectStackView::setMenu(QMenu *menu)
98 {
99     m_ui.buttonNew->setMenu(menu);
100 }
101
102 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
103 {
104     m_effectedit->updateProjectFormat(profile, t);
105 }
106
107 void EffectStackView::slotSaveEffect()
108 {
109     QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
110     if (name.isEmpty()) return;
111     QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
112     path = path + name + ".xml";
113     if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
114
115     int i = m_ui.effectlist->currentRow();
116     QDomDocument doc;
117     QDomElement effect = m_clipref->effectAt(i).cloneNode().toElement();
118     doc.appendChild(doc.importNode(effect, true));
119     effect = doc.firstChild().toElement();
120     effect.removeAttribute("kdenlive_ix");
121     effect.setAttribute("id", name);
122     effect.setAttribute("type", "custom");
123     QDomElement effectname = effect.firstChildElement("name");
124     effect.removeChild(effectname);
125     effectname = doc.createElement("name");
126     QDomText nametext = doc.createTextNode(name);
127     effectname.appendChild(nametext);
128     effect.insertBefore(effectname, QDomNode());
129     QDomElement effectprops = effect.firstChildElement("properties");
130     effectprops.setAttribute("id", name);
131     effectprops.setAttribute("type", "custom");
132
133     QFile file(path);
134     if (file.open(QFile::WriteOnly | QFile::Truncate)) {
135         QTextStream out(&file);
136         out << doc.toString();
137     }
138     file.close();
139     emit reloadEffects();
140 }
141
142 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
143 {
144     if (m_clipref)
145         emit updateClipEffect(m_clipref, old, e, m_ui.effectlist->currentRow());
146 }
147
148 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
149 {
150     if (c && !c->isEnabled()) return;
151     if (c && c == m_clipref) {
152         if (ix == -1) ix = m_ui.effectlist->currentRow();
153         //if (ix == -1 || ix == m_ui.effectlist->currentRow()) return;
154     } else {
155         m_clipref = c;
156         if (c) {
157             ix = c->selectedEffectIndex();
158             QString size = c->baseClip()->getProperty("frame_size");
159             double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
160             QPoint p((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
161             m_effectedit->setFrameSize(p);
162             m_effectedit->setFrameSize(p);
163         } else ix = 0;
164     }
165     if (m_clipref == NULL) {
166         m_ui.effectlist->blockSignals(true);
167         m_ui.effectlist->clear();
168         m_effectedit->transferParamDesc(QDomElement(), 0, 0);
169         m_ui.effectlist->blockSignals(false);
170         setEnabled(false);
171         return;
172     }
173     setEnabled(true);
174     setupListView(ix);
175 }
176
177 void EffectStackView::slotItemChanged(QListWidgetItem *item)
178 {
179     bool disable = true;
180     if (item->checkState() == Qt::Checked) disable = false;
181     m_ui.frame->setEnabled(!disable);
182     m_ui.buttonReset->setEnabled(!disable);
183     int activeRow = m_ui.effectlist->currentRow();
184     if (activeRow >= 0) {
185         m_effectedit->updateParameter("disable", QString::number((int) disable));
186         emit changeEffectState(m_clipref, activeRow, disable);
187     }
188     slotUpdateCheckAllButton();
189 }
190
191
192 void EffectStackView::setupListView(int ix)
193 {
194     m_ui.effectlist->blockSignals(true);
195     m_ui.effectlist->clear();
196
197     // Issue 238: Add icons for effect type in effectstack.
198     KIcon videoIcon("kdenlive-show-video");
199     KIcon audioIcon("kdenlive-show-audio");
200     KIcon customIcon("kdenlive-custom-effect");
201     QListWidgetItem* item;
202
203     for (int i = 0; i < m_clipref->effectsCount(); i++) {
204         const QDomElement d = m_clipref->effectAt(i);
205         if (d.isNull()) {
206             kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
207             continue;
208         }
209
210         /*QDomDocument doc;
211         doc.appendChild(doc.importNode(d, true));
212         kDebug() << "IMPORTED STK: " << doc.toString();*/
213
214         QDomNode namenode = d.elementsByTagName("name").item(0);
215         if (!namenode.isNull()) {
216             // Issue 238: Add icons for effect type in effectstack.
217             // Logic more or less copied from initeffects.cpp
218             QString type = d.attribute("type", QString());
219             if ("audio" == type) {
220                 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
221             } else if ("custom" == type) {
222                 item = new QListWidgetItem(customIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
223             } else {
224                 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
225             }
226             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
227             if (d.attribute("disable") == "1") item->setCheckState(Qt::Unchecked);
228             else item->setCheckState(Qt::Checked);
229         }
230     }
231     if (m_ui.effectlist->count() == 0) {
232         m_ui.buttonDel->setEnabled(false);
233         m_ui.buttonSave->setEnabled(false);
234         m_ui.buttonReset->setEnabled(false);
235         m_ui.buttonUp->setEnabled(false);
236         m_ui.buttonDown->setEnabled(false);
237         m_ui.checkAll->setEnabled(false);
238     } else {
239         if (ix < 0) ix = 0;
240         if (ix > m_ui.effectlist->count() - 1) ix = m_ui.effectlist->count() - 1;
241         m_ui.effectlist->setCurrentRow(ix);
242         m_ui.checkAll->setEnabled(true);
243     }
244     m_ui.effectlist->blockSignals(false);
245     if (m_ui.effectlist->count() == 0) m_effectedit->transferParamDesc(QDomElement(), 0, 0);
246     else slotItemSelectionChanged(false);
247     slotUpdateCheckAllButton();
248 }
249
250 void EffectStackView::slotItemSelectionChanged(bool update)
251 {
252     bool hasItem = m_ui.effectlist->currentItem();
253     int activeRow = m_ui.effectlist->currentRow();
254     bool isChecked = false;
255     if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
256     if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
257         m_effectedit->transferParamDesc(m_clipref->effectAt(activeRow),
258                                         m_clipref->cropStart().frames(KdenliveSettings::project_fps()),
259                                         m_clipref->cropDuration().frames(KdenliveSettings::project_fps())); //minx max frame
260     }
261     if (m_clipref && update) m_clipref->setSelectedEffect(activeRow);
262     m_ui.buttonDel->setEnabled(hasItem);
263     m_ui.buttonSave->setEnabled(hasItem);
264     m_ui.buttonReset->setEnabled(hasItem && isChecked);
265     m_ui.buttonUp->setEnabled(activeRow > 0);
266     m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
267     m_ui.frame->setEnabled(isChecked);
268 }
269
270 void EffectStackView::slotItemUp()
271 {
272     int activeRow = m_ui.effectlist->currentRow();
273     if (activeRow <= 0) return;
274     emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
275 }
276
277 void EffectStackView::slotItemDown()
278 {
279     int activeRow = m_ui.effectlist->currentRow();
280     if (activeRow >= m_ui.effectlist->count() - 1) return;
281     emit changeEffectPosition(m_clipref, activeRow + 1, activeRow + 2);
282 }
283
284 void EffectStackView::slotItemDel()
285 {
286     int activeRow = m_ui.effectlist->currentRow();
287     if (activeRow >= 0) {
288         emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
289         slotUpdateCheckAllButton();
290     }
291 }
292
293 void EffectStackView::slotResetEffect()
294 {
295     int activeRow = m_ui.effectlist->currentRow();
296     if (activeRow < 0) return;
297     QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
298     QDomElement dom;
299     QString effectName = m_ui.effectlist->currentItem()->text();
300     foreach(const QString &type, m_effectLists.keys()) {
301         EffectsList *list = m_effectLists[type];
302         if (list->effectNames().contains(effectName)) {
303             dom = list->getEffectByName(effectName).cloneNode().toElement();
304             break;
305         }
306     }
307     if (!dom.isNull()) {
308         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
309         m_clipref->initEffect(dom);
310         m_effectedit->transferParamDesc(dom, m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
311         emit updateClipEffect(m_clipref, old, dom, activeRow);
312     }
313 }
314
315
316 void EffectStackView::raiseWindow(QWidget* dock)
317 {
318     if (m_clipref && dock)
319         dock->raise();
320 }
321
322 void EffectStackView::clear()
323 {
324     m_ui.effectlist->blockSignals(true);
325     m_ui.effectlist->clear();
326     m_ui.buttonDel->setEnabled(false);
327     m_ui.buttonSave->setEnabled(false);
328     m_ui.buttonReset->setEnabled(false);
329     m_ui.buttonUp->setEnabled(false);
330     m_ui.buttonDown->setEnabled(false);
331     m_ui.checkAll->setEnabled(false);
332     m_effectedit->transferParamDesc(QDomElement(), 0, 0);
333     m_ui.effectlist->blockSignals(false);
334 }
335
336
337 void EffectStackView::slotSeekTimeline(int pos)
338 {
339     if (m_clipref)
340         emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
341 }
342
343 void EffectStackView::slotUpdateCheckAllButton()
344 {
345     bool hasEnabled = false;
346     bool hasDisabled = false;
347     for (int i = 0; i < m_ui.effectlist->count(); ++i) {
348         if (m_ui.effectlist->item(i)->checkState() == Qt::Checked)
349             hasEnabled = true;
350         else
351             hasDisabled = true;
352     }
353
354     m_ui.checkAll->blockSignals(true);
355     if (hasEnabled && hasDisabled)
356         m_ui.checkAll->setCheckState(Qt::PartiallyChecked);
357     else if (hasEnabled)
358         m_ui.checkAll->setCheckState(Qt::Checked);
359     else
360         m_ui.checkAll->setCheckState(Qt::Unchecked);
361     m_ui.checkAll->blockSignals(false);
362 }
363
364 void EffectStackView::slotCheckAll(int state)
365 {
366     if (state == 1) {
367         state = 2;
368         m_ui.checkAll->blockSignals(true);
369         m_ui.checkAll->setCheckState(Qt::Checked);
370         m_ui.checkAll->blockSignals(false);
371     }
372
373     for (int i = 0; i < m_ui.effectlist->count(); ++i)
374         m_ui.effectlist->item(i)->setCheckState((Qt::CheckState)state);
375 }
376
377 #include "effectstackview.moc"