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