]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
Remember last selected effect when adding / removing a custom effect
[kdenlive] / src / effectslistwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include "effectslistwidget.h"
22 #include "effectslist.h"
23 #include "mainwindow.h"
24
25 #include <KDebug>
26 #include <KStandardDirs>
27
28 #include <QApplication>
29 #include <QMouseEvent>
30 #include <QMenu>
31
32
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;
37
38 const int TypeRole = Qt::UserRole;
39 const int IdRole = TypeRole + 1;
40
41
42 EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent) :
43         QTreeWidget(parent),
44         m_menu(menu)
45 {
46     //setSelectionMode(QAbstractItemView::ExtendedSelection);
47     //setDragDropMode(QAbstractItemView::DragDrop);
48     setColumnCount(1);
49     setDropIndicatorShown(true);
50     //setAlternatingRowColors(true);
51     setDragEnabled(true);
52     setAcceptDrops(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);
59     setPalette(p);
60     initList();
61     connect(this, SIGNAL(activated(const QModelIndex &)), this, SLOT(slotExpandItem(const QModelIndex &)));
62 }
63
64 EffectsListWidget::~EffectsListWidget()
65 {
66 }
67
68 void EffectsListWidget::slotExpandItem(const QModelIndex & index)
69 {
70     setExpanded(index, !isExpanded(index));
71 }
72
73 void EffectsListWidget::initList()
74 {
75     QString current;
76     QString currentFolder;
77     QTreeWidgetItem *item;
78     QTreeWidgetItem *parentItem;
79     bool found = false;
80
81     if (currentItem()) {
82         current = currentItem()->text(0);
83         if (currentItem()->parent()) currentFolder = currentItem()->parent()->text(0);
84     }
85     clear();
86
87     QString effectName;
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");
93
94     QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
95     QDomDocument doc;
96     QFile file(effectCategory);
97     doc.setContent(&file, false);
98     file.close();
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);
107     }
108
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));
112
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));
116
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));
120
121     insertTopLevelItem(0, custom);
122     insertTopLevelItem(0, misc);
123     insertTopLevelItem(0, audio);
124     insertTopLevelItems(0, folders);
125
126
127     int ct = MainWindow::videoEffects.count();
128     for (int ix = 0; ix < ct; ix ++) {
129         effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
130         parentItem = NULL;
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);
135                 break;
136             }
137         }
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);
146                 found = true;
147             }
148         }
149     }
150
151     ct = MainWindow::audioEffects.count();
152     for (int ix = 0; ix < ct; ix ++) {
153         effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
154         parentItem = NULL;
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);
159                 break;
160             }
161         }
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);
170                 found = true;
171             }
172         }
173     }
174
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);
186                 found = true;
187             }
188         }
189     }
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);
196                 break;
197             }
198         }
199
200     }
201     setSortingEnabled(true);
202     sortByColumn(0, Qt::AscendingOrder);
203 }
204
205 const QDomElement EffectsListWidget::currentEffect() const
206 {
207     return itemEffect(currentItem());
208 }
209
210 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
211 {
212     QDomElement effect;
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()) {
217     case 1:
218         effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
219         break;
220     case 2:
221         effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
222         break;
223     default:
224         effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
225         break;
226     }
227     return effect;
228 }
229
230
231 QString EffectsListWidget::currentInfo()
232 {
233     QTreeWidgetItem *item = currentItem();
234     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
235     QString info;
236     QStringList effectInfo = item->data(0, IdRole).toStringList();
237     switch (item->data(0, TypeRole).toInt()) {
238     case 1:
239         info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
240         break;
241     case 2:
242         info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
243         break;
244     default:
245         info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
246         break;
247     }
248     return info;
249 }
250
251 // virtual
252 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
253 {
254     if (event->button() == Qt::LeftButton) {
255         m_DragStartPosition = event->pos();
256         m_dragStarted = true;
257     }
258     QTreeWidget::mousePressEvent(event);
259 }
260
261 // virtual
262 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
263 {
264     if (!m_dragStarted) return;
265     if ((event->pos() - m_DragStartPosition).manhattanLength()
266             < QApplication::startDragDistance())
267         return;
268
269     {
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();
275             QDomDocument doc;
276             foreach(QTreeWidgetItem *item, list) {
277                 const QDomElement e = itemEffect(item);
278                 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
279             }
280             QByteArray data;
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);
288         }
289         event->accept();
290     }
291 }
292
293 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
294 {
295     event->setDropAction(Qt::CopyAction);
296     if (event->mimeData()->hasText()) {
297         event->acceptProposedAction();
298     }
299     //}
300 }
301
302 //virtual
303 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
304 {
305     QTreeWidgetItem *item = itemAt(event->pos());
306     if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
307 }
308
309 #include "effectslistwidget.moc"