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