]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
on-monitor editing: show controls next to the edit monitor
[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(0, 0, 0, 0);
53     vbox1->setSpacing(0);
54     vbox1->addWidget(m_effectedit);
55
56     //m_ui.region_url->fileDialog()->setFilter(ProjectList::getExtensions());
57     //m_ui.effectlist->horizontalHeader()->setVisible(false);
58     //m_ui.effectlist->verticalHeader()->setVisible(false);
59
60     m_ui.buttonNew->setIcon(KIcon("document-new"));
61     m_ui.buttonNew->setToolTip(i18n("Add new effect"));
62     m_ui.buttonUp->setIcon(KIcon("go-up"));
63     m_ui.buttonUp->setToolTip(i18n("Move effect up"));
64     m_ui.buttonDown->setIcon(KIcon("go-down"));
65     m_ui.buttonDown->setToolTip(i18n("Move effect down"));
66     m_ui.buttonDel->setIcon(KIcon("edit-delete"));
67     m_ui.buttonDel->setToolTip(i18n("Delete effect"));
68     m_ui.buttonSave->setIcon(KIcon("document-save"));
69     m_ui.buttonSave->setToolTip(i18n("Save effect"));
70     m_ui.buttonReset->setIcon(KIcon("view-refresh"));
71     m_ui.buttonReset->setToolTip(i18n("Reset effect"));
72     m_ui.checkAll->setToolTip(i18n("Enable/Disable all effects"));
73     m_ui.buttonShowComments->setIcon(KIcon("help-about"));
74     m_ui.buttonShowComments->setToolTip(i18n("Show additional information for the parameters"));
75
76     m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop); //use internal if drop is recognised right
77
78     m_ui.labelComment->setHidden(true);
79
80     //connect(m_ui.region_url, SIGNAL(urlSelected(const KUrl &)), this , SLOT(slotRegionChanged()));
81     //connect(m_ui.region_url, SIGNAL(returnPressed()), this , SLOT(slotRegionChanged()));
82     connect(m_ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
83     connect(m_ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
84     connect(m_ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
85     connect(m_ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
86     connect(m_ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
87     connect(m_ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
88     connect(m_ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
89     connect(m_ui.checkAll, SIGNAL(stateChanged(int)), this, SLOT(slotCheckAll(int)));
90     connect(m_ui.buttonShowComments, SIGNAL(clicked()), this, SLOT(slotShowComments()));
91     connect(m_effectedit, SIGNAL(parameterChanged(const QDomElement, const QDomElement)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement)));
92     connect(m_effectedit, SIGNAL(seekTimeline(int)), this , SLOT(slotSeekTimeline(int)));
93     connect(m_effectedit, SIGNAL(displayMessage(const QString&, int)), this, SIGNAL(displayMessage(const QString&, int)));
94     connect(m_effectedit, SIGNAL(checkMonitorPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
95     connect(monitor, SIGNAL(renderPosition(int)), this, SLOT(slotRenderPos(int)));
96     connect(this, SIGNAL(showComments(bool)), m_effectedit, SIGNAL(showComments(bool)));
97     m_effectLists["audio"] = &MainWindow::audioEffects;
98     m_effectLists["video"] = &MainWindow::videoEffects;
99     m_effectLists["custom"] = &MainWindow::customEffects;
100     m_ui.splitter->setStretchFactor(1, 10);
101     m_ui.splitter->setStretchFactor(0, 1);
102
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() > 20) {
181                 m_ui.checkAll->setToolTip(i18n("Effects for %1").arg(cname));
182                 cname.truncate(17);
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         m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
201         //m_ui.region_url->clear();
202         m_ui.effectlist->blockSignals(false);
203         m_ui.checkAll->setToolTip(QString());
204         m_ui.checkAll->setText(QString());
205         m_ui.labelComment->setText(QString());
206         setEnabled(false);
207         return;
208     }
209     setEnabled(true);
210     m_trackMode = false;
211     m_currentEffectList = m_clipref->effectList();
212     setupListView(ix);
213 }
214
215 void EffectStackView::slotTrackItemSelected(int ix, const TrackInfo info)
216 {
217     m_clipref = NULL;
218     m_trackMode = true;
219     m_currentEffectList = info.effectsList;
220     m_trackInfo = info;
221     kDebug() << "// TRACK; " << ix << ", EFFECTS: " << m_currentEffectList.count();
222     setEnabled(true);
223     m_ui.checkAll->setToolTip(QString());
224     m_ui.checkAll->setText(i18n("Effects for track %1").arg(info.trackName.isEmpty() ? QString::number(ix) : info.trackName));
225     m_trackindex = ix;
226     setupListView(0);
227 }
228
229 void EffectStackView::slotItemChanged(QListWidgetItem *item)
230 {
231     bool disable = true;
232     if (item->checkState() == Qt::Checked) disable = false;
233     m_ui.frame->setEnabled(!disable);
234     m_ui.buttonReset->setEnabled(!disable);
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         QDomNode namenode = d.elementsByTagName("name").item(0);
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.toElement().text().toUtf8().data()), m_ui.effectlist);
276             } else if ("custom" == type) {
277                 item = new QListWidgetItem(customIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
278             } else {
279                 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().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         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     QDomElement eff;
317     if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
318         eff = m_currentEffectList.at(activeRow);
319         if (m_trackMode) {
320             // showing track effects
321             m_effectedit->transferParamDesc(eff, 0, 0, m_trackInfo.duration);
322         } else m_effectedit->transferParamDesc(eff,
323                                                    0,
324                                                    m_clipref->cropStart().frames(KdenliveSettings::project_fps()),
325                                                    (m_clipref->cropStart() + m_clipref->cropDuration()).frames(KdenliveSettings::project_fps()) - 1); //minx max frame
326         //m_ui.region_url->setUrl(KUrl(eff.attribute("region")));
327         m_ui.labelComment->setText(i18n(eff.firstChildElement("description").firstChildElement("full").text().toUtf8().data()));
328     }
329     if (!m_trackMode && m_clipref && update) m_clipref->setSelectedEffect(activeRow);
330     m_ui.buttonDel->setEnabled(hasItem);
331     m_ui.buttonSave->setEnabled(hasItem);
332     m_ui.buttonReset->setEnabled(hasItem && isChecked);
333     m_ui.buttonUp->setEnabled(activeRow > 0);
334     m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
335     m_ui.frame->setEnabled(isChecked);
336     m_ui.buttonShowComments->setEnabled(hasItem);
337
338     emit showComments(m_ui.buttonShowComments->isChecked());
339     m_ui.labelComment->setVisible(hasItem && m_ui.labelComment->text().count() && (m_ui.buttonShowComments->isChecked() || !eff.elementsByTagName("parameter").count()));
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     emit showComments(m_ui.buttonShowComments->isChecked());
400     m_ui.labelComment->setHidden(!m_ui.buttonShowComments->isChecked() || !m_ui.labelComment->text().count());
401 }
402
403
404 void EffectStackView::raiseWindow(QWidget* dock)
405 {
406     if ((m_clipref || m_trackMode) && dock)
407         dock->raise();
408 }
409
410 void EffectStackView::clear()
411 {
412     m_ui.effectlist->blockSignals(true);
413     m_ui.effectlist->clear();
414     m_ui.buttonDel->setEnabled(false);
415     m_ui.buttonSave->setEnabled(false);
416     m_ui.buttonReset->setEnabled(false);
417     m_ui.buttonUp->setEnabled(false);
418     m_ui.buttonDown->setEnabled(false);
419     m_ui.checkAll->setEnabled(false);
420     m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
421     //m_ui.region_url->clear();
422     m_ui.buttonShowComments->setEnabled(false);
423     m_ui.labelComment->setText(QString());
424     m_ui.effectlist->blockSignals(false);
425 }
426
427
428 void EffectStackView::slotSeekTimeline(int pos)
429 {
430     if (m_trackMode) {
431         emit seekTimeline(pos);
432     } else if (m_clipref) {
433         emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
434     }
435 }
436
437 void EffectStackView::slotUpdateCheckAllButton()
438 {
439     bool hasEnabled = false;
440     bool hasDisabled = false;
441     for (int i = 0; i < m_ui.effectlist->count(); ++i) {
442         if (m_ui.effectlist->item(i)->checkState() == Qt::Checked)
443             hasEnabled = true;
444         else
445             hasDisabled = true;
446     }
447
448     m_ui.checkAll->blockSignals(true);
449     if (hasEnabled && hasDisabled)
450         m_ui.checkAll->setCheckState(Qt::PartiallyChecked);
451     else if (hasEnabled)
452         m_ui.checkAll->setCheckState(Qt::Checked);
453     else
454         m_ui.checkAll->setCheckState(Qt::Unchecked);
455     m_ui.checkAll->blockSignals(false);
456 }
457
458 void EffectStackView::slotCheckAll(int state)
459 {
460     if (state == 1) {
461         state = 2;
462         m_ui.checkAll->blockSignals(true);
463         m_ui.checkAll->setCheckState(Qt::Checked);
464         m_ui.checkAll->blockSignals(false);
465     }
466
467     bool disabled = (state != 2);
468     m_effectedit->updateParameter("disable", QString::number((int) disabled));
469     for (int i = 0; i < m_ui.effectlist->count(); ++i) {
470         if (m_ui.effectlist->item(i)->checkState() != (Qt::CheckState)state) {
471             m_ui.effectlist->item(i)->setCheckState((Qt::CheckState)state);
472             if (m_trackMode)
473                 emit changeEffectState(NULL, m_trackindex, i, disabled);
474             else
475                 emit changeEffectState(m_clipref, -1, i, disabled);
476         }
477     }
478 }
479
480 /*void EffectStackView::slotRegionChanged()
481 {
482     if (!m_trackMode) emit updateClipRegion(m_clipref, m_ui.effectlist->currentRow(), m_ui.region_url->text());
483 }*/
484
485 void EffectStackView::slotCheckMonitorPosition(int renderPos)
486 {
487     if (m_trackMode || (renderPos >= m_clipref->startPos().frames(KdenliveSettings::project_fps()) && renderPos <= m_clipref->endPos().frames(KdenliveSettings::project_fps()))) {
488         if (!m_monitor->getEffectEdit()->getScene()->views().at(0)->isVisible())
489             m_monitor->slotEffectScene(true);
490     } else {
491         m_monitor->slotEffectScene(false);
492     }
493 }
494
495 void EffectStackView::slotRenderPos(int pos)
496 {
497     if (m_effectedit) {
498         if (m_trackMode) {
499             m_effectedit->slotSyncEffectsPos(pos);
500         } else if (m_clipref) {
501             m_effectedit->slotSyncEffectsPos(pos - m_clipref->startPos().frames(KdenliveSettings::project_fps()));
502         }
503     }
504 }
505
506 int EffectStackView::isTrackMode(bool *ok) const
507 {
508     *ok = m_trackMode;
509     return m_trackindex;
510 }
511
512 void EffectStackView::slotShowComments()
513 {
514     m_ui.labelComment->setHidden(!m_ui.buttonShowComments->isChecked() || !m_ui.labelComment->text().count());
515     emit showComments(m_ui.buttonShowComments->isChecked());
516 }
517
518 #include "effectstackview.moc"