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