1 /***************************************************************************
2 * Copyright (C) 2007 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 "effectslistwidget.h"
22 #include "effectslist.h"
23 #include "mainwindow.h"
26 #include <KStandardDirs>
28 #include <QApplication>
29 #include <QMouseEvent>
33 static const int EFFECT_VIDEO = 1;
34 static const int EFFECT_AUDIO = 2;
35 static const int EFFECT_CUSTOM = 3;
36 static const int EFFECT_FOLDER = 4;
38 const int TypeRole = Qt::UserRole;
39 const int IdRole = TypeRole + 1;
42 EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent) :
46 //setSelectionMode(QAbstractItemView::ExtendedSelection);
47 //setDragDropMode(QAbstractItemView::DragDrop);
49 setDropIndicatorShown(true);
50 //setAlternatingRowColors(true);
53 setHeaderHidden(true);
54 setFrameShape(QFrame::NoFrame);
55 setAutoFillBackground(false);
56 setRootIsDecorated(false);
57 QPalette p = palette();
58 p.setBrush(QPalette::Base, Qt::NoBrush);
61 connect(this, SIGNAL(activated(const QModelIndex &)), this, SLOT(slotExpandItem(const QModelIndex &)));
64 EffectsListWidget::~EffectsListWidget()
68 void EffectsListWidget::slotExpandItem(const QModelIndex & index)
70 setExpanded(index, !isExpanded(index));
73 void EffectsListWidget::initList()
76 QString currentFolder;
77 QTreeWidgetItem *item = NULL;
78 QTreeWidgetItem *parentItem;
82 current = currentItem()->text(0);
83 if (currentItem()->parent()) currentFolder = currentItem()->parent()->text(0);
84 else if (currentItem()->data(0, TypeRole) == EFFECT_FOLDER) currentFolder = currentItem()->text(0);
88 QStringList effectInfo;
89 KIcon videoIcon("kdenlive-show-video");
90 KIcon audioIcon("kdenlive-show-audio");
91 KIcon customIcon("kdenlive-custom-effect");
92 KIcon folderIcon("folder");
94 QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
96 QFile file(effectCategory);
97 doc.setContent(&file, false);
99 QList <QTreeWidgetItem *> folders;
100 QStringList folderNames;
101 QDomNodeList groups = doc.documentElement().elementsByTagName("group");
102 for (int i = 0; i < groups.count(); i++) {
103 folderNames << groups.at(i).firstChild().firstChild().nodeValue();
105 for (int i = 0; i < topLevelItemCount(); i++) {
106 topLevelItem(i)->takeChildren();
107 QString currentName = topLevelItem(i)->text(0);
108 if (currentName != i18n("Misc") && currentName != i18n("Audio") && currentName != i18n("Custom") && !folderNames.contains(currentName)) {
114 for (int i = 0; i < groups.count(); i++) {
115 item = findFolder(folderNames.at(i));
117 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
119 item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(folderNames.at(i)));
120 item->setIcon(0, folderIcon);
121 item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
122 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
123 insertTopLevelItem(0, item);
125 folders.append(item);
128 QTreeWidgetItem *misc = findFolder(i18n("Misc"));
130 misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
131 misc->setIcon(0, folderIcon);
132 misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
133 insertTopLevelItem(0, misc);
136 QTreeWidgetItem *audio = findFolder(i18n("Audio"));
138 audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
139 audio->setIcon(0, folderIcon);
140 audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
141 insertTopLevelItem(0, audio);
144 QTreeWidgetItem *custom = findFolder(i18n("Custom"));
145 if (custom == NULL) {
146 custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
147 custom->setIcon(0, folderIcon);
148 custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
149 insertTopLevelItem(0, custom);
152 //insertTopLevelItems(0, folders);
155 int ct = MainWindow::videoEffects.count();
156 for (int ix = 0; ix < ct; ix ++) {
157 effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
159 for (int i = 0; i < folders.count(); i++) {
160 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
161 if (l.contains(effectInfo.at(2))) {
162 parentItem = folders.at(i);
166 if (parentItem == NULL) parentItem = misc;
167 if (!effectInfo.isEmpty()) {
168 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
169 item->setIcon(0, videoIcon);
170 item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
171 item->setData(0, IdRole, effectInfo);
172 if (item->text(0) == current) {
173 setCurrentItem(item);
179 ct = MainWindow::audioEffects.count();
180 for (int ix = 0; ix < ct; ix ++) {
181 effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
183 for (int i = 0; i < folders.count(); i++) {
184 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
185 if (l.contains(effectInfo.at(2))) {
186 parentItem = folders.at(i);
190 if (parentItem == NULL) parentItem = audio;
191 if (!effectInfo.isEmpty()) {
192 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
193 item->setIcon(0, audioIcon);
194 item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
195 item->setData(0, IdRole, effectInfo);
196 if (item->text(0) == current) {
197 setCurrentItem(item);
203 ct = MainWindow::customEffects.count();
204 kDebug() << "--- REBUILDING;: " << ct;
205 for (int ix = 0; ix < ct; ix ++) {
206 effectInfo = MainWindow::customEffects.effectIdInfo(ix);
207 if (!effectInfo.isEmpty()) {
208 item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
209 item->setIcon(0, customIcon);
210 item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
211 item->setData(0, IdRole, effectInfo);
212 if (item->text(0) == current) {
213 setCurrentItem(item);
218 if (!found && !currentFolder.isEmpty()) {
219 // previously selected effect was removed, focus on its parent folder
220 for (int i = 0; i < topLevelItemCount(); i++) {
221 if (topLevelItem(i)->text(0) == currentFolder) {
222 setCurrentItem(topLevelItem(i));
228 setSortingEnabled(true);
229 sortByColumn(0, Qt::AscendingOrder);
232 QTreeWidgetItem *EffectsListWidget::findFolder(const QString name)
234 QTreeWidgetItem *item = NULL;
235 QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
236 if (!result.isEmpty()) {
237 for (int j = 0; j < result.count(); j++) {
238 if (result.at(j)->data(0, TypeRole) == EFFECT_FOLDER) {
247 const QDomElement EffectsListWidget::currentEffect() const
249 return itemEffect(currentItem());
252 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
255 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
256 QStringList effectInfo = item->data(0, IdRole).toStringList();
257 kDebug() << "// EFFECT SELECTED: " << effectInfo;
258 switch (item->data(0, TypeRole).toInt()) {
260 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
263 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
266 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
273 QString EffectsListWidget::currentInfo()
275 QTreeWidgetItem *item = currentItem();
276 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
278 QStringList effectInfo = item->data(0, IdRole).toStringList();
279 switch (item->data(0, TypeRole).toInt()) {
281 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
284 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
287 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
294 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
296 if (event->button() == Qt::LeftButton) {
297 m_DragStartPosition = event->pos();
298 m_dragStarted = true;
300 QTreeWidget::mousePressEvent(event);
304 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
306 if (!m_dragStarted) return;
307 if ((event->pos() - m_DragStartPosition).manhattanLength()
308 < QApplication::startDragDistance())
312 QTreeWidgetItem *clickItem = itemAt(event->pos());
313 if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) {
314 QDrag *drag = new QDrag(this);
315 QMimeData *mimeData = new QMimeData;
316 const QList <QTreeWidgetItem *>list = selectedItems();
318 foreach(QTreeWidgetItem *item, list) {
319 const QDomElement e = itemEffect(item);
320 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
323 data.append(doc.toString().toUtf8());
324 mimeData->setData("kdenlive/effectslist", data);
325 drag->setMimeData(mimeData);
326 //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
327 //drag->setPixmap(pix);
328 //drag->setHotSpot(QPoint(0, 50));
329 drag->start(Qt::CopyAction);
335 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
337 event->setDropAction(Qt::CopyAction);
338 if (event->mimeData()->hasText()) {
339 event->acceptProposedAction();
345 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
347 QTreeWidgetItem *item = itemAt(event->pos());
348 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
351 #include "effectslistwidget.moc"