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