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);
87 QStringList effectInfo;
88 KIcon videoIcon("kdenlive-show-video");
89 KIcon audioIcon("kdenlive-show-audio");
90 KIcon customIcon("kdenlive-custom-effect");
91 KIcon folderIcon("folder");
93 QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
95 QFile file(effectCategory);
96 doc.setContent(&file, false);
98 QList <QTreeWidgetItem *> folders;
99 QStringList folderNames;
100 QDomNodeList groups = doc.documentElement().elementsByTagName("group");
101 for (int i = 0; i < groups.count(); i++) {
102 folderNames << groups.at(i).firstChild().firstChild().nodeValue();
104 for (int i = 0; i < topLevelItemCount(); i++) {
105 topLevelItem(i)->takeChildren();
106 if (!folderNames.contains(topLevelItem(i)->text(0))) {
112 for (int i = 0; i < groups.count(); i++) {
113 QList<QTreeWidgetItem *> result = findItems(folderNames.at(i), Qt::MatchExactly);
114 if (!result.isEmpty()) item = result.at(0);
115 else item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(folderNames.at(i)));
116 item->setIcon(0, folderIcon);
117 item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
118 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
119 folders.append(item);
122 QTreeWidgetItem *misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
123 misc->setIcon(0, folderIcon);
124 misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
126 QTreeWidgetItem *audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
127 audio->setIcon(0, folderIcon);
128 audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
130 QTreeWidgetItem *custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
131 custom->setIcon(0, folderIcon);
132 custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
134 insertTopLevelItem(0, custom);
135 insertTopLevelItem(0, misc);
136 insertTopLevelItem(0, audio);
137 insertTopLevelItems(0, folders);
140 int ct = MainWindow::videoEffects.count();
141 for (int ix = 0; ix < ct; ix ++) {
142 effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
144 for (int i = 0; i < folders.count(); i++) {
145 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
146 if (l.contains(effectInfo.at(2))) {
147 parentItem = folders.at(i);
151 if (parentItem == NULL) parentItem = misc;
152 if (!effectInfo.isEmpty()) {
153 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
154 item->setIcon(0, videoIcon);
155 item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
156 item->setData(0, IdRole, effectInfo);
157 if (item->text(0) == current) {
158 setCurrentItem(item);
164 ct = MainWindow::audioEffects.count();
165 for (int ix = 0; ix < ct; ix ++) {
166 effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
168 for (int i = 0; i < folders.count(); i++) {
169 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
170 if (l.contains(effectInfo.at(2))) {
171 parentItem = folders.at(i);
175 if (parentItem == NULL) parentItem = audio;
176 if (!effectInfo.isEmpty()) {
177 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
178 item->setIcon(0, audioIcon);
179 item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
180 item->setData(0, IdRole, effectInfo);
181 if (item->text(0) == current) {
182 setCurrentItem(item);
188 ct = MainWindow::customEffects.count();
189 kDebug() << "--- REBUILDING;: " << ct;
190 for (int ix = 0; ix < ct; ix ++) {
191 effectInfo = MainWindow::customEffects.effectIdInfo(ix);
192 if (!effectInfo.isEmpty()) {
193 item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
194 item->setIcon(0, customIcon);
195 item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
196 item->setData(0, IdRole, effectInfo);
197 if (item->text(0) == current) {
198 setCurrentItem(item);
203 if (!found && !currentFolder.isEmpty()) {
204 // previously selected effect was removed, focus on its parent folder
205 for (int i = 0; i < topLevelItemCount(); i++) {
206 if (topLevelItem(i)->text(0) == currentFolder) {
207 setCurrentItem(topLevelItem(i));
208 topLevelItem(i)->setExpanded(true);
214 setSortingEnabled(true);
215 sortByColumn(0, Qt::AscendingOrder);
218 const QDomElement EffectsListWidget::currentEffect() const
220 return itemEffect(currentItem());
223 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
226 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
227 QStringList effectInfo = item->data(0, IdRole).toStringList();
228 kDebug() << "// EFFECT SELECTED: " << effectInfo;
229 switch (item->data(0, TypeRole).toInt()) {
231 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
234 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
237 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
244 QString EffectsListWidget::currentInfo()
246 QTreeWidgetItem *item = currentItem();
247 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
249 QStringList effectInfo = item->data(0, IdRole).toStringList();
250 switch (item->data(0, TypeRole).toInt()) {
252 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
255 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
258 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
265 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
267 if (event->button() == Qt::LeftButton) {
268 m_DragStartPosition = event->pos();
269 m_dragStarted = true;
271 QTreeWidget::mousePressEvent(event);
275 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
277 if (!m_dragStarted) return;
278 if ((event->pos() - m_DragStartPosition).manhattanLength()
279 < QApplication::startDragDistance())
283 QTreeWidgetItem *clickItem = itemAt(event->pos());
284 if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) {
285 QDrag *drag = new QDrag(this);
286 QMimeData *mimeData = new QMimeData;
287 const QList <QTreeWidgetItem *>list = selectedItems();
289 foreach(QTreeWidgetItem *item, list) {
290 const QDomElement e = itemEffect(item);
291 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
294 data.append(doc.toString().toUtf8());
295 mimeData->setData("kdenlive/effectslist", data);
296 drag->setMimeData(mimeData);
297 //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
298 //drag->setPixmap(pix);
299 //drag->setHotSpot(QPoint(0, 50));
300 drag->start(Qt::CopyAction);
306 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
308 event->setDropAction(Qt::CopyAction);
309 if (event->mimeData()->hasText()) {
310 event->acceptProposedAction();
316 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
318 QTreeWidgetItem *item = itemAt(event->pos());
319 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
322 #include "effectslistwidget.moc"