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