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);
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;
76 void EffectStackView::setMenu(QMenu *menu) {
77 ui.buttonNew->setMenu(menu);
80 void EffectStackView::updateProjectFormat(MltVideoProfile profile) {
81 effectedit->updateProjectFormat(profile);
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();
155 for (int i = 0;i < clipref->effectsCount();i++) {
156 QDomElement d = clipref->effectAt(i);
157 // Issue 238: Add icons for effect type in effectstack.
158 KIcon videoIcon("kdenlive-show-video");
159 KIcon audioIcon("kdenlive-show-audio");
161 QDomNode namenode = d.elementsByTagName("name").item(0);
162 if (!namenode.isNull()) {
163 // Issue 238: Add icons for effect type in effectstack.
164 // Logic more or less copied from initeffects.cpp
165 QString type = d.attribute("type", QString::null);
166 QListWidgetItem* item;
167 if ("audio" == type) {
168 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
169 } else if ( "custom" == type) {
170 item = new QListWidgetItem(i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
172 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), ui.effectlist);
174 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
175 if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
176 else item->setCheckState(Qt::Checked);
179 if (clipref->effectsCount() == 0) {
180 emit transferParamDesc(QDomElement(), 0, 100);
181 ui.buttonDel->setEnabled(false);
182 ui.buttonSave->setEnabled(false);
183 ui.buttonReset->setEnabled(false);
184 ui.buttonUp->setEnabled(false);
185 ui.buttonDown->setEnabled(false);
188 if (ix > ui.effectlist->count() - 1) ix = ui.effectlist->count() - 1;
189 ui.effectlist->setCurrentRow(ix);
190 ui.buttonDel->setEnabled(true);
191 ui.buttonSave->setEnabled(true);
192 ui.buttonReset->setEnabled(true);
193 ui.buttonUp->setEnabled(ix > 0);
194 ui.buttonDown->setEnabled(ix < clipref->effectsCount() - 1);
198 void EffectStackView::slotItemSelectionChanged() {
199 bool hasItem = ui.effectlist->currentItem();
200 int activeRow = ui.effectlist->currentRow();
201 bool isChecked = false;
202 if (hasItem && ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
203 if (hasItem && ui.effectlist->currentItem()->isSelected()) {
204 emit transferParamDesc(clipref->effectAt(activeRow), 0, 100);//minx max frame
206 if (clipref) clipref->setSelectedEffect(activeRow);
207 ui.buttonDel->setEnabled(hasItem);
208 ui.buttonSave->setEnabled(hasItem);
209 ui.buttonReset->setEnabled(hasItem && isChecked);
210 ui.buttonUp->setEnabled(activeRow > 0);
211 ui.buttonDown->setEnabled((activeRow < ui.effectlist->count() - 1) && hasItem);
214 void EffectStackView::slotItemUp() {
215 int activeRow = ui.effectlist->currentRow();
216 if (activeRow <= 0) return;
217 emit changeEffectPosition(clipref, activeRow + 1, activeRow);
220 void EffectStackView::slotItemDown() {
221 int activeRow = ui.effectlist->currentRow();
222 if (activeRow >= ui.effectlist->count() - 1) return;
223 emit changeEffectPosition(clipref, activeRow + 1, activeRow + 2);
226 void EffectStackView::slotItemDel() {
227 int activeRow = ui.effectlist->currentRow();
228 if (activeRow >= 0) {
229 emit removeEffect(clipref, clipref->effectAt(activeRow));
233 void EffectStackView::slotResetEffect() {
234 int activeRow = ui.effectlist->currentRow();
235 if (activeRow < 0) return;
236 QDomElement old = clipref->effectAt(activeRow).cloneNode().toElement();
238 QString effectName = ui.effectlist->currentItem()->text();
239 foreach(const QString &type, effectLists.keys()) {
240 EffectsList *list = effectLists[type];
241 if (list->effectNames().contains(effectName)) {
242 dom = list->getEffectByName(effectName);
247 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
248 emit transferParamDesc(dom, 0, 100);//minx max frame
249 emit updateClipEffect(clipref, old, dom, activeRow);
254 void EffectStackView::raiseWindow(QWidget* dock) {
259 void EffectStackView::clear() {
260 ui.effectlist->clear();
261 ui.buttonDel->setEnabled(false);
262 ui.buttonSave->setEnabled(false);
263 ui.buttonReset->setEnabled(false);
264 ui.buttonUp->setEnabled(false);
265 ui.buttonDown->setEnabled(false);
266 effectedit->transferParamDesc(QDomElement(), 0, 0);
269 #include "effectstackview.moc"