]> git.sesse.net Git - kdenlive/blob - src/effectstack/collapsiblegroup.cpp
Fix effect stack global enable/disable checkbox
[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(3, 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     m_menu->addAction(KIcon("list-remove"), i18n("Ungroup"), this, SLOT(slotUnGroup()));
82     setAcceptDrops(true);
83     menuButton->setIcon(KIcon("kdenlive-menu"));
84     menuButton->setMenu(m_menu);
85     
86     enabledButton->setChecked(false);
87     enabledButton->setIcon(KIcon("visible"));
88
89     connect(collapseButton, SIGNAL(clicked()), this, SLOT(slotSwitch()));
90     connect(enabledButton, 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(slotDeleteGroup()));
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 disable)
126 {
127     m_title->setEnabled(!disable);
128     enabledButton->blockSignals(true);
129     enabledButton->setChecked(disable);
130     enabledButton->setIcon(disable ? KIcon("novisible") : KIcon("visible"));
131     enabledButton->blockSignals(false);
132     for (int i = 0; i < m_subWidgets.count(); i++)
133         m_subWidgets.at(i)->slotEnable(disable);
134 }
135
136 void CollapsibleGroup::slotDeleteGroup()
137 {
138     QDomDocument doc;
139     // delete effects from the last one to the first, otherwise each deletion would trigger an update
140     // in other effects's kdenlive_ix index.
141     for (int i = m_subWidgets.count() - 1; i >= 0; i--)
142         doc.appendChild(doc.importNode(m_subWidgets.at(i)->effect(), true));
143     emit deleteGroup(doc);
144 }
145
146 void CollapsibleGroup::slotEffectUp()
147 {
148     emit changeGroupPosition(groupIndex(), true);
149 }
150
151 void CollapsibleGroup::slotEffectDown()
152 {
153     emit changeGroupPosition(groupIndex(), false);
154 }
155
156 void CollapsibleGroup::slotSaveGroup()
157 {
158     QString name = QInputDialog::getText(this, i18n("Save Group"), i18n("Name for saved group: "), QLineEdit::Normal, m_title->text());
159     if (name.isEmpty()) return;
160     QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
161     path = path + name + ".xml";
162     if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", path)) == KMessageBox::No) return;
163
164     QDomDocument doc = effectsData();
165     QDomElement base = doc.documentElement();
166     QDomNodeList effects = base.elementsByTagName("effect");
167     for (int i = 0; i < effects.count(); i++) {
168         QDomElement eff = effects.at(i).toElement();
169         eff.removeAttribute("kdenlive_ix");
170         QString kdenliveInfo = eff.attribute("kdenlive_info");
171         // Make sure all effects have the correct new group name
172         if (kdenliveInfo.count('/') >= 2) {
173             eff.setAttribute("kdenlive_info", kdenliveInfo.section('/', 0, 1) + "/" + name);
174         }
175     }
176     
177     base.setAttribute("name", name);
178     base.setAttribute("id", name);
179     base.setAttribute("type", "custom");  
180
181     QFile file(path);
182     if (file.open(QFile::WriteOnly | QFile::Truncate)) {
183         QTextStream out(&file);
184         out << doc.toString();
185     }
186     file.close();
187     emit reloadEffects();
188 }
189
190 void CollapsibleGroup::slotResetGroup()
191 {
192     QMutexLocker lock(&m_mutex);
193     for (int i = 0; i < m_subWidgets.count(); i++)
194         m_subWidgets.at(i)->slotResetEffect();
195 }
196
197 void CollapsibleGroup::slotSwitch()
198 {
199     bool enable = !widgetFrame->isVisible();
200     slotShow(enable);
201 }
202
203 void CollapsibleGroup::slotShow(bool show)
204 {
205     widgetFrame->setVisible(show);
206     if (show) {
207         collapseButton->setArrowType(Qt::DownArrow);
208         m_info.isCollapsed = false;
209     }
210     else {
211         collapseButton->setArrowType(Qt::RightArrow);
212         m_info.isCollapsed = true;
213     }
214     //emit parameterChanged(m_original_effect, m_effect, effectIndex());   
215 }
216
217 QWidget *CollapsibleGroup::title() const
218 {
219     return m_title;
220 }
221
222 void CollapsibleGroup::addGroupEffect(CollapsibleEffect *effect)
223 {
224     QMutexLocker lock(&m_mutex);
225     QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
226     if (vbox == NULL) {
227         vbox = new QVBoxLayout();
228         vbox->setContentsMargins(0, 0, 0, 0);
229         vbox->setSpacing(2);
230         widgetFrame->setLayout(vbox);
231     }
232     effect->setGroupIndex(groupIndex());
233     effect->setGroupName(m_title->text());
234     effect->decoframe->setObjectName("decoframesub");
235     m_subWidgets.append(effect);
236     vbox->addWidget(effect);
237 }
238
239 QString CollapsibleGroup::infoString() const
240 {
241     return m_info.toString();
242 }
243
244 void CollapsibleGroup::removeGroup(int ix, QVBoxLayout *layout)
245 {
246     QMutexLocker lock(&m_mutex);
247     QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
248     if (vbox == NULL) return;
249     for (int i = m_subWidgets.count() - 1; i >= 0 ; i--) {
250         vbox->removeWidget(m_subWidgets.at(i));
251         layout->insertWidget(ix, m_subWidgets.at(i));
252         m_subWidgets.at(i)->decoframe->setObjectName("decoframe");
253         m_subWidgets.at(i)->removeFromGroup();
254     }
255     m_subWidgets.clear();
256 }
257
258 int CollapsibleGroup::groupIndex() const
259 {
260     return m_index;
261 }
262
263 bool CollapsibleGroup::isGroup() const
264 {
265     return true;
266 }
267
268 void CollapsibleGroup::updateTimecodeFormat()
269 {
270     QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
271     if (vbox == NULL) return;
272     for (int j = vbox->count() - 1; j >= 0; j--) {
273         CollapsibleEffect *e = static_cast<CollapsibleEffect *>(vbox->itemAt(j)->widget());
274         if (e) e->updateTimecodeFormat();
275     }
276 }
277
278 void CollapsibleGroup::dragEnterEvent(QDragEnterEvent *event)
279 {
280     if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
281         framegroup->setProperty("target", true);
282         framegroup->setStyleSheet(framegroup->styleSheet());
283         event->acceptProposedAction();
284     }
285 }
286
287 void CollapsibleGroup::dragLeaveEvent(QDragLeaveEvent */*event*/)
288 {
289     framegroup->setProperty("target", false);
290     framegroup->setStyleSheet(framegroup->styleSheet());
291 }
292
293 void CollapsibleGroup::dropEvent(QDropEvent *event)
294 {
295     framegroup->setProperty("target", false);
296     framegroup->setStyleSheet(framegroup->styleSheet());
297     const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
298     //event->acceptProposedAction();
299     QDomDocument doc;
300     doc.setContent(effects, true);
301     QDomElement e = doc.documentElement();
302     int ix = e.attribute("kdenlive_ix").toInt();
303     if (ix == 0) {
304         // effect dropped from effects list, add it
305         e.setAttribute("kdenlive_ix", ix);
306         event->setDropAction(Qt::CopyAction);
307         event->accept();
308         emit addEffect(e);
309         return;
310     }
311     if (m_subWidgets.isEmpty()) return;
312     int new_index = m_subWidgets.last()->effectIndex();
313     emit moveEffect(ix, new_index, m_index, m_title->text());
314     event->setDropAction(Qt::MoveAction);
315     event->accept();
316 }
317
318 void CollapsibleGroup::slotRenameGroup()
319 {
320     m_title->setReadOnly(true);
321     if (m_title->text().isEmpty()) m_title->setText(i18n("Effect Group"));
322     for (int j = 0; j < m_subWidgets.count(); j++) {
323         m_subWidgets.at(j)->setGroupName(m_title->text());
324     }
325     emit groupRenamed(this);
326 }
327
328 QList <CollapsibleEffect*> CollapsibleGroup::effects()
329 {
330     QMutexLocker lock(&m_mutex);
331     return m_subWidgets;
332 }
333
334 QDomDocument CollapsibleGroup::effectsData()
335 {
336     QMutexLocker lock(&m_mutex);
337     QDomDocument doc;
338     QDomElement list = doc.createElement("effectgroup");
339     list.setAttribute("name", m_title->text());
340     doc.appendChild(list);
341     for (int j = 0; j < m_subWidgets.count(); j++) {
342         list.appendChild(doc.importNode(m_subWidgets.at(j)->effect(), true));
343     }
344     return doc;
345 }
346