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