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