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 ***************************************************************************/
19 #include "effectstackview.h"
20 #include "effectslist.h"
22 #include "mainwindow.h"
23 #include "kdenlivesettings.h"
27 #include <KMessageBox>
28 #include <KStandardDirs>
31 #include <QTextStream>
33 #include <QInputDialog>
36 EffectStackView::EffectStackView(QWidget *parent) :
40 effectedit = new EffectStackEdit(ui.frame);
41 //ui.effectlist->horizontalHeader()->setVisible(false);
42 //ui.effectlist->verticalHeader()->setVisible(false);
45 ui.buttonNew->setIcon(KIcon("document-new"));
46 ui.buttonNew->setToolTip(i18n("Add new effect"));
47 ui.buttonUp->setIcon(KIcon("go-up"));
48 ui.buttonUp->setToolTip(i18n("Move effect up"));
49 ui.buttonDown->setIcon(KIcon("go-down"));
50 ui.buttonDown->setToolTip(i18n("Move effect down"));
51 ui.buttonDel->setIcon(KIcon("trash-empty"));
52 ui.buttonDel->setToolTip(i18n("Delete effect"));
53 ui.buttonSave->setIcon(KIcon("document-save"));
54 ui.buttonSave->setToolTip(i18n("Save effect"));
55 ui.buttonReset->setIcon(KIcon("view-refresh"));
56 ui.buttonReset->setToolTip(i18n("Reset effect"));
59 ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop);//use internal if drop is recognised right
61 connect(ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
62 connect(ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
63 connect(ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
64 connect(ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
65 connect(ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
66 connect(ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
67 connect(ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
68 connect(this, SIGNAL(transferParamDesc(const QDomElement&, int , int)), effectedit , SLOT(transferParamDesc(const QDomElement&, int , int)));
69 connect(effectedit, SIGNAL(parameterChanged(const QDomElement&, const QDomElement&)), this , SLOT(slotUpdateEffectParams(const QDomElement&, const QDomElement&)));
70 effectLists["audio"] = &MainWindow::audioEffects;
71 effectLists["video"] = &MainWindow::videoEffects;
72 effectLists["custom"] = &MainWindow::customEffects;
73 ui.splitter->setStretchFactor(1, 10);
74 ui.splitter->setStretchFactor(0, 1);
78 void EffectStackView::setMenu(QMenu *menu)
80 ui.buttonNew->setMenu(menu);
83 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
85 effectedit->updateProjectFormat(profile, t);
88 void EffectStackView::slotSaveEffect()
90 QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
91 if (name.isEmpty()) return;
92 QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
93 path = path + name + ".xml";
94 if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
96 int i = ui.effectlist->currentRow();
98 QDomElement effect = clipref->effectAt(i).cloneNode().toElement();
99 doc.appendChild(doc.importNode(effect, true));
100 effect = doc.firstChild().toElement();
101 effect.removeAttribute("kdenlive_ix");
102 effect.setAttribute("id", name);
103 effect.setAttribute("type", "custom");
104 QDomElement effectname = effect.firstChildElement("name");
105 effect.removeChild(effectname);
106 effectname = doc.createElement("name");
107 QDomText nametext = doc.createTextNode(name);
108 effectname.appendChild(nametext);
109 effect.insertBefore(effectname, QDomNode());
110 QDomElement effectprops = effect.firstChildElement("properties");
111 effectprops.setAttribute("id", name);
112 effectprops.setAttribute("type", "custom");
116 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
117 QTextStream out(&file);
118 out << doc.toString();
121 emit reloadEffects();
124 void EffectStackView::slotUpdateEffectParams(const QDomElement& old, const QDomElement& e)
127 emit updateClipEffect(clipref, old, e, ui.effectlist->currentRow());
130 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
132 if (c && c == clipref) {
133 if (ix == -1) ix = ui.effectlist->currentRow();
136 if (c) ix = c->selectedEffectIndex();
139 if (clipref == NULL) {
140 ui.effectlist->clear();
141 effectedit->transferParamDesc(QDomElement(), 0, 0);
149 void EffectStackView::slotItemChanged(QListWidgetItem *item)
152 if (item->checkState() == Qt::Checked) disable = false;
153 ui.buttonReset->setEnabled(!disable);
154 int activeRow = ui.effectlist->currentRow();
155 if (activeRow >= 0) {
156 emit changeEffectState(clipref, activeRow, disable);
161 void EffectStackView::setupListView(int ix)
163 ui.effectlist->clear();
165 // Issue 238: Add icons for effect type in effectstack.
166 KIcon videoIcon("kdenlive-show-video");
167 KIcon audioIcon("kdenlive-show-audio");
168 QListWidgetItem* item;
170 for (int i = 0;i < clipref->effectsCount();i++) {
171 QDomElement d = clipref->effectAt(i);
173 QDomNode namenode = d.elementsByTagName("name").item(0);
174 if (!namenode.isNull()) {
175 // Issue 238: Add icons for effect type in effectstack.
176 // Logic more or less copied from initeffects.cpp
177 QString type = d.attribute("type", QString());
178 if ("audio" == type) {
179 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
180 } else if ("custom" == type) {
181 item = new QListWidgetItem(i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
183 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
185 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
186 if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
187 else item->setCheckState(Qt::Checked);
190 if (clipref->effectsCount() == 0) {
191 emit transferParamDesc(QDomElement(), 0, 0);
192 ui.buttonDel->setEnabled(false);
193 ui.buttonSave->setEnabled(false);
194 ui.buttonReset->setEnabled(false);
195 ui.buttonUp->setEnabled(false);
196 ui.buttonDown->setEnabled(false);
199 if (ix > ui.effectlist->count() - 1) ix = ui.effectlist->count() - 1;
200 ui.effectlist->setCurrentRow(ix);
201 ui.buttonDel->setEnabled(true);
202 ui.buttonSave->setEnabled(true);
203 ui.buttonReset->setEnabled(true);
204 ui.buttonUp->setEnabled(ix > 0);
205 ui.buttonDown->setEnabled(ix < clipref->effectsCount() - 1);
209 void EffectStackView::slotItemSelectionChanged()
211 bool hasItem = ui.effectlist->currentItem();
212 int activeRow = ui.effectlist->currentRow();
213 bool isChecked = false;
214 if (hasItem && ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
215 if (hasItem && ui.effectlist->currentItem()->isSelected()) {
216 emit transferParamDesc(clipref->effectAt(activeRow), clipref->cropStart().frames(KdenliveSettings::project_fps()), clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
218 if (clipref) clipref->setSelectedEffect(activeRow);
219 ui.buttonDel->setEnabled(hasItem);
220 ui.buttonSave->setEnabled(hasItem);
221 ui.buttonReset->setEnabled(hasItem && isChecked);
222 ui.buttonUp->setEnabled(activeRow > 0);
223 ui.buttonDown->setEnabled((activeRow < ui.effectlist->count() - 1) && hasItem);
226 void EffectStackView::slotItemUp()
228 int activeRow = ui.effectlist->currentRow();
229 if (activeRow <= 0) return;
230 emit changeEffectPosition(clipref, activeRow + 1, activeRow);
233 void EffectStackView::slotItemDown()
235 int activeRow = ui.effectlist->currentRow();
236 if (activeRow >= ui.effectlist->count() - 1) return;
237 emit changeEffectPosition(clipref, activeRow + 1, activeRow + 2);
240 void EffectStackView::slotItemDel()
242 int activeRow = ui.effectlist->currentRow();
243 if (activeRow >= 0) {
244 emit removeEffect(clipref, clipref->effectAt(activeRow));
248 void EffectStackView::slotResetEffect()
250 int activeRow = ui.effectlist->currentRow();
251 if (activeRow < 0) return;
252 QDomElement old = clipref->effectAt(activeRow).cloneNode().toElement();
254 QString effectName = ui.effectlist->currentItem()->text();
255 foreach(const QString &type, effectLists.keys()) {
256 EffectsList *list = effectLists[type];
257 if (list->effectNames().contains(effectName)) {
258 dom = list->getEffectByName(effectName);
263 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
264 emit transferParamDesc(dom, clipref->cropStart().frames(KdenliveSettings::project_fps()), clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
265 emit updateClipEffect(clipref, old, dom, activeRow);
270 void EffectStackView::raiseWindow(QWidget* dock)
276 void EffectStackView::clear()
278 ui.effectlist->clear();
279 ui.buttonDel->setEnabled(false);
280 ui.buttonSave->setEnabled(false);
281 ui.buttonReset->setEnabled(false);
282 ui.buttonUp->setEnabled(false);
283 ui.buttonDown->setEnabled(false);
284 effectedit->transferParamDesc(QDomElement(), 0, 0);
287 #include "effectstackview.moc"