]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
Effects context menu: use submenu structure similar to effects list widget folders
[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                 a->setData(item->data(0, IdRole));
178                 a->setIconVisibleInMenu(false);
179                 sub->addAction(a);
180                 effectActions->addAction("video_effect_" + item->text(0), a);
181         }
182     }
183 }
184
185 void EffectsListWidget::loadEffects(const EffectsList *effectlist, KIcon icon, QTreeWidgetItem *defaultFolder, const QList<QTreeWidgetItem *> *folders, const QString type, const QString current, bool *found)
186 {
187     QStringList effectInfo, l;
188     QTreeWidgetItem *parentItem;
189     QTreeWidgetItem *item;
190     int ct = effectlist->count();
191
192     for (int ix = 0; ix < ct; ix ++) {
193         effectInfo = effectlist->effectIdInfo(ix);
194         effectInfo.append(type);
195         parentItem = NULL;
196
197         if (folders) {
198             for (int i = 0; i < folders->count(); i++) {
199                 l = folders->at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
200                 if (l.contains(effectInfo.at(2))) {
201                     parentItem = folders->at(i);
202                     break;
203                 }
204             }
205         }
206         if (parentItem == NULL)
207             parentItem = defaultFolder;
208
209         if (!effectInfo.isEmpty()) {
210             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
211             item->setIcon(0, icon);
212             item->setData(0, TypeRole, type);
213             item->setData(0, IdRole, effectInfo);
214             item->setToolTip(0, effectlist->getInfo(effectInfo.at(0), effectInfo.at(1)));
215             if (item->text(0) == current) {
216                 setCurrentItem(item);
217                 *found = true;
218             }
219         }
220     }
221 }
222
223 QTreeWidgetItem *EffectsListWidget::findFolder(const QString name)
224 {
225     QTreeWidgetItem *item = NULL;
226     QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
227     if (!result.isEmpty()) {
228         for (int j = 0; j < result.count(); j++) {
229             if (result.at(j)->data(0, TypeRole) ==  EFFECT_FOLDER) {
230                 item = result.at(j);
231                 break;
232             }
233         }
234     }
235     return item;
236 }
237
238 const QDomElement EffectsListWidget::currentEffect() const
239 {
240     return itemEffect(currentItem());
241 }
242
243 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
244 {
245     QDomElement effect;
246     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
247     QStringList effectInfo = item->data(0, IdRole).toStringList();
248     kDebug() << "// EFFECT SELECTED: " << effectInfo;
249     switch (item->data(0, TypeRole).toInt()) {
250     case 1:
251         effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
252         break;
253     case 2:
254         effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
255         break;
256     default:
257         effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
258         break;
259     }
260     return effect;
261 }
262
263
264 QString EffectsListWidget::currentInfo()
265 {
266     QTreeWidgetItem *item = currentItem();
267     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
268     QString info;
269     QStringList effectInfo = item->data(0, IdRole).toStringList();
270     switch (item->data(0, TypeRole).toInt()) {
271     case 1:
272         info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
273         break;
274     case 2:
275         info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
276         break;
277     default:
278         info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
279         break;
280     }
281     return info;
282 }
283
284 //virtual
285 QMimeData * EffectsListWidget::mimeData(const QList<QTreeWidgetItem *> list) const
286 {
287     QDomDocument doc;
288     foreach(QTreeWidgetItem *item, list) {
289         if (item->flags() & Qt::ItemIsDragEnabled) {
290             const QDomElement e = itemEffect(item);
291             if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
292         }
293     }
294     QMimeData *mime = new QMimeData;
295     QByteArray data;
296     data.append(doc.toString().toUtf8());
297     mime->setData("kdenlive/effectslist", data);
298     return mime;
299 }
300
301 //virtual
302 void EffectsListWidget::dragMoveEvent(QDragMoveEvent *event)
303 {
304     if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
305         event->acceptProposedAction();
306     } else {
307         event->ignore();
308     }
309 }
310
311
312 //virtual
313 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
314 {
315     QTreeWidgetItem *item = itemAt(event->pos());
316     if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
317 }
318
319 #include "effectslistwidget.moc"