]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
dae8c12a33e45ab5a591261c55dcc553581521f0
[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     setColumnCount(1);
47     setDragEnabled(true);
48     setAcceptDrops(false);
49     setHeaderHidden(true);
50     setFrameShape(QFrame::NoFrame);
51     setAutoFillBackground(false);
52     setRootIsDecorated(false);
53     //setSelectionMode(QAbstractItemView::ExtendedSelection);
54     setDragDropMode(QAbstractItemView::DragOnly);
55     QPalette p = palette();
56     p.setBrush(QPalette::Base, Qt::NoBrush);
57     setPalette(p);
58     initList();
59     connect(this, SIGNAL(activated(const QModelIndex &)), this, SLOT(slotExpandItem(const QModelIndex &)));
60 }
61
62 EffectsListWidget::~EffectsListWidget()
63 {
64 }
65
66 void EffectsListWidget::slotExpandItem(const QModelIndex & index)
67 {
68     setExpanded(index, !isExpanded(index));
69 }
70
71 void EffectsListWidget::initList()
72 {
73     QString current;
74     QString currentFolder;
75     QTreeWidgetItem *item = NULL;
76     QTreeWidgetItem *parentItem;
77     bool found = false;
78
79     if (currentItem()) {
80         current = currentItem()->text(0);
81         if (currentItem()->parent()) currentFolder = currentItem()->parent()->text(0);
82         else if (currentItem()->data(0, TypeRole) ==  EFFECT_FOLDER)  currentFolder = currentItem()->text(0);
83     }
84
85     QString effectName;
86     QStringList effectInfo;
87     KIcon videoIcon("kdenlive-show-video");
88     KIcon audioIcon("kdenlive-show-audio");
89     KIcon customIcon("kdenlive-custom-effect");
90     KIcon folderIcon("folder");
91
92     QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
93     QDomDocument doc;
94     QFile file(effectCategory);
95     doc.setContent(&file, false);
96     file.close();
97     QList <QTreeWidgetItem *> folders;
98     QStringList folderNames;
99     QDomNodeList groups = doc.documentElement().elementsByTagName("group");
100     for (int i = 0; i < groups.count(); i++) {
101         folderNames << groups.at(i).firstChild().firstChild().nodeValue();
102     }
103     for (int i = 0; i < topLevelItemCount(); i++) {
104         topLevelItem(i)->takeChildren();
105         QString currentName = topLevelItem(i)->text(0);
106         if (currentName != i18n("Misc") && currentName != i18n("Audio") && currentName != i18n("Custom") && !folderNames.contains(currentName)) {
107             takeTopLevelItem(i);
108             i--;
109         }
110     }
111
112     for (int i = 0; i < groups.count(); i++) {
113         item = findFolder(folderNames.at(i));
114         if (item) {
115             item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
116         } else {
117             item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(folderNames.at(i)));
118             item->setIcon(0, folderIcon);
119             item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
120             item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
121             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
122             insertTopLevelItem(0, item);
123         }
124         folders.append(item);
125     }
126
127     QTreeWidgetItem *misc = findFolder(i18n("Misc"));
128     if (misc == NULL) {
129         misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
130         misc->setIcon(0, folderIcon);
131         misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
132         misc->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
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         audio->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
142         insertTopLevelItem(0, audio);
143     }
144
145     QTreeWidgetItem *custom = findFolder(i18n("Custom"));
146     if (custom == NULL) {
147         custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
148         custom->setIcon(0, folderIcon);
149         custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
150         custom->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
151         insertTopLevelItem(0, custom);
152     }
153
154     //insertTopLevelItems(0, folders);
155
156
157     int ct = MainWindow::videoEffects.count();
158     for (int ix = 0; ix < ct; ix ++) {
159         effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
160         parentItem = NULL;
161         for (int i = 0; i < folders.count(); i++) {
162             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
163             if (l.contains(effectInfo.at(2))) {
164                 parentItem = folders.at(i);
165                 break;
166             }
167         }
168         if (parentItem == NULL) parentItem = misc;
169         if (!effectInfo.isEmpty()) {
170             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
171             item->setIcon(0, videoIcon);
172             item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
173             item->setData(0, IdRole, effectInfo);
174             if (item->text(0) == current) {
175                 setCurrentItem(item);
176                 found = true;
177             }
178         }
179     }
180
181     ct = MainWindow::audioEffects.count();
182     for (int ix = 0; ix < ct; ix ++) {
183         effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
184         parentItem = NULL;
185         for (int i = 0; i < folders.count(); i++) {
186             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
187             if (l.contains(effectInfo.at(2))) {
188                 parentItem = folders.at(i);
189                 break;
190             }
191         }
192         if (parentItem == NULL) parentItem = audio;
193         if (!effectInfo.isEmpty()) {
194             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
195             item->setIcon(0, audioIcon);
196             item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
197             item->setData(0, IdRole, effectInfo);
198             if (item->text(0) == current) {
199                 setCurrentItem(item);
200                 found = true;
201             }
202         }
203     }
204
205     ct = MainWindow::customEffects.count();
206     kDebug() << "--- REBUILDING;: " << ct;
207     for (int ix = 0; ix < ct; ix ++) {
208         effectInfo = MainWindow::customEffects.effectIdInfo(ix);
209         if (!effectInfo.isEmpty()) {
210             item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
211             item->setIcon(0, customIcon);
212             item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
213             item->setData(0, IdRole, effectInfo);
214             if (item->text(0) == current) {
215                 setCurrentItem(item);
216                 found = true;
217             }
218         }
219     }
220     if (!found && !currentFolder.isEmpty()) {
221         // previously selected effect was removed, focus on its parent folder
222         for (int i = 0; i < topLevelItemCount(); i++) {
223             if (topLevelItem(i)->text(0) == currentFolder) {
224                 setCurrentItem(topLevelItem(i));
225                 break;
226             }
227         }
228
229     }
230     setSortingEnabled(true);
231     sortByColumn(0, Qt::AscendingOrder);
232 }
233
234 QTreeWidgetItem *EffectsListWidget::findFolder(const QString name)
235 {
236     QTreeWidgetItem *item = NULL;
237     QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
238     if (!result.isEmpty()) {
239         for (int j = 0; j < result.count(); j++) {
240             if (result.at(j)->data(0, TypeRole) ==  EFFECT_FOLDER) {
241                 item = result.at(j);
242                 break;
243             }
244         }
245     }
246     return item;
247 }
248
249 const QDomElement EffectsListWidget::currentEffect() const
250 {
251     return itemEffect(currentItem());
252 }
253
254 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
255 {
256     QDomElement effect;
257     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
258     QStringList effectInfo = item->data(0, IdRole).toStringList();
259     kDebug() << "// EFFECT SELECTED: " << effectInfo;
260     switch (item->data(0, TypeRole).toInt()) {
261     case 1:
262         effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
263         break;
264     case 2:
265         effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
266         break;
267     default:
268         effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
269         break;
270     }
271     return effect;
272 }
273
274
275 QString EffectsListWidget::currentInfo()
276 {
277     QTreeWidgetItem *item = currentItem();
278     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
279     QString info;
280     QStringList effectInfo = item->data(0, IdRole).toStringList();
281     switch (item->data(0, TypeRole).toInt()) {
282     case 1:
283         info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
284         break;
285     case 2:
286         info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
287         break;
288     default:
289         info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
290         break;
291     }
292     return info;
293 }
294
295 //virtual
296 QMimeData * EffectsListWidget::mimeData(const QList<QTreeWidgetItem *> list) const
297 {
298     QDomDocument doc;
299     foreach(QTreeWidgetItem *item, list) {
300         if (item->flags() & Qt::ItemIsDragEnabled) {
301             const QDomElement e = itemEffect(item);
302             if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
303         }
304     }
305     QMimeData *mime = new QMimeData;
306     QByteArray data;
307     data.append(doc.toString().toUtf8());
308     mime->setData("kdenlive/effectslist", data);
309     return mime;
310 }
311
312 //virtual
313 void EffectsListWidget::dragMoveEvent(QDragMoveEvent *event)
314 {
315     if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
316         event->acceptProposedAction();
317     } else {
318         event->ignore();
319     }
320 }
321
322
323 //virtual
324 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
325 {
326     QTreeWidgetItem *item = itemAt(event->pos());
327     if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
328 }
329
330 #include "effectslistwidget.moc"