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 "docclipbase.h"
24 #include "kdenlivesettings.h"
28 #include <KMessageBox>
29 #include <KStandardDirs>
32 #include <QTextStream>
34 #include <QInputDialog>
37 EffectStackView::EffectStackView(QWidget *parent) :
41 m_effectedit = new EffectStackEdit(m_ui.frame);
42 //m_ui.effectlist->horizontalHeader()->setVisible(false);
43 //m_ui.effectlist->verticalHeader()->setVisible(false);
46 m_ui.buttonNew->setIcon(KIcon("document-new"));
47 m_ui.buttonNew->setToolTip(i18n("Add new effect"));
48 m_ui.buttonUp->setIcon(KIcon("go-up"));
49 m_ui.buttonUp->setToolTip(i18n("Move effect up"));
50 m_ui.buttonDown->setIcon(KIcon("go-down"));
51 m_ui.buttonDown->setToolTip(i18n("Move effect down"));
52 m_ui.buttonDel->setIcon(KIcon("edit-delete"));
53 m_ui.buttonDel->setToolTip(i18n("Delete effect"));
54 m_ui.buttonSave->setIcon(KIcon("document-save"));
55 m_ui.buttonSave->setToolTip(i18n("Save effect"));
56 m_ui.buttonReset->setIcon(KIcon("view-refresh"));
57 m_ui.buttonReset->setToolTip(i18n("Reset effect"));
60 m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop);//use internal if drop is recognised right
62 connect(m_ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
63 connect(m_ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
64 connect(m_ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
65 connect(m_ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
66 connect(m_ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
67 connect(m_ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
68 connect(m_ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
69 connect(m_effectedit, SIGNAL(parameterChanged(const QDomElement, const QDomElement)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement)));
70 m_effectLists["audio"] = &MainWindow::audioEffects;
71 m_effectLists["video"] = &MainWindow::videoEffects;
72 m_effectLists["custom"] = &MainWindow::customEffects;
73 m_ui.splitter->setStretchFactor(1, 10);
74 m_ui.splitter->setStretchFactor(0, 1);
78 EffectStackView::~EffectStackView()
80 m_effectLists.clear();
84 void EffectStackView::setMenu(QMenu *menu)
86 m_ui.buttonNew->setMenu(menu);
89 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
91 m_effectedit->updateProjectFormat(profile, t);
94 void EffectStackView::slotSaveEffect()
96 QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
97 if (name.isEmpty()) return;
98 QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
99 path = path + name + ".xml";
100 if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
102 int i = m_ui.effectlist->currentRow();
104 QDomElement effect = m_clipref->effectAt(i).cloneNode().toElement();
105 doc.appendChild(doc.importNode(effect, true));
106 effect = doc.firstChild().toElement();
107 effect.removeAttribute("kdenlive_ix");
108 effect.setAttribute("id", name);
109 effect.setAttribute("type", "custom");
110 QDomElement effectname = effect.firstChildElement("name");
111 effect.removeChild(effectname);
112 effectname = doc.createElement("name");
113 QDomText nametext = doc.createTextNode(name);
114 effectname.appendChild(nametext);
115 effect.insertBefore(effectname, QDomNode());
116 QDomElement effectprops = effect.firstChildElement("properties");
117 effectprops.setAttribute("id", name);
118 effectprops.setAttribute("type", "custom");
122 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
123 QTextStream out(&file);
124 out << doc.toString();
127 emit reloadEffects();
130 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
133 emit updateClipEffect(m_clipref, old, e, m_ui.effectlist->currentRow());
136 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
138 if (c && c == m_clipref) {
139 if (ix == -1) ix = m_ui.effectlist->currentRow();
143 ix = c->selectedEffectIndex();
144 QString size = c->baseClip()->getProperty("frame_size");
145 QPoint p(size.section('x', 0, 0).toInt(), size.section('x', 1, 1).toInt());
146 m_effectedit->setFrameSize(p);
149 if (m_clipref == NULL) {
150 m_ui.effectlist->blockSignals(true);
151 m_ui.effectlist->clear();
152 m_effectedit->transferParamDesc(QDomElement(), 0, 0);
153 m_ui.effectlist->blockSignals(false);
161 void EffectStackView::slotItemChanged(QListWidgetItem *item)
164 if (item->checkState() == Qt::Checked) disable = false;
165 m_ui.frame->setEnabled(!disable);
166 m_ui.buttonReset->setEnabled(!disable);
167 int activeRow = m_ui.effectlist->currentRow();
168 if (activeRow >= 0) {
169 emit changeEffectState(m_clipref, activeRow, disable);
174 void EffectStackView::setupListView(int ix)
176 m_ui.effectlist->blockSignals(true);
177 m_ui.effectlist->clear();
179 // Issue 238: Add icons for effect type in effectstack.
180 KIcon videoIcon("kdenlive-show-video");
181 KIcon audioIcon("kdenlive-show-audio");
182 QListWidgetItem* item;
184 for (int i = 0; i < m_clipref->effectsCount(); i++) {
185 const QDomElement d = m_clipref->effectAt(i);
187 kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
192 doc.appendChild(doc.importNode(d, true));
193 kDebug() << "IMPORTED STK: " << doc.toString();*/
195 QDomNode namenode = d.elementsByTagName("name").item(0);
196 if (!namenode.isNull()) {
197 // Issue 238: Add icons for effect type in effectstack.
198 // Logic more or less copied from initeffects.cpp
199 QString type = d.attribute("type", QString());
200 if ("audio" == type) {
201 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
202 } else if ("custom" == type) {
203 item = new QListWidgetItem(i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
205 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
207 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
208 if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
209 else item->setCheckState(Qt::Checked);
212 if (m_ui.effectlist->count() == 0) {
213 m_ui.buttonDel->setEnabled(false);
214 m_ui.buttonSave->setEnabled(false);
215 m_ui.buttonReset->setEnabled(false);
216 m_ui.buttonUp->setEnabled(false);
217 m_ui.buttonDown->setEnabled(false);
220 if (ix > m_ui.effectlist->count() - 1) ix = m_ui.effectlist->count() - 1;
221 m_ui.effectlist->setCurrentRow(ix);
223 m_ui.effectlist->blockSignals(false);
224 if (m_ui.effectlist->count() == 0) m_effectedit->transferParamDesc(QDomElement(), 0, 0);
225 else slotItemSelectionChanged(false);
228 void EffectStackView::slotItemSelectionChanged(bool update)
230 bool hasItem = m_ui.effectlist->currentItem();
231 int activeRow = m_ui.effectlist->currentRow();
232 bool isChecked = false;
233 if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
234 if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
235 m_effectedit->transferParamDesc(m_clipref->effectAt(activeRow), m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
237 if (m_clipref && update) m_clipref->setSelectedEffect(activeRow);
238 m_ui.buttonDel->setEnabled(hasItem);
239 m_ui.buttonSave->setEnabled(hasItem);
240 m_ui.buttonReset->setEnabled(hasItem && isChecked);
241 m_ui.buttonUp->setEnabled(activeRow > 0);
242 m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
243 m_ui.frame->setEnabled(isChecked);
246 void EffectStackView::slotItemUp()
248 int activeRow = m_ui.effectlist->currentRow();
249 if (activeRow <= 0) return;
250 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
253 void EffectStackView::slotItemDown()
255 int activeRow = m_ui.effectlist->currentRow();
256 if (activeRow >= m_ui.effectlist->count() - 1) return;
257 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow + 2);
260 void EffectStackView::slotItemDel()
262 int activeRow = m_ui.effectlist->currentRow();
263 if (activeRow >= 0) {
264 emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
268 void EffectStackView::slotResetEffect()
270 int activeRow = m_ui.effectlist->currentRow();
271 if (activeRow < 0) return;
272 QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
274 QString effectName = m_ui.effectlist->currentItem()->text();
275 foreach(const QString &type, m_effectLists.keys()) {
276 EffectsList *list = m_effectLists[type];
277 if (list->effectNames().contains(effectName)) {
278 dom = list->getEffectByName(effectName).cloneNode().toElement();
283 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
284 m_clipref->initEffect(dom);
285 m_effectedit->transferParamDesc(dom, m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
286 emit updateClipEffect(m_clipref, old, dom, activeRow);
291 void EffectStackView::raiseWindow(QWidget* dock)
293 if (m_clipref && dock)
297 void EffectStackView::clear()
299 m_ui.effectlist->blockSignals(true);
300 m_ui.effectlist->clear();
301 m_ui.buttonDel->setEnabled(false);
302 m_ui.buttonSave->setEnabled(false);
303 m_ui.buttonReset->setEnabled(false);
304 m_ui.buttonUp->setEnabled(false);
305 m_ui.buttonDown->setEnabled(false);
306 m_effectedit->transferParamDesc(QDomElement(), 0, 0);
307 m_ui.effectlist->blockSignals(false);
310 #include "effectstackview.moc"