]> git.sesse.net Git - kdenlive/blob - src/effectstack/collapsiblegroup.cpp
new: saving of effect group
[kdenlive] / src / effectstack / collapsiblegroup.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "collapsiblegroup.h"
22
23
24 #include <QMenu>
25 #include <QVBoxLayout>
26 #include <QInputDialog>
27 #include <QDragEnterEvent>
28 #include <QDropEvent>
29 #include <QMutexLocker>
30
31 #include <KDebug>
32 #include <KGlobalSettings>
33 #include <KLocale>
34 #include <KMessageBox>
35 #include <KStandardDirs>
36 #include <KFileDialog>
37 #include <KUrlRequester>
38 #include <KColorScheme>
39
40 MyEditableLabel::MyEditableLabel(QWidget * parent):
41     QLineEdit(parent)
42 {
43     setFrame(false);
44     setReadOnly(true);
45     setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
46 }
47
48 void MyEditableLabel::mouseDoubleClickEvent ( QMouseEvent * e )
49 {
50     setReadOnly(false);
51     selectAll();
52     e->accept();
53 }
54
55
56 CollapsibleGroup::CollapsibleGroup(int ix, bool firstGroup, bool lastGroup, QString groupName, QWidget * parent) :
57         AbstractCollapsibleWidget(parent),
58         m_index(ix)
59 {
60     setupUi(this);
61     m_subWidgets = QList <CollapsibleEffect *> ();
62     setFont(KGlobalSettings::smallestReadableFont());
63     QHBoxLayout *l = static_cast <QHBoxLayout *>(framegroup->layout());
64     m_title = new MyEditableLabel(this);
65     l->insertWidget(4, m_title);
66     m_title->setText(groupName.isEmpty() ? i18n("Effect Group") : groupName);
67     connect(m_title, SIGNAL(editingFinished()), this, SLOT(slotRenameGroup()));
68     buttonUp->setIcon(KIcon("kdenlive-up"));
69     buttonUp->setToolTip(i18n("Move effect up"));
70     buttonDown->setIcon(KIcon("kdenlive-down"));
71     buttonDown->setToolTip(i18n("Move effect down"));
72
73     buttonDel->setIcon(KIcon("kdenlive-deleffect"));
74     buttonDel->setToolTip(i18n("Delete effect"));
75     if (firstGroup) buttonUp->setVisible(false);
76     if (lastGroup) buttonDown->setVisible(false);
77     m_menu = new QMenu;
78     m_menu->addAction(KIcon("view-refresh"), i18n("Reset Group"), this, SLOT(slotResetGroup()));
79     m_menu->addAction(KIcon("document-save"), i18n("Save Group"), this, SLOT(slotSaveGroup()));
80     
81     effecticon->setPixmap(KIcon("folder").pixmap(16,16));
82     m_menu->addAction(KIcon("list-remove"), i18n("Ungroup"), this, SLOT(slotUnGroup()));
83     setAcceptDrops(true);
84     menuButton->setIcon(KIcon("kdenlive-menu"));
85     menuButton->setMenu(m_menu);
86     
87     enabledBox->setChecked(true);
88
89     connect(collapseButton, SIGNAL(clicked()), this, SLOT(slotSwitch()));
90     connect(enabledBox, SIGNAL(toggled(bool)), this, SLOT(slotEnable(bool)));
91     connect(buttonUp, SIGNAL(clicked()), this, SLOT(slotEffectUp()));
92     connect(buttonDown, SIGNAL(clicked()), this, SLOT(slotEffectDown()));
93     connect(buttonDel, SIGNAL(clicked()), this, SLOT(slotDeleteEffect()));
94
95 }
96
97 CollapsibleGroup::~CollapsibleGroup()
98 {
99     delete m_menu;
100 }
101
102 void CollapsibleGroup::slotUnGroup()
103 {
104     emit unGroup(this);
105 }
106
107 bool CollapsibleGroup::isActive() const
108 {
109     return decoframegroup->property("active").toBool();
110 }
111
112 void CollapsibleGroup::setActive(bool activate)
113 {
114     decoframegroup->setProperty("active", activate);
115     decoframegroup->setStyleSheet(decoframegroup->styleSheet());
116 }
117
118 void CollapsibleGroup::mouseDoubleClickEvent ( QMouseEvent * event )
119 {
120     if (framegroup->underMouse() && collapseButton->isEnabled()) slotSwitch();
121     QWidget::mouseDoubleClickEvent(event);
122 }
123
124
125 void CollapsibleGroup::slotEnable(bool enable)
126 {
127     m_title->setEnabled(enable);
128     enabledBox->blockSignals(true);
129     enabledBox->setChecked(enable);
130     enabledBox->blockSignals(false);
131     QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
132     if (vbox == NULL) return;
133     for (int i = 0; i < vbox->count(); i++) {
134         CollapsibleGroup *e = static_cast<CollapsibleGroup *>(vbox->itemAt(i)->widget());
135         if (e) e->enabledBox->setChecked(enable);// slotEnable(enable);
136     }
137 }
138
139 void CollapsibleGroup::slotDeleteEffect()
140 {
141     emit deleteGroup(groupIndex());
142 }
143
144 void CollapsibleGroup::slotEffectUp()
145 {
146     emit changeGroupPosition(groupIndex(), true);
147 }
148
149 void CollapsibleGroup::slotEffectDown()
150 {
151     emit changeGroupPosition(groupIndex(), false);
152 }
153
154 void CollapsibleGroup::slotSaveGroup()
155 {
156     QString name = QInputDialog::getText(this, i18n("Save Group"), i18n("Name for saved group: "), QLineEdit::Normal, m_title->text());
157     if (name.isEmpty()) return;
158     QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
159     path = path + name + ".xml";
160     if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", path)) == KMessageBox::No) return;
161
162     QDomDocument doc = effectsData();
163     QDomElement base = doc.documentElement();
164     QDomNodeList effects = base.elementsByTagName("effect");
165     for (int i = 0; i < effects.count(); i++) {
166         QDomElement eff = effects.at(i).toElement();
167         eff.removeAttribute("kdenlive_ix");
168         QString kdenliveInfo = eff.attribute("kdenlive_info");
169         // Make sure all effects have the correct new group name
170         if (kdenliveInfo.count('/') >= 2) {
171             eff.setAttribute("kdenlive_info", kdenliveInfo.section('/', 0, 1) + "/" + name);
172         }
173     }
174     
175     base.setAttribute("name", name);
176     base.setAttribute("id", name);
177     base.setAttribute("type", "custom");  
178
179     QFile file(path);
180     if (file.open(QFile::WriteOnly | QFile::Truncate)) {
181         QTextStream out(&file);
182         out << doc.toString();
183     }
184     file.close();
185     emit reloadEffects();
186 }
187
188 void CollapsibleGroup::slotResetGroup()
189 {
190     QMutexLocker lock(&m_mutex);
191     for (int i = 0; i < m_subWidgets.count(); i++)
192         m_subWidgets.at(i)->slotResetEffect();
193 }
194
195 void CollapsibleGroup::slotSwitch()
196 {
197     bool enable = !widgetFrame->isVisible();
198     slotShow(enable);
199 }
200
201 void CollapsibleGroup::slotShow(bool show)
202 {
203     widgetFrame->setVisible(show);
204     if (show) {
205         collapseButton->setArrowType(Qt::DownArrow);
206         m_info.isCollapsed = false;
207     }
208     else {
209         collapseButton->setArrowType(Qt::RightArrow);
210         m_info.isCollapsed = true;
211     }
212     //emit parameterChanged(m_original_effect, m_effect, effectIndex());   
213 }
214
215 QWidget *CollapsibleGroup::title() const
216 {
217     return m_title;
218 }
219
220 void CollapsibleGroup::addGroupEffect(CollapsibleEffect *effect)
221 {
222     QMutexLocker lock(&m_mutex);
223     QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
224     if (vbox == NULL) {
225         vbox = new QVBoxLayout();
226         vbox->setContentsMargins(0, 0, 0, 0);
227         vbox->setSpacing(2);
228         widgetFrame->setLayout(vbox);
229     }
230     effect->setGroupIndex(groupIndex());
231     effect->setGroupName(m_title->text());
232     m_subWidgets.append(effect);
233     vbox->addWidget(effect);
234 }
235
236 QString CollapsibleGroup::infoString() const
237 {
238     return m_info.toString();
239 }
240
241 void CollapsibleGroup::removeGroup(int ix, QVBoxLayout *layout)
242 {
243     QMutexLocker lock(&m_mutex);
244     QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
245     if (vbox == NULL) return;
246     for (int i = m_subWidgets.count() - 1; i >= 0 ; i--) {
247         vbox->removeWidget(m_subWidgets.at(i));
248         layout->insertWidget(ix, m_subWidgets.at(i));
249         m_subWidgets.at(i)->removeFromGroup();
250     }
251     m_subWidgets.clear();
252 }
253
254 int CollapsibleGroup::groupIndex() const
255 {
256     return m_index;
257 }
258
259 bool CollapsibleGroup::isGroup() const
260 {
261     return true;
262 }
263
264 void CollapsibleGroup::updateTimecodeFormat()
265 {
266     QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
267     if (vbox == NULL) return;
268     for (int j = vbox->count() - 1; j >= 0; j--) {
269         CollapsibleEffect *e = static_cast<CollapsibleEffect *>(vbox->itemAt(j)->widget());
270         if (e) e->updateTimecodeFormat();
271     }
272 }
273
274 void CollapsibleGroup::dragEnterEvent(QDragEnterEvent *event)
275 {
276     if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
277         framegroup->setProperty("active", true);
278         framegroup->setStyleSheet(framegroup->styleSheet());
279         event->acceptProposedAction();
280     }
281 }
282
283 void CollapsibleGroup::dragLeaveEvent(QDragLeaveEvent */*event*/)
284 {
285     framegroup->setProperty("active", false);
286     framegroup->setStyleSheet(framegroup->styleSheet());
287 }
288
289 void CollapsibleGroup::dropEvent(QDropEvent *event)
290 {
291     QMutexLocker lock(&m_mutex);
292     framegroup->setProperty("active", false);
293     framegroup->setStyleSheet(framegroup->styleSheet());
294     const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
295     //event->acceptProposedAction();
296     QDomDocument doc;
297     doc.setContent(effects, true);
298     QDomElement e = doc.documentElement();
299     int ix = e.attribute("kdenlive_ix").toInt();
300     if (ix == 0) {
301         // effect dropped from effects list, add it
302         e.setAttribute("kdenlive_ix", ix);
303         event->setDropAction(Qt::CopyAction);
304         event->accept();
305         emit addEffect(e);
306         return;
307     }
308     if (m_subWidgets.isEmpty()) return;
309     int new_index = m_subWidgets.at(m_subWidgets.count() - 1)->effectIndex();
310     emit moveEffect(ix, new_index, m_index, m_title->text());
311     event->setDropAction(Qt::MoveAction);
312     event->accept();
313 }
314
315 void CollapsibleGroup::slotRenameGroup()
316 {
317     m_title->setReadOnly(true);
318     if (m_title->text().isEmpty()) m_title->setText(i18n("Effect Group"));
319     for (int j = 0; j < m_subWidgets.count(); j++) {
320         m_subWidgets.at(j)->setGroupName(m_title->text());
321     }
322     emit groupRenamed(this);
323 }
324
325 QList <CollapsibleEffect*> CollapsibleGroup::effects()
326 {
327     QMutexLocker lock(&m_mutex);
328     return m_subWidgets;
329 }
330
331 QDomDocument CollapsibleGroup::effectsData()
332 {
333     QMutexLocker lock(&m_mutex);
334     QDomDocument doc;
335     QDomElement list = doc.createElement("effectgroup");
336     list.setAttribute("name", m_title->text());
337     doc.appendChild(list);
338     for (int j = 0; j < m_subWidgets.count(); j++) {
339         list.appendChild(doc.importNode(m_subWidgets.at(j)->effect(), true));
340     }
341     return doc;
342 }
343