]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
Several fixes for keyframes, fix bug in clip resize start undo and cleanup
[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
64
65     m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop);//use internal if drop is recognised right
66
67     connect(m_ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
68     connect(m_ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
69     connect(m_ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
70     connect(m_ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
71     connect(m_ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
72     connect(m_ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
73     connect(m_ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
74     connect(m_effectedit, SIGNAL(parameterChanged(const QDomElement, const QDomElement)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement)));
75     m_effectLists["audio"] = &MainWindow::audioEffects;
76     m_effectLists["video"] = &MainWindow::videoEffects;
77     m_effectLists["custom"] = &MainWindow::customEffects;
78     m_ui.splitter->setStretchFactor(1, 10);
79     m_ui.splitter->setStretchFactor(0, 1);
80     setEnabled(false);
81 }
82
83 EffectStackView::~EffectStackView()
84 {
85     m_effectLists.clear();
86     delete m_effectedit;
87 }
88
89 void EffectStackView::setMenu(QMenu *menu)
90 {
91     m_ui.buttonNew->setMenu(menu);
92 }
93
94 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
95 {
96     m_effectedit->updateProjectFormat(profile, t);
97 }
98
99 void EffectStackView::slotSaveEffect()
100 {
101     QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
102     if (name.isEmpty()) return;
103     QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
104     path = path + name + ".xml";
105     if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
106
107     int i = m_ui.effectlist->currentRow();
108     QDomDocument doc;
109     QDomElement effect = m_clipref->effectAt(i).cloneNode().toElement();
110     doc.appendChild(doc.importNode(effect, true));
111     effect = doc.firstChild().toElement();
112     effect.removeAttribute("kdenlive_ix");
113     effect.setAttribute("id", name);
114     effect.setAttribute("type", "custom");
115     QDomElement effectname = effect.firstChildElement("name");
116     effect.removeChild(effectname);
117     effectname = doc.createElement("name");
118     QDomText nametext = doc.createTextNode(name);
119     effectname.appendChild(nametext);
120     effect.insertBefore(effectname, QDomNode());
121     QDomElement effectprops = effect.firstChildElement("properties");
122     effectprops.setAttribute("id", name);
123     effectprops.setAttribute("type", "custom");
124
125
126     QFile file(path);
127     if (file.open(QFile::WriteOnly | QFile::Truncate)) {
128         QTextStream out(&file);
129         out << doc.toString();
130     }
131     file.close();
132     emit reloadEffects();
133 }
134
135 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
136 {
137     if (m_clipref)
138         emit updateClipEffect(m_clipref, old, e, m_ui.effectlist->currentRow());
139 }
140
141 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
142 {
143     if (c && c == m_clipref) {
144         if (ix == -1) ix = m_ui.effectlist->currentRow();
145     } else {
146         m_clipref = c;
147         if (c) {
148             ix = c->selectedEffectIndex();
149             QString size = c->baseClip()->getProperty("frame_size");
150             double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
151             QPoint p((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
152             m_effectedit->setFrameSize(p);
153             m_effectedit->setFrameSize(p);
154         } else ix = 0;
155     }
156     if (m_clipref == NULL) {
157         m_ui.effectlist->blockSignals(true);
158         m_ui.effectlist->clear();
159         m_effectedit->transferParamDesc(QDomElement(), 0, 0);
160         m_ui.effectlist->blockSignals(false);
161         setEnabled(false);
162         return;
163     }
164     setEnabled(true);
165     setupListView(ix);
166 }
167
168 void EffectStackView::slotItemChanged(QListWidgetItem *item)
169 {
170     bool disable = true;
171     if (item->checkState() == Qt::Checked) disable = false;
172     m_ui.frame->setEnabled(!disable);
173     m_ui.buttonReset->setEnabled(!disable);
174     int activeRow = m_ui.effectlist->currentRow();
175     if (activeRow >= 0) {
176         emit changeEffectState(m_clipref, activeRow, disable);
177     }
178 }
179
180
181 void EffectStackView::setupListView(int ix)
182 {
183     m_ui.effectlist->blockSignals(true);
184     m_ui.effectlist->clear();
185
186     // Issue 238: Add icons for effect type in effectstack.
187     KIcon videoIcon("kdenlive-show-video");
188     KIcon audioIcon("kdenlive-show-audio");
189     QListWidgetItem* item;
190
191     for (int i = 0; i < m_clipref->effectsCount(); i++) {
192         const QDomElement d = m_clipref->effectAt(i);
193         if (d.isNull()) {
194             kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
195             continue;
196         }
197
198         /*QDomDocument doc;
199         doc.appendChild(doc.importNode(d, true));
200         kDebug() << "IMPORTED STK: " << doc.toString();*/
201
202         QDomNode namenode = d.elementsByTagName("name").item(0);
203         if (!namenode.isNull()) {
204             // Issue 238: Add icons for effect type in effectstack.
205             // Logic more or less copied from initeffects.cpp
206             QString type = d.attribute("type", QString());
207             if ("audio" == type) {
208                 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
209             } else if ("custom" == type) {
210                 item = new QListWidgetItem(i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
211             } else {
212                 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
213             }
214             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
215             if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
216             else item->setCheckState(Qt::Checked);
217         }
218     }
219     if (m_ui.effectlist->count() == 0) {
220         m_ui.buttonDel->setEnabled(false);
221         m_ui.buttonSave->setEnabled(false);
222         m_ui.buttonReset->setEnabled(false);
223         m_ui.buttonUp->setEnabled(false);
224         m_ui.buttonDown->setEnabled(false);
225     } else {
226         if (ix < 0) ix = 0;
227         if (ix > m_ui.effectlist->count() - 1) ix = m_ui.effectlist->count() - 1;
228         m_ui.effectlist->setCurrentRow(ix);
229     }
230     m_ui.effectlist->blockSignals(false);
231     if (m_ui.effectlist->count() == 0) m_effectedit->transferParamDesc(QDomElement(), 0, 0);
232     else slotItemSelectionChanged(false);
233 }
234
235 void EffectStackView::slotItemSelectionChanged(bool update)
236 {
237     bool hasItem = m_ui.effectlist->currentItem();
238     int activeRow = m_ui.effectlist->currentRow();
239     bool isChecked = false;
240     if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
241     if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
242         m_effectedit->transferParamDesc(m_clipref->effectAt(activeRow), m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
243     }
244     if (m_clipref && update) m_clipref->setSelectedEffect(activeRow);
245     m_ui.buttonDel->setEnabled(hasItem);
246     m_ui.buttonSave->setEnabled(hasItem);
247     m_ui.buttonReset->setEnabled(hasItem && isChecked);
248     m_ui.buttonUp->setEnabled(activeRow > 0);
249     m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
250     m_ui.frame->setEnabled(isChecked);
251 }
252
253 void EffectStackView::slotItemUp()
254 {
255     int activeRow = m_ui.effectlist->currentRow();
256     if (activeRow <= 0) return;
257     emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
258 }
259
260 void EffectStackView::slotItemDown()
261 {
262     int activeRow = m_ui.effectlist->currentRow();
263     if (activeRow >= m_ui.effectlist->count() - 1) return;
264     emit changeEffectPosition(m_clipref, activeRow + 1, activeRow + 2);
265 }
266
267 void EffectStackView::slotItemDel()
268 {
269     int activeRow = m_ui.effectlist->currentRow();
270     if (activeRow >= 0) {
271         emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
272     }
273 }
274
275 void EffectStackView::slotResetEffect()
276 {
277     int activeRow = m_ui.effectlist->currentRow();
278     if (activeRow < 0) return;
279     QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
280     QDomElement dom;
281     QString effectName = m_ui.effectlist->currentItem()->text();
282     foreach(const QString &type, m_effectLists.keys()) {
283         EffectsList *list = m_effectLists[type];
284         if (list->effectNames().contains(effectName)) {
285             dom = list->getEffectByName(effectName).cloneNode().toElement();
286             break;
287         }
288     }
289     if (!dom.isNull()) {
290         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
291         m_clipref->initEffect(dom);
292         m_effectedit->transferParamDesc(dom, m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
293         emit updateClipEffect(m_clipref, old, dom, activeRow);
294     }
295 }
296
297
298 void EffectStackView::raiseWindow(QWidget* dock)
299 {
300     if (m_clipref && dock)
301         dock->raise();
302 }
303
304 void EffectStackView::clear()
305 {
306     m_ui.effectlist->blockSignals(true);
307     m_ui.effectlist->clear();
308     m_ui.buttonDel->setEnabled(false);
309     m_ui.buttonSave->setEnabled(false);
310     m_ui.buttonReset->setEnabled(false);
311     m_ui.buttonUp->setEnabled(false);
312     m_ui.buttonDown->setEnabled(false);
313     m_effectedit->transferParamDesc(QDomElement(), 0, 0);
314     m_ui.effectlist->blockSignals(false);
315 }
316
317 #include "effectstackview.moc"