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