]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
Moving choosecolorwidget and colorpickerwidget in the widget folder.
[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(QModelIndex)), this, SLOT(slotExpandItem(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, EFFECT_VIDEO, current, &found);
150     loadEffects(&MainWindow::audioEffects, KIcon("kdenlive-show-audio"), audio, &folders, EFFECT_AUDIO, current, &found);
151     loadEffects(&MainWindow::customEffects, KIcon("kdenlive-custom-effect"), custom, static_cast<QList<QTreeWidgetItem *> *>(0), 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, int type, const QString &current, bool *found)
224 {
225     QStringList effectInfo, l;
226     QTreeWidgetItem *parentItem;
227     QTreeWidgetItem *item;
228     int ct = effectlist->count();
229
230     
231     for (int ix = 0; ix < ct; ++ix) {
232         effectInfo = effectlist->effectIdInfo(ix);
233         effectInfo.append(QString::number(type));
234         parentItem = NULL;
235
236         if (folders) {
237             for (int i = 0; i < folders->count(); ++i) {
238                 l = folders->at(i)->data(0, IdRole).toString().split(QLatin1Char(','), QString::SkipEmptyParts);
239                 if (l.contains(effectInfo.at(2))) {
240                     parentItem = folders->at(i);
241                     break;
242                 }
243             }
244         }
245         if (parentItem == NULL)
246             parentItem = defaultFolder;
247
248         if (!effectInfo.isEmpty()) {
249             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
250             if (effectInfo.count() == 4) item->setIcon(0, KIcon("folder"));
251             else item->setIcon(0, icon);
252             item->setData(0, TypeRole, type);
253             item->setData(0, IdRole, effectInfo);
254             item->setToolTip(0, effectlist->getInfo(effectInfo.at(0), effectInfo.at(1)));
255             if (item->text(0) == current) {
256                 setCurrentItem(item);
257                 *found = true;
258             }
259         }
260     }
261 }
262
263 QTreeWidgetItem *EffectsListWidget::findFolder(const QString &name)
264 {
265     QTreeWidgetItem *item = NULL;
266     QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
267     if (!result.isEmpty()) {
268         for (int j = 0; j < result.count(); ++j) {
269             if (result.at(j)->data(0, TypeRole) ==  EFFECT_FOLDER) {
270                 item = result.at(j);
271                 break;
272             }
273         }
274     }
275     return item;
276 }
277
278 const QDomElement EffectsListWidget::currentEffect() const
279 {
280     return itemEffect(currentItem());
281 }
282
283 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
284 {
285     QDomElement effect;
286     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
287     QStringList effectInfo = item->data(0, IdRole).toStringList();
288     switch (item->data(0, TypeRole).toInt()) {
289     case 1:
290         effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
291         break;
292     case 2:
293         effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
294         break;
295     default:
296         effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
297         break;
298     }
299     return effect;
300 }
301
302
303 QString EffectsListWidget::currentInfo() const
304 {
305     QTreeWidgetItem *item = currentItem();
306     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
307     QString info;
308     QStringList effectInfo = item->data(0, IdRole).toStringList();
309     switch (item->data(0, TypeRole).toInt()) {
310     case 1:
311         info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
312         break;
313     case 2:
314         info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
315         break;
316     default:
317         info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
318         break;
319     }
320     return info;
321 }
322
323 //virtual
324 void EffectsListWidget::keyPressEvent(QKeyEvent *e)
325 {
326     if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
327         emit applyEffect(currentEffect());
328         e->accept();
329         return;
330     }
331     QTreeWidget::keyPressEvent(e);
332 }
333
334 //virtual
335 QMimeData * EffectsListWidget::mimeData(const QList<QTreeWidgetItem *> list) const
336 {
337     QDomDocument doc;
338     foreach(QTreeWidgetItem *item, list) {
339         if (item->flags() & Qt::ItemIsDragEnabled) {
340             const QDomElement e = itemEffect(item);
341             if (!e.isNull())
342                 doc.appendChild(doc.importNode(e, true));
343         }
344     }
345     QMimeData *mime = new QMimeData;
346     QByteArray data;
347     data.append(doc.toString().toUtf8());
348     mime->setData("kdenlive/effectslist", data);
349     return mime;
350 }
351
352 //virtual
353 void EffectsListWidget::dragMoveEvent(QDragMoveEvent *event)
354 {
355     if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
356         event->acceptProposedAction();
357     } else {
358         event->ignore();
359     }
360 }
361
362
363 //virtual
364 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
365 {
366     QTreeWidgetItem *item = itemAt(event->pos());
367     if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM)
368         m_menu->popup(event->globalPos());
369 }
370
371 #include "effectslistwidget.moc"