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 ui.effectlist->setCurrentRow(ix);
166 ui.buttonDel->setEnabled(true);
167 ui.buttonSave->setEnabled(true);
168 ui.buttonReset->setEnabled(true);
169 ui.buttonUp->setEnabled(ix > 0);
170 ui.buttonDown->setEnabled(ix < clipref->effectsCount() - 1);
174 void EffectStackView::slotItemSelectionChanged() {
175 bool hasItem = ui.effectlist->currentItem();
176 int activeRow = ui.effectlist->currentRow();
177 bool isChecked = false;
178 if (hasItem && ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
179 if (hasItem && ui.effectlist->currentItem()->isSelected()) {
180 emit transferParamDesc(clipref->effectAt(activeRow), 0, 100);//minx max frame
182 if (clipref) clipref->setSelectedEffect(activeRow);
183 ui.buttonDel->setEnabled(hasItem);
184 ui.buttonSave->setEnabled(hasItem);
185 ui.buttonReset->setEnabled(hasItem && isChecked);
186 ui.buttonUp->setEnabled(activeRow > 0);
187 ui.buttonDown->setEnabled((activeRow < ui.effectlist->count() - 1) && hasItem);
190 void EffectStackView::slotItemUp() {
191 int activeRow = ui.effectlist->currentRow();
193 QDomElement act = clipref->effectAt(activeRow).cloneNode().toElement();
194 QDomElement before = clipref->effectAt(activeRow - 1).cloneNode().toElement();
195 clipref->setEffectAt(activeRow - 1, act);
196 clipref->setEffectAt(activeRow, before);
198 QListWidgetItem *item = ui.effectlist->takeItem(activeRow);
199 ui.effectlist->insertItem(activeRow - 1, item);
200 ui.effectlist->setCurrentItem(item);
201 emit changeEffectPosition(clipref, activeRow + 1, activeRow);
204 void EffectStackView::slotItemDown() {
205 int activeRow = ui.effectlist->currentRow();
206 if (activeRow < ui.effectlist->count() - 1) {
207 QDomElement act = clipref->effectAt(activeRow).cloneNode().toElement();
208 QDomElement after = clipref->effectAt(activeRow + 1).cloneNode().toElement();
209 clipref->setEffectAt(activeRow + 1, act);
210 clipref->setEffectAt(activeRow, after);
212 QListWidgetItem *item = ui.effectlist->takeItem(activeRow);
213 ui.effectlist->insertItem(activeRow + 1, item);
214 ui.effectlist->setCurrentItem(item);
215 emit changeEffectPosition(clipref, activeRow + 1, activeRow + 2);
218 void EffectStackView::slotItemDel() {
219 int activeRow = ui.effectlist->currentRow();
220 if (activeRow >= 0) {
221 emit removeEffect(clipref, clipref->effectAt(activeRow));
225 void EffectStackView::slotResetEffect() {
226 int activeRow = ui.effectlist->currentRow();
227 if (activeRow < 0) return;
228 QDomElement old = clipref->effectAt(activeRow).cloneNode().toElement();
230 QString effectName = ui.effectlist->currentItem()->text();
231 foreach(const QString &type, effectLists.keys()) {
232 EffectsList *list = effectLists[type];
233 if (list->effectNames().contains(effectName)) {
234 dom = list->getEffectByName(effectName);
239 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
240 emit transferParamDesc(dom, 0, 100);//minx max frame
241 emit updateClipEffect(clipref, old, dom, activeRow);
245 void EffectStackView::slotNewEffect() {
246 int ix = ui.effectlist->currentRow();
247 QMenu *displayMenu = new QMenu(this);
248 displayMenu->setTitle("Filters");
249 foreach(const QString &type, effectLists.keys()) {
250 QAction *a = new QAction(type, displayMenu);
251 EffectsList *list = effectLists[type];
253 QMenu *parts = new QMenu(type, displayMenu);
254 parts->setTitle(type);
255 foreach(const QString &name, list->effectNames()) {
256 QAction *entry = new QAction(name, parts);
257 entry->setData(name);
258 entry->setToolTip(list->getInfo(name));
259 entry->setStatusTip(list->getInfo(name));
260 parts->addAction(entry);
263 displayMenu->addMenu(parts);
267 QAction *result = displayMenu->exec(mapToGlobal(ui.buttonNew->pos() + ui.buttonNew->rect().bottomRight()));
270 //TODO effects.append(result->data().toString());
271 foreach(const EffectsList *e, effectLists.values()) {
272 QDomElement dom = e->getEffectByName(result->data().toString());
274 clipref->addEffect(dom);
275 slotClipItemSelected(clipref);
279 //kDebug()<< result->data();
285 void EffectStackView::raiseWindow(QWidget* dock) {
290 #include "effectstackview.moc"