]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
Disable effect parameters if effect is disabled
[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     m_ui.setupUi(this);
40     m_effectedit = new EffectStackEdit(m_ui.frame);
41     //m_ui.effectlist->horizontalHeader()->setVisible(false);
42     //m_ui.effectlist->verticalHeader()->setVisible(false);
43     m_clipref = NULL;
44
45     m_ui.buttonNew->setIcon(KIcon("document-new"));
46     m_ui.buttonNew->setToolTip(i18n("Add new effect"));
47     m_ui.buttonUp->setIcon(KIcon("go-up"));
48     m_ui.buttonUp->setToolTip(i18n("Move effect up"));
49     m_ui.buttonDown->setIcon(KIcon("go-down"));
50     m_ui.buttonDown->setToolTip(i18n("Move effect down"));
51     m_ui.buttonDel->setIcon(KIcon("edit-delete"));
52     m_ui.buttonDel->setToolTip(i18n("Delete effect"));
53     m_ui.buttonSave->setIcon(KIcon("document-save"));
54     m_ui.buttonSave->setToolTip(i18n("Save effect"));
55     m_ui.buttonReset->setIcon(KIcon("view-refresh"));
56     m_ui.buttonReset->setToolTip(i18n("Reset effect"));
57
58
59     m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop);//use internal if drop is recognised right
60
61     connect(m_ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
62     connect(m_ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
63     connect(m_ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
64     connect(m_ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
65     connect(m_ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
66     connect(m_ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
67     connect(m_ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
68     //connect(this, SIGNAL(transferParamDesc(const QDomElement&, int , int)), m_effectedit , SLOT(transferParamDesc(const QDomElement&, int , int)));
69     connect(m_effectedit, SIGNAL(parameterChanged(const QDomElement&, const QDomElement&)), this , SLOT(slotUpdateEffectParams(const QDomElement&, const QDomElement&)));
70     m_effectLists["audio"] = &MainWindow::audioEffects;
71     m_effectLists["video"] = &MainWindow::videoEffects;
72     m_effectLists["custom"] = &MainWindow::customEffects;
73     m_ui.splitter->setStretchFactor(1, 10);
74     m_ui.splitter->setStretchFactor(0, 1);
75     setEnabled(false);
76 }
77
78 void EffectStackView::setMenu(QMenu *menu)
79 {
80     m_ui.buttonNew->setMenu(menu);
81 }
82
83 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
84 {
85     m_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 = m_ui.effectlist->currentRow();
97     QDomDocument doc;
98     QDomElement effect = m_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 (m_clipref)
127         emit updateClipEffect(m_clipref, old, e, m_ui.effectlist->currentRow());
128 }
129
130 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
131 {
132     if (c && c == m_clipref) {
133         if (ix == -1) ix = m_ui.effectlist->currentRow();
134     } else {
135         m_clipref = c;
136         if (c) ix = c->selectedEffectIndex();
137         else ix = 0;
138     }
139     if (m_clipref == NULL) {
140         m_ui.effectlist->clear();
141         m_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     m_ui.frame->setEnabled(!disable);
154     m_ui.buttonReset->setEnabled(!disable);
155     int activeRow = m_ui.effectlist->currentRow();
156     if (activeRow >= 0) {
157         emit changeEffectState(m_clipref, activeRow, disable);
158     }
159 }
160
161
162 void EffectStackView::setupListView(int ix)
163 {
164     m_ui.effectlist->clear();
165
166     // Issue 238: Add icons for effect type in effectstack.
167     KIcon videoIcon("kdenlive-show-video");
168     KIcon audioIcon("kdenlive-show-audio");
169     QListWidgetItem* item;
170
171     for (int i = 0; i < m_clipref->effectsCount(); i++) {
172         QDomElement d = m_clipref->effectAt(i);
173
174         QDomNode namenode = d.elementsByTagName("name").item(0);
175         if (!namenode.isNull()) {
176             // Issue 238: Add icons for effect type in effectstack.
177             // Logic more or less copied from initeffects.cpp
178             QString type = d.attribute("type", QString());
179             if ("audio" == type) {
180                 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
181             } else if ("custom" == type) {
182                 item = new QListWidgetItem(i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
183             } else {
184                 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
185             }
186             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
187             if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
188             else item->setCheckState(Qt::Checked);
189         }
190     }
191     if (m_clipref->effectsCount() == 0) {
192         m_effectedit->transferParamDesc(QDomElement(), 0, 0);
193         m_ui.buttonDel->setEnabled(false);
194         m_ui.buttonSave->setEnabled(false);
195         m_ui.buttonReset->setEnabled(false);
196         m_ui.buttonUp->setEnabled(false);
197         m_ui.buttonDown->setEnabled(false);
198     } else {
199         if (ix < 0) ix = 0;
200         if (ix > m_ui.effectlist->count() - 1) ix = m_ui.effectlist->count() - 1;
201         m_ui.effectlist->setCurrentRow(ix);
202         m_ui.buttonDel->setEnabled(true);
203         m_ui.buttonSave->setEnabled(true);
204         m_ui.buttonReset->setEnabled(true);
205         m_ui.buttonUp->setEnabled(ix > 0);
206         m_ui.buttonDown->setEnabled(ix < m_clipref->effectsCount() - 1);
207     }
208 }
209
210 void EffectStackView::slotItemSelectionChanged()
211 {
212     bool hasItem = m_ui.effectlist->currentItem();
213     int activeRow = m_ui.effectlist->currentRow();
214     bool isChecked = false;
215     if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
216     if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
217         m_effectedit->transferParamDesc(m_clipref->effectAt(activeRow), m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
218     }
219     if (m_clipref) m_clipref->setSelectedEffect(activeRow);
220     m_ui.buttonDel->setEnabled(hasItem);
221     m_ui.buttonSave->setEnabled(hasItem);
222     m_ui.buttonReset->setEnabled(hasItem && isChecked);
223     m_ui.buttonUp->setEnabled(activeRow > 0);
224     m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
225     m_ui.frame->setEnabled(isChecked);
226 }
227
228 void EffectStackView::slotItemUp()
229 {
230     int activeRow = m_ui.effectlist->currentRow();
231     if (activeRow <= 0) return;
232     emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
233 }
234
235 void EffectStackView::slotItemDown()
236 {
237     int activeRow = m_ui.effectlist->currentRow();
238     if (activeRow >= m_ui.effectlist->count() - 1) return;
239     emit changeEffectPosition(m_clipref, activeRow + 1, activeRow + 2);
240 }
241
242 void EffectStackView::slotItemDel()
243 {
244     int activeRow = m_ui.effectlist->currentRow();
245     if (activeRow >= 0) {
246         emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
247     }
248 }
249
250 void EffectStackView::slotResetEffect()
251 {
252     int activeRow = m_ui.effectlist->currentRow();
253     if (activeRow < 0) return;
254     QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
255     QDomElement dom;
256     QString effectName = m_ui.effectlist->currentItem()->text();
257     foreach(const QString &type, m_effectLists.keys()) {
258         EffectsList *list = m_effectLists[type];
259         if (list->effectNames().contains(effectName)) {
260             dom = list->getEffectByName(effectName).cloneNode().toElement();
261             break;
262         }
263     }
264     if (!dom.isNull()) {
265         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
266         m_clipref->initEffect(dom);
267         m_effectedit->transferParamDesc(dom, m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
268         emit updateClipEffect(m_clipref, old, dom, activeRow);
269     }
270 }
271
272
273 void EffectStackView::raiseWindow(QWidget* dock)
274 {
275     if (m_clipref && dock)
276         dock->raise();
277 }
278
279 void EffectStackView::clear()
280 {
281     m_ui.effectlist->clear();
282     m_ui.buttonDel->setEnabled(false);
283     m_ui.buttonSave->setEnabled(false);
284     m_ui.buttonReset->setEnabled(false);
285     m_ui.buttonUp->setEnabled(false);
286     m_ui.buttonDown->setEnabled(false);
287     m_effectedit->transferParamDesc(QDomElement(), 0, 0);
288 }
289
290 #include "effectstackview.moc"