]> git.sesse.net Git - kdenlive/blob - src/effectstackview.cpp
Various fixes to improve general stability in Qt 4.5.2
[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 "kdenlivesettings.h"
25
26 #include <KDebug>
27 #include <KLocale>
28 #include <KMessageBox>
29 #include <KStandardDirs>
30
31 #include <QMenu>
32 #include <QTextStream>
33 #include <QFile>
34 #include <QInputDialog>
35
36
37 EffectStackView::EffectStackView(QWidget *parent) :
38         QWidget(parent)
39 {
40     m_ui.setupUi(this);
41     m_effectedit = new EffectStackEdit(m_ui.frame);
42     //m_ui.effectlist->horizontalHeader()->setVisible(false);
43     //m_ui.effectlist->verticalHeader()->setVisible(false);
44     m_clipref = NULL;
45
46     m_ui.buttonNew->setIcon(KIcon("document-new"));
47     m_ui.buttonNew->setToolTip(i18n("Add new effect"));
48     m_ui.buttonUp->setIcon(KIcon("go-up"));
49     m_ui.buttonUp->setToolTip(i18n("Move effect up"));
50     m_ui.buttonDown->setIcon(KIcon("go-down"));
51     m_ui.buttonDown->setToolTip(i18n("Move effect down"));
52     m_ui.buttonDel->setIcon(KIcon("edit-delete"));
53     m_ui.buttonDel->setToolTip(i18n("Delete effect"));
54     m_ui.buttonSave->setIcon(KIcon("document-save"));
55     m_ui.buttonSave->setToolTip(i18n("Save effect"));
56     m_ui.buttonReset->setIcon(KIcon("view-refresh"));
57     m_ui.buttonReset->setToolTip(i18n("Reset effect"));
58
59
60     m_ui.effectlist->setDragDropMode(QAbstractItemView::NoDragDrop);//use internal if drop is recognised right
61
62     connect(m_ui.effectlist, SIGNAL(itemSelectionChanged()), this , SLOT(slotItemSelectionChanged()));
63     connect(m_ui.effectlist, SIGNAL(itemChanged(QListWidgetItem *)), this , SLOT(slotItemChanged(QListWidgetItem *)));
64     connect(m_ui.buttonUp, SIGNAL(clicked()), this, SLOT(slotItemUp()));
65     connect(m_ui.buttonDown, SIGNAL(clicked()), this, SLOT(slotItemDown()));
66     connect(m_ui.buttonDel, SIGNAL(clicked()), this, SLOT(slotItemDel()));
67     connect(m_ui.buttonSave, SIGNAL(clicked()), this, SLOT(slotSaveEffect()));
68     connect(m_ui.buttonReset, SIGNAL(clicked()), this, SLOT(slotResetEffect()));
69     connect(m_effectedit, SIGNAL(parameterChanged(const QDomElement, const QDomElement)), this , SLOT(slotUpdateEffectParams(const QDomElement, const QDomElement)));
70     m_effectLists["audio"] = &MainWindow::audioEffects;
71     m_effectLists["video"] = &MainWindow::videoEffects;
72     m_effectLists["custom"] = &MainWindow::customEffects;
73     m_ui.splitter->setStretchFactor(1, 10);
74     m_ui.splitter->setStretchFactor(0, 1);
75     setEnabled(false);
76 }
77
78 EffectStackView::~EffectStackView()
79 {
80     m_effectLists.clear();
81     delete m_effectedit;
82 }
83
84 void EffectStackView::setMenu(QMenu *menu)
85 {
86     m_ui.buttonNew->setMenu(menu);
87 }
88
89 void EffectStackView::updateProjectFormat(MltVideoProfile profile, Timecode t)
90 {
91     m_effectedit->updateProjectFormat(profile, t);
92 }
93
94 void EffectStackView::slotSaveEffect()
95 {
96     QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
97     if (name.isEmpty()) return;
98     QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
99     path = path + name + ".xml";
100     if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File already exists.\nDo you want to overwrite it?")) == KMessageBox::No) return;
101
102     int i = m_ui.effectlist->currentRow();
103     QDomDocument doc;
104     QDomElement effect = m_clipref->effectAt(i).cloneNode().toElement();
105     doc.appendChild(doc.importNode(effect, true));
106     effect = doc.firstChild().toElement();
107     effect.removeAttribute("kdenlive_ix");
108     effect.setAttribute("id", name);
109     effect.setAttribute("type", "custom");
110     QDomElement effectname = effect.firstChildElement("name");
111     effect.removeChild(effectname);
112     effectname = doc.createElement("name");
113     QDomText nametext = doc.createTextNode(name);
114     effectname.appendChild(nametext);
115     effect.insertBefore(effectname, QDomNode());
116     QDomElement effectprops = effect.firstChildElement("properties");
117     effectprops.setAttribute("id", name);
118     effectprops.setAttribute("type", "custom");
119
120
121     QFile file(path);
122     if (file.open(QFile::WriteOnly | QFile::Truncate)) {
123         QTextStream out(&file);
124         out << doc.toString();
125     }
126     file.close();
127     emit reloadEffects();
128 }
129
130 void EffectStackView::slotUpdateEffectParams(const QDomElement old, const QDomElement e)
131 {
132     if (m_clipref)
133         emit updateClipEffect(m_clipref, old, e, m_ui.effectlist->currentRow());
134 }
135
136 void EffectStackView::slotClipItemSelected(ClipItem* c, int ix)
137 {
138     if (c && c == m_clipref) {
139         if (ix == -1) ix = m_ui.effectlist->currentRow();
140     } else {
141         m_clipref = c;
142         if (c) {
143             ix = c->selectedEffectIndex();
144             QString size = c->baseClip()->getProperty("frame_size");
145             double factor = c->baseClip()->getProperty("aspect_ratio").toDouble();
146             QPoint p((int)(size.section('x', 0, 0).toInt() * factor + 0.5), size.section('x', 1, 1).toInt());
147             m_effectedit->setFrameSize(p);
148         } else ix = 0;
149     }
150     if (m_clipref == NULL) {
151         m_ui.effectlist->blockSignals(true);
152         m_ui.effectlist->clear();
153         m_effectedit->transferParamDesc(QDomElement(), 0, 0);
154         m_ui.effectlist->blockSignals(false);
155         setEnabled(false);
156         return;
157     }
158     setEnabled(true);
159     setupListView(ix);
160 }
161
162 void EffectStackView::slotItemChanged(QListWidgetItem *item)
163 {
164     bool disable = true;
165     if (item->checkState() == Qt::Checked) disable = false;
166     m_ui.frame->setEnabled(!disable);
167     m_ui.buttonReset->setEnabled(!disable);
168     int activeRow = m_ui.effectlist->currentRow();
169     if (activeRow >= 0) {
170         emit changeEffectState(m_clipref, activeRow, disable);
171     }
172 }
173
174
175 void EffectStackView::setupListView(int ix)
176 {
177     m_ui.effectlist->blockSignals(true);
178     m_ui.effectlist->clear();
179
180     // Issue 238: Add icons for effect type in effectstack.
181     KIcon videoIcon("kdenlive-show-video");
182     KIcon audioIcon("kdenlive-show-audio");
183     QListWidgetItem* item;
184
185     for (int i = 0; i < m_clipref->effectsCount(); i++) {
186         const QDomElement d = m_clipref->effectAt(i);
187         if (d.isNull()) {
188             kDebug() << " . . . . WARNING, NULL EFFECT IN STACK!!!!!!!!!";
189             continue;
190         }
191
192         /*QDomDocument doc;
193         doc.appendChild(doc.importNode(d, true));
194         kDebug() << "IMPORTED STK: " << doc.toString();*/
195
196         QDomNode namenode = d.elementsByTagName("name").item(0);
197         if (!namenode.isNull()) {
198             // Issue 238: Add icons for effect type in effectstack.
199             // Logic more or less copied from initeffects.cpp
200             QString type = d.attribute("type", QString());
201             if ("audio" == type) {
202                 item = new QListWidgetItem(audioIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
203             } else if ("custom" == type) {
204                 item = new QListWidgetItem(i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
205             } else {
206                 item = new QListWidgetItem(videoIcon, i18n(namenode.toElement().text().toUtf8().data()), m_ui.effectlist);
207             }
208             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
209             if (d.attribute("disabled") == "1") item->setCheckState(Qt::Unchecked);
210             else item->setCheckState(Qt::Checked);
211         }
212     }
213     if (m_ui.effectlist->count() == 0) {
214         m_ui.buttonDel->setEnabled(false);
215         m_ui.buttonSave->setEnabled(false);
216         m_ui.buttonReset->setEnabled(false);
217         m_ui.buttonUp->setEnabled(false);
218         m_ui.buttonDown->setEnabled(false);
219     } else {
220         if (ix < 0) ix = 0;
221         if (ix > m_ui.effectlist->count() - 1) ix = m_ui.effectlist->count() - 1;
222         m_ui.effectlist->setCurrentRow(ix);
223     }
224     m_ui.effectlist->blockSignals(false);
225     if (m_ui.effectlist->count() == 0) m_effectedit->transferParamDesc(QDomElement(), 0, 0);
226     else slotItemSelectionChanged(false);
227 }
228
229 void EffectStackView::slotItemSelectionChanged(bool update)
230 {
231     bool hasItem = m_ui.effectlist->currentItem();
232     int activeRow = m_ui.effectlist->currentRow();
233     bool isChecked = false;
234     if (hasItem && m_ui.effectlist->currentItem()->checkState() == Qt::Checked) isChecked = true;
235     if (hasItem && m_ui.effectlist->currentItem()->isSelected()) {
236         m_effectedit->transferParamDesc(m_clipref->effectAt(activeRow), m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
237     }
238     if (m_clipref && update) m_clipref->setSelectedEffect(activeRow);
239     m_ui.buttonDel->setEnabled(hasItem);
240     m_ui.buttonSave->setEnabled(hasItem);
241     m_ui.buttonReset->setEnabled(hasItem && isChecked);
242     m_ui.buttonUp->setEnabled(activeRow > 0);
243     m_ui.buttonDown->setEnabled((activeRow < m_ui.effectlist->count() - 1) && hasItem);
244     m_ui.frame->setEnabled(isChecked);
245 }
246
247 void EffectStackView::slotItemUp()
248 {
249     int activeRow = m_ui.effectlist->currentRow();
250     if (activeRow <= 0) return;
251     emit changeEffectPosition(m_clipref, activeRow + 1, activeRow);
252 }
253
254 void EffectStackView::slotItemDown()
255 {
256     int activeRow = m_ui.effectlist->currentRow();
257     if (activeRow >= m_ui.effectlist->count() - 1) return;
258     emit changeEffectPosition(m_clipref, activeRow + 1, activeRow + 2);
259 }
260
261 void EffectStackView::slotItemDel()
262 {
263     int activeRow = m_ui.effectlist->currentRow();
264     if (activeRow >= 0) {
265         emit removeEffect(m_clipref, m_clipref->effectAt(activeRow));
266     }
267 }
268
269 void EffectStackView::slotResetEffect()
270 {
271     int activeRow = m_ui.effectlist->currentRow();
272     if (activeRow < 0) return;
273     QDomElement old = m_clipref->effectAt(activeRow).cloneNode().toElement();
274     QDomElement dom;
275     QString effectName = m_ui.effectlist->currentItem()->text();
276     foreach(const QString &type, m_effectLists.keys()) {
277         EffectsList *list = m_effectLists[type];
278         if (list->effectNames().contains(effectName)) {
279             dom = list->getEffectByName(effectName).cloneNode().toElement();
280             break;
281         }
282     }
283     if (!dom.isNull()) {
284         dom.setAttribute("kdenlive_ix", old.attribute("kdenlive_ix"));
285         m_clipref->initEffect(dom);
286         m_effectedit->transferParamDesc(dom, m_clipref->cropStart().frames(KdenliveSettings::project_fps()), m_clipref->cropDuration().frames(KdenliveSettings::project_fps()));//minx max frame
287         emit updateClipEffect(m_clipref, old, dom, activeRow);
288     }
289 }
290
291
292 void EffectStackView::raiseWindow(QWidget* dock)
293 {
294     if (m_clipref && dock)
295         dock->raise();
296 }
297
298 void EffectStackView::clear()
299 {
300     m_ui.effectlist->blockSignals(true);
301     m_ui.effectlist->clear();
302     m_ui.buttonDel->setEnabled(false);
303     m_ui.buttonSave->setEnabled(false);
304     m_ui.buttonReset->setEnabled(false);
305     m_ui.buttonUp->setEnabled(false);
306     m_ui.buttonDown->setEnabled(false);
307     m_effectedit->transferParamDesc(QDomElement(), 0, 0);
308     m_ui.effectlist->blockSignals(false);
309 }
310
311 #include "effectstackview.moc"