]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
Introduce very basic geometry editing on the monitor.
[kdenlive] / src / effectstackview.cpp
1 /***************************************************************************
2                           effecstackview.cpp  -  description
3                              -------------------
4     begin                : Feb 15 2008
5     copyright            : (C) 2008 by Marco Gittler
6     email                : g.marco@freenet.de
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
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.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18
19 #include "effectstackview.h"
20 #include "effectslist.h"
21 #include "clipitem.h"
22 #include "mainwindow.h"
23 #include "docclipbase.h"
24 #include "projectlist.h"
25 #include "kthumb.h"
26 #include "kdenlivesettings.h"
27
28 #include <KDebug>
29 #include <KLocale>
30 #include <KMessageBox>
31 #include <KStandardDirs>
32 #include <KFileDialog>
33
34 #include <QMenu>
35 #include <QTextStream>
36 #include <QFile>
37 #include <QInputDialog>
38
39
40 EffectStackView::EffectStackView(Monitor *monitor, QWidget *parent) :
41         QWidget(parent)
42 {
43     m_ui.setupUi(this);
44     QVBoxLayout *vbox1 = new QVBoxLayout(m_ui.frame);
45     m_effectedit = new EffectStackEdit(monitor, m_ui.frame);
46     vbox1->setContentsMargins(0, 0, 0, 0);
47     vbox1->setSpacing(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);
53     m_clipref = NULL;
54
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"));
68
69
70     m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop); //use internal if drop is recognised right
71
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);
90     setEnabled(false);
91 }
92
93 EffectStackView::~EffectStackView()
94 {
95     m_effectLists.clear();
96     delete m_effectedit;
97 }
98
99 void EffectStackView::updateTimecodeFormat()
100 {
101     m_effectedit->updateTimecodeFormat();
102 }
103
104 void EffectStackView::setMenu(QMenu *menu)
105 {
106     m_ui.buttonNew->setMenu(menu);
107 }
108
109 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
110 {
111     m_effectedit->updateProjectFormat(profile, t);
112 }
113
114 void EffectStackView::slotSaveEffect()
115 {
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;
121
122     int i = m_ui.effectlist->currentRow();
123     QDomDocument doc;
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");
139
140     QFile file(path);
141     if (file.open(QFile::WriteOnly | QFile::Truncate)) {
142         QTextStream out(&file);
143         out << doc.toString();
144     }
145     file.close();
146     emit reloadEffects();
147 }
148
149 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
150 {
151     if (m_clipref)
152         emit updateClipEffect(m_clipref, old, e, m_ui.effectlist->currentRow());
153 }
154
155 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
156 {
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;
161     } else {
162         m_clipref = c;
163         if (c) {
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);
170         } else ix = 0;
171     }
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);
178         setEnabled(false);
179         return;
180     }
181     setEnabled(true);
182     setupListView(ix);
183 }
184
185 void EffectStackView::slotItemChanged(QListWidgetItem *item)
186 {
187     bool disable = true;
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);
195     }
196     slotUpdateCheckAllButton();
197 }
198
199
200 void EffectStackView::setupListView(int ix)
201 {
202     m_ui.effectlist->blockSignals(true);
203     m_ui.effectlist->clear();
204
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;
210
211     for (int i = 0; i < m_clipref->effectsCount(); i++) {
212         const QDomElement d = m_clipref->effectAt(i);
213         if (d.isNull()) {
214             kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
215             continue;
216         }
217
218         /*QDomDocument doc;
219         doc.appendChild(doc.importNode(d, true));
220         kDebug() << "IMPORTED STK: " << doc.toString();*/
221
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);
231             } else {
232                 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
233             }
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);
237         }
238     }
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);
246     } else {
247         if (ix < 0) ix = 0;
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);
251     }
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();
258 }
259
260 void EffectStackView::slotItemSelectionChanged(bool update)
261 {
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,
269                                         0,
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")));
273     }
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);
281 }
282
283 void EffectStackView::slotItemUp()
284 {
285     int activeRow = m_ui.effectlist->currentRow();
286     if (activeRow <= 0) return;
287     emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
288 }
289
290 void EffectStackView::slotItemDown()
291 {
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);
295 }
296
297 void EffectStackView::slotItemDel()
298 {
299     int activeRow = m_ui.effectlist->currentRow();
300     if (activeRow >= 0) {
301         emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
302         slotUpdateCheckAllButton();
303     }
304 }
305
306 void EffectStackView::slotResetEffect()
307 {
308     int activeRow = m_ui.effectlist->currentRow();
309     if (activeRow < 0) return;
310     QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
311     QDomElement dom;
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();
317             break;
318         }
319     }
320     if (!dom.isNull()) {
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);
326     }
327 }
328
329
330 void EffectStackView::raiseWindow(QWidget* dock)
331 {
332     if (m_clipref && dock)
333         dock->raise();
334 }
335
336 void EffectStackView::clear()
337 {
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);
349 }
350
351
352 void EffectStackView::slotSeekTimeline(int pos)
353 {
354     if (m_clipref)
355         emit seekTimeline(m_clipref->startPos().frames(KdenliveSettings::project_fps()) + pos);
356 }
357
358 void EffectStackView::slotUpdateCheckAllButton()
359 {
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)
364             hasEnabled = true;
365         else
366             hasDisabled = true;
367     }
368
369     m_ui.checkAll->blockSignals(true);
370     if (hasEnabled && hasDisabled)
371         m_ui.checkAll->setCheckState(Qt::PartiallyChecked);
372     else if (hasEnabled)
373         m_ui.checkAll->setCheckState(Qt::Checked);
374     else
375         m_ui.checkAll->setCheckState(Qt::Unchecked);
376     m_ui.checkAll->blockSignals(false);
377 }
378
379 void EffectStackView::slotCheckAll(int state)
380 {
381     if (state == 1) {
382         state = 2;
383         m_ui.checkAll->blockSignals(true);
384         m_ui.checkAll->setCheckState(Qt::Checked);
385         m_ui.checkAll->blockSignals(false);
386     }
387
388     for (int i = 0; i < m_ui.effectlist->count(); ++i)
389         m_ui.effectlist->item(i)->setCheckState((Qt::CheckState)state);
390 }
391
392 void EffectStackView::slotRegionChanged()
393 {
394     emit updateClipRegion(m_clipref, m_ui.effectlist->currentRow(), m_ui.region_url->text());
395 }
396
397 #include "effectstackview.moc"