1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "effectslistwidget.h"
22 #include "effectslist.h"
23 #include "mainwindow.h"
26 #include <KStandardDirs>
28 #include <QApplication>
29 #include <QMouseEvent>
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;
38 const int TypeRole = Qt::UserRole;
39 const int IdRole = TypeRole + 1;
42 EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent) :
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);
59 connect(this, SIGNAL(activated(const QModelIndex &)), this, SLOT(slotExpandItem(const QModelIndex &)));
62 EffectsListWidget::~EffectsListWidget()
66 void EffectsListWidget::slotExpandItem(const QModelIndex & index)
68 setExpanded(index, !isExpanded(index));
71 void EffectsListWidget::initList()
74 QString currentFolder;
75 QTreeWidgetItem *item = NULL;
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);
86 KIcon folderIcon("folder");
88 QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
90 QFile file(effectCategory);
91 doc.setContent(&file, false);
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();
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)) {
108 for (int i = 0; i < groups.count(); i++) {
109 item = findFolder(folderNames.at(i));
111 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
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);
120 folders.append(item);
123 QTreeWidgetItem *misc = findFolder(i18n("Misc"));
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);
132 QTreeWidgetItem *audio = findFolder(i18n("Audio"));
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);
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);
150 //insertTopLevelItems(0, folders);
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);
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));
166 setSortingEnabled(true);
167 sortByColumn(0, Qt::AscendingOrder);
170 void EffectsListWidget::loadEffects(const EffectsList *effectlist, KIcon icon, QTreeWidgetItem *defaultFolder, const QList<QTreeWidgetItem *> *folders, const QString type, const QString current, bool *found)
172 QStringList effectInfo, l;
173 QTreeWidgetItem *parentItem;
174 QTreeWidgetItem *item;
175 int ct = effectlist->count();
177 for (int ix = 0; ix < ct; ix ++) {
178 effectInfo = effectlist->effectIdInfo(ix);
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);
188 if (parentItem == NULL)
189 parentItem = defaultFolder;
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);
205 QTreeWidgetItem *EffectsListWidget::findFolder(const QString name)
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) {
220 const QDomElement EffectsListWidget::currentEffect() const
222 return itemEffect(currentItem());
225 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
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()) {
233 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
236 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
239 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
246 QString EffectsListWidget::currentInfo()
248 QTreeWidgetItem *item = currentItem();
249 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
251 QStringList effectInfo = item->data(0, IdRole).toStringList();
252 switch (item->data(0, TypeRole).toInt()) {
254 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
257 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
260 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
267 QMimeData * EffectsListWidget::mimeData(const QList<QTreeWidgetItem *> list) const
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));
276 QMimeData *mime = new QMimeData;
278 data.append(doc.toString().toUtf8());
279 mime->setData("kdenlive/effectslist", data);
284 void EffectsListWidget::dragMoveEvent(QDragMoveEvent *event)
286 if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
287 event->acceptProposedAction();
295 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
297 QTreeWidgetItem *item = itemAt(event->pos());
298 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
301 #include "effectslistwidget.moc"