1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "collapsiblegroup.h"
25 #include <QVBoxLayout>
26 #include <QInputDialog>
27 #include <QDragEnterEvent>
29 #include <QMutexLocker>
32 #include <KGlobalSettings>
34 #include <KMessageBox>
35 #include <KStandardDirs>
36 #include <KFileDialog>
37 #include <KUrlRequester>
38 #include <KColorScheme>
40 MyEditableLabel::MyEditableLabel(QWidget * parent):
45 setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
48 void MyEditableLabel::mouseDoubleClickEvent ( QMouseEvent * e )
56 CollapsibleGroup::CollapsibleGroup(int ix, bool firstGroup, bool lastGroup, EffectInfo info, QWidget * parent) :
57 AbstractCollapsibleWidget(parent)
60 m_info.groupIndex = ix;
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(info.groupName.isEmpty() ? i18n("Effect Group") : info.groupName);
67 m_info.groupName = m_title->text();
68 connect(m_title, SIGNAL(editingFinished()), this, SLOT(slotRenameGroup()));
69 buttonUp->setIcon(KIcon("kdenlive-up"));
70 buttonUp->setToolTip(i18n("Move effect up"));
71 buttonDown->setIcon(KIcon("kdenlive-down"));
72 buttonDown->setToolTip(i18n("Move effect down"));
74 buttonDel->setIcon(KIcon("kdenlive-deleffect"));
75 buttonDel->setToolTip(i18n("Delete effect"));
76 if (firstGroup) buttonUp->setVisible(false);
77 if (lastGroup) buttonDown->setVisible(false);
79 m_menu->addAction(KIcon("view-refresh"), i18n("Reset Group"), this, SLOT(slotResetGroup()));
80 m_menu->addAction(KIcon("document-save"), i18n("Save Group"), this, SLOT(slotSaveGroup()));
82 m_menu->addAction(KIcon("list-remove"), i18n("Ungroup"), this, SLOT(slotUnGroup()));
84 menuButton->setIcon(KIcon("kdenlive-menu"));
85 menuButton->setMenu(m_menu);
87 enabledButton->setChecked(false);
88 enabledButton->setIcon(KIcon("visible"));
90 if (info.groupIsCollapsed) {
94 connect(collapseButton, SIGNAL(clicked()), this, SLOT(slotSwitch()));
95 connect(enabledButton, SIGNAL(toggled(bool)), this, SLOT(slotEnable(bool)));
96 connect(buttonUp, SIGNAL(clicked()), this, SLOT(slotEffectUp()));
97 connect(buttonDown, SIGNAL(clicked()), this, SLOT(slotEffectDown()));
98 connect(buttonDel, SIGNAL(clicked()), this, SLOT(slotDeleteGroup()));
102 CollapsibleGroup::~CollapsibleGroup()
107 void CollapsibleGroup::slotUnGroup()
112 bool CollapsibleGroup::isActive() const
114 return decoframegroup->property("active").toBool();
117 void CollapsibleGroup::setActive(bool activate)
119 decoframegroup->setProperty("active", activate);
120 decoframegroup->setStyleSheet(decoframegroup->styleSheet());
123 void CollapsibleGroup::mouseDoubleClickEvent ( QMouseEvent * event )
125 if (framegroup->underMouse() && collapseButton->isEnabled()) slotSwitch();
126 QWidget::mouseDoubleClickEvent(event);
130 void CollapsibleGroup::slotEnable(bool disable)
132 m_title->setEnabled(!disable);
133 enabledButton->blockSignals(true);
134 enabledButton->setChecked(disable);
135 enabledButton->setIcon(disable ? KIcon("novisible") : KIcon("visible"));
136 enabledButton->blockSignals(false);
137 for (int i = 0; i < m_subWidgets.count(); i++)
138 m_subWidgets.at(i)->slotEnable(disable);
141 void CollapsibleGroup::slotDeleteGroup()
144 // delete effects from the last one to the first, otherwise each deletion would trigger an update
145 // in other effects's kdenlive_ix index.
146 for (int i = m_subWidgets.count() - 1; i >= 0; i--)
147 doc.appendChild(doc.importNode(m_subWidgets.at(i)->effect(), true));
148 emit deleteGroup(doc);
151 void CollapsibleGroup::slotEffectUp()
153 emit changeGroupPosition(groupIndex(), true);
156 void CollapsibleGroup::slotEffectDown()
158 emit changeGroupPosition(groupIndex(), false);
161 void CollapsibleGroup::slotSaveGroup()
163 QString name = QInputDialog::getText(this, i18n("Save Group"), i18n("Name for saved group: "), QLineEdit::Normal, m_title->text());
164 if (name.isEmpty()) return;
165 QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
166 path = path + name + ".xml";
167 if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", path)) == KMessageBox::No) return;
169 QDomDocument doc = effectsData();
170 QDomElement base = doc.documentElement();
171 QDomNodeList effects = base.elementsByTagName("effect");
172 for (int i = 0; i < effects.count(); i++) {
173 QDomElement eff = effects.at(i).toElement();
174 eff.removeAttribute("kdenlive_ix");
176 info.fromString(eff.attribute("kdenlive_info"));
177 // Make sure all effects have the correct new group name
178 info.groupName = name;
179 // Saved effect group should have a group index of -1
180 info.groupIndex = -1;
181 eff.setAttribute("kdenlive_info", info.toString());
185 base.setAttribute("name", name);
186 base.setAttribute("id", name);
187 base.setAttribute("type", "custom");
190 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
191 QTextStream out(&file);
192 out << doc.toString();
195 emit reloadEffects();
198 void CollapsibleGroup::slotResetGroup()
200 QMutexLocker lock(&m_mutex);
201 for (int i = 0; i < m_subWidgets.count(); i++)
202 m_subWidgets.at(i)->slotResetEffect();
205 void CollapsibleGroup::slotSwitch()
207 bool enable = !widgetFrame->isVisible();
211 void CollapsibleGroup::slotShow(bool show)
213 widgetFrame->setVisible(show);
215 collapseButton->setArrowType(Qt::DownArrow);
216 m_info.groupIsCollapsed = false;
219 collapseButton->setArrowType(Qt::RightArrow);
220 m_info.groupIsCollapsed = true;
222 if (!m_subWidgets.isEmpty()) m_subWidgets.at(0)->groupStateChanged(m_info.groupIsCollapsed);
225 QWidget *CollapsibleGroup::title() const
230 void CollapsibleGroup::addGroupEffect(CollapsibleEffect *effect)
232 QMutexLocker lock(&m_mutex);
233 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
235 vbox = new QVBoxLayout();
236 vbox->setContentsMargins(0, 0, 0, 0);
238 widgetFrame->setLayout(vbox);
240 effect->setGroupIndex(groupIndex());
241 effect->setGroupName(m_title->text());
242 effect->decoframe->setObjectName("decoframesub");
243 m_subWidgets.append(effect);
244 vbox->addWidget(effect);
247 QString CollapsibleGroup::infoString() const
249 return m_info.toString();
252 void CollapsibleGroup::removeGroup(int ix, QVBoxLayout *layout)
254 QMutexLocker lock(&m_mutex);
255 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
256 if (vbox == NULL) return;
257 for (int i = m_subWidgets.count() - 1; i >= 0 ; i--) {
258 vbox->removeWidget(m_subWidgets.at(i));
259 layout->insertWidget(ix, m_subWidgets.at(i));
260 m_subWidgets.at(i)->decoframe->setObjectName("decoframe");
261 m_subWidgets.at(i)->removeFromGroup();
263 m_subWidgets.clear();
266 int CollapsibleGroup::groupIndex() const
268 return m_info.groupIndex;
271 bool CollapsibleGroup::isGroup() const
276 void CollapsibleGroup::updateTimecodeFormat()
278 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
279 if (vbox == NULL) return;
280 for (int j = vbox->count() - 1; j >= 0; j--) {
281 CollapsibleEffect *e = static_cast<CollapsibleEffect *>(vbox->itemAt(j)->widget());
282 if (e) e->updateTimecodeFormat();
286 void CollapsibleGroup::dragEnterEvent(QDragEnterEvent *event)
288 if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
289 framegroup->setProperty("target", true);
290 framegroup->setStyleSheet(framegroup->styleSheet());
291 event->acceptProposedAction();
295 void CollapsibleGroup::dragLeaveEvent(QDragLeaveEvent */*event*/)
297 framegroup->setProperty("target", false);
298 framegroup->setStyleSheet(framegroup->styleSheet());
301 void CollapsibleGroup::dropEvent(QDropEvent *event)
303 framegroup->setProperty("target", false);
304 framegroup->setStyleSheet(framegroup->styleSheet());
305 const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
306 //event->acceptProposedAction();
308 doc.setContent(effects, true);
309 QDomElement e = doc.documentElement();
310 int ix = e.attribute("kdenlive_ix").toInt();
311 if (ix == 0 || e.tagName() == "effectgroup") {
312 if (e.tagName() == "effectgroup") {
313 // dropped a group on another group
314 QDomNodeList pastedEffects = e.elementsByTagName("effect");
315 if (pastedEffects.isEmpty() || m_subWidgets.isEmpty()) {
316 // Buggy groups, should not happen
320 QList <int> pastedEffectIndexes;
321 QList <int> currentEffectIndexes;
322 EffectInfo pasteInfo;
323 pasteInfo.fromString(pastedEffects.at(0).toElement().attribute("kdenlive_info"));
324 if (pasteInfo.groupIndex == -1) {
325 // Group dropped from effects list, add effect
326 e.setAttribute("kdenlive_ix", m_subWidgets.last()->effectIndex());
328 event->setDropAction(Qt::CopyAction);
333 for (int i = 0; i < pastedEffects.count(); i++) {
334 pastedEffectIndexes << pastedEffects.at(i).toElement().attribute("kdenlive_ix").toInt();
336 for (int i = 0; i < m_subWidgets.count(); i++) {
337 currentEffectIndexes << m_subWidgets.at(i)->effectIndex();
339 kDebug()<<"PASTING: "<<pastedEffectIndexes<<" TO "<<currentEffectIndexes;
340 if (pastedEffectIndexes.at(0) < currentEffectIndexes.at(0)) {
341 // Pasting group after current one:
342 emit moveEffect(pastedEffectIndexes, currentEffectIndexes.last(), pasteInfo.groupIndex, pasteInfo.groupName);
345 // Group moved before current one
346 emit moveEffect(pastedEffectIndexes, currentEffectIndexes.first(), pasteInfo.groupIndex, pasteInfo.groupName);
348 event->setDropAction(Qt::MoveAction);
352 // effect dropped from effects list, add it
353 e.setAttribute("kdenlive_info", m_info.toString());
354 if (!m_subWidgets.isEmpty()) {
355 e.setAttribute("kdenlive_ix", m_subWidgets.at(0)->effectIndex());
358 event->setDropAction(Qt::CopyAction);
362 if (m_subWidgets.isEmpty()) return;
363 int new_index = m_subWidgets.last()->effectIndex();
364 emit moveEffect(QList <int> () <<ix, new_index, m_info.groupIndex, m_title->text());
365 event->setDropAction(Qt::MoveAction);
369 void CollapsibleGroup::slotRenameGroup()
371 m_title->setReadOnly(true);
372 if (m_title->text().isEmpty()) m_title->setText(i18n("Effect Group"));
373 for (int j = 0; j < m_subWidgets.count(); j++) {
374 m_subWidgets.at(j)->setGroupName(m_title->text());
376 m_info.groupName = m_title->text();
377 emit groupRenamed(this);
380 QList <CollapsibleEffect*> CollapsibleGroup::effects()
382 QMutexLocker lock(&m_mutex);
386 QDomDocument CollapsibleGroup::effectsData()
388 QMutexLocker lock(&m_mutex);
390 QDomElement list = doc.createElement("effectgroup");
391 list.setAttribute("name", m_title->text());
392 doc.appendChild(list);
393 for (int j = 0; j < m_subWidgets.count(); j++) {
394 list.appendChild(doc.importNode(m_subWidgets.at(j)->effect(), true));