]> git.sesse.net Git - kdenlive/blob - src/effectstack/collapsiblegroup.cpp
4e7afd847b63680592549e85e87ccb66f8f73176
[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         effects.at(i).toElement().removeAttribute("kdenlive_ix");
167     
168     base.setAttribute("id", name);
169     base.setAttribute("type", "custom");  
170
171     QFile file(path);
172     if (file.open(QFile::WriteOnly | QFile::Truncate)) {
173         QTextStream out(&file);
174         out << doc.toString();
175     }
176     file.close();
177     emit reloadEffects();
178 }
179
180 void CollapsibleGroup::slotResetGroup()
181 {
182     QMutexLocker lock(&m_mutex);
183     for (int i = 0; i < m_subWidgets.count(); i++)
184         m_subWidgets.at(i)->slotResetEffect();
185 }
186
187 void CollapsibleGroup::slotSwitch()
188 {
189     bool enable = !widgetFrame->isVisible();
190     slotShow(enable);
191 }
192
193 void CollapsibleGroup::slotShow(bool show)
194 {
195     widgetFrame->setVisible(show);
196     if (show) {
197         collapseButton->setArrowType(Qt::DownArrow);
198         m_info.isCollapsed = false;
199     }
200     else {
201         collapseButton->setArrowType(Qt::RightArrow);
202         m_info.isCollapsed = true;
203     }
204     //emit parameterChanged(m_original_effect, m_effect, effectIndex());   
205 }
206
207 QWidget *CollapsibleGroup::title() const
208 {
209     return m_title;
210 }
211
212 void CollapsibleGroup::addGroupEffect(CollapsibleEffect *effect)
213 {
214     QMutexLocker lock(&m_mutex);
215     QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
216     if (vbox == NULL) {
217         vbox = new QVBoxLayout();
218         vbox->setContentsMargins(0, 0, 0, 0);
219         vbox->setSpacing(2);
220         widgetFrame->setLayout(vbox);
221     }
222     effect->setGroupIndex(groupIndex());
223     effect->setGroupName(m_title->text());
224     m_subWidgets.append(effect);
225     vbox->addWidget(effect);
226 }
227
228 QString CollapsibleGroup::infoString() const
229 {
230     return m_info.toString();
231 }
232
233 void CollapsibleGroup::removeGroup(int ix, QVBoxLayout *layout)
234 {
235     QMutexLocker lock(&m_mutex);
236     QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
237     if (vbox == NULL) return;
238     for (int i = m_subWidgets.count() - 1; i >= 0 ; i--) {
239         vbox->removeWidget(m_subWidgets.at(i));
240         layout->insertWidget(ix, m_subWidgets.at(i));
241         m_subWidgets.at(i)->removeFromGroup();
242     }
243     m_subWidgets.clear();
244 }
245
246 int CollapsibleGroup::groupIndex() const
247 {
248     return m_index;
249 }
250
251 bool CollapsibleGroup::isGroup() const
252 {
253     return true;
254 }
255
256 void CollapsibleGroup::updateTimecodeFormat()
257 {
258     QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
259     if (vbox == NULL) return;
260     for (int j = vbox->count() - 1; j >= 0; j--) {
261         CollapsibleEffect *e = static_cast<CollapsibleEffect *>(vbox->itemAt(j)->widget());
262         if (e) e->updateTimecodeFormat();
263     }
264 }
265
266 void CollapsibleGroup::dragEnterEvent(QDragEnterEvent *event)
267 {
268     if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
269         framegroup->setProperty("active", true);
270         framegroup->setStyleSheet(framegroup->styleSheet());
271         event->acceptProposedAction();
272     }
273 }
274
275 void CollapsibleGroup::dragLeaveEvent(QDragLeaveEvent */*event*/)
276 {
277     framegroup->setProperty("active", false);
278     framegroup->setStyleSheet(framegroup->styleSheet());
279 }
280
281 void CollapsibleGroup::dropEvent(QDropEvent *event)
282 {
283     QMutexLocker lock(&m_mutex);
284     framegroup->setProperty("active", false);
285     framegroup->setStyleSheet(framegroup->styleSheet());
286     const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
287     //event->acceptProposedAction();
288     QDomDocument doc;
289     doc.setContent(effects, true);
290     QDomElement e = doc.documentElement();
291     int ix = e.attribute("kdenlive_ix").toInt();
292     if (ix == 0) {
293         // effect dropped from effects list, add it
294         e.setAttribute("kdenlive_ix", ix);
295         event->setDropAction(Qt::CopyAction);
296         event->accept();
297         emit addEffect(e);
298         return;
299     }
300     if (m_subWidgets.isEmpty()) return;
301     int new_index = m_subWidgets.at(m_subWidgets.count() - 1)->effectIndex();
302     emit moveEffect(ix, new_index, m_index, m_title->text());
303     event->setDropAction(Qt::MoveAction);
304     event->accept();
305 }
306
307 void CollapsibleGroup::slotRenameGroup()
308 {
309     m_title->setReadOnly(true);
310     if (m_title->text().isEmpty()) m_title->setText(i18n("Effect Group"));
311     for (int j = 0; j < m_subWidgets.count(); j++) {
312         m_subWidgets.at(j)->setGroupName(m_title->text());
313     }
314     emit groupRenamed(this);
315 }
316
317 QList <CollapsibleEffect*> CollapsibleGroup::effects()
318 {
319     QMutexLocker lock(&m_mutex);
320     return m_subWidgets;
321 }
322
323 QDomDocument CollapsibleGroup::effectsData()
324 {
325     QMutexLocker lock(&m_mutex);
326     QDomDocument doc;
327     QDomElement list = doc.createElement("list");
328     list.setAttribute("name", m_title->text());
329     doc.appendChild(list);
330     for (int j = 0; j < m_subWidgets.count(); j++) {
331         list.appendChild(doc.importNode(m_subWidgets.at(j)->effect(), true));
332     }
333     return doc;
334 }
335