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, QString groupName, QWidget * parent) :
57 AbstractCollapsibleWidget(parent),
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"));
73 buttonDel->setIcon(KIcon("kdenlive-deleffect"));
74 buttonDel->setToolTip(i18n("Delete effect"));
75 if (firstGroup) buttonUp->setVisible(false);
76 if (lastGroup) buttonDown->setVisible(false);
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()));
81 m_menu->addAction(KIcon("list-remove"), i18n("Ungroup"), this, SLOT(slotUnGroup()));
83 menuButton->setIcon(KIcon("kdenlive-menu"));
84 menuButton->setMenu(m_menu);
86 enabledButton->setChecked(false);
87 enabledButton->setIcon(KIcon("visible"));
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()));
97 CollapsibleGroup::~CollapsibleGroup()
102 void CollapsibleGroup::slotUnGroup()
107 bool CollapsibleGroup::isActive() const
109 return decoframegroup->property("active").toBool();
112 void CollapsibleGroup::setActive(bool activate)
114 decoframegroup->setProperty("active", activate);
115 decoframegroup->setStyleSheet(decoframegroup->styleSheet());
118 void CollapsibleGroup::mouseDoubleClickEvent ( QMouseEvent * event )
120 if (framegroup->underMouse() && collapseButton->isEnabled()) slotSwitch();
121 QWidget::mouseDoubleClickEvent(event);
125 void CollapsibleGroup::slotEnable(bool disable)
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);
136 void CollapsibleGroup::slotDeleteGroup()
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);
146 void CollapsibleGroup::slotEffectUp()
148 emit changeGroupPosition(groupIndex(), true);
151 void CollapsibleGroup::slotEffectDown()
153 emit changeGroupPosition(groupIndex(), false);
156 void CollapsibleGroup::slotSaveGroup()
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;
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");
171 info.fromString(eff.attribute("kdenlive_info"));
172 // Make sure all effects have the correct new group name
173 info.groupName = name;
174 // Saved effect group should have a group index of -1
175 info.groupIndex = -1;
176 eff.setAttribute("kdenlive_info", info.toString());
180 base.setAttribute("name", name);
181 base.setAttribute("id", name);
182 base.setAttribute("type", "custom");
185 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
186 QTextStream out(&file);
187 out << doc.toString();
190 emit reloadEffects();
193 void CollapsibleGroup::slotResetGroup()
195 QMutexLocker lock(&m_mutex);
196 for (int i = 0; i < m_subWidgets.count(); i++)
197 m_subWidgets.at(i)->slotResetEffect();
200 void CollapsibleGroup::slotSwitch()
202 bool enable = !widgetFrame->isVisible();
206 void CollapsibleGroup::slotShow(bool show)
208 widgetFrame->setVisible(show);
210 collapseButton->setArrowType(Qt::DownArrow);
211 m_info.isCollapsed = false;
214 collapseButton->setArrowType(Qt::RightArrow);
215 m_info.isCollapsed = true;
217 //emit parameterChanged(m_original_effect, m_effect, effectIndex());
220 QWidget *CollapsibleGroup::title() const
225 void CollapsibleGroup::addGroupEffect(CollapsibleEffect *effect)
227 QMutexLocker lock(&m_mutex);
228 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
230 vbox = new QVBoxLayout();
231 vbox->setContentsMargins(0, 0, 0, 0);
233 widgetFrame->setLayout(vbox);
235 effect->setGroupIndex(groupIndex());
236 effect->setGroupName(m_title->text());
237 effect->decoframe->setObjectName("decoframesub");
238 m_subWidgets.append(effect);
239 vbox->addWidget(effect);
242 QString CollapsibleGroup::infoString() const
244 return m_info.toString();
247 void CollapsibleGroup::removeGroup(int ix, QVBoxLayout *layout)
249 QMutexLocker lock(&m_mutex);
250 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
251 if (vbox == NULL) return;
252 for (int i = m_subWidgets.count() - 1; i >= 0 ; i--) {
253 vbox->removeWidget(m_subWidgets.at(i));
254 layout->insertWidget(ix, m_subWidgets.at(i));
255 m_subWidgets.at(i)->decoframe->setObjectName("decoframe");
256 m_subWidgets.at(i)->removeFromGroup();
258 m_subWidgets.clear();
261 int CollapsibleGroup::groupIndex() const
266 bool CollapsibleGroup::isGroup() const
271 void CollapsibleGroup::updateTimecodeFormat()
273 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
274 if (vbox == NULL) return;
275 for (int j = vbox->count() - 1; j >= 0; j--) {
276 CollapsibleEffect *e = static_cast<CollapsibleEffect *>(vbox->itemAt(j)->widget());
277 if (e) e->updateTimecodeFormat();
281 void CollapsibleGroup::dragEnterEvent(QDragEnterEvent *event)
283 if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
284 framegroup->setProperty("target", true);
285 framegroup->setStyleSheet(framegroup->styleSheet());
286 event->acceptProposedAction();
290 void CollapsibleGroup::dragLeaveEvent(QDragLeaveEvent */*event*/)
292 framegroup->setProperty("target", false);
293 framegroup->setStyleSheet(framegroup->styleSheet());
296 void CollapsibleGroup::dropEvent(QDropEvent *event)
298 framegroup->setProperty("target", false);
299 framegroup->setStyleSheet(framegroup->styleSheet());
300 const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
301 //event->acceptProposedAction();
303 doc.setContent(effects, true);
304 QDomElement e = doc.documentElement();
305 int ix = e.attribute("kdenlive_ix").toInt();
307 // effect dropped from effects list, add it
308 e.setAttribute("kdenlive_ix", ix);
309 event->setDropAction(Qt::CopyAction);
314 if (m_subWidgets.isEmpty()) return;
315 int new_index = m_subWidgets.last()->effectIndex();
316 emit moveEffect(ix, new_index, m_index, m_title->text());
317 event->setDropAction(Qt::MoveAction);
321 void CollapsibleGroup::slotRenameGroup()
323 m_title->setReadOnly(true);
324 if (m_title->text().isEmpty()) m_title->setText(i18n("Effect Group"));
325 for (int j = 0; j < m_subWidgets.count(); j++) {
326 m_subWidgets.at(j)->setGroupName(m_title->text());
328 emit groupRenamed(this);
331 QList <CollapsibleEffect*> CollapsibleGroup::effects()
333 QMutexLocker lock(&m_mutex);
337 QDomDocument CollapsibleGroup::effectsData()
339 QMutexLocker lock(&m_mutex);
341 QDomElement list = doc.createElement("effectgroup");
342 list.setAttribute("name", m_title->text());
343 doc.appendChild(list);
344 for (int j = 0; j < m_subWidgets.count(); j++) {
345 list.appendChild(doc.importNode(m_subWidgets.at(j)->effect(), true));