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"
33 #include "kdenlivesettings.h"
35 EffectStackView::EffectStackView(QWidget *parent)
38 effectedit = new EffectStackEdit(ui.frame);
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.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);
76 void EffectStackView::setMenu(QMenu *menu) {
77 ui.buttonNew->setMenu(menu);
80 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t) {
81 effectedit->updateProjectFormat(profile, t);
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;
91 int i = ui.effectlist->currentRow();
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");
111 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
112 QTextStream out(&file);
113 out << doc.toString();
116 emit reloadEffects();
119 void EffectStackView::slotUpdateEffectParams(const QDomElement& old, const QDomElement& e) {
121 emit updateClipEffect(clipref, old, e, ui.effectlist->currentRow());
124 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix) {
125 if (c && c == clipref) {
126 if (ix == -1) ix = ui.effectlist->currentRow();
129 if (c) ix = c->selectedEffectIndex();
132 if (clipref == NULL) {
133 ui.effectlist->clear();
134 effectedit->transferParamDesc(QDomElement(), 0, 0);
142 void EffectStackView::slotItemChanged(QListWidgetItem *item) {
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);
153 void EffectStackView::setupListView(int ix) {
154 ui.effectlist->clear();
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;
161 for (int i = 0;i < clipref->effectsCount();i++) {
162 QDomElement d = clipref->effectAt(i);
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);
174 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
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);
181 if (clipref->effectsCount() == 0) {
182 emit transferParamDesc(QDomElement(), 0, 0);
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);
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);
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), clipref->cropStart().frames(KdenliveSettings::project_fps()), clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
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);
216 void EffectStackView::slotItemUp() {
217 int activeRow = ui.effectlist->currentRow();
218 if (activeRow <= 0) return;
219 emit changeEffectPosition(clipref, activeRow + 1, activeRow);
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);
228 void EffectStackView::slotItemDel() {
229 int activeRow = ui.effectlist->currentRow();
230 if (activeRow >= 0) {
231 emit removeEffect(clipref, clipref->effectAt(activeRow));
235 void EffectStackView::slotResetEffect() {
236 int activeRow = ui.effectlist->currentRow();
237 if (activeRow < 0) return;
238 QDomElement old = clipref->effectAt(activeRow).cloneNode().toElement();
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);
249 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
250 emit transferParamDesc(dom, clipref->cropStart().frames(KdenliveSettings::project_fps()), clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
251 emit updateClipEffect(clipref, old, dom, activeRow);
256 void EffectStackView::raiseWindow(QWidget* dock) {
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);
271 #include "effectstackview.moc"