]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
Prepare checking of removed / deleted files in a project:
[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 "kdenlivesettings.h"
25
26 #include <KDebug>
27 #include <KLocale>
28 #include <KMessageBox>
29 #include <KStandardDirs>
30
31 #include <QMenu>
32 #include <QTextStream>
33 #include <QFile>
34 #include <QInputDialog>
35
36
37 EffectStackView::EffectStackView(QWidget *parent) :
38         QWidget(parent)
39 {
40     m_ui.setupUi(this);
41     QVBoxLayout *vbox1 = new QVBoxLayout(m_ui.frame);
42     m_effectedit = new EffectStackEdit(m_ui.frame);
43     vbox1->setContentsMargins(0, 0, 0, 0);
44     vbox1->setSpacing(0);
45     vbox1->addWidget(m_effectedit);
46     m_ui.frame->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
47     //m_ui.effectlist->horizontalHeader()->setVisible(false);
48     //m_ui.effectlist->verticalHeader()->setVisible(false);
49     m_clipref = NULL;
50
51     m_ui.buttonNew->setIcon(KIcon("document-new"));
52     m_ui.buttonNew->setToolTip(i18n("Add new effect"));
53     m_ui.buttonUp->setIcon(KIcon("go-up"));
54     m_ui.buttonUp->setToolTip(i18n("Move effect up"));
55     m_ui.buttonDown->setIcon(KIcon("go-down"));
56     m_ui.buttonDown->setToolTip(i18n("Move effect down"));
57     m_ui.buttonDel->setIcon(KIcon("edit-delete"));
58     m_ui.buttonDel->setToolTip(i18n("Delete effect"));
59     m_ui.buttonSave->setIcon(KIcon("document-save"));
60     m_ui.buttonSave->setToolTip(i18n("Save effect"));
61     m_ui.buttonReset->setIcon(KIcon("view-refresh"));
62     m_ui.buttonReset->setToolTip(i18n("Reset effect"));
63
64
65     m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop);//use internal if drop is recognised right
66
67     connect(m_ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
68     connect(m_ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
69     connect(m_ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
70     connect(m_ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
71     connect(m_ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
72     connect(m_ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
73     connect(m_ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
74     connect(m_effectedit, SIGNAL(parameterChanged(const QDomElement, const QDomElement)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement)));
75     connect(m_effectedit, SIGNAL(seekTimeline(int)), this , SLOT(slotSeekTimeline(int)));
76     m_effectLists["audio"] = &MainWindow::audioEffects;
77     m_effectLists["video"] = &MainWindow::videoEffects;
78     m_effectLists["custom"] = &MainWindow::customEffects;
79     m_ui.splitter->setStretchFactor(1, 10);
80     m_ui.splitter->setStretchFactor(0, 1);
81     setEnabled(false);
82 }
83
84 EffectStackView::~EffectStackView()
85 {
86     m_effectLists.clear();
87     delete m_effectedit;
88 }
89
90 void EffectStackView::setMenu(QMenu *menu)
91 {
92     m_ui.buttonNew->setMenu(menu);
93 }
94
95 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
96 {
97     m_effectedit->updateProjectFormat(profile, t);
98 }
99
100 void EffectStackView::slotSaveEffect()
101 {
102     QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
103     if (name.isEmpty()) return;
104     QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
105     path = path + name + ".xml";
106     if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
107
108     int i = m_ui.effectlist->currentRow();
109     QDomDocument doc;
110     QDomElement effect = m_clipref->effectAt(i).cloneNode().toElement();
111     doc.appendChild(doc.importNode(effect, true));
112     effect = doc.firstChild().toElement();
113     effect.removeAttribute("kdenlive_ix");
114     effect.setAttribute("id", name);
115     effect.setAttribute("type", "custom");
116     QDomElement effectname = effect.firstChildElement("name");
117     effect.removeChild(effectname);
118     effectname = doc.createElement("name");
119     QDomText nametext = doc.createTextNode(name);
120     effectname.appendChild(nametext);
121     effect.insertBefore(effectname, QDomNode());
122     QDomElement effectprops = effect.firstChildElement("properties");
123     effectprops.setAttribute("id", name);
124     effectprops.setAttribute("type", "custom");
125
126
127     QFile file(path);
128     if (file.open(QFile::WriteOnly | QFile::Truncate)) {
129         QTextStream out(&file);
130         out << doc.toString();
131     }
132     file.close();
133     emit reloadEffects();
134 }
135
136 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
137 {
138     if (m_clipref)
139         emit updateClipEffect(m_clipref, old, e, m_ui.effectlist->currentRow());
140 }
141
142 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
143 {
144     if (c && !c->isEnabled()) return;
145     if (c && c == m_clipref) {
146         if (ix == -1) ix = m_ui.effectlist->currentRow();
147     } else {
148         m_clipref = c;
149         if (c) {
150             ix = c->selectedEffectIndex();
151             QString size = c->baseClip()->getProperty("frame_size");
152             double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
153             QPoint p((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
154             m_effectedit->setFrameSize(p);
155             m_effectedit->setFrameSize(p);
156         } else ix = 0;
157     }
158     if (m_clipref == NULL) {
159         m_ui.effectlist->blockSignals(true);
160         m_ui.effectlist->clear();
161         m_effectedit->transferParamDesc(QDomElement(), 0, 0);
162         m_ui.effectlist->blockSignals(false);
163         setEnabled(false);
164         return;
165     }
166     setEnabled(true);
167     setupListView(ix);
168 }
169
170 void EffectStackView::slotItemChanged(QListWidgetItem *item)
171 {
172     bool disable = true;
173     if (item->checkState() == Qt::Checked) disable = false;
174     m_ui.frame->setEnabled(!disable);
175     m_ui.buttonReset->setEnabled(!disable);
176     int activeRow = m_ui.effectlist->currentRow();
177     if (activeRow >= 0) {
178         m_effectedit->updateParameter("disable", QString::number((int) disable));
179         emit changeEffectState(m_clipref, activeRow, disable);
180     }
181 }
182
183
184 void EffectStackView::setupListView(int ix)
185 {
186     m_ui.effectlist->blockSignals(true);
187     m_ui.effectlist->clear();
188
189     // Issue 238: Add icons for effect type in effectstack.
190     KIcon videoIcon("kdenlive-show-video");
191     KIcon audioIcon("kdenlive-show-audio");
192     KIcon customIcon("kdenlive-custom-effect");
193     QListWidgetItem* item;
194
195     for (int i = 0; i < m_clipref->effectsCount(); i++) {
196         const QDomElement d = m_clipref->effectAt(i);
197         if (d.isNull()) {
198             kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
199             continue;
200         }
201
202         /*QDomDocument doc;
203         doc.appendChild(doc.importNode(d, true));
204         kDebug() << "IMPORTED STK: " << doc.toString();*/
205
206         QDomNode namenode = d.elementsByTagName("name").item(0);
207         if (!namenode.isNull()) {
208             // Issue 238: Add icons for effect type in effectstack.
209             // Logic more or less copied from initeffects.cpp
210             QString type = d.attribute("type", QString());
211             if ("audio" == type) {
212                 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
213             } else if ("custom" == type) {
214                 item = new QListWidgetItem(customIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
215             } else {
216                 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
217             }
218             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
219             if (d.attribute("disable") == "1") item->setCheckState(Qt::Unchecked);
220             else item->setCheckState(Qt::Checked);
221         }
222     }
223     if (m_ui.effectlist->count() == 0) {
224         m_ui.buttonDel->setEnabled(false);
225         m_ui.buttonSave->setEnabled(false);
226         m_ui.buttonReset->setEnabled(false);
227         m_ui.buttonUp->setEnabled(false);
228         m_ui.buttonDown->setEnabled(false);
229     } else {
230         if (ix < 0) ix = 0;
231         if (ix > m_ui.effectlist->count() - 1) ix = m_ui.effectlist->count() - 1;
232         m_ui.effectlist->setCurrentRow(ix);
233     }
234     m_ui.effectlist->blockSignals(false);
235     if (m_ui.effectlist->count() == 0) m_effectedit->transferParamDesc(QDomElement(), 0, 0);
236     else slotItemSelectionChanged(false);
237 }
238
239 void EffectStackView::slotItemSelectionChanged(bool update)
240 {
241     bool hasItem = m_ui.effectlist->currentItem();
242     int activeRow = m_ui.effectlist->currentRow();
243     bool isChecked = false;
244     if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
245     if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
246         m_effectedit->transferParamDesc(m_clipref->effectAt(activeRow), m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
247     }
248     if (m_clipref && update) m_clipref->setSelectedEffect(activeRow);
249     m_ui.buttonDel->setEnabled(hasItem);
250     m_ui.buttonSave->setEnabled(hasItem);
251     m_ui.buttonReset->setEnabled(hasItem && isChecked);
252     m_ui.buttonUp->setEnabled(activeRow > 0);
253     m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
254     m_ui.frame->setEnabled(isChecked);
255 }
256
257 void EffectStackView::slotItemUp()
258 {
259     int activeRow = m_ui.effectlist->currentRow();
260     if (activeRow <= 0) return;
261     emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
262 }
263
264 void EffectStackView::slotItemDown()
265 {
266     int activeRow = m_ui.effectlist->currentRow();
267     if (activeRow >= m_ui.effectlist->count() - 1) return;
268     emit changeEffectPosition(m_clipref, activeRow + 1, activeRow + 2);
269 }
270
271 void EffectStackView::slotItemDel()
272 {
273     int activeRow = m_ui.effectlist->currentRow();
274     if (activeRow >= 0) {
275         emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
276     }
277 }
278
279 void EffectStackView::slotResetEffect()
280 {
281     int activeRow = m_ui.effectlist->currentRow();
282     if (activeRow < 0) return;
283     QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
284     QDomElement dom;
285     QString effectName = m_ui.effectlist->currentItem()->text();
286     foreach(const QString &type, m_effectLists.keys()) {
287         EffectsList *list = m_effectLists[type];
288         if (list->effectNames().contains(effectName)) {
289             dom = list->getEffectByName(effectName).cloneNode().toElement();
290             break;
291         }
292     }
293     if (!dom.isNull()) {
294         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
295         m_clipref->initEffect(dom);
296         m_effectedit->transferParamDesc(dom, m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
297         emit updateClipEffect(m_clipref, old, dom, activeRow);
298     }
299 }
300
301
302 void EffectStackView::raiseWindow(QWidget* dock)
303 {
304     if (m_clipref && dock)
305         dock->raise();
306 }
307
308 void EffectStackView::clear()
309 {
310     m_ui.effectlist->blockSignals(true);
311     m_ui.effectlist->clear();
312     m_ui.buttonDel->setEnabled(false);
313     m_ui.buttonSave->setEnabled(false);
314     m_ui.buttonReset->setEnabled(false);
315     m_ui.buttonUp->setEnabled(false);
316     m_ui.buttonDown->setEnabled(false);
317     m_effectedit->transferParamDesc(QDomElement(), 0, 0);
318     m_ui.effectlist->blockSignals(false);
319 }
320
321
322 void EffectStackView::slotSeekTimeline(int pos)
323 {
324     if (!m_clipref) return;
325     emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
326 }
327
328 #include "effectstackview.moc"