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 double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
146 QPoint p((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
147 m_effectedit->setFrameSize(p);
150 if (m_clipref == NULL) {
151 m_ui.effectlist->blockSignals(true);
152 m_ui.effectlist->clear();
153 m_effectedit->transferParamDesc(QDomElement(), 0, 0);
154 m_ui.effectlist->blockSignals(false);
162 void EffectStackView::slotItemChanged(QListWidgetItem *item)
165 if (item->checkState() == Qt::Checked) disable = false;
166 m_ui.frame->setEnabled(!disable);
167 m_ui.buttonReset->setEnabled(!disable);
168 int activeRow = m_ui.effectlist->currentRow();
169 if (activeRow >= 0) {
170 emit changeEffectState(m_clipref, activeRow, disable);
175 void EffectStackView::setupListView(int ix)
177 m_ui.effectlist->blockSignals(true);
178 m_ui.effectlist->clear();
180 // Issue 238: Add icons for effect type in effectstack.
181 KIcon videoIcon("kdenlive-show-video");
182 KIcon audioIcon("kdenlive-show-audio");
183 QListWidgetItem* item;
185 for (int i = 0; i < m_clipref->effectsCount(); i++) {
186 const QDomElement d = m_clipref->effectAt(i);
188 kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
193 doc.appendChild(doc.importNode(d, true));
194 kDebug() << "IMPORTED STK: " << doc.toString();*/
196 QDomNode namenode = d.elementsByTagName("name").item(0);
197 if (!namenode.isNull()) {
198 // Issue 238: Add icons for effect type in effectstack.
199 // Logic more or less copied from initeffects.cpp
200 QString type = d.attribute("type", QString());
201 if ("audio" == type) {
202 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
203 } else if ("custom" == type) {
204 item = new QListWidgetItem(i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
206 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
208 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
209 if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
210 else item->setCheckState(Qt::Checked);
213 if (m_ui.effectlist->count() == 0) {
214 m_ui.buttonDel->setEnabled(false);
215 m_ui.buttonSave->setEnabled(false);
216 m_ui.buttonReset->setEnabled(false);
217 m_ui.buttonUp->setEnabled(false);
218 m_ui.buttonDown->setEnabled(false);
221 if (ix > m_ui.effectlist->count() - 1) ix = m_ui.effectlist->count() - 1;
222 m_ui.effectlist->setCurrentRow(ix);
224 m_ui.effectlist->blockSignals(false);
225 if (m_ui.effectlist->count() == 0) m_effectedit->transferParamDesc(QDomElement(), 0, 0);
226 else slotItemSelectionChanged(false);
229 void EffectStackView::slotItemSelectionChanged(bool update)
231 bool hasItem = m_ui.effectlist->currentItem();
232 int activeRow = m_ui.effectlist->currentRow();
233 bool isChecked = false;
234 if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
235 if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
236 m_effectedit->transferParamDesc(m_clipref->effectAt(activeRow), m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
238 if (m_clipref && update) m_clipref->setSelectedEffect(activeRow);
239 m_ui.buttonDel->setEnabled(hasItem);
240 m_ui.buttonSave->setEnabled(hasItem);
241 m_ui.buttonReset->setEnabled(hasItem && isChecked);
242 m_ui.buttonUp->setEnabled(activeRow > 0);
243 m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
244 m_ui.frame->setEnabled(isChecked);
247 void EffectStackView::slotItemUp()
249 int activeRow = m_ui.effectlist->currentRow();
250 if (activeRow <= 0) return;
251 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
254 void EffectStackView::slotItemDown()
256 int activeRow = m_ui.effectlist->currentRow();
257 if (activeRow >= m_ui.effectlist->count() - 1) return;
258 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow + 2);
261 void EffectStackView::slotItemDel()
263 int activeRow = m_ui.effectlist->currentRow();
264 if (activeRow >= 0) {
265 emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
269 void EffectStackView::slotResetEffect()
271 int activeRow = m_ui.effectlist->currentRow();
272 if (activeRow < 0) return;
273 QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
275 QString effectName = m_ui.effectlist->currentItem()->text();
276 foreach(const QString &type, m_effectLists.keys()) {
277 EffectsList *list = m_effectLists[type];
278 if (list->effectNames().contains(effectName)) {
279 dom = list->getEffectByName(effectName).cloneNode().toElement();
284 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
285 m_clipref->initEffect(dom);
286 m_effectedit->transferParamDesc(dom, m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
287 emit updateClipEffect(m_clipref, old, dom, activeRow);
292 void EffectStackView::raiseWindow(QWidget* dock)
294 if (m_clipref && dock)
298 void EffectStackView::clear()
300 m_ui.effectlist->blockSignals(true);
301 m_ui.effectlist->clear();
302 m_ui.buttonDel->setEnabled(false);
303 m_ui.buttonSave->setEnabled(false);
304 m_ui.buttonReset->setEnabled(false);
305 m_ui.buttonUp->setEnabled(false);
306 m_ui.buttonDown->setEnabled(false);
307 m_effectedit->transferParamDesc(QDomElement(), 0, 0);
308 m_ui.effectlist->blockSignals(false);
311 #include "effectstackview.moc"