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 "monitoreditwidget.h"
27 #include "monitorscene.h"
28 #include "kdenlivesettings.h"
32 #include <KMessageBox>
33 #include <KStandardDirs>
34 #include <KFileDialog>
37 #include <QTextStream>
39 #include <QInputDialog>
42 EffectStackView::EffectStackView(Monitor *monitor, QWidget *parent) :
50 QVBoxLayout *vbox1 = new QVBoxLayout(m_ui.frame);
51 m_effectedit = new EffectStackEdit(monitor, m_ui.frame);
52 m_ui.frame->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
53 vbox1->setContentsMargins(2, 0, 2, 0);
55 vbox1->addWidget(m_effectedit);
56 m_ui.splitter->setStretchFactor(0, 1);
57 m_ui.splitter->setStretchFactor(1, 20);
59 //m_ui.region_url->fileDialog()->setFilter(ProjectList::getExtensions());
60 //m_ui.effectlist->horizontalHeader()->setVisible(false);
61 //m_ui.effectlist->verticalHeader()->setVisible(false);
63 m_ui.buttonNew->setIcon(KIcon("document-new"));
64 m_ui.buttonNew->setToolTip(i18n("Add new effect"));
65 m_ui.buttonUp->setIcon(KIcon("go-up"));
66 m_ui.buttonUp->setToolTip(i18n("Move effect up"));
67 m_ui.buttonDown->setIcon(KIcon("go-down"));
68 m_ui.buttonDown->setToolTip(i18n("Move effect down"));
69 m_ui.buttonDel->setIcon(KIcon("edit-delete"));
70 m_ui.buttonDel->setToolTip(i18n("Delete effect"));
71 m_ui.buttonSave->setIcon(KIcon("document-save"));
72 m_ui.buttonSave->setToolTip(i18n("Save effect"));
73 m_ui.buttonReset->setIcon(KIcon("view-refresh"));
74 m_ui.buttonReset->setToolTip(i18n("Reset effect"));
75 m_ui.checkAll->setToolTip(i18n("Enable/Disable all effects"));
76 m_ui.buttonShowComments->setIcon(KIcon("help-about"));
77 m_ui.buttonShowComments->setToolTip(i18n("Show additional information for the parameters"));
79 m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop); //use internal if drop is recognised right
81 m_ui.labelComment->setHidden(true);
83 //connect(m_ui.region_url, SIGNAL(urlSelected(const KUrl &)), this , SLOT(slotRegionChanged()));
84 //connect(m_ui.region_url, SIGNAL(returnPressed()), this , SLOT(slotRegionChanged()));
85 connect(m_ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
86 connect(m_ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
87 connect(m_ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
88 connect(m_ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
89 connect(m_ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
90 connect(m_ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
91 connect(m_ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
92 connect(m_ui.checkAll, SIGNAL(stateChanged(int)), this, SLOT(slotCheckAll(int)));
93 connect(m_ui.buttonShowComments, SIGNAL(clicked()), this, SLOT(slotShowComments()));
94 connect(m_effectedit, SIGNAL(parameterChanged(const QDomElement, const QDomElement)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement)));
95 connect(m_effectedit, SIGNAL(seekTimeline(int)), this , SLOT(slotSeekTimeline(int)));
96 connect(m_effectedit, SIGNAL(displayMessage(const QString&, int)), this, SIGNAL(displayMessage(const QString&, int)));
97 connect(m_effectedit, SIGNAL(checkMonitorPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
98 connect(monitor, SIGNAL(renderPosition(int)), this, SLOT(slotRenderPos(int)));
99 connect(this, SIGNAL(showComments(bool)), m_effectedit, SIGNAL(showComments(bool)));
100 m_effectLists["audio"] = &MainWindow::audioEffects;
101 m_effectLists["video"] = &MainWindow::videoEffects;
102 m_effectLists["custom"] = &MainWindow::customEffects;
106 EffectStackView::~EffectStackView()
108 m_effectLists.clear();
112 void EffectStackView::updateTimecodeFormat()
114 m_effectedit->updateTimecodeFormat();
117 void EffectStackView::setMenu(QMenu *menu)
119 m_ui.buttonNew->setMenu(menu);
122 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
124 m_effectedit->updateProjectFormat(profile, t);
127 void EffectStackView::slotSaveEffect()
129 QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
130 if (name.isEmpty()) return;
131 QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
132 path = path + name + ".xml";
133 if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", path)) == KMessageBox::No) return;
135 int i = m_ui.effectlist->currentRow();
137 QDomElement effect = m_currentEffectList.at(i).cloneNode().toElement();
138 doc.appendChild(doc.importNode(effect, true));
139 effect = doc.firstChild().toElement();
140 effect.removeAttribute("kdenlive_ix");
141 effect.setAttribute("id", name);
142 effect.setAttribute("type", "custom");
143 QDomElement effectname = effect.firstChildElement("name");
144 effect.removeChild(effectname);
145 effectname = doc.createElement("name");
146 QDomText nametext = doc.createTextNode(name);
147 effectname.appendChild(nametext);
148 effect.insertBefore(effectname, QDomNode());
149 QDomElement effectprops = effect.firstChildElement("properties");
150 effectprops.setAttribute("id", name);
151 effectprops.setAttribute("type", "custom");
154 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
155 QTextStream out(&file);
156 out << doc.toString();
159 emit reloadEffects();
162 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
165 emit updateEffect(NULL, m_trackindex, old, e, m_ui.effectlist->currentRow());
167 emit updateEffect(m_clipref, -1, old, e, m_ui.effectlist->currentRow());
170 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
172 if (c && !c->isEnabled()) return;
173 if (c && c == m_clipref) {
174 if (ix == -1) ix = m_ui.effectlist->currentRow();
175 //if (ix == -1 || ix == m_ui.effectlist->currentRow()) return;
179 QString cname = m_clipref->clipName();
180 if (cname.length() > 30) {
181 m_ui.checkAll->setToolTip(i18n("Effects for %1").arg(cname));
183 m_ui.checkAll->setText(i18n("Effects for %1").arg(cname) + "...");
185 m_ui.checkAll->setToolTip(QString());
186 m_ui.checkAll->setText(i18n("Effects for %1").arg(cname));
188 ix = c->selectedEffectIndex();
189 QString size = c->baseClip()->getProperty("frame_size");
190 double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
191 QPoint p((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
192 m_effectedit->setFrameSize(p);
197 if (m_clipref == NULL) {
198 m_ui.effectlist->blockSignals(true);
199 m_ui.effectlist->clear();
201 m_effectedit->transferParamDesc(QDomElement(), info);
202 //m_ui.region_url->clear();
203 m_ui.effectlist->blockSignals(false);
204 m_ui.checkAll->setToolTip(QString());
205 m_ui.checkAll->setText(QString());
206 m_ui.labelComment->setText(QString());
212 m_currentEffectList = m_clipref->effectList();
216 void EffectStackView::slotTrackItemSelected(int ix, const TrackInfo info)
220 m_currentEffectList = info.effectsList;
222 kDebug() << "// TRACK; " << ix << ", EFFECTS: " << m_currentEffectList.count();
224 m_ui.checkAll->setToolTip(QString());
225 m_ui.checkAll->setText(i18n("Effects for track %1").arg(info.trackName.isEmpty() ? QString::number(ix) : info.trackName));
230 void EffectStackView::slotItemChanged(QListWidgetItem *item)
233 if (item->checkState() == Qt::Checked) disable = false;
234 m_ui.buttonReset->setEnabled(!disable || !KdenliveSettings::disable_effect_parameters());
235 int activeRow = m_ui.effectlist->currentRow();
236 if (activeRow >= 0) {
237 m_effectedit->updateParameter("disable", QString::number((int) disable));
239 emit changeEffectState(NULL, m_trackindex, activeRow, disable);
241 emit changeEffectState(m_clipref, -1, activeRow, disable);
243 slotUpdateCheckAllButton();
247 void EffectStackView::setupListView(int ix)
249 m_ui.effectlist->blockSignals(true);
250 m_ui.effectlist->clear();
252 // Issue 238: Add icons for effect type in effectstack.
253 KIcon videoIcon("kdenlive-show-video");
254 KIcon audioIcon("kdenlive-show-audio");
255 KIcon customIcon("kdenlive-custom-effect");
256 QListWidgetItem* item;
258 for (int i = 0; i < m_currentEffectList.count(); i++) {
259 const QDomElement d = m_currentEffectList.at(i).cloneNode().toElement();
261 kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
266 doc.appendChild(doc.importNode(d, true));
267 kDebug() << "IMPORTED STK: " << doc.toString();*/
269 QDomElement namenode = d.firstChildElement("name");
270 if (!namenode.isNull()) {
271 // Issue 238: Add icons for effect type in effectstack.
272 // Logic more or less copied from initeffects.cpp
273 QString type = d.attribute("type", QString());
274 if ("audio" == type) {
275 item = new QListWidgetItem(audioIcon, i18n(namenode.text().toUtf8().data()), m_ui.effectlist);
276 } else if ("custom" == type) {
277 item = new QListWidgetItem(customIcon, i18n(namenode.text().toUtf8().data()), m_ui.effectlist);
279 item = new QListWidgetItem(videoIcon, i18n(namenode.text().toUtf8().data()), m_ui.effectlist);
281 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
282 if (d.attribute("disable") == "1")
283 item->setCheckState(Qt::Unchecked);
285 item->setCheckState(Qt::Checked);
288 if (m_ui.effectlist->count() == 0) {
289 m_ui.buttonDel->setEnabled(false);
290 m_ui.buttonSave->setEnabled(false);
291 m_ui.buttonReset->setEnabled(false);
292 m_ui.buttonUp->setEnabled(false);
293 m_ui.buttonDown->setEnabled(false);
294 m_ui.checkAll->setEnabled(false);
295 m_ui.buttonShowComments->setEnabled(false);
296 m_ui.labelComment->setHidden(true);
298 ix = qBound(0, ix, m_ui.effectlist->count() - 1);
299 m_ui.effectlist->setCurrentRow(ix);
300 m_ui.checkAll->setEnabled(true);
302 m_ui.effectlist->blockSignals(false);
303 if (m_ui.effectlist->count() == 0) {
305 m_effectedit->transferParamDesc(QDomElement(), info);
306 //m_ui.region_url->clear();
307 } else slotItemSelectionChanged(false);
308 slotUpdateCheckAllButton();
311 void EffectStackView::slotItemSelectionChanged(bool update)
313 bool hasItem = m_ui.effectlist->currentItem();
314 int activeRow = m_ui.effectlist->currentRow();
315 bool isChecked = false;
316 if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
318 if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
319 eff = m_currentEffectList.at(activeRow);
321 // showing track effects
323 info.track = m_trackInfo.type;
324 info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
325 info.cropStart = GenTime(0);
326 info.startPos = GenTime(-1);
328 m_effectedit->transferParamDesc(eff, info);
330 m_effectedit->transferParamDesc(eff, m_clipref->info());
332 //m_ui.region_url->setUrl(KUrl(eff.attribute("region")));
333 m_ui.labelComment->setText(i18n(eff.firstChildElement("description").firstChildElement("full").text().toUtf8().data()));
335 if (!m_trackMode && m_clipref && update) m_clipref->setSelectedEffect(activeRow);
336 m_ui.buttonDel->setEnabled(hasItem);
337 m_ui.buttonSave->setEnabled(hasItem);
338 m_ui.buttonReset->setEnabled(hasItem && (isChecked || !KdenliveSettings::disable_effect_parameters()));
339 m_ui.buttonUp->setEnabled(activeRow > 0);
340 m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
341 m_ui.buttonShowComments->setEnabled(hasItem);
343 emit showComments(m_ui.buttonShowComments->isChecked());
344 m_ui.labelComment->setVisible(hasItem && m_ui.labelComment->text().count() && (m_ui.buttonShowComments->isChecked() || !eff.elementsByTagName("parameter").count()));
347 void EffectStackView::slotItemUp()
349 int activeRow = m_ui.effectlist->currentRow();
350 if (activeRow <= 0) return;
351 if (m_trackMode) emit changeEffectPosition(NULL, m_trackindex, activeRow + 1, activeRow);
352 else emit changeEffectPosition(m_clipref, -1, activeRow + 1, activeRow);
355 void EffectStackView::slotItemDown()
357 int activeRow = m_ui.effectlist->currentRow();
358 if (activeRow >= m_ui.effectlist->count() - 1) return;
359 if (m_trackMode) emit changeEffectPosition(NULL, m_trackindex, activeRow + 1, activeRow + 2);
360 else emit changeEffectPosition(m_clipref, -1, activeRow + 1, activeRow + 2);
363 void EffectStackView::slotItemDel()
365 int activeRow = m_ui.effectlist->currentRow();
366 if (activeRow >= 0) {
368 emit removeEffect(NULL, m_trackindex, m_currentEffectList.at(activeRow).cloneNode().toElement());
370 emit removeEffect(m_clipref, -1, m_clipref->effectAt(activeRow));
371 slotUpdateCheckAllButton();
375 void EffectStackView::slotResetEffect()
377 int activeRow = m_ui.effectlist->currentRow();
378 if (activeRow < 0) return;
379 QDomElement old = m_currentEffectList.at(activeRow).cloneNode().toElement();
381 QString effectName = m_ui.effectlist->currentItem()->text();
382 foreach(const QString &type, m_effectLists.keys()) {
383 EffectsList *list = m_effectLists[type];
384 if (list->effectNames().contains(effectName)) {
385 dom = list->getEffectByName(effectName).cloneNode().toElement();
390 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
392 EffectsList::setParameter(dom, "in", QString::number(0));
393 EffectsList::setParameter(dom, "out", QString::number(m_trackInfo.duration));
395 info.track = m_trackInfo.type;
396 info.cropDuration = GenTime(m_trackInfo.duration, KdenliveSettings::project_fps());
397 info.cropStart = GenTime(0);
398 info.startPos = GenTime(-1);
400 m_effectedit->transferParamDesc(dom, info);
401 emit updateEffect(NULL, m_trackindex, old, dom, activeRow);
403 m_clipref->initEffect(dom);
404 m_effectedit->transferParamDesc(dom, m_clipref->info());
405 //m_ui.region_url->setUrl(KUrl(dom.attribute("region")));
406 emit updateEffect(m_clipref, -1, old, dom, activeRow);
410 emit showComments(m_ui.buttonShowComments->isChecked());
411 m_ui.labelComment->setHidden(!m_ui.buttonShowComments->isChecked() || !m_ui.labelComment->text().count());
415 void EffectStackView::raiseWindow(QWidget* dock)
417 if ((m_clipref || m_trackMode) && dock)
421 void EffectStackView::clear()
423 m_ui.effectlist->blockSignals(true);
424 m_ui.effectlist->clear();
425 m_ui.buttonDel->setEnabled(false);
426 m_ui.buttonSave->setEnabled(false);
427 m_ui.buttonReset->setEnabled(false);
428 m_ui.buttonUp->setEnabled(false);
429 m_ui.buttonDown->setEnabled(false);
430 m_ui.checkAll->setEnabled(false);
432 m_effectedit->transferParamDesc(QDomElement(), info);
433 //m_ui.region_url->clear();
434 m_ui.buttonShowComments->setEnabled(false);
435 m_ui.labelComment->setText(QString());
436 m_ui.effectlist->blockSignals(false);
440 void EffectStackView::slotSeekTimeline(int pos)
443 emit seekTimeline(pos);
444 } else if (m_clipref) {
445 emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
449 void EffectStackView::slotUpdateCheckAllButton()
451 bool hasEnabled = false;
452 bool hasDisabled = false;
453 for (int i = 0; i < m_ui.effectlist->count(); ++i) {
454 if (m_ui.effectlist->item(i)->checkState() == Qt::Checked)
460 m_ui.checkAll->blockSignals(true);
461 if (hasEnabled && hasDisabled)
462 m_ui.checkAll->setCheckState(Qt::PartiallyChecked);
464 m_ui.checkAll->setCheckState(Qt::Checked);
466 m_ui.checkAll->setCheckState(Qt::Unchecked);
467 m_ui.checkAll->blockSignals(false);
470 void EffectStackView::slotCheckAll(int state)
474 m_ui.checkAll->blockSignals(true);
475 m_ui.checkAll->setCheckState(Qt::Checked);
476 m_ui.checkAll->blockSignals(false);
479 bool disabled = (state != 2);
480 m_effectedit->updateParameter("disable", QString::number((int) disabled));
481 for (int i = 0; i < m_ui.effectlist->count(); ++i) {
482 if (m_ui.effectlist->item(i)->checkState() != (Qt::CheckState)state) {
483 m_ui.effectlist->item(i)->setCheckState((Qt::CheckState)state);
485 emit changeEffectState(NULL, m_trackindex, i, disabled);
487 emit changeEffectState(m_clipref, -1, i, disabled);
492 /*void EffectStackView::slotRegionChanged()
494 if (!m_trackMode) emit updateClipRegion(m_clipref, m_ui.effectlist->currentRow(), m_ui.region_url->text());
497 void EffectStackView::slotCheckMonitorPosition(int renderPos)
499 if (m_trackMode || (m_clipref && renderPos >= m_clipref->startPos().frames(KdenliveSettings::project_fps()) && renderPos <= m_clipref->endPos().frames(KdenliveSettings::project_fps()))) {
500 if (!m_monitor->getEffectEdit()->getScene()->views().at(0)->isVisible())
501 m_monitor->slotEffectScene(true);
503 m_monitor->slotEffectScene(false);
507 void EffectStackView::slotRenderPos(int pos)
511 m_effectedit->slotSyncEffectsPos(pos);
512 } else if (m_clipref) {
513 m_effectedit->slotSyncEffectsPos(pos - m_clipref->startPos().frames(KdenliveSettings::project_fps()));
518 int EffectStackView::isTrackMode(bool *ok) const
524 void EffectStackView::slotShowComments()
526 m_ui.labelComment->setHidden(!m_ui.buttonShowComments->isChecked() || !m_ui.labelComment->text().count());
527 emit showComments(m_ui.buttonShowComments->isChecked());
530 #include "effectstackview.moc"