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):
47 void MyEditableLabel::mouseDoubleClickEvent ( QMouseEvent * e )
54 CollapsibleGroup::CollapsibleGroup(int ix, bool firstGroup, bool lastGroup, QString groupName, QWidget * parent) :
55 AbstractCollapsibleWidget(parent),
59 setFont(KGlobalSettings::smallestReadableFont());
60 QHBoxLayout *l = static_cast <QHBoxLayout *>(frame->layout());
61 m_title = new MyEditableLabel(this);
62 l->insertWidget(4, m_title);
63 m_title->setText(groupName.isEmpty() ? i18n("Effect Group") : groupName);
64 connect(m_title, SIGNAL(editingFinished()), this, SLOT(slotRenameGroup()));
65 buttonUp->setIcon(KIcon("kdenlive-up"));
66 buttonUp->setToolTip(i18n("Move effect up"));
67 buttonDown->setIcon(KIcon("kdenlive-down"));
68 buttonDown->setToolTip(i18n("Move effect down"));
70 buttonDel->setIcon(KIcon("kdenlive-deleffect"));
71 buttonDel->setToolTip(i18n("Delete effect"));
72 if (firstGroup) buttonUp->setVisible(false);
73 if (lastGroup) buttonDown->setVisible(false);
75 m_menu->addAction(KIcon("view-refresh"), i18n("Reset effect"), this, SLOT(slotResetEffect()));
76 m_menu->addAction(KIcon("document-save"), i18n("Save effect"), this, SLOT(slotSaveEffect()));
78 effecticon->setPixmap(KIcon("folder").pixmap(16,16));
79 m_menu->addAction(KIcon("list-remove"), i18n("Ungroup"), this, SLOT(slotUnGroup()));
81 menuButton->setIcon(KIcon("kdenlive-menu"));
82 menuButton->setMenu(m_menu);
84 enabledBox->setChecked(true);
86 connect(collapseButton, SIGNAL(clicked()), this, SLOT(slotSwitch()));
87 connect(enabledBox, SIGNAL(toggled(bool)), this, SLOT(slotEnable(bool)));
88 connect(buttonUp, SIGNAL(clicked()), this, SLOT(slotEffectUp()));
89 connect(buttonDown, SIGNAL(clicked()), this, SLOT(slotEffectDown()));
90 connect(buttonDel, SIGNAL(clicked()), this, SLOT(slotDeleteEffect()));
94 CollapsibleGroup::~CollapsibleGroup()
99 void CollapsibleGroup::slotUnGroup()
104 bool CollapsibleGroup::isActive() const
106 return decoframegroup->property("active").toBool();
109 void CollapsibleGroup::setActive(bool activate)
111 decoframegroup->setProperty("active", activate);
112 decoframegroup->setStyleSheet(decoframegroup->styleSheet());
115 void CollapsibleGroup::mouseDoubleClickEvent ( QMouseEvent * event )
117 if (frame->underMouse() && collapseButton->isEnabled()) slotSwitch();
118 QWidget::mouseDoubleClickEvent(event);
122 void CollapsibleGroup::slotEnable(bool enable)
124 m_title->setEnabled(enable);
125 enabledBox->blockSignals(true);
126 enabledBox->setChecked(enable);
127 enabledBox->blockSignals(false);
128 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
129 if (vbox == NULL) return;
130 for (int i = 0; i < vbox->count(); i++) {
131 CollapsibleGroup *e = static_cast<CollapsibleGroup *>(vbox->itemAt(i)->widget());
132 if (e) e->enabledBox->setChecked(enable);// slotEnable(enable);
136 void CollapsibleGroup::slotDeleteEffect()
138 emit deleteGroup(groupIndex());
141 void CollapsibleGroup::slotEffectUp()
143 emit changeGroupPosition(groupIndex(), true);
146 void CollapsibleGroup::slotEffectDown()
148 emit changeGroupPosition(groupIndex(), false);
151 void CollapsibleGroup::slotSaveEffect()
153 QString name = QInputDialog::getText(this, i18n("Save Effect"), i18n("Name for saved effect: "));
154 if (name.isEmpty()) return;
155 QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
156 path = path + name + ".xml";
157 if (QFile::exists(path)) if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", path)) == KMessageBox::No) return;
161 QDomElement effect = m_effect.cloneNode().toElement();
162 doc.appendChild(doc.importNode(effect, true));
163 effect = doc.firstChild().toElement();
164 effect.removeAttribute("kdenlive_ix");
165 effect.setAttribute("id", name);
166 effect.setAttribute("type", "custom");
167 QDomElement effectname = effect.firstChildElement("name");
168 effect.removeChild(effectname);
169 effectname = doc.createElement("name");
170 QDomText nametext = doc.createTextNode(name);
171 effectname.appendChild(nametext);
172 effect.insertBefore(effectname, QDomNode());
173 QDomElement effectprops = effect.firstChildElement("properties");
174 effectprops.setAttribute("id", name);
175 effectprops.setAttribute("type", "custom");
178 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
179 QTextStream out(&file);
180 out << doc.toString();
183 emit reloadEffects();*/
186 void CollapsibleGroup::slotResetEffect()
188 //TODO: emit resetEffect(effectIndex());
191 void CollapsibleGroup::slotSwitch()
193 bool enable = !widgetFrame->isVisible();
197 void CollapsibleGroup::slotShow(bool show)
199 widgetFrame->setVisible(show);
201 collapseButton->setArrowType(Qt::DownArrow);
202 m_info.isCollapsed = false;
205 collapseButton->setArrowType(Qt::RightArrow);
206 m_info.isCollapsed = true;
208 //emit parameterChanged(m_original_effect, m_effect, effectIndex());
211 void CollapsibleGroup::updateGroupIndex(int groupIndex)
214 m_info.groupIndex = groupIndex;
215 m_effect.setAttribute("kdenlive_info", m_info.toString());
216 emit parameterChanged(m_original_effect, m_effect, effectIndex());*/
219 void CollapsibleGroup::addGroupEffect(CollapsibleEffect *effect)
221 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
223 vbox = new QVBoxLayout();
224 vbox->setContentsMargins(10, 0, 0, 0);
226 widgetFrame->setLayout(vbox);
228 effect->setGroupIndex(groupIndex());
229 vbox->addWidget(effect);
232 QString CollapsibleGroup::infoString() const
234 return m_info.toString();
237 void CollapsibleGroup::removeGroup(int ix, QVBoxLayout *layout)
239 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
240 if (vbox == NULL) return;
242 for (int j = vbox->count() - 1; j >= 0; j--) {
243 QLayoutItem *child = vbox->takeAt(j);
244 CollapsibleGroup *e = static_cast<CollapsibleGroup *>(child->widget());
245 layout->insertWidget(ix, e);
246 e->updateGroupIndex(-1);
251 int CollapsibleGroup::groupIndex() const
258 bool CollapsibleGroup::isGroup() const
263 void CollapsibleGroup::updateTimecodeFormat()
265 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
266 if (vbox == NULL) return;
267 for (int j = vbox->count() - 1; j >= 0; j--) {
268 CollapsibleEffect *e = static_cast<CollapsibleEffect *>(vbox->itemAt(j)->widget());
269 if (e) e->updateTimecodeFormat();
273 void CollapsibleGroup::dragEnterEvent(QDragEnterEvent *event)
275 if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
276 frame->setProperty("active", true);
277 frame->setStyleSheet(frame->styleSheet());
278 event->acceptProposedAction();
282 void CollapsibleGroup::dragLeaveEvent(QDragLeaveEvent */*event*/)
284 frame->setProperty("active", false);
285 frame->setStyleSheet(frame->styleSheet());
288 void CollapsibleGroup::dropEvent(QDropEvent *event)
290 frame->setProperty("active", false);
291 frame->setStyleSheet(frame->styleSheet());
292 const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
293 //event->acceptProposedAction();
295 doc.setContent(effects, true);
296 QDomElement e = doc.documentElement();
297 int ix = e.attribute("kdenlive_ix").toInt();
299 // effect dropped from effects list, add it
300 e.setAttribute("kdenlive_ix", ix);
301 event->setDropAction(Qt::CopyAction);
307 QVBoxLayout *vbox = static_cast<QVBoxLayout *>(widgetFrame->layout());
308 if (vbox == NULL) return;
309 CollapsibleEffect *effect = static_cast<CollapsibleEffect *>(vbox->itemAt(vbox->count() -1)->widget());
310 new_index = effect->effectIndex();
311 emit moveEffect(ix, new_index, m_index);
312 event->setDropAction(Qt::MoveAction);
316 void CollapsibleGroup::slotRenameGroup()
318 m_title->setReadOnly(true);
319 if (m_title->text().isEmpty()) m_title->setText(i18n("Effect Group"));
320 QList <CollapsibleEffect*> effects = findChildren<CollapsibleEffect*>();
321 for (int j = 0; j < effects.count(); j++) {
322 effects.at(j)->setGroupName(m_title->text());
324 emit groupRenamed(this);
327 QList <CollapsibleEffect*> CollapsibleGroup::effects()
329 QList <CollapsibleEffect*> result = findChildren<CollapsibleEffect*>();