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)
39 effectedit = new EffectStackEdit(ui.frame);
40 //ui.effectlist->horizontalHeader()->setVisible(false);
41 //ui.effectlist->verticalHeader()->setVisible(false);
44 ui.buttonNew->setIcon(KIcon("document-new"));
45 ui.buttonNew->setToolTip(i18n("Add new effect"));
46 ui.buttonUp->setIcon(KIcon("go-up"));
47 ui.buttonUp->setToolTip(i18n("Move effect up"));
48 ui.buttonDown->setIcon(KIcon("go-down"));
49 ui.buttonDown->setToolTip(i18n("Move effect down"));
50 ui.buttonDel->setIcon(KIcon("trash-empty"));
51 ui.buttonDel->setToolTip(i18n("Delete effect"));
52 ui.buttonSave->setIcon(KIcon("document-save"));
53 ui.buttonSave->setToolTip(i18n("Save effect"));
54 ui.buttonReset->setIcon(KIcon("view-refresh"));
55 ui.buttonReset->setToolTip(i18n("Reset effect"));
58 ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop);//use internal if drop is recognised right
60 connect(ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
61 connect(ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
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 ui.splitter->setStretchFactor(1, 10);
73 ui.splitter->setStretchFactor(0, 1);
77 void EffectStackView::setMenu(QMenu *menu) {
78 ui.buttonNew->setMenu(menu);
81 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t) {
82 effectedit->updateProjectFormat(profile, t);
85 void EffectStackView::slotSaveEffect() {
86 QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
87 if (name.isEmpty()) return;
88 QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
89 path = path + name + ".xml";
90 if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
92 int i = ui.effectlist->currentRow();
94 QDomElement effect = clipref->effectAt(i).cloneNode().toElement();
95 doc.appendChild(doc.importNode(effect, true));
96 effect = doc.firstChild().toElement();
97 effect.removeAttribute("kdenlive_ix");
98 effect.setAttribute("id", name);
99 effect.setAttribute("type", "custom");
100 QDomElement effectname = effect.firstChildElement("name");
101 effect.removeChild(effectname);
102 effectname = doc.createElement("name");
103 QDomText nametext = doc.createTextNode(name);
104 effectname.appendChild(nametext);
105 effect.insertBefore(effectname, QDomNode());
106 QDomElement effectprops = effect.firstChildElement("properties");
107 effectprops.setAttribute("id", name);
108 effectprops.setAttribute("type", "custom");
112 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
113 QTextStream out(&file);
114 out << doc.toString();
117 emit reloadEffects();
120 void EffectStackView::slotUpdateEffectParams(const QDomElement& old, const QDomElement& e) {
122 emit updateClipEffect(clipref, old, e, ui.effectlist->currentRow());
125 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix) {
126 if (c && c == clipref) {
127 if (ix == -1) ix = ui.effectlist->currentRow();
130 if (c) ix = c->selectedEffectIndex();
133 if (clipref == NULL) {
134 ui.effectlist->clear();
135 effectedit->transferParamDesc(QDomElement(), 0, 0);
143 void EffectStackView::slotItemChanged(QListWidgetItem *item) {
145 if (item->checkState() == Qt::Checked) disable = false;
146 ui.buttonReset->setEnabled(!disable);
147 int activeRow = ui.effectlist->currentRow();
148 if (activeRow >= 0) {
149 emit changeEffectState(clipref, activeRow, disable);
154 void EffectStackView::setupListView(int ix) {
155 ui.effectlist->clear();
157 // Issue 238: Add icons for effect type in effectstack.
158 KIcon videoIcon("kdenlive-show-video");
159 KIcon audioIcon("kdenlive-show-audio");
160 QListWidgetItem* item;
162 for (int i = 0;i < clipref->effectsCount();i++) {
163 QDomElement d = clipref->effectAt(i);
165 QDomNode namenode = d.elementsByTagName("name").item(0);
166 if (!namenode.isNull()) {
167 // Issue 238: Add icons for effect type in effectstack.
168 // Logic more or less copied from initeffects.cpp
169 QString type = d.attribute("type", QString());
170 if ("audio" == type) {
171 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
172 } else if ("custom" == type) {
173 item = new QListWidgetItem(i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
175 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
177 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
178 if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
179 else item->setCheckState(Qt::Checked);
182 if (clipref->effectsCount() == 0) {
183 emit transferParamDesc(QDomElement(), 0, 0);
184 ui.buttonDel->setEnabled(false);
185 ui.buttonSave->setEnabled(false);
186 ui.buttonReset->setEnabled(false);
187 ui.buttonUp->setEnabled(false);
188 ui.buttonDown->setEnabled(false);
191 if (ix > ui.effectlist->count() - 1) ix = ui.effectlist->count() - 1;
192 ui.effectlist->setCurrentRow(ix);
193 ui.buttonDel->setEnabled(true);
194 ui.buttonSave->setEnabled(true);
195 ui.buttonReset->setEnabled(true);
196 ui.buttonUp->setEnabled(ix > 0);
197 ui.buttonDown->setEnabled(ix < clipref->effectsCount() - 1);
201 void EffectStackView::slotItemSelectionChanged() {
202 bool hasItem = ui.effectlist->currentItem();
203 int activeRow = ui.effectlist->currentRow();
204 bool isChecked = false;
205 if (hasItem && ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
206 if (hasItem && ui.effectlist->currentItem()->isSelected()) {
207 emit transferParamDesc(clipref->effectAt(activeRow), clipref->cropStart().frames(KdenliveSettings::project_fps()), clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
209 if (clipref) clipref->setSelectedEffect(activeRow);
210 ui.buttonDel->setEnabled(hasItem);
211 ui.buttonSave->setEnabled(hasItem);
212 ui.buttonReset->setEnabled(hasItem && isChecked);
213 ui.buttonUp->setEnabled(activeRow > 0);
214 ui.buttonDown->setEnabled((activeRow < ui.effectlist->count() - 1) && hasItem);
217 void EffectStackView::slotItemUp() {
218 int activeRow = ui.effectlist->currentRow();
219 if (activeRow <= 0) return;
220 emit changeEffectPosition(clipref, activeRow + 1, activeRow);
223 void EffectStackView::slotItemDown() {
224 int activeRow = ui.effectlist->currentRow();
225 if (activeRow >= ui.effectlist->count() - 1) return;
226 emit changeEffectPosition(clipref, activeRow + 1, activeRow + 2);
229 void EffectStackView::slotItemDel() {
230 int activeRow = ui.effectlist->currentRow();
231 if (activeRow >= 0) {
232 emit removeEffect(clipref, clipref->effectAt(activeRow));
236 void EffectStackView::slotResetEffect() {
237 int activeRow = ui.effectlist->currentRow();
238 if (activeRow < 0) return;
239 QDomElement old = clipref->effectAt(activeRow).cloneNode().toElement();
241 QString effectName = ui.effectlist->currentItem()->text();
242 foreach(const QString &type, effectLists.keys()) {
243 EffectsList *list = effectLists[type];
244 if (list->effectNames().contains(effectName)) {
245 dom = list->getEffectByName(effectName);
250 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
251 emit transferParamDesc(dom, clipref->cropStart().frames(KdenliveSettings::project_fps()), clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
252 emit updateClipEffect(clipref, old, dom, activeRow);
257 void EffectStackView::raiseWindow(QWidget* dock) {
262 void EffectStackView::clear() {
263 ui.effectlist->clear();
264 ui.buttonDel->setEnabled(false);
265 ui.buttonSave->setEnabled(false);
266 ui.buttonReset->setEnabled(false);
267 ui.buttonUp->setEnabled(false);
268 ui.buttonDown->setEnabled(false);
269 effectedit->transferParamDesc(QDomElement(), 0, 0);
272 #include "effectstackview.moc"