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