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 "monitorscene.h"
27 #include "kdenlivesettings.h"
31 #include <KMessageBox>
32 #include <KStandardDirs>
33 #include <KFileDialog>
36 #include <QTextStream>
38 #include <QInputDialog>
41 EffectStackView::EffectStackView(Monitor *monitor, QWidget *parent) :
46 QVBoxLayout *vbox1 = new QVBoxLayout(m_ui.frame);
47 m_effectedit = new EffectStackEdit(monitor, m_ui.frame);
48 vbox1->setContentsMargins(0, 0, 0, 0);
50 vbox1->addWidget(m_effectedit);
51 m_ui.frame->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
52 m_ui.region_url->fileDialog()->setFilter(ProjectList::getExtensions());
53 //m_ui.effectlist->horizontalHeader()->setVisible(false);
54 //m_ui.effectlist->verticalHeader()->setVisible(false);
57 m_ui.buttonNew->setIcon(KIcon("document-new"));
58 m_ui.buttonNew->setToolTip(i18n("Add new effect"));
59 m_ui.buttonUp->setIcon(KIcon("go-up"));
60 m_ui.buttonUp->setToolTip(i18n("Move effect up"));
61 m_ui.buttonDown->setIcon(KIcon("go-down"));
62 m_ui.buttonDown->setToolTip(i18n("Move effect down"));
63 m_ui.buttonDel->setIcon(KIcon("edit-delete"));
64 m_ui.buttonDel->setToolTip(i18n("Delete effect"));
65 m_ui.buttonSave->setIcon(KIcon("document-save"));
66 m_ui.buttonSave->setToolTip(i18n("Save effect"));
67 m_ui.buttonReset->setIcon(KIcon("view-refresh"));
68 m_ui.buttonReset->setToolTip(i18n("Reset effect"));
69 m_ui.checkAll->setToolTip(i18n("Enable/Disable all effects"));
72 m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop); //use internal if drop is recognised right
74 connect(m_ui.region_url, SIGNAL(urlSelected(const KUrl &)), this , SLOT(slotRegionChanged()));
75 connect(m_ui.region_url, SIGNAL(returnPressed()), this , SLOT(slotRegionChanged()));
76 connect(m_ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
77 connect(m_ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
78 connect(m_ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
79 connect(m_ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
80 connect(m_ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
81 connect(m_ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
82 connect(m_ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
83 connect(m_ui.checkAll, SIGNAL(stateChanged(int)), this, SLOT(slotCheckAll(int)));
84 connect(m_effectedit, SIGNAL(parameterChanged(const QDomElement, const QDomElement)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement)));
85 connect(m_effectedit, SIGNAL(seekTimeline(int)), this , SLOT(slotSeekTimeline(int)));
86 connect(m_effectedit, SIGNAL(displayMessage(const QString&, int)), this, SIGNAL(displayMessage(const QString&, int)));
87 connect(m_effectedit, SIGNAL(checkMonitorPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
88 connect(monitor, SIGNAL(renderPosition(int)), this, SLOT(slotRenderPos(int)));
89 m_effectLists["audio"] = &MainWindow::audioEffects;
90 m_effectLists["video"] = &MainWindow::videoEffects;
91 m_effectLists["custom"] = &MainWindow::customEffects;
92 m_ui.splitter->setStretchFactor(1, 10);
93 m_ui.splitter->setStretchFactor(0, 1);
97 EffectStackView::~EffectStackView()
99 m_effectLists.clear();
103 void EffectStackView::updateTimecodeFormat()
105 m_effectedit->updateTimecodeFormat();
108 void EffectStackView::setMenu(QMenu *menu)
110 m_ui.buttonNew->setMenu(menu);
113 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
115 m_effectedit->updateProjectFormat(profile, t);
118 void EffectStackView::slotSaveEffect()
120 QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
121 if (name.isEmpty()) return;
122 QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
123 path = path + name + ".xml";
124 if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
126 int i = m_ui.effectlist->currentRow();
128 QDomElement effect = m_clipref->effectAt(i).cloneNode().toElement();
129 doc.appendChild(doc.importNode(effect, true));
130 effect = doc.firstChild().toElement();
131 effect.removeAttribute("kdenlive_ix");
132 effect.setAttribute("id", name);
133 effect.setAttribute("type", "custom");
134 QDomElement effectname = effect.firstChildElement("name");
135 effect.removeChild(effectname);
136 effectname = doc.createElement("name");
137 QDomText nametext = doc.createTextNode(name);
138 effectname.appendChild(nametext);
139 effect.insertBefore(effectname, QDomNode());
140 QDomElement effectprops = effect.firstChildElement("properties");
141 effectprops.setAttribute("id", name);
142 effectprops.setAttribute("type", "custom");
145 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
146 QTextStream out(&file);
147 out << doc.toString();
150 emit reloadEffects();
153 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
156 emit updateClipEffect(m_clipref, old, e, m_ui.effectlist->currentRow());
159 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
161 if (c && !c->isEnabled()) return;
162 if (c && c == m_clipref) {
163 if (ix == -1) ix = m_ui.effectlist->currentRow();
164 //if (ix == -1 || ix == m_ui.effectlist->currentRow()) return;
168 ix = c->selectedEffectIndex();
169 QString size = c->baseClip()->getProperty("frame_size");
170 double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
171 QPoint p((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
172 m_effectedit->setFrameSize(p);
173 m_effectedit->setFrameSize(p);
176 if (m_clipref == NULL) {
177 m_ui.effectlist->blockSignals(true);
178 m_ui.effectlist->clear();
179 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
180 m_ui.region_url->clear();
181 m_ui.effectlist->blockSignals(false);
189 void EffectStackView::slotItemChanged(QListWidgetItem *item)
192 if (item->checkState() == Qt::Checked) disable = false;
193 m_ui.frame_layout->setEnabled(!disable);
194 m_ui.buttonReset->setEnabled(!disable);
195 int activeRow = m_ui.effectlist->currentRow();
196 if (activeRow >= 0) {
197 m_effectedit->updateParameter("disable", QString::number((int) disable));
198 emit changeEffectState(m_clipref, activeRow, disable);
200 slotUpdateCheckAllButton();
204 void EffectStackView::setupListView(int ix)
206 m_ui.effectlist->blockSignals(true);
207 m_ui.effectlist->clear();
209 // Issue 238: Add icons for effect type in effectstack.
210 KIcon videoIcon("kdenlive-show-video");
211 KIcon audioIcon("kdenlive-show-audio");
212 KIcon customIcon("kdenlive-custom-effect");
213 QListWidgetItem* item;
215 for (int i = 0; i < m_clipref->effectsCount(); i++) {
216 const QDomElement d = m_clipref->effectAt(i);
218 kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
223 doc.appendChild(doc.importNode(d, true));
224 kDebug() << "IMPORTED STK: " << doc.toString();*/
226 QDomNode namenode = d.elementsByTagName("name").item(0);
227 if (!namenode.isNull()) {
228 // Issue 238: Add icons for effect type in effectstack.
229 // Logic more or less copied from initeffects.cpp
230 QString type = d.attribute("type", QString());
231 if ("audio" == type) {
232 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
233 } else if ("custom" == type) {
234 item = new QListWidgetItem(customIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
236 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
238 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
239 if (d.attribute("disable") == "1") item->setCheckState(Qt::Unchecked);
240 else item->setCheckState(Qt::Checked);
243 if (m_ui.effectlist->count() == 0) {
244 m_ui.buttonDel->setEnabled(false);
245 m_ui.buttonSave->setEnabled(false);
246 m_ui.buttonReset->setEnabled(false);
247 m_ui.buttonUp->setEnabled(false);
248 m_ui.buttonDown->setEnabled(false);
249 m_ui.checkAll->setEnabled(false);
252 if (ix > m_ui.effectlist->count() - 1) ix = m_ui.effectlist->count() - 1;
253 m_ui.effectlist->setCurrentRow(ix);
254 m_ui.checkAll->setEnabled(true);
256 m_ui.effectlist->blockSignals(false);
257 if (m_ui.effectlist->count() == 0) {
258 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
259 m_ui.region_url->clear();
260 } else slotItemSelectionChanged(false);
261 slotUpdateCheckAllButton();
264 void EffectStackView::slotItemSelectionChanged(bool update)
266 bool hasItem = m_ui.effectlist->currentItem();
267 int activeRow = m_ui.effectlist->currentRow();
268 bool isChecked = false;
269 if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
270 if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
271 QDomElement eff = m_clipref->effectAt(activeRow);
272 m_effectedit->transferParamDesc(eff,
274 m_clipref->cropStart().frames(KdenliveSettings::project_fps()),
275 (m_clipref->cropStart() + m_clipref->cropDuration()).frames(KdenliveSettings::project_fps())); //minx max frame
276 m_ui.region_url->setUrl(KUrl(eff.attribute("region")));
278 if (m_clipref && update) m_clipref->setSelectedEffect(activeRow);
279 m_ui.buttonDel->setEnabled(hasItem);
280 m_ui.buttonSave->setEnabled(hasItem);
281 m_ui.buttonReset->setEnabled(hasItem && isChecked);
282 m_ui.buttonUp->setEnabled(activeRow > 0);
283 m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
284 m_ui.frame_layout->setEnabled(isChecked);
287 void EffectStackView::slotItemUp()
289 int activeRow = m_ui.effectlist->currentRow();
290 if (activeRow <= 0) return;
291 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
294 void EffectStackView::slotItemDown()
296 int activeRow = m_ui.effectlist->currentRow();
297 if (activeRow >= m_ui.effectlist->count() - 1) return;
298 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow + 2);
301 void EffectStackView::slotItemDel()
303 int activeRow = m_ui.effectlist->currentRow();
304 if (activeRow >= 0) {
305 emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
306 slotUpdateCheckAllButton();
310 void EffectStackView::slotResetEffect()
312 int activeRow = m_ui.effectlist->currentRow();
313 if (activeRow < 0) return;
314 QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
316 QString effectName = m_ui.effectlist->currentItem()->text();
317 foreach(const QString &type, m_effectLists.keys()) {
318 EffectsList *list = m_effectLists[type];
319 if (list->effectNames().contains(effectName)) {
320 dom = list->getEffectByName(effectName).cloneNode().toElement();
325 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
326 m_clipref->initEffect(dom);
327 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
328 m_ui.region_url->setUrl(KUrl(dom.attribute("region")));
329 emit updateClipEffect(m_clipref, old, dom, activeRow);
334 void EffectStackView::raiseWindow(QWidget* dock)
336 if (m_clipref && dock)
340 void EffectStackView::clear()
342 m_ui.effectlist->blockSignals(true);
343 m_ui.effectlist->clear();
344 m_ui.buttonDel->setEnabled(false);
345 m_ui.buttonSave->setEnabled(false);
346 m_ui.buttonReset->setEnabled(false);
347 m_ui.buttonUp->setEnabled(false);
348 m_ui.buttonDown->setEnabled(false);
349 m_ui.checkAll->setEnabled(false);
350 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
351 m_ui.region_url->clear();
352 m_ui.effectlist->blockSignals(false);
356 void EffectStackView::slotSeekTimeline(int pos)
359 emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
362 void EffectStackView::slotUpdateCheckAllButton()
364 bool hasEnabled = false;
365 bool hasDisabled = false;
366 for (int i = 0; i < m_ui.effectlist->count(); ++i) {
367 if (m_ui.effectlist->item(i)->checkState() == Qt::Checked)
373 m_ui.checkAll->blockSignals(true);
374 if (hasEnabled && hasDisabled)
375 m_ui.checkAll->setCheckState(Qt::PartiallyChecked);
377 m_ui.checkAll->setCheckState(Qt::Checked);
379 m_ui.checkAll->setCheckState(Qt::Unchecked);
380 m_ui.checkAll->blockSignals(false);
383 void EffectStackView::slotCheckAll(int state)
387 m_ui.checkAll->blockSignals(true);
388 m_ui.checkAll->setCheckState(Qt::Checked);
389 m_ui.checkAll->blockSignals(false);
392 for (int i = 0; i < m_ui.effectlist->count(); ++i)
393 m_ui.effectlist->item(i)->setCheckState((Qt::CheckState)state);
396 void EffectStackView::slotRegionChanged()
398 emit updateClipRegion(m_clipref, m_ui.effectlist->currentRow(), m_ui.region_url->text());
401 void EffectStackView::slotCheckMonitorPosition(int renderPos)
403 if (renderPos >= m_clipref->startPos().frames(KdenliveSettings::project_fps()) && renderPos <= m_clipref->endPos().frames(KdenliveSettings::project_fps())) {
404 if (!m_monitor->getEffectScene()->views().at(0)->isVisible())
405 m_monitor->slotEffectScene(true);
407 m_monitor->slotEffectScene(false);
411 void EffectStackView::slotRenderPos(int pos)
413 if (m_clipref && m_effectedit)
414 m_effectedit->slotSyncEffectsPos(pos - m_clipref->startPos().frames(KdenliveSettings::project_fps()));
417 #include "effectstackview.moc"