]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
Fix stupid shortcut that accidentally stole keypresses
[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(true);
54     setIndentation(10);
55     //setSelectionMode(QAbstractItemView::ExtendedSelection);
56     setDragDropMode(QAbstractItemView::DragOnly);
57     QPalette p = palette();
58     p.setBrush(QPalette::Base, Qt::NoBrush);
59     setPalette(p);
60     connect(this, SIGNAL(activated(const QModelIndex &)), this, SLOT(slotExpandItem(const QModelIndex &)));
61 }
62
63 EffectsListWidget::~EffectsListWidget()
64 {
65 }
66
67 void EffectsListWidget::slotExpandItem(const QModelIndex & index)
68 {
69     setExpanded(index, !isExpanded(index));
70 }
71
72 void EffectsListWidget::initList(QMenu *effectsMenu, KActionCategory *effectActions)
73 {
74     QString current;
75     QString currentFolder;
76     QTreeWidgetItem *item = NULL;
77     bool found = false;
78     effectsMenu->clear();
79     
80     if (currentItem()) {
81         current = currentItem()->text(0);
82         if (currentItem()->parent())
83             currentFolder = currentItem()->parent()->text(0);
84         else if (currentItem()->data(0, TypeRole) ==  EFFECT_FOLDER)
85             currentFolder = currentItem()->text(0);
86     }
87
88     QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
89     QDomDocument doc;
90     QFile file(effectCategory);
91     doc.setContent(&file, false);
92     file.close();
93     QList <QTreeWidgetItem *> folders;
94     QStringList folderNames;
95     QDomNodeList groups = doc.documentElement().elementsByTagName("group");
96     for (int i = 0; i < groups.count(); i++) {
97         folderNames << i18n(groups.at(i).firstChild().firstChild().nodeValue().toUtf8().constData());
98     }
99     for (int i = 0; i < topLevelItemCount(); i++) {
100         topLevelItem(i)->takeChildren();
101         QString currentName = topLevelItem(i)->text(0);
102         if (currentName != i18n("Misc") && currentName != i18n("Audio") && currentName != i18nc("Folder Name", "Custom") && !folderNames.contains(currentName)) {
103             takeTopLevelItem(i);
104             i--;
105         }
106     }
107
108     for (int i = 0; i < groups.count(); i++) {
109         item = findFolder(folderNames.at(i));
110         if (item) {
111             item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
112         } else {
113             item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(folderNames.at(i)));
114             item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
115             item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
116             item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
117             item->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicatorWhenChildless);
118             insertTopLevelItem(0, item);
119         }
120         folders.append(item);
121     }
122
123     QTreeWidgetItem *misc = findFolder(i18n("Misc"));
124     if (misc == NULL) {
125         misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
126         misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
127         misc->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
128         insertTopLevelItem(0, misc);
129     }
130
131     QTreeWidgetItem *audio = findFolder(i18n("Audio"));
132     if (audio == NULL) {
133         audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
134         audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
135         audio->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
136         insertTopLevelItem(0, audio);
137     }
138
139     QTreeWidgetItem *custom = findFolder(i18nc("Folder Name", "Custom"));
140     if (custom == NULL) {
141         custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18nc("Folder Name", "Custom")));
142         custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
143         custom->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
144         insertTopLevelItem(0, custom);
145     }
146
147     //insertTopLevelItems(0, folders);
148
149     loadEffects(&MainWindow::videoEffects, KIcon("kdenlive-show-video"), misc, &folders, QString::number((int) EFFECT_VIDEO), current, &found);
150     loadEffects(&MainWindow::audioEffects, KIcon("kdenlive-show-audio"), audio, &folders, QString::number((int) EFFECT_AUDIO), current, &found);
151     loadEffects(&MainWindow::customEffects, KIcon("kdenlive-custom-effect"), custom, static_cast<QList<QTreeWidgetItem *> *>(0), QString::number((int) EFFECT_CUSTOM), current, &found);
152
153     if (!found && !currentFolder.isEmpty()) {
154         // previously selected effect was removed, focus on its parent folder
155         for (int i = 0; i < topLevelItemCount(); i++) {
156             if (topLevelItem(i)->text(0) == currentFolder) {
157                 setCurrentItem(topLevelItem(i));
158                 break;
159             }
160         }
161
162     }
163     setSortingEnabled(true);
164     sortByColumn(0, Qt::AscendingOrder);
165
166     // populate effects menu
167     QMenu *sub1 = NULL;
168     QMenu *sub2 = NULL;
169     QMenu *sub3 = NULL;
170     QMenu *sub4 = NULL;
171     for (int i = 0; i < topLevelItemCount(); i++) {
172         if (!topLevelItem(i)->childCount())
173             continue;
174         QMenu *sub = new QMenu(topLevelItem(i)->text(0), effectsMenu);
175         effectsMenu->addMenu(sub);
176         int effectsInCategory = topLevelItem(i)->childCount();
177         bool hasSubCategories = false;
178         if (effectsInCategory > 60) {
179             // create subcategories if there are too many effects
180             hasSubCategories = true;
181             sub1 = new QMenu(i18nc("menu name for effects names between these 2 letters", "0 - F"), sub);
182             sub->addMenu(sub1);
183             sub2 = new QMenu(i18nc("menu name for effects names between these 2 letters", "G - L"), sub);
184             sub->addMenu(sub2);
185             sub3 = new QMenu(i18nc("menu name for effects names between these 2 letters", "M - R"), sub);
186             sub->addMenu(sub3);
187             sub4 = new QMenu(i18nc("menu name for effects names between these 2 letters", "S - Z"), sub);
188             sub->addMenu(sub4);
189         }
190         for (int j = 0; j < effectsInCategory; j++) {
191                 QTreeWidgetItem *item = topLevelItem(i)->child(j);
192                 KAction *a = new KAction(KIcon(item->icon(0)), item->text(0), sub);
193                 QStringList data = item->data(0, IdRole).toStringList();
194                 QString id = data.at(1);
195                 if (id.isEmpty()) id = data.at(0);
196                 a->setData(data);
197                 a->setIconVisibleInMenu(false);
198                 if (hasSubCategories) {
199                     // put action in sub category
200                     QRegExp rx("^[s-z].+");
201                     if (rx.exactMatch(item->text(0).toLower())) {
202                         sub4->addAction(a);
203                     } else {
204                         rx.setPattern("^[m-r].+");
205                         if (rx.exactMatch(item->text(0).toLower())) {
206                             sub3->addAction(a);
207                         }
208                         else {
209                             rx.setPattern("^[g-l].+");
210                             if (rx.exactMatch(item->text(0).toLower())) {
211                                 sub2->addAction(a);
212                             }
213                             else sub1->addAction(a);
214                         }
215                     }
216                 }
217                 else sub->addAction(a);
218                 effectActions->addAction("video_effect_" + id, a);
219         }
220     }
221 }
222
223 void EffectsListWidget::loadEffects(const EffectsList *effectlist, KIcon icon, QTreeWidgetItem *defaultFolder, const QList<QTreeWidgetItem *> *folders, const QString type, const QString current, bool *found)
224 {
225     QStringList effectInfo, l;
226     QTreeWidgetItem *parentItem;
227     QTreeWidgetItem *item;
228     int ct = effectlist->count();
229
230     for (int ix = 0; ix < ct; ix ++) {
231         effectInfo = effectlist->effectIdInfo(ix);
232         effectInfo.append(type);
233         parentItem = NULL;
234
235         if (folders) {
236             for (int i = 0; i < folders->count(); i++) {
237                 l = folders->at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
238                 if (l.contains(effectInfo.at(2))) {
239                     parentItem = folders->at(i);
240                     break;
241                 }
242             }
243         }
244         if (parentItem == NULL)
245             parentItem = defaultFolder;
246
247         if (!effectInfo.isEmpty()) {
248             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
249             if (effectInfo.count() == 4) item->setIcon(0, KIcon("folder"));
250             else item->setIcon(0, icon);
251             item->setData(0, TypeRole, type);
252             item->setData(0, IdRole, effectInfo);
253             item->setToolTip(0, effectlist->getInfo(effectInfo.at(0), effectInfo.at(1)));
254             if (item->text(0) == current) {
255                 setCurrentItem(item);
256                 *found = true;
257             }
258         }
259     }
260 }
261
262 QTreeWidgetItem *EffectsListWidget::findFolder(const QString name)
263 {
264     QTreeWidgetItem *item = NULL;
265     QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
266     if (!result.isEmpty()) {
267         for (int j = 0; j < result.count(); j++) {
268             if (result.at(j)->data(0, TypeRole) ==  EFFECT_FOLDER) {
269                 item = result.at(j);
270                 break;
271             }
272         }
273     }
274     return item;
275 }
276
277 const QDomElement EffectsListWidget::currentEffect() const
278 {
279     return itemEffect(currentItem());
280 }
281
282 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
283 {
284     QDomElement effect;
285     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
286     QStringList effectInfo = item->data(0, IdRole).toStringList();
287     switch (item->data(0, TypeRole).toInt()) {
288     case 1:
289         effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
290         break;
291     case 2:
292         effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
293         break;
294     default:
295         effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
296         break;
297     }
298     return effect;
299 }
300
301
302 QString EffectsListWidget::currentInfo()
303 {
304     QTreeWidgetItem *item = currentItem();
305     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
306     QString info;
307     QStringList effectInfo = item->data(0, IdRole).toStringList();
308     switch (item->data(0, TypeRole).toInt()) {
309     case 1:
310         info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
311         break;
312     case 2:
313         info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
314         break;
315     default:
316         info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
317         break;
318     }
319     return info;
320 }
321
322 //virtual
323 void EffectsListWidget::keyPressEvent(QKeyEvent *e)
324 {
325     if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
326         emit applyEffect(currentEffect());
327         e->accept();
328         return;
329     }
330     QTreeWidget::keyPressEvent(e);
331 }
332
333 //virtual
334 QMimeData * EffectsListWidget::mimeData(const QList<QTreeWidgetItem *> list) const
335 {
336     QDomDocument doc;
337     foreach(QTreeWidgetItem *item, list) {
338         if (item->flags() & Qt::ItemIsDragEnabled) {
339             const QDomElement e = itemEffect(item);
340             if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
341         }
342     }
343     QMimeData *mime = new QMimeData;
344     QByteArray data;
345     data.append(doc.toString().toUtf8());
346     mime->setData("kdenlive/effectslist", data);
347     return mime;
348 }
349
350 //virtual
351 void EffectsListWidget::dragMoveEvent(QDragMoveEvent *event)
352 {
353     if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
354         event->acceptProposedAction();
355     } else {
356         event->ignore();
357     }
358 }
359
360
361 //virtual
362 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
363 {
364     QTreeWidgetItem *item = itemAt(event->pos());
365     if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
366 }
367
368 #include "effectslistwidget.moc"