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 "projectlist.h"
26 #include "kdenlivesettings.h"
30 #include <KMessageBox>
31 #include <KStandardDirs>
32 #include <KFileDialog>
35 #include <QTextStream>
37 #include <QInputDialog>
40 EffectStackView::EffectStackView(QWidget *parent) :
44 QVBoxLayout *vbox1 = new QVBoxLayout(m_ui.frame);
45 m_effectedit = new EffectStackEdit(m_ui.frame);
46 vbox1->setContentsMargins(0, 0, 0, 0);
48 vbox1->addWidget(m_effectedit);
49 m_ui.frame->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
50 m_ui.region_url->fileDialog()->setFilter(ProjectList::getExtensions());
51 //m_ui.effectlist->horizontalHeader()->setVisible(false);
52 //m_ui.effectlist->verticalHeader()->setVisible(false);
55 m_ui.buttonNew->setIcon(KIcon("document-new"));
56 m_ui.buttonNew->setToolTip(i18n("Add new effect"));
57 m_ui.buttonUp->setIcon(KIcon("go-up"));
58 m_ui.buttonUp->setToolTip(i18n("Move effect up"));
59 m_ui.buttonDown->setIcon(KIcon("go-down"));
60 m_ui.buttonDown->setToolTip(i18n("Move effect down"));
61 m_ui.buttonDel->setIcon(KIcon("edit-delete"));
62 m_ui.buttonDel->setToolTip(i18n("Delete effect"));
63 m_ui.buttonSave->setIcon(KIcon("document-save"));
64 m_ui.buttonSave->setToolTip(i18n("Save effect"));
65 m_ui.buttonReset->setIcon(KIcon("view-refresh"));
66 m_ui.buttonReset->setToolTip(i18n("Reset effect"));
67 m_ui.checkAll->setToolTip(i18n("Enable/Disable all effects"));
70 m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop); //use internal if drop is recognised right
72 connect(m_ui.region_url, SIGNAL(urlSelected(const KUrl &)), this , SLOT(slotRegionChanged()));
73 connect(m_ui.region_url, SIGNAL(returnPressed()), this , SLOT(slotRegionChanged()));
74 connect(m_ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
75 connect(m_ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
76 connect(m_ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
77 connect(m_ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
78 connect(m_ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
79 connect(m_ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
80 connect(m_ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
81 connect(m_ui.checkAll, SIGNAL(stateChanged(int)), this, SLOT(slotCheckAll(int)));
82 connect(m_effectedit, SIGNAL(parameterChanged(const QDomElement, const QDomElement)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement)));
83 connect(m_effectedit, SIGNAL(seekTimeline(int)), this , SLOT(slotSeekTimeline(int)));
84 m_effectLists["audio"] = &MainWindow::audioEffects;
85 m_effectLists["video"] = &MainWindow::videoEffects;
86 m_effectLists["custom"] = &MainWindow::customEffects;
87 m_ui.splitter->setStretchFactor(1, 10);
88 m_ui.splitter->setStretchFactor(0, 1);
92 EffectStackView::~EffectStackView()
94 m_effectLists.clear();
98 void EffectStackView::updateTimecodeFormat()
100 m_effectedit->updateTimecodeFormat();
103 void EffectStackView::setMenu(QMenu *menu)
105 m_ui.buttonNew->setMenu(menu);
108 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
110 m_effectedit->updateProjectFormat(profile, t);
113 void EffectStackView::slotSaveEffect()
115 QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
116 if (name.isEmpty()) return;
117 QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
118 path = path + name + ".xml";
119 if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
121 int i = m_ui.effectlist->currentRow();
123 QDomElement effect = m_clipref->effectAt(i).cloneNode().toElement();
124 doc.appendChild(doc.importNode(effect, true));
125 effect = doc.firstChild().toElement();
126 effect.removeAttribute("kdenlive_ix");
127 effect.setAttribute("id", name);
128 effect.setAttribute("type", "custom");
129 QDomElement effectname = effect.firstChildElement("name");
130 effect.removeChild(effectname);
131 effectname = doc.createElement("name");
132 QDomText nametext = doc.createTextNode(name);
133 effectname.appendChild(nametext);
134 effect.insertBefore(effectname, QDomNode());
135 QDomElement effectprops = effect.firstChildElement("properties");
136 effectprops.setAttribute("id", name);
137 effectprops.setAttribute("type", "custom");
140 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
141 QTextStream out(&file);
142 out << doc.toString();
145 emit reloadEffects();
148 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
151 emit updateClipEffect(m_clipref, old, e, m_ui.effectlist->currentRow());
154 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
156 if (c && !c->isEnabled()) return;
157 if (c && c == m_clipref) {
158 if (ix == -1) ix = m_ui.effectlist->currentRow();
159 //if (ix == -1 || ix == m_ui.effectlist->currentRow()) return;
163 ix = c->selectedEffectIndex();
164 QString size = c->baseClip()->getProperty("frame_size");
165 double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
166 QPoint p((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
167 m_effectedit->setFrameSize(p);
168 m_effectedit->setFrameSize(p);
171 if (m_clipref == NULL) {
172 m_ui.effectlist->blockSignals(true);
173 m_ui.effectlist->clear();
174 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
175 m_ui.region_url->clear();
176 m_ui.effectlist->blockSignals(false);
184 void EffectStackView::slotItemChanged(QListWidgetItem *item)
187 if (item->checkState() == Qt::Checked) disable = false;
188 m_ui.frame_layout->setEnabled(!disable);
189 m_ui.buttonReset->setEnabled(!disable);
190 int activeRow = m_ui.effectlist->currentRow();
191 if (activeRow >= 0) {
192 m_effectedit->updateParameter("disable", QString::number((int) disable));
193 emit changeEffectState(m_clipref, activeRow, disable);
195 slotUpdateCheckAllButton();
199 void EffectStackView::setupListView(int ix)
201 m_ui.effectlist->blockSignals(true);
202 m_ui.effectlist->clear();
204 // Issue 238: Add icons for effect type in effectstack.
205 KIcon videoIcon("kdenlive-show-video");
206 KIcon audioIcon("kdenlive-show-audio");
207 KIcon customIcon("kdenlive-custom-effect");
208 QListWidgetItem* item;
210 for (int i = 0; i < m_clipref->effectsCount(); i++) {
211 const QDomElement d = m_clipref->effectAt(i);
213 kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
218 doc.appendChild(doc.importNode(d, true));
219 kDebug() << "IMPORTED STK: " << doc.toString();*/
221 QDomNode namenode = d.elementsByTagName("name").item(0);
222 if (!namenode.isNull()) {
223 // Issue 238: Add icons for effect type in effectstack.
224 // Logic more or less copied from initeffects.cpp
225 QString type = d.attribute("type", QString());
226 if ("audio" == type) {
227 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
228 } else if ("custom" == type) {
229 item = new QListWidgetItem(customIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
231 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
233 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
234 if (d.attribute("disable") == "1") item->setCheckState(Qt::Unchecked);
235 else item->setCheckState(Qt::Checked);
238 if (m_ui.effectlist->count() == 0) {
239 m_ui.buttonDel->setEnabled(false);
240 m_ui.buttonSave->setEnabled(false);
241 m_ui.buttonReset->setEnabled(false);
242 m_ui.buttonUp->setEnabled(false);
243 m_ui.buttonDown->setEnabled(false);
244 m_ui.checkAll->setEnabled(false);
247 if (ix > m_ui.effectlist->count() - 1) ix = m_ui.effectlist->count() - 1;
248 m_ui.effectlist->setCurrentRow(ix);
249 m_ui.checkAll->setEnabled(true);
251 m_ui.effectlist->blockSignals(false);
252 if (m_ui.effectlist->count() == 0) {
253 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
254 m_ui.region_url->clear();
255 } else slotItemSelectionChanged(false);
256 slotUpdateCheckAllButton();
259 void EffectStackView::slotItemSelectionChanged(bool update)
261 bool hasItem = m_ui.effectlist->currentItem();
262 int activeRow = m_ui.effectlist->currentRow();
263 bool isChecked = false;
264 if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
265 if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
266 QDomElement eff = m_clipref->effectAt(activeRow);
267 m_effectedit->transferParamDesc(eff,
269 m_clipref->cropStart().frames(KdenliveSettings::project_fps()),
270 (m_clipref->cropStart() + m_clipref->cropDuration()).frames(KdenliveSettings::project_fps())); //minx max frame
271 m_ui.region_url->setUrl(KUrl(eff.attribute("region")));
273 if (m_clipref && update) m_clipref->setSelectedEffect(activeRow);
274 m_ui.buttonDel->setEnabled(hasItem);
275 m_ui.buttonSave->setEnabled(hasItem);
276 m_ui.buttonReset->setEnabled(hasItem && isChecked);
277 m_ui.buttonUp->setEnabled(activeRow > 0);
278 m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
279 m_ui.frame_layout->setEnabled(isChecked);
282 void EffectStackView::slotItemUp()
284 int activeRow = m_ui.effectlist->currentRow();
285 if (activeRow <= 0) return;
286 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
289 void EffectStackView::slotItemDown()
291 int activeRow = m_ui.effectlist->currentRow();
292 if (activeRow >= m_ui.effectlist->count() - 1) return;
293 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow + 2);
296 void EffectStackView::slotItemDel()
298 int activeRow = m_ui.effectlist->currentRow();
299 if (activeRow >= 0) {
300 emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
301 slotUpdateCheckAllButton();
305 void EffectStackView::slotResetEffect()
307 int activeRow = m_ui.effectlist->currentRow();
308 if (activeRow < 0) return;
309 QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
311 QString effectName = m_ui.effectlist->currentItem()->text();
312 foreach(const QString &type, m_effectLists.keys()) {
313 EffectsList *list = m_effectLists[type];
314 if (list->effectNames().contains(effectName)) {
315 dom = list->getEffectByName(effectName).cloneNode().toElement();
320 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
321 m_clipref->initEffect(dom);
322 m_effectedit->transferParamDesc(dom, 0, m_clipref->cropStart().frames(KdenliveSettings::project_fps()), (m_clipref->cropStart() + m_clipref->cropDuration()).frames(KdenliveSettings::project_fps()));//minx max frame
323 m_ui.region_url->setUrl(KUrl(dom.attribute("region")));
324 emit updateClipEffect(m_clipref, old, dom, activeRow);
329 void EffectStackView::raiseWindow(QWidget* dock)
331 if (m_clipref && dock)
335 void EffectStackView::clear()
337 m_ui.effectlist->blockSignals(true);
338 m_ui.effectlist->clear();
339 m_ui.buttonDel->setEnabled(false);
340 m_ui.buttonSave->setEnabled(false);
341 m_ui.buttonReset->setEnabled(false);
342 m_ui.buttonUp->setEnabled(false);
343 m_ui.buttonDown->setEnabled(false);
344 m_ui.checkAll->setEnabled(false);
345 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
346 m_ui.region_url->clear();
347 m_ui.effectlist->blockSignals(false);
351 void EffectStackView::slotSeekTimeline(int pos)
354 emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
357 void EffectStackView::slotUpdateCheckAllButton()
359 bool hasEnabled = false;
360 bool hasDisabled = false;
361 for (int i = 0; i < m_ui.effectlist->count(); ++i) {
362 if (m_ui.effectlist->item(i)->checkState() == Qt::Checked)
368 m_ui.checkAll->blockSignals(true);
369 if (hasEnabled && hasDisabled)
370 m_ui.checkAll->setCheckState(Qt::PartiallyChecked);
372 m_ui.checkAll->setCheckState(Qt::Checked);
374 m_ui.checkAll->setCheckState(Qt::Unchecked);
375 m_ui.checkAll->blockSignals(false);
378 void EffectStackView::slotCheckAll(int state)
382 m_ui.checkAll->blockSignals(true);
383 m_ui.checkAll->setCheckState(Qt::Checked);
384 m_ui.checkAll->blockSignals(false);
387 for (int i = 0; i < m_ui.effectlist->count(); ++i)
388 m_ui.effectlist->item(i)->setCheckState((Qt::CheckState)state);
391 void EffectStackView::slotRegionChanged()
393 emit updateClipRegion(m_clipref, m_ui.effectlist->currentRow(), m_ui.region_url->text());
396 #include "effectstackview.moc"