]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
Fix effects list folders closed when adding an 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 = NULL;
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         else if (currentItem()->data(0, TypeRole) ==  EFFECT_FOLDER)  currentFolder = currentItem()->text(0);
85     }
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     QStringList folderNames;
101     QDomNodeList groups = doc.documentElement().elementsByTagName("group");
102     for (int i = 0; i < groups.count(); i++) {
103         folderNames << groups.at(i).firstChild().firstChild().nodeValue();
104     }
105     for (int i = 0; i < topLevelItemCount(); i++) {
106         topLevelItem(i)->takeChildren();
107         QString currentName = topLevelItem(i)->text(0);
108         if (currentName != i18n("Misc") && currentName != i18n("Audio") && currentName != i18n("Custom") && !folderNames.contains(currentName)) {
109             takeTopLevelItem(i);
110             i--;
111         }
112     }
113
114     for (int i = 0; i < groups.count(); i++) {
115         item = findFolder(folderNames.at(i));
116         if (item) {
117             item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
118         } else {
119             item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(folderNames.at(i)));
120             item->setIcon(0, folderIcon);
121             item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
122             item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
123             insertTopLevelItem(0, item);
124         }
125         folders.append(item);
126     }
127
128     QTreeWidgetItem *misc = findFolder(i18n("Misc"));
129     if (misc == NULL) {
130         misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
131         misc->setIcon(0, folderIcon);
132         misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
133         insertTopLevelItem(0, misc);
134     }
135
136     QTreeWidgetItem *audio = findFolder(i18n("Audio"));
137     if (audio == NULL) {
138         audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
139         audio->setIcon(0, folderIcon);
140         audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
141         insertTopLevelItem(0, audio);
142     }
143
144     QTreeWidgetItem *custom = findFolder(i18n("Custom"));
145     if (custom == NULL) {
146         custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
147         custom->setIcon(0, folderIcon);
148         custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
149         insertTopLevelItem(0, custom);
150     }
151
152     //insertTopLevelItems(0, folders);
153
154
155     int ct = MainWindow::videoEffects.count();
156     for (int ix = 0; ix < ct; ix ++) {
157         effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
158         parentItem = NULL;
159         for (int i = 0; i < folders.count(); i++) {
160             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
161             if (l.contains(effectInfo.at(2))) {
162                 parentItem = folders.at(i);
163                 break;
164             }
165         }
166         if (parentItem == NULL) parentItem = misc;
167         if (!effectInfo.isEmpty()) {
168             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
169             item->setIcon(0, videoIcon);
170             item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
171             item->setData(0, IdRole, effectInfo);
172             if (item->text(0) == current) {
173                 setCurrentItem(item);
174                 found = true;
175             }
176         }
177     }
178
179     ct = MainWindow::audioEffects.count();
180     for (int ix = 0; ix < ct; ix ++) {
181         effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
182         parentItem = NULL;
183         for (int i = 0; i < folders.count(); i++) {
184             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
185             if (l.contains(effectInfo.at(2))) {
186                 parentItem = folders.at(i);
187                 break;
188             }
189         }
190         if (parentItem == NULL) parentItem = audio;
191         if (!effectInfo.isEmpty()) {
192             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
193             item->setIcon(0, audioIcon);
194             item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
195             item->setData(0, IdRole, effectInfo);
196             if (item->text(0) == current) {
197                 setCurrentItem(item);
198                 found = true;
199             }
200         }
201     }
202
203     ct = MainWindow::customEffects.count();
204     kDebug() << "--- REBUILDING;: " << ct;
205     for (int ix = 0; ix < ct; ix ++) {
206         effectInfo = MainWindow::customEffects.effectIdInfo(ix);
207         if (!effectInfo.isEmpty()) {
208             item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
209             item->setIcon(0, customIcon);
210             item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
211             item->setData(0, IdRole, effectInfo);
212             if (item->text(0) == current) {
213                 setCurrentItem(item);
214                 found = true;
215             }
216         }
217     }
218     if (!found && !currentFolder.isEmpty()) {
219         // previously selected effect was removed, focus on its parent folder
220         for (int i = 0; i < topLevelItemCount(); i++) {
221             if (topLevelItem(i)->text(0) == currentFolder) {
222                 setCurrentItem(topLevelItem(i));
223                 break;
224             }
225         }
226
227     }
228     setSortingEnabled(true);
229     sortByColumn(0, Qt::AscendingOrder);
230 }
231
232 QTreeWidgetItem *EffectsListWidget::findFolder(const QString name)
233 {
234     QTreeWidgetItem *item = NULL;
235     QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
236     if (!result.isEmpty()) {
237         for (int j = 0; j < result.count(); j++) {
238             if (result.at(j)->data(0, TypeRole) ==  EFFECT_FOLDER) {
239                 item = result.at(j);
240                 break;
241             }
242         }
243     }
244     return item;
245 }
246
247 const QDomElement EffectsListWidget::currentEffect() const
248 {
249     return itemEffect(currentItem());
250 }
251
252 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
253 {
254     QDomElement effect;
255     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
256     QStringList effectInfo = item->data(0, IdRole).toStringList();
257     kDebug() << "// EFFECT SELECTED: " << effectInfo;
258     switch (item->data(0, TypeRole).toInt()) {
259     case 1:
260         effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
261         break;
262     case 2:
263         effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
264         break;
265     default:
266         effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
267         break;
268     }
269     return effect;
270 }
271
272
273 QString EffectsListWidget::currentInfo()
274 {
275     QTreeWidgetItem *item = currentItem();
276     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
277     QString info;
278     QStringList effectInfo = item->data(0, IdRole).toStringList();
279     switch (item->data(0, TypeRole).toInt()) {
280     case 1:
281         info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
282         break;
283     case 2:
284         info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
285         break;
286     default:
287         info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
288         break;
289     }
290     return info;
291 }
292
293 // virtual
294 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
295 {
296     if (event->button() == Qt::LeftButton) {
297         m_DragStartPosition = event->pos();
298         m_dragStarted = true;
299     }
300     QTreeWidget::mousePressEvent(event);
301 }
302
303 // virtual
304 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
305 {
306     if (!m_dragStarted) return;
307     if ((event->pos() - m_DragStartPosition).manhattanLength()
308             < QApplication::startDragDistance())
309         return;
310
311     {
312         QTreeWidgetItem *clickItem = itemAt(event->pos());
313         if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) {
314             QDrag *drag = new QDrag(this);
315             QMimeData *mimeData = new QMimeData;
316             const QList <QTreeWidgetItem *>list = selectedItems();
317             QDomDocument doc;
318             foreach(QTreeWidgetItem *item, list) {
319                 const QDomElement e = itemEffect(item);
320                 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
321             }
322             QByteArray data;
323             data.append(doc.toString().toUtf8());
324             mimeData->setData("kdenlive/effectslist", data);
325             drag->setMimeData(mimeData);
326             //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
327             //drag->setPixmap(pix);
328             //drag->setHotSpot(QPoint(0, 50));
329             drag->start(Qt::CopyAction);
330         }
331         event->accept();
332     }
333 }
334
335 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
336 {
337     event->setDropAction(Qt::CopyAction);
338     if (event->mimeData()->hasText()) {
339         event->acceptProposedAction();
340     }
341     //}
342 }
343
344 //virtual
345 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
346 {
347     QTreeWidgetItem *item = itemAt(event->pos());
348     if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
349 }
350
351 #include "effectslistwidget.moc"