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) :
49 QVBoxLayout *vbox1 = new QVBoxLayout(m_ui.frame);
50 m_effectedit = new EffectStackEdit(monitor, m_ui.frame);
51 vbox1->setContentsMargins(0, 0, 0, 0);
53 vbox1->addWidget(m_effectedit);
55 //m_ui.region_url->fileDialog()->setFilter(ProjectList::getExtensions());
56 //m_ui.effectlist->horizontalHeader()->setVisible(false);
57 //m_ui.effectlist->verticalHeader()->setVisible(false);
59 m_ui.buttonNew->setIcon(KIcon("document-new"));
60 m_ui.buttonNew->setToolTip(i18n("Add new effect"));
61 m_ui.buttonUp->setIcon(KIcon("go-up"));
62 m_ui.buttonUp->setToolTip(i18n("Move effect up"));
63 m_ui.buttonDown->setIcon(KIcon("go-down"));
64 m_ui.buttonDown->setToolTip(i18n("Move effect down"));
65 m_ui.buttonDel->setIcon(KIcon("edit-delete"));
66 m_ui.buttonDel->setToolTip(i18n("Delete effect"));
67 m_ui.buttonSave->setIcon(KIcon("document-save"));
68 m_ui.buttonSave->setToolTip(i18n("Save effect"));
69 m_ui.buttonReset->setIcon(KIcon("view-refresh"));
70 m_ui.buttonReset->setToolTip(i18n("Reset effect"));
71 m_ui.checkAll->setToolTip(i18n("Enable/Disable all effects"));
73 m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop); //use internal if drop is recognised right
75 //connect(m_ui.region_url, SIGNAL(urlSelected(const KUrl &)), this , SLOT(slotRegionChanged()));
76 //connect(m_ui.region_url, SIGNAL(returnPressed()), this , SLOT(slotRegionChanged()));
77 connect(m_ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
78 connect(m_ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
79 connect(m_ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
80 connect(m_ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
81 connect(m_ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
82 connect(m_ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
83 connect(m_ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
84 connect(m_ui.checkAll, SIGNAL(stateChanged(int)), this, SLOT(slotCheckAll(int)));
85 connect(m_effectedit, SIGNAL(parameterChanged(const QDomElement, const QDomElement)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement)));
86 connect(m_effectedit, SIGNAL(seekTimeline(int)), this , SLOT(slotSeekTimeline(int)));
87 connect(m_effectedit, SIGNAL(displayMessage(const QString&, int)), this, SIGNAL(displayMessage(const QString&, int)));
88 connect(m_effectedit, SIGNAL(checkMonitorPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
89 connect(m_effectedit, SIGNAL(showComment(const QString&)), this, SLOT(slotUpdateComment(const QString&)));
90 connect(monitor, SIGNAL(renderPosition(int)), this, SLOT(slotRenderPos(int)));
91 m_effectLists["audio"] = &MainWindow::audioEffects;
92 m_effectLists["video"] = &MainWindow::videoEffects;
93 m_effectLists["custom"] = &MainWindow::customEffects;
94 m_ui.splitter->setStretchFactor(1, 10);
95 m_ui.splitter->setStretchFactor(0, 1);
97 m_ui.labelComment->setHidden(true);
102 EffectStackView::~EffectStackView()
104 m_effectLists.clear();
108 void EffectStackView::updateTimecodeFormat()
110 m_effectedit->updateTimecodeFormat();
113 void EffectStackView::setMenu(QMenu *menu)
115 m_ui.buttonNew->setMenu(menu);
118 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
120 m_effectedit->updateProjectFormat(profile, t);
123 void EffectStackView::slotSaveEffect()
125 QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
126 if (name.isEmpty()) return;
127 QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
128 path = path + name + ".xml";
129 if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", path)) == KMessageBox::No) return;
131 int i = m_ui.effectlist->currentRow();
133 QDomElement effect = m_currentEffectList.at(i).cloneNode().toElement();
134 doc.appendChild(doc.importNode(effect, true));
135 effect = doc.firstChild().toElement();
136 effect.removeAttribute("kdenlive_ix");
137 effect.setAttribute("id", name);
138 effect.setAttribute("type", "custom");
139 QDomElement effectname = effect.firstChildElement("name");
140 effect.removeChild(effectname);
141 effectname = doc.createElement("name");
142 QDomText nametext = doc.createTextNode(name);
143 effectname.appendChild(nametext);
144 effect.insertBefore(effectname, QDomNode());
145 QDomElement effectprops = effect.firstChildElement("properties");
146 effectprops.setAttribute("id", name);
147 effectprops.setAttribute("type", "custom");
150 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
151 QTextStream out(&file);
152 out << doc.toString();
155 emit reloadEffects();
158 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
161 emit updateEffect(NULL, m_trackindex, old, e, m_ui.effectlist->currentRow());
163 emit updateEffect(m_clipref, -1, old, e, m_ui.effectlist->currentRow());
166 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
168 if (c && !c->isEnabled()) return;
169 if (c && c == m_clipref) {
170 if (ix == -1) ix = m_ui.effectlist->currentRow();
171 //if (ix == -1 || ix == m_ui.effectlist->currentRow()) return;
175 QString cname = m_clipref->clipName();
176 if (cname.length() > 20) {
177 m_ui.checkAll->setToolTip(i18n("Effects for %1").arg(cname));
179 m_ui.checkAll->setText(i18n("Effects for %1").arg(cname) + "...");
181 m_ui.checkAll->setToolTip(QString());
182 m_ui.checkAll->setText(i18n("Effects for %1").arg(cname));
184 ix = c->selectedEffectIndex();
185 QString size = c->baseClip()->getProperty("frame_size");
186 double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
187 QPoint p((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
188 m_effectedit->setFrameSize(p);
189 m_effectedit->setFrameSize(p);
192 if (m_clipref == NULL) {
193 m_ui.effectlist->blockSignals(true);
194 m_ui.effectlist->clear();
195 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
196 //m_ui.region_url->clear();
197 m_ui.effectlist->blockSignals(false);
198 m_ui.checkAll->setToolTip(QString());
199 m_ui.checkAll->setText(QString());
205 m_currentEffectList = m_clipref->effectList();
211 void EffectStackView::slotTrackItemSelected(int ix, const TrackInfo info)
215 m_currentEffectList = info.effectsList;
217 kDebug() << "// TRACK; " << ix << ", EFFECTS: " << m_currentEffectList.count();
219 m_ui.checkAll->setToolTip(QString());
220 m_ui.checkAll->setText(i18n("Effects for track %1").arg(info.trackName.isEmpty() ? QString::number(ix) : info.trackName));
226 void EffectStackView::slotItemChanged(QListWidgetItem *item)
229 if (item->checkState() == Qt::Checked) disable = false;
230 m_ui.frame->setEnabled(!disable);
231 m_ui.buttonReset->setEnabled(!disable);
232 int activeRow = m_ui.effectlist->currentRow();
233 if (activeRow >= 0) {
234 m_effectedit->updateParameter("disable", QString::number((int) disable));
236 emit changeEffectState(NULL, m_trackindex, activeRow, disable);
238 emit changeEffectState(m_clipref, -1, activeRow, disable);
240 slotUpdateCheckAllButton();
244 void EffectStackView::setupListView(int ix)
246 m_ui.effectlist->blockSignals(true);
247 m_ui.effectlist->clear();
249 // Issue 238: Add icons for effect type in effectstack.
250 KIcon videoIcon("kdenlive-show-video");
251 KIcon audioIcon("kdenlive-show-audio");
252 KIcon customIcon("kdenlive-custom-effect");
253 QListWidgetItem* item;
255 for (int i = 0; i < m_currentEffectList.count(); i++) {
256 const QDomElement d = m_currentEffectList.at(i).cloneNode().toElement();
258 kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
263 doc.appendChild(doc.importNode(d, true));
264 kDebug() << "IMPORTED STK: " << doc.toString();*/
266 QDomNode namenode = d.elementsByTagName("name").item(0);
267 if (!namenode.isNull()) {
268 // Issue 238: Add icons for effect type in effectstack.
269 // Logic more or less copied from initeffects.cpp
270 QString type = d.attribute("type", QString());
271 if ("audio" == type) {
272 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
273 } else if ("custom" == type) {
274 item = new QListWidgetItem(customIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
276 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
278 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
279 if (d.attribute("disable") == "1")
280 item->setCheckState(Qt::Unchecked);
282 item->setCheckState(Qt::Checked);
285 if (m_ui.effectlist->count() == 0) {
286 m_ui.buttonDel->setEnabled(false);
287 m_ui.buttonSave->setEnabled(false);
288 m_ui.buttonReset->setEnabled(false);
289 m_ui.buttonUp->setEnabled(false);
290 m_ui.buttonDown->setEnabled(false);
291 m_ui.checkAll->setEnabled(false);
294 qMax(ix, m_ui.effectlist->count() - 1);
295 m_ui.effectlist->setCurrentRow(ix);
296 m_ui.checkAll->setEnabled(true);
298 m_ui.effectlist->blockSignals(false);
299 if (m_ui.effectlist->count() == 0) {
300 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
301 //m_ui.region_url->clear();
302 } else slotItemSelectionChanged(false);
303 slotUpdateCheckAllButton();
306 void EffectStackView::slotItemSelectionChanged(bool update)
308 bool hasItem = m_ui.effectlist->currentItem();
309 int activeRow = m_ui.effectlist->currentRow();
310 bool isChecked = false;
311 if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
312 if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
313 QDomElement eff = m_currentEffectList.at(activeRow);
315 // showing track effects
316 m_effectedit->transferParamDesc(eff, 0, 0, m_trackInfo.duration);
317 } else m_effectedit->transferParamDesc(eff,
319 m_clipref->cropStart().frames(KdenliveSettings::project_fps()),
320 (m_clipref->cropStart() + m_clipref->cropDuration()).frames(KdenliveSettings::project_fps()) - 1); //minx max frame
321 //m_ui.region_url->setUrl(KUrl(eff.attribute("region")));
323 if (!m_trackMode && m_clipref && update) m_clipref->setSelectedEffect(activeRow);
324 m_ui.buttonDel->setEnabled(hasItem);
325 m_ui.buttonSave->setEnabled(hasItem);
326 m_ui.buttonReset->setEnabled(hasItem && isChecked);
327 m_ui.buttonUp->setEnabled(activeRow > 0);
328 m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
329 m_ui.frame->setEnabled(isChecked);
333 void EffectStackView::slotItemUp()
335 int activeRow = m_ui.effectlist->currentRow();
336 if (activeRow <= 0) return;
337 if (m_trackMode) emit changeEffectPosition(NULL, m_trackindex, activeRow + 1, activeRow);
338 else emit changeEffectPosition(m_clipref, -1, activeRow + 1, activeRow);
341 void EffectStackView::slotItemDown()
343 int activeRow = m_ui.effectlist->currentRow();
344 if (activeRow >= m_ui.effectlist->count() - 1) return;
345 if (m_trackMode) emit changeEffectPosition(NULL, m_trackindex, activeRow + 1, activeRow + 2);
346 else emit changeEffectPosition(m_clipref, -1, activeRow + 1, activeRow + 2);
349 void EffectStackView::slotItemDel()
351 int activeRow = m_ui.effectlist->currentRow();
352 if (activeRow >= 0) {
354 emit removeEffect(NULL, m_trackindex, m_currentEffectList.at(activeRow).cloneNode().toElement());
356 emit removeEffect(m_clipref, -1, m_clipref->effectAt(activeRow));
357 slotUpdateCheckAllButton();
361 void EffectStackView::slotResetEffect()
363 int activeRow = m_ui.effectlist->currentRow();
364 if (activeRow < 0) return;
365 QDomElement old = m_currentEffectList.at(activeRow).cloneNode().toElement();
367 QString effectName = m_ui.effectlist->currentItem()->text();
368 foreach(const QString &type, m_effectLists.keys()) {
369 EffectsList *list = m_effectLists[type];
370 if (list->effectNames().contains(effectName)) {
371 dom = list->getEffectByName(effectName).cloneNode().toElement();
376 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
378 EffectsList::setParameter(dom, "in", QString::number(0));
379 EffectsList::setParameter(dom, "out", QString::number(m_trackInfo.duration));
380 m_effectedit->transferParamDesc(dom, 0, 0, m_trackInfo.duration);//minx max frame
381 emit updateEffect(NULL, m_trackindex, old, dom, activeRow);
383 m_clipref->initEffect(dom);
384 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
385 //m_ui.region_url->setUrl(KUrl(dom.attribute("region")));
386 emit updateEffect(m_clipref, -1, old, dom, activeRow);
392 void EffectStackView::raiseWindow(QWidget* dock)
394 if ((m_clipref || m_trackMode) && dock)
398 void EffectStackView::clear()
400 m_ui.effectlist->blockSignals(true);
401 m_ui.effectlist->clear();
402 m_ui.buttonDel->setEnabled(false);
403 m_ui.buttonSave->setEnabled(false);
404 m_ui.buttonReset->setEnabled(false);
405 m_ui.buttonUp->setEnabled(false);
406 m_ui.buttonDown->setEnabled(false);
407 m_ui.checkAll->setEnabled(false);
408 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
409 //m_ui.region_url->clear();
410 m_ui.effectlist->blockSignals(false);
414 void EffectStackView::slotSeekTimeline(int pos)
417 emit seekTimeline(pos);
418 } else if (m_clipref) {
419 emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
423 void EffectStackView::slotUpdateCheckAllButton()
425 bool hasEnabled = false;
426 bool hasDisabled = false;
427 for (int i = 0; i < m_ui.effectlist->count(); ++i) {
428 if (m_ui.effectlist->item(i)->checkState() == Qt::Checked)
434 m_ui.checkAll->blockSignals(true);
435 if (hasEnabled && hasDisabled)
436 m_ui.checkAll->setCheckState(Qt::PartiallyChecked);
438 m_ui.checkAll->setCheckState(Qt::Checked);
440 m_ui.checkAll->setCheckState(Qt::Unchecked);
441 m_ui.checkAll->blockSignals(false);
444 void EffectStackView::slotCheckAll(int state)
448 m_ui.checkAll->blockSignals(true);
449 m_ui.checkAll->setCheckState(Qt::Checked);
450 m_ui.checkAll->blockSignals(false);
453 bool disabled = (state != 2);
454 m_effectedit->updateParameter("disable", QString::number((int) disabled));
455 for (int i = 0; i < m_ui.effectlist->count(); ++i) {
456 if (m_ui.effectlist->item(i)->checkState() != (Qt::CheckState)state) {
457 m_ui.effectlist->item(i)->setCheckState((Qt::CheckState)state);
459 emit changeEffectState(NULL, m_trackindex, i, disabled);
461 emit changeEffectState(m_clipref, -1, i, disabled);
466 /*void EffectStackView::slotRegionChanged()
468 if (!m_trackMode) emit updateClipRegion(m_clipref, m_ui.effectlist->currentRow(), m_ui.region_url->text());
471 void EffectStackView::slotCheckMonitorPosition(int renderPos)
473 if (m_trackMode || (renderPos >= m_clipref->startPos().frames(KdenliveSettings::project_fps()) && renderPos <= m_clipref->endPos().frames(KdenliveSettings::project_fps()))) {
474 if (!m_monitor->getEffectScene()->views().at(0)->isVisible())
475 m_monitor->slotEffectScene(true);
477 m_monitor->slotEffectScene(false);
481 void EffectStackView::slotRenderPos(int pos)
485 m_effectedit->slotSyncEffectsPos(pos);
486 } else if (m_clipref) {
487 m_effectedit->slotSyncEffectsPos(pos - m_clipref->startPos().frames(KdenliveSettings::project_fps()));
492 int EffectStackView::isTrackMode(bool *ok) const
498 void EffectStackView::slotUpdateComment(const QString& comment)
500 if (comment == m_ui.labelComment->text())
501 m_ui.labelComment->setText(QString());
503 m_ui.labelComment->setText(comment);
505 m_ui.labelComment->setHidden(m_ui.labelComment->text() == QString());
508 #include "effectstackview.moc"