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;
41 EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent) :
45 //setSelectionMode(QAbstractItemView::ExtendedSelection);
46 //setDragDropMode(QAbstractItemView::DragDrop);
48 setDropIndicatorShown(true);
49 setAlternatingRowColors(true);
52 setHeaderHidden(true);
56 EffectsListWidget::~EffectsListWidget()
60 void EffectsListWidget::initList()
63 QTreeWidgetItem *item;
64 QTreeWidgetItem *parentItem;
66 QStringList effectInfo;
67 KIcon videoIcon("kdenlive-show-video");
68 KIcon audioIcon("kdenlive-show-audio");
69 KIcon customIcon("kdenlive-custom-effect");
70 KIcon folderIcon("folder");
72 QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
74 QFile file(effectCategory);
75 doc.setContent(&file, false);
77 QList <QTreeWidgetItem *> folders;
78 QDomNodeList groups = doc.documentElement().elementsByTagName("group");
79 for (int i = 0; i < groups.count(); i++) {
80 item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(groups.at(i).firstChild().firstChild().nodeValue()));
81 item->setIcon(0, folderIcon);
82 item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
83 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
87 QTreeWidgetItem *misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
88 misc->setIcon(0, folderIcon);
89 misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
91 QTreeWidgetItem *audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
92 audio->setIcon(0, folderIcon);
93 audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
95 QTreeWidgetItem *custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
96 custom->setIcon(0, folderIcon);
97 custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
99 insertTopLevelItem(0, custom);
100 insertTopLevelItem(0, misc);
101 insertTopLevelItem(0, audio);
102 insertTopLevelItems(0, folders);
105 int ct = MainWindow::videoEffects.count();
106 for (int ix = 0; ix < ct; ix ++) {
107 effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
109 for (int i = 0; i < folders.count(); i++) {
110 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
111 if (l.contains(effectInfo.at(2))) {
112 parentItem = folders.at(i);
116 if (parentItem == NULL) parentItem = misc;
117 if (!effectInfo.isEmpty()) {
118 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
119 item->setIcon(0, videoIcon);
120 item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
121 item->setData(0, IdRole, effectInfo);
125 ct = MainWindow::audioEffects.count();
126 for (int ix = 0; ix < ct; ix ++) {
127 effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
129 for (int i = 0; i < folders.count(); i++) {
130 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
131 if (l.contains(effectInfo.at(2))) {
132 parentItem = folders.at(i);
136 if (parentItem == NULL) parentItem = audio;
137 if (!effectInfo.isEmpty()) {
138 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
139 item->setIcon(0, audioIcon);
140 item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
141 item->setData(0, IdRole, effectInfo);
145 ct = MainWindow::customEffects.count();
146 for (int ix = 0; ix < ct; ix ++) {
147 effectInfo = MainWindow::customEffects.effectIdInfo(ix);
148 if (!effectInfo.isEmpty()) {
149 item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
150 item->setIcon(0, customIcon);
151 item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
152 item->setData(0, IdRole, effectInfo);
155 setSortingEnabled(true);
156 sortByColumn(0, Qt::AscendingOrder);
159 const QDomElement EffectsListWidget::currentEffect() const
161 return itemEffect(currentItem());
164 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
167 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
168 QStringList effectInfo = item->data(0, IdRole).toStringList();
169 kDebug() << "// EFFECT SELECTED: " << effectInfo;
170 switch (item->data(0, TypeRole).toInt()) {
172 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
175 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
178 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
185 QString EffectsListWidget::currentInfo()
187 QTreeWidgetItem *item = currentItem();
188 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
190 QStringList effectInfo = item->data(0, IdRole).toStringList();
191 switch (item->data(0, TypeRole).toInt()) {
193 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
196 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
199 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
206 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
208 if (event->button() == Qt::LeftButton) {
209 m_DragStartPosition = event->pos();
210 m_dragStarted = true;
212 QTreeWidget::mousePressEvent(event);
216 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
218 if (!m_dragStarted) return;
219 if ((event->pos() - m_DragStartPosition).manhattanLength()
220 < QApplication::startDragDistance())
224 QTreeWidgetItem *clickItem = itemAt(event->pos());
225 if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) {
226 QDrag *drag = new QDrag(this);
227 QMimeData *mimeData = new QMimeData;
228 const QList <QTreeWidgetItem *>list = selectedItems();
230 foreach(QTreeWidgetItem *item, list) {
231 const QDomElement e = itemEffect(item);
232 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
235 data.append(doc.toString().toUtf8());
236 mimeData->setData("kdenlive/effectslist", data);
237 drag->setMimeData(mimeData);
238 //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
239 //drag->setPixmap(pix);
240 //drag->setHotSpot(QPoint(0, 50));
241 drag->start(Qt::CopyAction);
247 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
249 event->setDropAction(Qt::CopyAction);
250 if (event->mimeData()->hasText()) {
251 event->acceptProposedAction();
257 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
259 QTreeWidgetItem *item = itemAt(event->pos());
260 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
263 #include "effectslistwidget.moc"