]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
Fix effect parameters disabled when they should not be
[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.buttonReset->setEnabled(!disable);
236     int activeRow = m_ui.effectlist->currentRow();
237     if (activeRow >= 0) {
238         m_effectedit->updateParameter("disable", QString::number((int) disable));
239         if (m_trackMode)
240             emit changeEffectState(NULL, m_trackindex, activeRow, disable);
241         else
242             emit changeEffectState(m_clipref, -1, activeRow, disable);
243     }
244     slotUpdateCheckAllButton();
245 }
246
247
248 void EffectStackView::setupListView(int ix)
249 {
250     m_ui.effectlist->blockSignals(true);
251     m_ui.effectlist->clear();
252
253     // Issue 238: Add icons for effect type in effectstack.
254     KIcon videoIcon("kdenlive-show-video");
255     KIcon audioIcon("kdenlive-show-audio");
256     KIcon customIcon("kdenlive-custom-effect");
257     QListWidgetItem* item;
258
259     for (int i = 0; i < m_currentEffectList.count(); i++) {
260         const QDomElement d = m_currentEffectList.at(i).cloneNode().toElement();
261         if (d.isNull()) {
262             kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
263             continue;
264         }
265
266         /*QDomDocument doc;
267         doc.appendChild(doc.importNode(d, true));
268         kDebug() << "IMPORTED STK: " << doc.toString();*/
269
270         QDomNode namenode = d.elementsByTagName("name").item(0);
271         if (!namenode.isNull()) {
272             // Issue 238: Add icons for effect type in effectstack.
273             // Logic more or less copied from initeffects.cpp
274             QString type = d.attribute("type", QString());
275             if ("audio" == type) {
276                 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
277             } else if ("custom" == type) {
278                 item = new QListWidgetItem(customIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
279             } else {
280                 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
281             }
282             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
283             if (d.attribute("disable") == "1")
284                 item->setCheckState(Qt::Unchecked);
285             else
286                 item->setCheckState(Qt::Checked);
287         }
288     }
289     if (m_ui.effectlist->count() == 0) {
290         m_ui.buttonDel->setEnabled(false);
291         m_ui.buttonSave->setEnabled(false);
292         m_ui.buttonReset->setEnabled(false);
293         m_ui.buttonUp->setEnabled(false);
294         m_ui.buttonDown->setEnabled(false);
295         m_ui.checkAll->setEnabled(false);
296         m_ui.buttonShowComments->setEnabled(false);
297         m_ui.labelComment->setHidden(true);
298     } else {
299         ix = qBound(0, ix, m_ui.effectlist->count() - 1);
300         m_ui.effectlist->setCurrentRow(ix);
301         m_ui.checkAll->setEnabled(true);
302     }
303     m_ui.effectlist->blockSignals(false);
304     if (m_ui.effectlist->count() == 0) {
305         m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
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             m_effectedit->transferParamDesc(eff, 0, 0, m_trackInfo.duration);
323         } else m_effectedit->transferParamDesc(eff,
324                                                    0,
325                                                    m_clipref->cropStart().frames(KdenliveSettings::project_fps()),
326                                                    (m_clipref->cropStart() + m_clipref->cropDuration()).frames(KdenliveSettings::project_fps()) - 1); //minx max frame
327         //m_ui.region_url->setUrl(KUrl(eff.attribute("region")));
328         m_ui.labelComment->setText(i18n(eff.firstChildElement("description").firstChildElement("full").text().toUtf8().data()));
329     }
330     if (!m_trackMode && m_clipref && update) m_clipref->setSelectedEffect(activeRow);
331     m_ui.buttonDel->setEnabled(hasItem);
332     m_ui.buttonSave->setEnabled(hasItem);
333     m_ui.buttonReset->setEnabled(hasItem && isChecked);
334     m_ui.buttonUp->setEnabled(activeRow > 0);
335     m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
336     m_ui.frame->setEnabled(isChecked);
337     m_ui.buttonShowComments->setEnabled(hasItem);
338
339     emit showComments(m_ui.buttonShowComments->isChecked());
340     m_ui.labelComment->setVisible(hasItem && m_ui.labelComment->text().count() && (m_ui.buttonShowComments->isChecked() || !eff.elementsByTagName("parameter").count()));
341 }
342
343 void EffectStackView::slotItemUp()
344 {
345     int activeRow = m_ui.effectlist->currentRow();
346     if (activeRow <= 0) return;
347     if (m_trackMode) emit changeEffectPosition(NULL, m_trackindex, activeRow + 1, activeRow);
348     else emit changeEffectPosition(m_clipref, -1, activeRow + 1, activeRow);
349 }
350
351 void EffectStackView::slotItemDown()
352 {
353     int activeRow = m_ui.effectlist->currentRow();
354     if (activeRow >= m_ui.effectlist->count() - 1) return;
355     if (m_trackMode) emit changeEffectPosition(NULL, m_trackindex, activeRow + 1, activeRow + 2);
356     else emit changeEffectPosition(m_clipref, -1, activeRow + 1, activeRow + 2);
357 }
358
359 void EffectStackView::slotItemDel()
360 {
361     int activeRow = m_ui.effectlist->currentRow();
362     if (activeRow >= 0) {
363         if (m_trackMode)
364             emit removeEffect(NULL, m_trackindex, m_currentEffectList.at(activeRow).cloneNode().toElement());
365         else
366             emit removeEffect(m_clipref, -1, m_clipref->effectAt(activeRow));
367         slotUpdateCheckAllButton();
368     }
369 }
370
371 void EffectStackView::slotResetEffect()
372 {
373     int activeRow = m_ui.effectlist->currentRow();
374     if (activeRow < 0) return;
375     QDomElement old = m_currentEffectList.at(activeRow).cloneNode().toElement();
376     QDomElement dom;
377     QString effectName = m_ui.effectlist->currentItem()->text();
378     foreach(const QString &type, m_effectLists.keys()) {
379         EffectsList *list = m_effectLists[type];
380         if (list->effectNames().contains(effectName)) {
381             dom = list->getEffectByName(effectName).cloneNode().toElement();
382             break;
383         }
384     }
385     if (!dom.isNull()) {
386         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
387         if (m_trackMode) {
388             EffectsList::setParameter(dom, "in", QString::number(0));
389             EffectsList::setParameter(dom, "out", QString::number(m_trackInfo.duration));
390             m_effectedit->transferParamDesc(dom, 0, 0, m_trackInfo.duration);//minx max frame
391             emit updateEffect(NULL, m_trackindex, old, dom, activeRow);
392         } else {
393             m_clipref->initEffect(dom);
394             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
395             //m_ui.region_url->setUrl(KUrl(dom.attribute("region")));
396             emit updateEffect(m_clipref, -1, old, dom, activeRow);
397         }
398     }
399
400     emit showComments(m_ui.buttonShowComments->isChecked());
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 || (m_clipref && renderPos >= m_clipref->startPos().frames(KdenliveSettings::project_fps()) && renderPos <= m_clipref->endPos().frames(KdenliveSettings::project_fps()))) {
489         if (!m_monitor->getEffectEdit()->getScene()->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(m_ui.buttonShowComments->isChecked());
517 }
518
519 #include "effectstackview.moc"