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, static_cast<QList<QTreeWidgetItem *> *>(0), 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);
182 for (int i = 0; i < folders->count(); i++) {
183 l = folders->at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
184 if (l.contains(effectInfo.at(2))) {
185 parentItem = folders->at(i);
190 if (parentItem == NULL)
191 parentItem = defaultFolder;
193 if (!effectInfo.isEmpty()) {
194 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
195 item->setIcon(0, icon);
196 item->setData(0, TypeRole, type);
197 item->setData(0, IdRole, effectInfo);
198 item->setToolTip(0, effectlist->getInfo(effectInfo.at(0), effectInfo.at(1)));
199 if (item->text(0) == current) {
200 setCurrentItem(item);
207 QTreeWidgetItem *EffectsListWidget::findFolder(const QString name)
209 QTreeWidgetItem *item = NULL;
210 QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
211 if (!result.isEmpty()) {
212 for (int j = 0; j < result.count(); j++) {
213 if (result.at(j)->data(0, TypeRole) == EFFECT_FOLDER) {
222 const QDomElement EffectsListWidget::currentEffect() const
224 return itemEffect(currentItem());
227 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
230 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
231 QStringList effectInfo = item->data(0, IdRole).toStringList();
232 kDebug() << "// EFFECT SELECTED: " << effectInfo;
233 switch (item->data(0, TypeRole).toInt()) {
235 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
238 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
241 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
248 QString EffectsListWidget::currentInfo()
250 QTreeWidgetItem *item = currentItem();
251 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
253 QStringList effectInfo = item->data(0, IdRole).toStringList();
254 switch (item->data(0, TypeRole).toInt()) {
256 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
259 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
262 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
269 QMimeData * EffectsListWidget::mimeData(const QList<QTreeWidgetItem *> list) const
272 foreach(QTreeWidgetItem *item, list) {
273 if (item->flags() & Qt::ItemIsDragEnabled) {
274 const QDomElement e = itemEffect(item);
275 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
278 QMimeData *mime = new QMimeData;
280 data.append(doc.toString().toUtf8());
281 mime->setData("kdenlive/effectslist", data);
286 void EffectsListWidget::dragMoveEvent(QDragMoveEvent *event)
288 if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
289 event->acceptProposedAction();
297 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
299 QTreeWidgetItem *item = itemAt(event->pos());
300 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
303 #include "effectslistwidget.moc"