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;
78 QTreeWidgetItem *parentItem;
82 current = currentItem()->text(0);
83 if (currentItem()->parent()) currentFolder = currentItem()->parent()->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 QDomNodeList groups = doc.documentElement().elementsByTagName("group");
101 for (int i = 0; i < groups.count(); i++) {
102 item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(groups.at(i).firstChild().firstChild().nodeValue()));
103 item->setIcon(0, folderIcon);
104 item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
105 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
106 folders.append(item);
109 QTreeWidgetItem *misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
110 misc->setIcon(0, folderIcon);
111 misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
113 QTreeWidgetItem *audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
114 audio->setIcon(0, folderIcon);
115 audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
117 QTreeWidgetItem *custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
118 custom->setIcon(0, folderIcon);
119 custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
121 insertTopLevelItem(0, custom);
122 insertTopLevelItem(0, misc);
123 insertTopLevelItem(0, audio);
124 insertTopLevelItems(0, folders);
127 int ct = MainWindow::videoEffects.count();
128 for (int ix = 0; ix < ct; ix ++) {
129 effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
131 for (int i = 0; i < folders.count(); i++) {
132 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
133 if (l.contains(effectInfo.at(2))) {
134 parentItem = folders.at(i);
138 if (parentItem == NULL) parentItem = misc;
139 if (!effectInfo.isEmpty()) {
140 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
141 item->setIcon(0, videoIcon);
142 item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
143 item->setData(0, IdRole, effectInfo);
144 if (item->text(0) == current) {
145 setCurrentItem(item);
151 ct = MainWindow::audioEffects.count();
152 for (int ix = 0; ix < ct; ix ++) {
153 effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
155 for (int i = 0; i < folders.count(); i++) {
156 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
157 if (l.contains(effectInfo.at(2))) {
158 parentItem = folders.at(i);
162 if (parentItem == NULL) parentItem = audio;
163 if (!effectInfo.isEmpty()) {
164 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
165 item->setIcon(0, audioIcon);
166 item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
167 item->setData(0, IdRole, effectInfo);
168 if (item->text(0) == current) {
169 setCurrentItem(item);
175 ct = MainWindow::customEffects.count();
176 kDebug() << "--- REBUILDING;: " << ct;
177 for (int ix = 0; ix < ct; ix ++) {
178 effectInfo = MainWindow::customEffects.effectIdInfo(ix);
179 if (!effectInfo.isEmpty()) {
180 item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
181 item->setIcon(0, customIcon);
182 item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
183 item->setData(0, IdRole, effectInfo);
184 if (item->text(0) == current) {
185 setCurrentItem(item);
190 if (!found && !currentFolder.isEmpty()) {
191 // previously selected effect was removed, focus on its parent folder
192 for (int i = 0; i < topLevelItemCount(); i++) {
193 if (topLevelItem(i)->text(0) == currentFolder) {
194 setCurrentItem(topLevelItem(i));
195 topLevelItem(i)->setExpanded(true);
201 setSortingEnabled(true);
202 sortByColumn(0, Qt::AscendingOrder);
205 const QDomElement EffectsListWidget::currentEffect() const
207 return itemEffect(currentItem());
210 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
213 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
214 QStringList effectInfo = item->data(0, IdRole).toStringList();
215 kDebug() << "// EFFECT SELECTED: " << effectInfo;
216 switch (item->data(0, TypeRole).toInt()) {
218 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
221 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
224 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
231 QString EffectsListWidget::currentInfo()
233 QTreeWidgetItem *item = currentItem();
234 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
236 QStringList effectInfo = item->data(0, IdRole).toStringList();
237 switch (item->data(0, TypeRole).toInt()) {
239 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
242 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
245 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
252 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
254 if (event->button() == Qt::LeftButton) {
255 m_DragStartPosition = event->pos();
256 m_dragStarted = true;
258 QTreeWidget::mousePressEvent(event);
262 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
264 if (!m_dragStarted) return;
265 if ((event->pos() - m_DragStartPosition).manhattanLength()
266 < QApplication::startDragDistance())
270 QTreeWidgetItem *clickItem = itemAt(event->pos());
271 if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) {
272 QDrag *drag = new QDrag(this);
273 QMimeData *mimeData = new QMimeData;
274 const QList <QTreeWidgetItem *>list = selectedItems();
276 foreach(QTreeWidgetItem *item, list) {
277 const QDomElement e = itemEffect(item);
278 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
281 data.append(doc.toString().toUtf8());
282 mimeData->setData("kdenlive/effectslist", data);
283 drag->setMimeData(mimeData);
284 //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
285 //drag->setPixmap(pix);
286 //drag->setHotSpot(QPoint(0, 50));
287 drag->start(Qt::CopyAction);
293 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
295 event->setDropAction(Qt::CopyAction);
296 if (event->mimeData()->hasText()) {
297 event->acceptProposedAction();
303 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
305 QTreeWidgetItem *item = itemAt(event->pos());
306 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
309 #include "effectslistwidget.moc"