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"
27 #include "QApplication"
28 #include "QMouseEvent"
32 static const int EFFECT_VIDEO = 1;
33 static const int EFFECT_AUDIO = 2;
34 static const int EFFECT_CUSTOM = 3;
35 static const int EFFECT_FOLDER = 4;
37 const int TypeRole = Qt::UserRole;
38 const int IdRole = TypeRole + 1;
40 EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent) :
44 //setSelectionMode(QAbstractItemView::ExtendedSelection);
45 //setDragDropMode(QAbstractItemView::DragDrop);
47 setDropIndicatorShown(true);
48 setAlternatingRowColors(true);
51 setHeaderHidden(true);
55 EffectsListWidget::~EffectsListWidget()
59 void EffectsListWidget::initList()
62 QTreeWidgetItem *item;
63 QTreeWidgetItem *parentItem;
65 QStringList effectInfo;
66 KIcon videoIcon("kdenlive-show-video");
67 KIcon audioIcon("kdenlive-show-audio");
68 KIcon customIcon("kdenlive-custom-effect");
69 KIcon folderIcon("folder");
71 KSharedConfigPtr config = KSharedConfig::openConfig("kdenliveeffectscategoryrc");
72 KConfigGroup transConfig(config, "Category");
74 QMap< QString, QString > profiles = transConfig.entryMap();
75 QMapIterator<QString, QString> i(profiles);
76 QList <QTreeWidgetItem *> folders;
79 item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n(i.key().toUtf8().data())));
80 item->setIcon(0, folderIcon);
81 item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
82 item->setData(0, IdRole, i.value());
85 QTreeWidgetItem *misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
86 misc->setIcon(0, folderIcon);
87 misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
89 QTreeWidgetItem *audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
90 audio->setIcon(0, folderIcon);
91 audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
93 QTreeWidgetItem *custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
94 custom->setIcon(0, folderIcon);
95 custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
97 insertTopLevelItem(0, custom);
98 insertTopLevelItem(0, misc);
99 insertTopLevelItem(0, audio);
100 insertTopLevelItems(0, folders);
103 int ct = MainWindow::videoEffects.count();
104 for (int ix = 0; ix < ct; ix ++) {
105 effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
107 for (int i = 0; i < folders.count(); i++) {
108 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
109 if (l.contains(effectInfo.at(2))) {
110 parentItem = folders.at(i);
114 if (parentItem == NULL) parentItem = misc;
115 if (!effectInfo.isEmpty()) {
116 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
117 item->setIcon(0, videoIcon);
118 item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
119 item->setData(0, IdRole, effectInfo);
123 ct = MainWindow::audioEffects.count();
124 for (int ix = 0; ix < ct; ix ++) {
125 effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
127 for (int i = 0; i < folders.count(); i++) {
128 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
129 if (l.contains(effectInfo.at(2))) {
130 parentItem = folders.at(i);
134 if (parentItem == NULL) parentItem = audio;
135 if (!effectInfo.isEmpty()) {
136 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
137 item->setIcon(0, audioIcon);
138 item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
139 item->setData(0, IdRole, effectInfo);
143 ct = MainWindow::customEffects.count();
144 for (int ix = 0; ix < ct; ix ++) {
145 effectInfo = MainWindow::customEffects.effectIdInfo(ix);
146 if (!effectInfo.isEmpty()) {
147 item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
148 item->setIcon(0, customIcon);
149 item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
150 item->setData(0, IdRole, effectInfo);
153 setSortingEnabled(true);
154 sortByColumn(0, Qt::AscendingOrder);
157 const QDomElement EffectsListWidget::currentEffect() const
159 return itemEffect(currentItem());
162 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
165 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
166 QStringList effectInfo = item->data(0, IdRole).toStringList();
167 kDebug() << "// EFFECT SELECTED: " << effectInfo;
168 switch (item->data(0, TypeRole).toInt()) {
170 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
173 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
176 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
183 QString EffectsListWidget::currentInfo()
185 QTreeWidgetItem *item = currentItem();
186 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
188 QStringList effectInfo = item->data(0, IdRole).toStringList();
189 switch (item->data(0, TypeRole).toInt()) {
191 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
194 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
197 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
204 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
206 if (event->button() == Qt::LeftButton) {
207 m_DragStartPosition = event->pos();
208 m_dragStarted = true;
210 QTreeWidget::mousePressEvent(event);
214 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
216 if (!m_dragStarted) return;
217 if ((event->pos() - m_DragStartPosition).manhattanLength()
218 < QApplication::startDragDistance())
222 QTreeWidgetItem *clickItem = itemAt(event->pos());
223 if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) {
224 QDrag *drag = new QDrag(this);
225 QMimeData *mimeData = new QMimeData;
226 const QList <QTreeWidgetItem *>list = selectedItems();
228 foreach(QTreeWidgetItem *item, list) {
229 const QDomElement e = itemEffect(item);
230 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
233 data.append(doc.toString().toUtf8());
234 mimeData->setData("kdenlive/effectslist", data);
235 drag->setMimeData(mimeData);
236 //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
237 //drag->setPixmap(pix);
238 //drag->setHotSpot(QPoint(0, 50));
239 drag->start(Qt::CopyAction);
245 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
247 event->setDropAction(Qt::CopyAction);
248 if (event->mimeData()->hasText()) {
249 event->acceptProposedAction();
255 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
257 QTreeWidgetItem *item = itemAt(event->pos());
258 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
261 #include "effectslistwidget.moc"