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