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 QTreeWidgetItem *item;
77 QTreeWidgetItem *parentItem;
79 QStringList effectInfo;
80 KIcon videoIcon("kdenlive-show-video");
81 KIcon audioIcon("kdenlive-show-audio");
82 KIcon customIcon("kdenlive-custom-effect");
83 KIcon folderIcon("folder");
85 QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
87 QFile file(effectCategory);
88 doc.setContent(&file, false);
90 QList <QTreeWidgetItem *> folders;
91 QDomNodeList groups = doc.documentElement().elementsByTagName("group");
92 for (int i = 0; i < groups.count(); i++) {
93 item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(groups.at(i).firstChild().firstChild().nodeValue()));
94 item->setIcon(0, folderIcon);
95 item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
96 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
100 QTreeWidgetItem *misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
101 misc->setIcon(0, folderIcon);
102 misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
104 QTreeWidgetItem *audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
105 audio->setIcon(0, folderIcon);
106 audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
108 QTreeWidgetItem *custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
109 custom->setIcon(0, folderIcon);
110 custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
112 insertTopLevelItem(0, custom);
113 insertTopLevelItem(0, misc);
114 insertTopLevelItem(0, audio);
115 insertTopLevelItems(0, folders);
118 int ct = MainWindow::videoEffects.count();
119 for (int ix = 0; ix < ct; ix ++) {
120 effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
122 for (int i = 0; i < folders.count(); i++) {
123 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
124 if (l.contains(effectInfo.at(2))) {
125 parentItem = folders.at(i);
129 if (parentItem == NULL) parentItem = misc;
130 if (!effectInfo.isEmpty()) {
131 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
132 item->setIcon(0, videoIcon);
133 item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
134 item->setData(0, IdRole, effectInfo);
138 ct = MainWindow::audioEffects.count();
139 for (int ix = 0; ix < ct; ix ++) {
140 effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
142 for (int i = 0; i < folders.count(); i++) {
143 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
144 if (l.contains(effectInfo.at(2))) {
145 parentItem = folders.at(i);
149 if (parentItem == NULL) parentItem = audio;
150 if (!effectInfo.isEmpty()) {
151 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
152 item->setIcon(0, audioIcon);
153 item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
154 item->setData(0, IdRole, effectInfo);
158 ct = MainWindow::customEffects.count();
159 for (int ix = 0; ix < ct; ix ++) {
160 effectInfo = MainWindow::customEffects.effectIdInfo(ix);
161 if (!effectInfo.isEmpty()) {
162 item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
163 item->setIcon(0, customIcon);
164 item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
165 item->setData(0, IdRole, effectInfo);
168 setSortingEnabled(true);
169 sortByColumn(0, Qt::AscendingOrder);
172 const QDomElement EffectsListWidget::currentEffect() const
174 return itemEffect(currentItem());
177 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
180 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
181 QStringList effectInfo = item->data(0, IdRole).toStringList();
182 kDebug() << "// EFFECT SELECTED: " << effectInfo;
183 switch (item->data(0, TypeRole).toInt()) {
185 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
188 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
191 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
198 QString EffectsListWidget::currentInfo()
200 QTreeWidgetItem *item = currentItem();
201 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
203 QStringList effectInfo = item->data(0, IdRole).toStringList();
204 switch (item->data(0, TypeRole).toInt()) {
206 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
209 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
212 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
219 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
221 if (event->button() == Qt::LeftButton) {
222 m_DragStartPosition = event->pos();
223 m_dragStarted = true;
225 QTreeWidget::mousePressEvent(event);
229 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
231 if (!m_dragStarted) return;
232 if ((event->pos() - m_DragStartPosition).manhattanLength()
233 < QApplication::startDragDistance())
237 QTreeWidgetItem *clickItem = itemAt(event->pos());
238 if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) {
239 QDrag *drag = new QDrag(this);
240 QMimeData *mimeData = new QMimeData;
241 const QList <QTreeWidgetItem *>list = selectedItems();
243 foreach(QTreeWidgetItem *item, list) {
244 const QDomElement e = itemEffect(item);
245 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
248 data.append(doc.toString().toUtf8());
249 mimeData->setData("kdenlive/effectslist", data);
250 drag->setMimeData(mimeData);
251 //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
252 //drag->setPixmap(pix);
253 //drag->setHotSpot(QPoint(0, 50));
254 drag->start(Qt::CopyAction);
260 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
262 event->setDropAction(Qt::CopyAction);
263 if (event->mimeData()->hasText()) {
264 event->acceptProposedAction();
270 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
272 QTreeWidgetItem *item = itemAt(event->pos());
273 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
276 #include "effectslistwidget.moc"