1 /***************************************************************************
2 effecstackview.cpp - description
5 copyright : (C) 2008 by Marco Gittler
6 email : g.marco@freenet.de
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
18 #include <QHeaderView>
20 #include <QInputDialog>
21 #include <QTextStream>
26 #include <KMessageBox>
27 #include <KStandardDirs>
29 #include "effectstackview.h"
30 #include "effectslist.h"
32 #include "mainwindow.h"
35 EffectStackView::EffectStackView(QWidget *parent)
38 effectedit = new EffectStackEdit(ui.frame, this);
39 //ui.effectlist->horizontalHeader()->setVisible(false);
40 //ui.effectlist->verticalHeader()->setVisible(false);
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"));
57 ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop);//use internal if drop is recognised right
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;
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;
84 int i = ui.effectlist->currentRow();
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");
103 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
104 QTextStream out(&file);
105 out << doc.toString();
108 emit reloadEffects();
111 void EffectStackView::slotUpdateEffectParams(const QDomElement& old, const QDomElement& e) {
113 emit updateClipEffect(clipref, old, e, ui.effectlist->currentRow());
116 void EffectStackView::slotClipItemSelected(ClipItem* c) {
118 if (c && c == clipref) {
119 ix = ui.effectlist->currentRow();
122 if (c) ix = c->selectedEffectIndex();
124 if (clipref == NULL) {
125 ui.effectlist->clear();
133 void EffectStackView::slotItemChanged(QListWidgetItem *item) {
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);
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);
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);
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);
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
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);
191 void EffectStackView::slotItemUp() {
192 int activeRow = ui.effectlist->currentRow();
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);
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);
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);
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);
219 void EffectStackView::slotItemDel() {
220 int activeRow = ui.effectlist->currentRow();
221 if (activeRow >= 0) {
222 emit removeEffect(clipref, clipref->effectAt(activeRow));
226 void EffectStackView::slotResetEffect() {
227 int activeRow = ui.effectlist->currentRow();
228 if (activeRow < 0) return;
229 QDomElement old = clipref->effectAt(activeRow).cloneNode().toElement();
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);
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);
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];
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);
264 displayMenu->addMenu(parts);
268 QAction *result = displayMenu->exec(mapToGlobal(ui.buttonNew->pos() + ui.buttonNew->rect().bottomRight()));
271 //TODO effects.append(result->data().toString());
272 foreach(const EffectsList *e, effectLists.values()) {
273 QDomElement dom = e->getEffectByName(result->data().toString());
275 clipref->addEffect(dom);
276 slotClipItemSelected(clipref);
280 //kDebug()<< result->data();
286 void EffectStackView::raiseWindow(QWidget* dock) {
291 #include "effectstackview.moc"