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