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>
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(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"));
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 effect"), this, SLOT(slotResetEffect()));
79 m_menu->addAction(KIcon("document-save"), i18n("Save effect"), this, SLOT(slotSaveEffect()));
81 effecticon->setPixmap(KIcon("folder").pixmap(16,16));
82 m_menu->addAction(KIcon("list-remove"), i18n("Ungroup"), this, SLOT(slotUnGroup()));
84 menuButton->setIcon(KIcon("kdenlive-menu"));
85 menuButton->setMenu(m_menu);
87 enabledBox->setChecked(true);
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()));
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 enable)
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);
139 void CollapsibleGroup::slotDeleteEffect()
141 emit deleteGroup(groupIndex());
144 void CollapsibleGroup::slotEffectUp()
146 emit changeGroupPosition(groupIndex(), true);
149 void CollapsibleGroup::slotEffectDown()
151 emit changeGroupPosition(groupIndex(), false);
154 void CollapsibleGroup::slotSaveEffect()
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;
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");
181 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
182 QTextStream out(&file);
183 out << doc.toString();
186 emit reloadEffects();*/
189 void CollapsibleGroup::slotResetEffect()
191 //TODO: emit resetEffect(effectIndex());
194 void CollapsibleGroup::slotSwitch()
196 bool enable = !widgetFrame->isVisible();
200 void CollapsibleGroup::slotShow(bool show)
202 widgetFrame->setVisible(show);
204 collapseButton->setArrowType(Qt::DownArrow);
205 m_info.isCollapsed = false;
208 collapseButton->setArrowType(Qt::RightArrow);
209 m_info.isCollapsed = true;
211 //emit parameterChanged(m_original_effect, m_effect, effectIndex());
214 QWidget *CollapsibleGroup::title() const
219 void CollapsibleGroup::addGroupEffect(CollapsibleEffect *effect)
221 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
223 vbox = new QVBoxLayout();
224 vbox->setContentsMargins(0, 0, 0, 0);
226 widgetFrame->setLayout(vbox);
228 effect->setGroupIndex(groupIndex());
229 effect->setGroupName(m_title->text());
230 m_subWidgets.append(effect);
231 vbox->addWidget(effect);
234 QString CollapsibleGroup::infoString() const
236 return m_info.toString();
239 void CollapsibleGroup::removeGroup(int ix, QVBoxLayout *layout)
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();
248 m_subWidgets.clear();
251 int CollapsibleGroup::groupIndex() const
256 bool CollapsibleGroup::isGroup() const
261 void CollapsibleGroup::updateTimecodeFormat()
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();
271 void CollapsibleGroup::dragEnterEvent(QDragEnterEvent *event)
273 if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
274 framegroup->setProperty("active", true);
275 framegroup->setStyleSheet(framegroup->styleSheet());
276 event->acceptProposedAction();
280 void CollapsibleGroup::dragLeaveEvent(QDragLeaveEvent */*event*/)
282 framegroup->setProperty("active", false);
283 framegroup->setStyleSheet(framegroup->styleSheet());
286 void CollapsibleGroup::dropEvent(QDropEvent *event)
288 framegroup->setProperty("active", false);
289 framegroup->setStyleSheet(framegroup->styleSheet());
290 const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
291 //event->acceptProposedAction();
293 doc.setContent(effects, true);
294 QDomElement e = doc.documentElement();
295 int ix = e.attribute("kdenlive_ix").toInt();
297 // effect dropped from effects list, add it
298 e.setAttribute("kdenlive_ix", ix);
299 event->setDropAction(Qt::CopyAction);
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);
311 void CollapsibleGroup::slotRenameGroup()
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());
318 emit groupRenamed(this);
321 QList <CollapsibleEffect*> CollapsibleGroup::effects()
326 QDomDocument CollapsibleGroup::effectsData()
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));