]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
some progress on slideshow clips
[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.buttonNew, SIGNAL(clicked()), this, SLOT(slotNewEffect()));
62     connect(ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
63     connect(ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
64     connect(ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
65     connect(ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
66     connect(ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
67     connect(this, SIGNAL(transferParamDesc(const QDomElement&, int , int)), effectedit , SLOT(transferParamDesc(const QDomElement&, int , int)));
68     connect(effectedit, SIGNAL(parameterChanged(const QDomElement&, const QDomElement&)), this , SLOT(slotUpdateEffectParams(const QDomElement&, const QDomElement&)));
69     effectLists["audio"] = &MainWindow::audioEffects;
70     effectLists["video"] = &MainWindow::videoEffects;
71     effectLists["custom"] = &MainWindow::customEffects;
72
73     ui.infoBox->hide();
74     setEnabled(false);
75 }
76
77 void EffectStackView::slotSaveEffect() {
78     QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
79     if (name.isEmpty()) return;
80     QString path = KStandardDirs::locateLocal("data", "kdenlive/effects/", true);
81     path = path + name + ".xml";
82     if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it ?")) == KMessageBox::No) return;
83
84     int i = ui.effectlist->currentRow();
85     QDomDocument doc;
86     QDomElement effect = clipref->effectAt(i).cloneNode().toElement();
87     doc.appendChild(doc.importNode(effect, true));
88     effect = doc.firstChild().toElement();
89     effect.removeAttribute("kdenlive_ix");
90     effect.setAttribute("id", name);
91     QDomElement effectname = effect.firstChildElement("name");
92     effect.removeChild(effectname);
93     effectname = doc.createElement("name");
94     QDomText nametext = doc.createTextNode(name);
95     effectname.appendChild(nametext);
96     effect.insertBefore(effectname, QDomNode());
97     QDomElement effectprops = effect.firstChildElement("properties");
98     effectprops.setAttribute("id", name);
99     effectprops.setAttribute("type", "custom");
100
101
102     QFile file(path);
103     if (file.open(QFile::WriteOnly | QFile::Truncate)) {
104         QTextStream out(&file);
105         out << doc.toString();
106     }
107     file.close();
108     emit reloadEffects();
109 }
110
111 void EffectStackView::slotUpdateEffectParams(const QDomElement& old, const QDomElement& e) {
112     if (clipref)
113         emit updateClipEffect(clipref, old, e, ui.effectlist->currentRow());
114 }
115
116 void EffectStackView::slotClipItemSelected(ClipItem* c) {
117     int ix = 0;
118     if (c && c == clipref) {
119         ix = ui.effectlist->currentRow();
120     } else {
121         clipref = c;
122         if (c) ix = c->selectedEffectIndex();
123     }
124     if (clipref == NULL) {
125         ui.effectlist->clear();
126         setEnabled(false);
127         return;
128     }
129     setEnabled(true);
130     setupListView(ix);
131 }
132
133 void EffectStackView::slotItemChanged(QListWidgetItem *item) {
134     bool disable = true;
135     if (item->checkState() == Qt::Checked) disable = false;
136     ui.buttonReset->setEnabled(!disable);
137     int activeRow = ui.effectlist->currentRow();
138     if (activeRow >= 0) {
139         emit changeEffectState(clipref, activeRow, disable);
140     }
141 }
142
143
144 void EffectStackView::setupListView(int ix) {
145     ui.effectlist->clear();
146     for (int i = 0;i < clipref->effectsCount();i++) {
147         QDomElement d = clipref->effectAt(i);
148         QDomNode namenode = d.elementsByTagName("name").item(0);
149         if (!namenode.isNull()) {
150             QListWidgetItem* item = new QListWidgetItem(namenode.toElement().text(), ui.effectlist);
151             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
152             if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
153             else item->setCheckState(Qt::Checked);
154         }
155     }
156     if (clipref->effectsCount() == 0) {
157         emit transferParamDesc(QDomElement(), 0, 100);
158         ui.buttonDel->setEnabled(false);
159         ui.buttonSave->setEnabled(false);
160         ui.buttonReset->setEnabled(false);
161         ui.buttonUp->setEnabled(false);
162         ui.buttonDown->setEnabled(false);
163     } else {
164         if (ix < 0) ix = 0;
165         if (ix > ui.effectlist->count() - 1) ix = ui.effectlist->count() - 1;
166         ui.effectlist->setCurrentRow(ix);
167         ui.buttonDel->setEnabled(true);
168         ui.buttonSave->setEnabled(true);
169         ui.buttonReset->setEnabled(true);
170         ui.buttonUp->setEnabled(ix > 0);
171         ui.buttonDown->setEnabled(ix < clipref->effectsCount() - 1);
172     }
173 }
174
175 void EffectStackView::slotItemSelectionChanged() {
176     bool hasItem = ui.effectlist->currentItem();
177     int activeRow = ui.effectlist->currentRow();
178     bool isChecked = false;
179     if (hasItem && ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
180     if (hasItem && ui.effectlist->currentItem()->isSelected()) {
181         emit transferParamDesc(clipref->effectAt(activeRow), 0, 100);//minx max frame
182     }
183     if (clipref) clipref->setSelectedEffect(activeRow);
184     ui.buttonDel->setEnabled(hasItem);
185     ui.buttonSave->setEnabled(hasItem);
186     ui.buttonReset->setEnabled(hasItem && isChecked);
187     ui.buttonUp->setEnabled(activeRow > 0);
188     ui.buttonDown->setEnabled((activeRow < ui.effectlist->count() - 1) && hasItem);
189 }
190
191 void EffectStackView::slotItemUp() {
192     int activeRow = ui.effectlist->currentRow();
193     if (activeRow > 0) {
194         QDomElement act = clipref->effectAt(activeRow).cloneNode().toElement();
195         QDomElement before = clipref->effectAt(activeRow - 1).cloneNode().toElement();
196         clipref->setEffectAt(activeRow - 1, act);
197         clipref->setEffectAt(activeRow, before);
198     }
199     QListWidgetItem *item = ui.effectlist->takeItem(activeRow);
200     ui.effectlist->insertItem(activeRow - 1, item);
201     ui.effectlist->setCurrentItem(item);
202     emit changeEffectPosition(clipref, activeRow + 1, activeRow);
203 }
204
205 void EffectStackView::slotItemDown() {
206     int activeRow = ui.effectlist->currentRow();
207     if (activeRow < ui.effectlist->count() - 1) {
208         QDomElement act = clipref->effectAt(activeRow).cloneNode().toElement();
209         QDomElement after = clipref->effectAt(activeRow + 1).cloneNode().toElement();
210         clipref->setEffectAt(activeRow + 1, act);
211         clipref->setEffectAt(activeRow, after);
212     }
213     QListWidgetItem *item = ui.effectlist->takeItem(activeRow);
214     ui.effectlist->insertItem(activeRow + 1, item);
215     ui.effectlist->setCurrentItem(item);
216     emit changeEffectPosition(clipref, activeRow + 1, activeRow + 2);
217 }
218
219 void EffectStackView::slotItemDel() {
220     int activeRow = ui.effectlist->currentRow();
221     if (activeRow >= 0) {
222         emit removeEffect(clipref, clipref->effectAt(activeRow));
223     }
224 }
225
226 void EffectStackView::slotResetEffect() {
227     int activeRow = ui.effectlist->currentRow();
228     if (activeRow < 0) return;
229     QDomElement old = clipref->effectAt(activeRow).cloneNode().toElement();
230     QDomElement dom;
231     QString effectName = ui.effectlist->currentItem()->text();
232     foreach(const QString &type, effectLists.keys()) {
233         EffectsList *list = effectLists[type];
234         if (list->effectNames().contains(effectName)) {
235             dom = list->getEffectByName(effectName);
236             break;
237         }
238     }
239     if (!dom.isNull()) {
240         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
241         emit transferParamDesc(dom, 0, 100);//minx max frame
242         emit updateClipEffect(clipref, old, dom, activeRow);
243     }
244 }
245
246 void EffectStackView::slotNewEffect() {
247     int ix = ui.effectlist->currentRow();
248     QMenu *displayMenu = new QMenu(this);
249     displayMenu->setTitle("Filters");
250     foreach(const QString &type, effectLists.keys()) {
251         QAction *a = new QAction(type, displayMenu);
252         EffectsList *list = effectLists[type];
253
254         QMenu *parts = new QMenu(type, displayMenu);
255         parts->setTitle(type);
256         foreach(const QString &name, list->effectNames()) {
257             QAction *entry = new QAction(name, parts);
258             entry->setData(name);
259             entry->setToolTip(list->getInfo(name));
260             entry->setStatusTip(list->getInfo(name));
261             parts->addAction(entry);
262             //QAction
263         }
264         displayMenu->addMenu(parts);
265
266     }
267
268     QAction *result = displayMenu->exec(mapToGlobal(ui.buttonNew->pos() + ui.buttonNew->rect().bottomRight()));
269
270     if (result) {
271         //TODO effects.append(result->data().toString());
272         foreach(const EffectsList *e, effectLists.values()) {
273             QDomElement dom = e->getEffectByName(result->data().toString());
274             if (clipref)
275                 clipref->addEffect(dom);
276             slotClipItemSelected(clipref);
277         }
278
279         setupListView(ix);
280         //kDebug()<< result->data();
281     }
282     delete displayMenu;
283
284 }
285
286 void EffectStackView::raiseWindow(QWidget* dock) {
287     if (clipref && dock)
288         dock->raise();
289 }
290
291 #include "effectstackview.moc"