]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
Try to fix undoing of moving effects (up and down in effects list), also related to
[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);
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, int ix) {
125     if (c && c == clipref) {
126         if (ix == -1) ix = ui.effectlist->currentRow();
127     } else {
128         clipref = c;
129         if (c) ix = c->selectedEffectIndex();
130         else ix = 0;
131     }
132     if (clipref == NULL) {
133         ui.effectlist->clear();
134         effectedit->transferParamDesc(QDomElement(), 0, 0);
135         setEnabled(false);
136         return;
137     }
138     setEnabled(true);
139     setupListView(ix);
140 }
141
142 void EffectStackView::slotItemChanged(QListWidgetItem *item) {
143     bool disable = true;
144     if (item->checkState() == Qt::Checked) disable = false;
145     ui.buttonReset->setEnabled(!disable);
146     int activeRow = ui.effectlist->currentRow();
147     if (activeRow >= 0) {
148         emit changeEffectState(clipref, activeRow, disable);
149     }
150 }
151
152
153 void EffectStackView::setupListView(int ix) {
154     ui.effectlist->clear();
155     for (int i = 0;i < clipref->effectsCount();i++) {
156         QDomElement d = clipref->effectAt(i);
157         QDomNode namenode = d.elementsByTagName("name").item(0);
158         if (!namenode.isNull()) {
159             QListWidgetItem* item = new QListWidgetItem(i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
160             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
161             if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
162             else item->setCheckState(Qt::Checked);
163         }
164     }
165     if (clipref->effectsCount() == 0) {
166         emit transferParamDesc(QDomElement(), 0, 100);
167         ui.buttonDel->setEnabled(false);
168         ui.buttonSave->setEnabled(false);
169         ui.buttonReset->setEnabled(false);
170         ui.buttonUp->setEnabled(false);
171         ui.buttonDown->setEnabled(false);
172     } else {
173         if (ix < 0) ix = 0;
174         if (ix > ui.effectlist->count() - 1) ix = ui.effectlist->count() - 1;
175         ui.effectlist->setCurrentRow(ix);
176         ui.buttonDel->setEnabled(true);
177         ui.buttonSave->setEnabled(true);
178         ui.buttonReset->setEnabled(true);
179         ui.buttonUp->setEnabled(ix > 0);
180         ui.buttonDown->setEnabled(ix < clipref->effectsCount() - 1);
181     }
182 }
183
184 void EffectStackView::slotItemSelectionChanged() {
185     bool hasItem = ui.effectlist->currentItem();
186     int activeRow = ui.effectlist->currentRow();
187     bool isChecked = false;
188     if (hasItem && ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
189     if (hasItem && ui.effectlist->currentItem()->isSelected()) {
190         emit transferParamDesc(clipref->effectAt(activeRow), 0, 100);//minx max frame
191     }
192     if (clipref) clipref->setSelectedEffect(activeRow);
193     ui.buttonDel->setEnabled(hasItem);
194     ui.buttonSave->setEnabled(hasItem);
195     ui.buttonReset->setEnabled(hasItem && isChecked);
196     ui.buttonUp->setEnabled(activeRow > 0);
197     ui.buttonDown->setEnabled((activeRow < ui.effectlist->count() - 1) && hasItem);
198 }
199
200 void EffectStackView::slotItemUp() {
201     int activeRow = ui.effectlist->currentRow();
202     if (activeRow <= 0) return;
203     emit changeEffectPosition(clipref, activeRow + 1, activeRow);
204 }
205
206 void EffectStackView::slotItemDown() {
207     int activeRow = ui.effectlist->currentRow();
208     if (activeRow >= ui.effectlist->count() - 1) return;
209     emit changeEffectPosition(clipref, activeRow + 1, activeRow + 2);
210 }
211
212 void EffectStackView::slotItemDel() {
213     int activeRow = ui.effectlist->currentRow();
214     if (activeRow >= 0) {
215         emit removeEffect(clipref, clipref->effectAt(activeRow));
216     }
217 }
218
219 void EffectStackView::slotResetEffect() {
220     int activeRow = ui.effectlist->currentRow();
221     if (activeRow < 0) return;
222     QDomElement old = clipref->effectAt(activeRow).cloneNode().toElement();
223     QDomElement dom;
224     QString effectName = ui.effectlist->currentItem()->text();
225     foreach(const QString &type, effectLists.keys()) {
226         EffectsList *list = effectLists[type];
227         if (list->effectNames().contains(effectName)) {
228             dom = list->getEffectByName(effectName);
229             break;
230         }
231     }
232     if (!dom.isNull()) {
233         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
234         emit transferParamDesc(dom, 0, 100);//minx max frame
235         emit updateClipEffect(clipref, old, dom, activeRow);
236     }
237 }
238
239
240 void EffectStackView::raiseWindow(QWidget* dock) {
241     if (clipref && dock)
242         dock->raise();
243 }
244
245 void EffectStackView::clear() {
246     ui.effectlist->clear();
247     ui.buttonDel->setEnabled(false);
248     ui.buttonSave->setEnabled(false);
249     ui.buttonReset->setEnabled(false);
250     ui.buttonUp->setEnabled(false);
251     ui.buttonDown->setEnabled(false);
252     effectedit->transferParamDesc(QDomElement(), 0, 0);
253 }
254
255 #include "effectstackview.moc"