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 connect(m_effectedit, SIGNAL(displayMessage(const QString&, int)), this, SIGNAL(displayMessage(const QString&, int)));
85 m_effectLists["audio"] = &MainWindow::audioEffects;
86 m_effectLists["video"] = &MainWindow::videoEffects;
87 m_effectLists["custom"] = &MainWindow::customEffects;
88 m_ui.splitter->setStretchFactor(1, 10);
89 m_ui.splitter->setStretchFactor(0, 1);
93 EffectStackView::~EffectStackView()
95 m_effectLists.clear();
99 void EffectStackView::updateTimecodeFormat()
101 m_effectedit->updateTimecodeFormat();
104 void EffectStackView::setMenu(QMenu *menu)
106 m_ui.buttonNew->setMenu(menu);
109 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
111 m_effectedit->updateProjectFormat(profile, t);
114 void EffectStackView::slotSaveEffect()
116 QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
117 if (name.isEmpty()) return;
118 QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
119 path = path + name + ".xml";
120 if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
122 int i = m_ui.effectlist->currentRow();
124 QDomElement effect = m_clipref->effectAt(i).cloneNode().toElement();
125 doc.appendChild(doc.importNode(effect, true));
126 effect = doc.firstChild().toElement();
127 effect.removeAttribute("kdenlive_ix");
128 effect.setAttribute("id", name);
129 effect.setAttribute("type", "custom");
130 QDomElement effectname = effect.firstChildElement("name");
131 effect.removeChild(effectname);
132 effectname = doc.createElement("name");
133 QDomText nametext = doc.createTextNode(name);
134 effectname.appendChild(nametext);
135 effect.insertBefore(effectname, QDomNode());
136 QDomElement effectprops = effect.firstChildElement("properties");
137 effectprops.setAttribute("id", name);
138 effectprops.setAttribute("type", "custom");
141 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
142 QTextStream out(&file);
143 out << doc.toString();
146 emit reloadEffects();
149 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
152 emit updateClipEffect(m_clipref, old, e, m_ui.effectlist->currentRow());
155 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
157 if (c && !c->isEnabled()) return;
158 if (c && c == m_clipref) {
159 if (ix == -1) ix = m_ui.effectlist->currentRow();
160 //if (ix == -1 || ix == m_ui.effectlist->currentRow()) return;
164 ix = c->selectedEffectIndex();
165 QString size = c->baseClip()->getProperty("frame_size");
166 double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
167 QPoint p((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
168 m_effectedit->setFrameSize(p);
169 m_effectedit->setFrameSize(p);
172 if (m_clipref == NULL) {
173 m_ui.effectlist->blockSignals(true);
174 m_ui.effectlist->clear();
175 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
176 m_ui.region_url->clear();
177 m_ui.effectlist->blockSignals(false);
185 void EffectStackView::slotItemChanged(QListWidgetItem *item)
188 if (item->checkState() == Qt::Checked) disable = false;
189 m_ui.frame_layout->setEnabled(!disable);
190 m_ui.buttonReset->setEnabled(!disable);
191 int activeRow = m_ui.effectlist->currentRow();
192 if (activeRow >= 0) {
193 m_effectedit->updateParameter("disable", QString::number((int) disable));
194 emit changeEffectState(m_clipref, activeRow, disable);
196 slotUpdateCheckAllButton();
200 void EffectStackView::setupListView(int ix)
202 m_ui.effectlist->blockSignals(true);
203 m_ui.effectlist->clear();
205 // Issue 238: Add icons for effect type in effectstack.
206 KIcon videoIcon("kdenlive-show-video");
207 KIcon audioIcon("kdenlive-show-audio");
208 KIcon customIcon("kdenlive-custom-effect");
209 QListWidgetItem* item;
211 for (int i = 0; i < m_clipref->effectsCount(); i++) {
212 const QDomElement d = m_clipref->effectAt(i);
214 kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
219 doc.appendChild(doc.importNode(d, true));
220 kDebug() << "IMPORTED STK: " << doc.toString();*/
222 QDomNode namenode = d.elementsByTagName("name").item(0);
223 if (!namenode.isNull()) {
224 // Issue 238: Add icons for effect type in effectstack.
225 // Logic more or less copied from initeffects.cpp
226 QString type = d.attribute("type", QString());
227 if ("audio" == type) {
228 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
229 } else if ("custom" == type) {
230 item = new QListWidgetItem(customIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
232 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
234 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
235 if (d.attribute("disable") == "1") item->setCheckState(Qt::Unchecked);
236 else item->setCheckState(Qt::Checked);
239 if (m_ui.effectlist->count() == 0) {
240 m_ui.buttonDel->setEnabled(false);
241 m_ui.buttonSave->setEnabled(false);
242 m_ui.buttonReset->setEnabled(false);
243 m_ui.buttonUp->setEnabled(false);
244 m_ui.buttonDown->setEnabled(false);
245 m_ui.checkAll->setEnabled(false);
248 if (ix > m_ui.effectlist->count() - 1) ix = m_ui.effectlist->count() - 1;
249 m_ui.effectlist->setCurrentRow(ix);
250 m_ui.checkAll->setEnabled(true);
252 m_ui.effectlist->blockSignals(false);
253 if (m_ui.effectlist->count() == 0) {
254 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
255 m_ui.region_url->clear();
256 } else slotItemSelectionChanged(false);
257 slotUpdateCheckAllButton();
260 void EffectStackView::slotItemSelectionChanged(bool update)
262 bool hasItem = m_ui.effectlist->currentItem();
263 int activeRow = m_ui.effectlist->currentRow();
264 bool isChecked = false;
265 if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
266 if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
267 QDomElement eff = m_clipref->effectAt(activeRow);
268 m_effectedit->transferParamDesc(eff,
270 m_clipref->cropStart().frames(KdenliveSettings::project_fps()),
271 (m_clipref->cropStart() + m_clipref->cropDuration()).frames(KdenliveSettings::project_fps())); //minx max frame
272 m_ui.region_url->setUrl(KUrl(eff.attribute("region")));
274 if (m_clipref && update) m_clipref->setSelectedEffect(activeRow);
275 m_ui.buttonDel->setEnabled(hasItem);
276 m_ui.buttonSave->setEnabled(hasItem);
277 m_ui.buttonReset->setEnabled(hasItem && isChecked);
278 m_ui.buttonUp->setEnabled(activeRow > 0);
279 m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
280 m_ui.frame_layout->setEnabled(isChecked);
283 void EffectStackView::slotItemUp()
285 int activeRow = m_ui.effectlist->currentRow();
286 if (activeRow <= 0) return;
287 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
290 void EffectStackView::slotItemDown()
292 int activeRow = m_ui.effectlist->currentRow();
293 if (activeRow >= m_ui.effectlist->count() - 1) return;
294 emit changeEffectPosition(m_clipref, activeRow + 1, activeRow + 2);
297 void EffectStackView::slotItemDel()
299 int activeRow = m_ui.effectlist->currentRow();
300 if (activeRow >= 0) {
301 emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
302 slotUpdateCheckAllButton();
306 void EffectStackView::slotResetEffect()
308 int activeRow = m_ui.effectlist->currentRow();
309 if (activeRow < 0) return;
310 QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
312 QString effectName = m_ui.effectlist->currentItem()->text();
313 foreach(const QString &type, m_effectLists.keys()) {
314 EffectsList *list = m_effectLists[type];
315 if (list->effectNames().contains(effectName)) {
316 dom = list->getEffectByName(effectName).cloneNode().toElement();
321 dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
322 m_clipref->initEffect(dom);
323 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
324 m_ui.region_url->setUrl(KUrl(dom.attribute("region")));
325 emit updateClipEffect(m_clipref, old, dom, activeRow);
330 void EffectStackView::raiseWindow(QWidget* dock)
332 if (m_clipref && dock)
336 void EffectStackView::clear()
338 m_ui.effectlist->blockSignals(true);
339 m_ui.effectlist->clear();
340 m_ui.buttonDel->setEnabled(false);
341 m_ui.buttonSave->setEnabled(false);
342 m_ui.buttonReset->setEnabled(false);
343 m_ui.buttonUp->setEnabled(false);
344 m_ui.buttonDown->setEnabled(false);
345 m_ui.checkAll->setEnabled(false);
346 m_effectedit->transferParamDesc(QDomElement(), 0, 0, 0);
347 m_ui.region_url->clear();
348 m_ui.effectlist->blockSignals(false);
352 void EffectStackView::slotSeekTimeline(int pos)
355 emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
358 void EffectStackView::slotUpdateCheckAllButton()
360 bool hasEnabled = false;
361 bool hasDisabled = false;
362 for (int i = 0; i < m_ui.effectlist->count(); ++i) {
363 if (m_ui.effectlist->item(i)->checkState() == Qt::Checked)
369 m_ui.checkAll->blockSignals(true);
370 if (hasEnabled && hasDisabled)
371 m_ui.checkAll->setCheckState(Qt::PartiallyChecked);
373 m_ui.checkAll->setCheckState(Qt::Checked);
375 m_ui.checkAll->setCheckState(Qt::Unchecked);
376 m_ui.checkAll->blockSignals(false);
379 void EffectStackView::slotCheckAll(int state)
383 m_ui.checkAll->blockSignals(true);
384 m_ui.checkAll->setCheckState(Qt::Checked);
385 m_ui.checkAll->blockSignals(false);
388 for (int i = 0; i < m_ui.effectlist->count(); ++i)
389 m_ui.effectlist->item(i)->setCheckState((Qt::CheckState)state);
392 void EffectStackView::slotRegionChanged()
394 emit updateClipRegion(m_clipref, m_ui.effectlist->currentRow(), m_ui.region_url->text());
397 #include "effectstackview.moc"