]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
5a6a61fbddc3ffbf36856d09ce946302e71297bc
[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     ui.splitter->setStretchFactor(1, 10);
72     ui.splitter->setStretchFactor(0, 1);
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
156     // Issue 238: Add icons for effect type in effectstack.
157     KIcon videoIcon("kdenlive-show-video");
158     KIcon audioIcon("kdenlive-show-audio");
159     QListWidgetItem* item;
160
161     for (int i = 0;i < clipref->effectsCount();i++) {
162         QDomElement d = clipref->effectAt(i);
163
164         QDomNode namenode = d.elementsByTagName("name").item(0);
165         if (!namenode.isNull()) {
166             // Issue 238: Add icons for effect type in effectstack.
167             // Logic more or less copied from initeffects.cpp
168             QString type = d.attribute("type", QString::null);
169             if ("audio" == type) {
170                 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
171             } else if ("custom" == type) {
172                 item = new QListWidgetItem(i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
173             } else {
174                 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
175             }
176             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
177             if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
178             else item->setCheckState(Qt::Checked);
179         }
180     }
181     if (clipref->effectsCount() == 0) {
182         emit transferParamDesc(QDomElement(), 0, 100);
183         ui.buttonDel->setEnabled(false);
184         ui.buttonSave->setEnabled(false);
185         ui.buttonReset->setEnabled(false);
186         ui.buttonUp->setEnabled(false);
187         ui.buttonDown->setEnabled(false);
188     } else {
189         if (ix < 0) ix = 0;
190         if (ix > ui.effectlist->count() - 1) ix = ui.effectlist->count() - 1;
191         ui.effectlist->setCurrentRow(ix);
192         ui.buttonDel->setEnabled(true);
193         ui.buttonSave->setEnabled(true);
194         ui.buttonReset->setEnabled(true);
195         ui.buttonUp->setEnabled(ix > 0);
196         ui.buttonDown->setEnabled(ix < clipref->effectsCount() - 1);
197     }
198 }
199
200 void EffectStackView::slotItemSelectionChanged() {
201     bool hasItem = ui.effectlist->currentItem();
202     int activeRow = ui.effectlist->currentRow();
203     bool isChecked = false;
204     if (hasItem && ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
205     if (hasItem && ui.effectlist->currentItem()->isSelected()) {
206         emit transferParamDesc(clipref->effectAt(activeRow), 0, 100);//minx max frame
207     }
208     if (clipref) clipref->setSelectedEffect(activeRow);
209     ui.buttonDel->setEnabled(hasItem);
210     ui.buttonSave->setEnabled(hasItem);
211     ui.buttonReset->setEnabled(hasItem && isChecked);
212     ui.buttonUp->setEnabled(activeRow > 0);
213     ui.buttonDown->setEnabled((activeRow < ui.effectlist->count() - 1) && hasItem);
214 }
215
216 void EffectStackView::slotItemUp() {
217     int activeRow = ui.effectlist->currentRow();
218     if (activeRow <= 0) return;
219     emit changeEffectPosition(clipref, activeRow + 1, activeRow);
220 }
221
222 void EffectStackView::slotItemDown() {
223     int activeRow = ui.effectlist->currentRow();
224     if (activeRow >= ui.effectlist->count() - 1) return;
225     emit changeEffectPosition(clipref, activeRow + 1, activeRow + 2);
226 }
227
228 void EffectStackView::slotItemDel() {
229     int activeRow = ui.effectlist->currentRow();
230     if (activeRow >= 0) {
231         emit removeEffect(clipref, clipref->effectAt(activeRow));
232     }
233 }
234
235 void EffectStackView::slotResetEffect() {
236     int activeRow = ui.effectlist->currentRow();
237     if (activeRow < 0) return;
238     QDomElement old = clipref->effectAt(activeRow).cloneNode().toElement();
239     QDomElement dom;
240     QString effectName = ui.effectlist->currentItem()->text();
241     foreach(const QString &type, effectLists.keys()) {
242         EffectsList *list = effectLists[type];
243         if (list->effectNames().contains(effectName)) {
244             dom = list->getEffectByName(effectName);
245             break;
246         }
247     }
248     if (!dom.isNull()) {
249         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
250         emit transferParamDesc(dom, 0, 100);//minx max frame
251         emit updateClipEffect(clipref, old, dom, activeRow);
252     }
253 }
254
255
256 void EffectStackView::raiseWindow(QWidget* dock) {
257     if (clipref && dock)
258         dock->raise();
259 }
260
261 void EffectStackView::clear() {
262     ui.effectlist->clear();
263     ui.buttonDel->setEnabled(false);
264     ui.buttonSave->setEnabled(false);
265     ui.buttonReset->setEnabled(false);
266     ui.buttonUp->setEnabled(false);
267     ui.buttonDown->setEnabled(false);
268     effectedit->transferParamDesc(QDomElement(), 0, 0);
269 }
270
271 #include "effectstackview.moc"