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