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