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