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