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 m_effectLists["audio"] = &MainWindow::audioEffects;
89 m_effectLists["video"] = &MainWindow::videoEffects;
90 m_effectLists["custom"] = &MainWindow::customEffects;
91 m_ui.splitter->setStretchFactor(1, 10);
92 m_ui.splitter->setStretchFactor(0, 1);
96 EffectStackView::~EffectStackView()
98 m_effectLists.clear();
102 void EffectStackView::updateTimecodeFormat()
104 m_effectedit->updateTimecodeFormat();
107 void EffectStackView::setMenu(QMenu *menu)
109 m_ui.buttonNew->setMenu(menu);
112 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
114 m_effectedit->updateProjectFormat(profile, t);
117 void EffectStackView::slotSaveEffect()
119 QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
120 if (name.isEmpty()) return;
121 QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
122 path = path + name + ".xml";
123 if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
125 int i = m_ui.effectlist->currentRow();
127 QDomElement effect = m_clipref->effectAt(i).cloneNode().toElement();
128 doc.appendChild(doc.importNode(effect, true));
129 effect = doc.firstChild().toElement();
130 effect.removeAttribute("kdenlive_ix");
131 effect.setAttribute("id", name);
132 effect.setAttribute("type", "custom");
133 QDomElement effectname = effect.firstChildElement("name");
134 effect.removeChild(effectname);
135 effectname = doc.createElement("name");
136 QDomText nametext = doc.createTextNode(name);
137 effectname.appendChild(nametext);
138 effect.insertBefore(effectname, QDomNode());
139 QDomElement effectprops = effect.firstChildElement("properties");
140 effectprops.setAttribute("id", name);
141 effectprops.setAttribute("type", "custom");
144 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
145 QTextStream out(&file);
146 out << doc.toString();
149 emit reloadEffects();
152 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
155 emit updateClipEffect(m_clipref, old, e, m_ui.effectlist->currentRow());
158 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
160 if (c && !c->isEnabled()) return;
161 if (c && c == m_clipref) {
162 if (ix == -1) ix = m_ui.effectlist->currentRow();
163 //if (ix == -1 || ix == m_ui.effectlist->currentRow()) return;
167 ix = c->selectedEffectIndex();
168 QString size = c->baseClip()->getProperty("frame_size");
169 double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
170 QPoint p((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
171 m_effectedit->setFrameSize(p);
172 m_effectedit->setFrameSize(p);
175 if (m_clipref == NULL) {
176 m_ui.effectlist->blockSignals(true);
177 m_ui.effectlist->clear();
178 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
179 m_ui.region_url->clear();
180 m_ui.effectlist->blockSignals(false);
188 void EffectStackView::slotItemChanged(QListWidgetItem *item)
191 if (item->checkState() == Qt::Checked) disable = false;
192 m_ui.frame_layout->setEnabled(!disable);
193 m_ui.buttonReset->setEnabled(!disable);
194 int activeRow = m_ui.effectlist->currentRow();
195 if (activeRow >= 0) {
196 m_effectedit->updateParameter("disable", QString::number((int) disable));
197 emit changeEffectState(m_clipref, activeRow, disable);
199 slotUpdateCheckAllButton();
203 void EffectStackView::setupListView(int ix)
205 m_ui.effectlist->blockSignals(true);
206 m_ui.effectlist->clear();
208 // Issue 238: Add icons for effect type in effectstack.
209 KIcon videoIcon("kdenlive-show-video");
210 KIcon audioIcon("kdenlive-show-audio");
211 KIcon customIcon("kdenlive-custom-effect");
212 QListWidgetItem* item;
214 for (int i = 0; i < m_clipref->effectsCount(); i++) {
215 const QDomElement d = m_clipref->effectAt(i);
217 kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
222 doc.appendChild(doc.importNode(d, true));
223 kDebug() << "IMPORTED STK: " << doc.toString();*/
225 QDomNode namenode = d.elementsByTagName("name").item(0);
226 if (!namenode.isNull()) {
227 // Issue 238: Add icons for effect type in effectstack.
228 // Logic more or less copied from initeffects.cpp
229 QString type = d.attribute("type", QString());
230 if ("audio" == type) {
231 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
232 } else if ("custom" == type) {
233 item = new QListWidgetItem(customIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
235 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
237 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
238 if (d.attribute("disable") == "1") item->setCheckState(Qt::Unchecked);
239 else item->setCheckState(Qt::Checked);
242 if (m_ui.effectlist->count() == 0) {
243 m_ui.buttonDel->setEnabled(false);
244 m_ui.buttonSave->setEnabled(false);
245 m_ui.buttonReset->setEnabled(false);
246 m_ui.buttonUp->setEnabled(false);
247 m_ui.buttonDown->setEnabled(false);
248 m_ui.checkAll->setEnabled(false);
251 if (ix > m_ui.effectlist->count() - 1) ix = m_ui.effectlist->count() - 1;
252 m_ui.effectlist->setCurrentRow(ix);
253 m_ui.checkAll->setEnabled(true);
255 m_ui.effectlist->blockSignals(false);
256 if (m_ui.effectlist->count() == 0) {
257 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
258 m_ui.region_url->clear();
259 } else slotItemSelectionChanged(false);
260 slotUpdateCheckAllButton();
263 void EffectStackView::slotItemSelectionChanged(bool update)
265 bool hasItem = m_ui.effectlist->currentItem();
266 int activeRow = m_ui.effectlist->currentRow();
267 bool isChecked = false;
268 if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
269 if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
270 QDomElement eff = m_clipref->effectAt(activeRow);
271 m_effectedit->transferParamDesc(eff,
273 m_clipref->cropStart().frames(KdenliveSettings::project_fps()),
274 (m_clipref->cropStart() + m_clipref->cropDuration()).frames(KdenliveSettings::project_fps())); //minx max frame
275 m_ui.region_url->setUrl(KUrl(eff.attribute("region")));
277 if (m_clipref && update) m_clipref->setSelectedEffect(activeRow);
278 m_ui.buttonDel->setEnabled(hasItem);
279 m_ui.buttonSave->setEnabled(hasItem);
280 m_ui.buttonReset->setEnabled(hasItem && isChecked);
281 m_ui.buttonUp->setEnabled(activeRow > 0);
282 m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
283 m_ui.frame_layout->setEnabled(isChecked);
286 void EffectStackView::slotItemUp()
288 int activeRow = m_ui.effectlist->currentRow();
289 if (activeRow <= 0) return;
290 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
293 void EffectStackView::slotItemDown()
295 int activeRow = m_ui.effectlist->currentRow();
296 if (activeRow >= m_ui.effectlist->count() - 1) return;
297 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow + 2);
300 void EffectStackView::slotItemDel()
302 int activeRow = m_ui.effectlist->currentRow();
303 if (activeRow >= 0) {
304 emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
305 slotUpdateCheckAllButton();
309 void EffectStackView::slotResetEffect()
311 int activeRow = m_ui.effectlist->currentRow();
312 if (activeRow < 0) return;
313 QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
315 QString effectName = m_ui.effectlist->currentItem()->text();
316 foreach(const QString &type, m_effectLists.keys()) {
317 EffectsList *list = m_effectLists[type];
318 if (list->effectNames().contains(effectName)) {
319 dom = list->getEffectByName(effectName).cloneNode().toElement();
324 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
325 m_clipref->initEffect(dom);
326 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
327 m_ui.region_url->setUrl(KUrl(dom.attribute("region")));
328 emit updateClipEffect(m_clipref, old, dom, activeRow);
333 void EffectStackView::raiseWindow(QWidget* dock)
335 if (m_clipref && dock)
339 void EffectStackView::clear()
341 m_ui.effectlist->blockSignals(true);
342 m_ui.effectlist->clear();
343 m_ui.buttonDel->setEnabled(false);
344 m_ui.buttonSave->setEnabled(false);
345 m_ui.buttonReset->setEnabled(false);
346 m_ui.buttonUp->setEnabled(false);
347 m_ui.buttonDown->setEnabled(false);
348 m_ui.checkAll->setEnabled(false);
349 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
350 m_ui.region_url->clear();
351 m_ui.effectlist->blockSignals(false);
355 void EffectStackView::slotSeekTimeline(int pos)
358 emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
361 void EffectStackView::slotUpdateCheckAllButton()
363 bool hasEnabled = false;
364 bool hasDisabled = false;
365 for (int i = 0; i < m_ui.effectlist->count(); ++i) {
366 if (m_ui.effectlist->item(i)->checkState() == Qt::Checked)
372 m_ui.checkAll->blockSignals(true);
373 if (hasEnabled && hasDisabled)
374 m_ui.checkAll->setCheckState(Qt::PartiallyChecked);
376 m_ui.checkAll->setCheckState(Qt::Checked);
378 m_ui.checkAll->setCheckState(Qt::Unchecked);
379 m_ui.checkAll->blockSignals(false);
382 void EffectStackView::slotCheckAll(int state)
386 m_ui.checkAll->blockSignals(true);
387 m_ui.checkAll->setCheckState(Qt::Checked);
388 m_ui.checkAll->blockSignals(false);
391 for (int i = 0; i < m_ui.effectlist->count(); ++i)
392 m_ui.effectlist->item(i)->setCheckState((Qt::CheckState)state);
395 void EffectStackView::slotRegionChanged()
397 emit updateClipRegion(m_clipref, m_ui.effectlist->currentRow(), m_ui.region_url->text());
400 void EffectStackView::slotCheckMonitorPosition(int renderPos)
402 if (renderPos >= m_clipref->startPos().frames(KdenliveSettings::project_fps()) && renderPos <= m_clipref->endPos().frames(KdenliveSettings::project_fps())) {
403 if (!m_monitor->getEffectScene()->views().at(0)->isVisible())
404 m_monitor->slotEffectScene(true);
406 m_monitor->slotEffectScene(false);
410 #include "effectstackview.moc"