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