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>
29 #include <QApplication>
30 #include <QMouseEvent>
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;
39 const int TypeRole = Qt::UserRole;
40 const int IdRole = TypeRole + 1;
43 EffectsListWidget::EffectsListWidget(QMenu *contextMenu, QWidget *parent) :
49 setAcceptDrops(false);
50 setHeaderHidden(true);
51 setFrameShape(QFrame::NoFrame);
52 setAutoFillBackground(false);
53 setRootIsDecorated(false);
54 //setSelectionMode(QAbstractItemView::ExtendedSelection);
55 setDragDropMode(QAbstractItemView::DragOnly);
56 QPalette p = palette();
57 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(QMenu *effectsMenu, KActionCategory *effectActions)
74 QString currentFolder;
75 QTreeWidgetItem *item = NULL;
80 current = currentItem()->text(0);
81 if (currentItem()->parent())
82 currentFolder = currentItem()->parent()->text(0);
83 else if (currentItem()->data(0, TypeRole) == EFFECT_FOLDER)
84 currentFolder = currentItem()->text(0);
87 KIcon folderIcon("folder");
89 QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
91 QFile file(effectCategory);
92 doc.setContent(&file, false);
94 QList <QTreeWidgetItem *> folders;
95 QStringList folderNames;
96 QDomNodeList groups = doc.documentElement().elementsByTagName("group");
97 for (int i = 0; i < groups.count(); i++) {
98 folderNames << i18n(groups.at(i).firstChild().firstChild().nodeValue().toUtf8().constData());
100 for (int i = 0; i < topLevelItemCount(); i++) {
101 topLevelItem(i)->takeChildren();
102 QString currentName = topLevelItem(i)->text(0);
103 if (currentName != i18n("Misc") && currentName != i18n("Audio") && currentName != i18nc("Folder Name", "Custom") && !folderNames.contains(currentName)) {
109 for (int i = 0; i < groups.count(); i++) {
110 item = findFolder(folderNames.at(i));
112 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
114 item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(folderNames.at(i)));
115 item->setIcon(0, folderIcon);
116 item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
117 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
118 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
119 insertTopLevelItem(0, item);
121 folders.append(item);
124 QTreeWidgetItem *misc = findFolder(i18n("Misc"));
126 misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
127 misc->setIcon(0, folderIcon);
128 misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
129 misc->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
130 insertTopLevelItem(0, misc);
133 QTreeWidgetItem *audio = findFolder(i18n("Audio"));
135 audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
136 audio->setIcon(0, folderIcon);
137 audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
138 audio->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
139 insertTopLevelItem(0, audio);
142 QTreeWidgetItem *custom = findFolder(i18nc("Folder Name", "Custom"));
143 if (custom == NULL) {
144 custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18nc("Folder Name", "Custom")));
145 custom->setIcon(0, folderIcon);
146 custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
147 custom->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
148 insertTopLevelItem(0, custom);
151 //insertTopLevelItems(0, folders);
153 loadEffects(&MainWindow::videoEffects, KIcon("kdenlive-show-video"), misc, &folders, QString::number((int) EFFECT_VIDEO), current, &found);
154 loadEffects(&MainWindow::audioEffects, KIcon("kdenlive-show-audio"), audio, &folders, QString::number((int) EFFECT_AUDIO), current, &found);
155 loadEffects(&MainWindow::customEffects, KIcon("kdenlive-custom-effect"), custom, static_cast<QList<QTreeWidgetItem *> *>(0), QString::number((int) EFFECT_CUSTOM), current, &found);
157 if (!found && !currentFolder.isEmpty()) {
158 // previously selected effect was removed, focus on its parent folder
159 for (int i = 0; i < topLevelItemCount(); i++) {
160 if (topLevelItem(i)->text(0) == currentFolder) {
161 setCurrentItem(topLevelItem(i));
167 setSortingEnabled(true);
168 sortByColumn(0, Qt::AscendingOrder);
170 // populate effects menu
171 for (int i = 0; i < topLevelItemCount(); i++) {
172 QMenu *sub = new QMenu(topLevelItem(i)->text(0), effectsMenu);
173 effectsMenu->addMenu(sub);
174 for (int j = 0; j < topLevelItem(i)->childCount(); j++) {
175 QTreeWidgetItem *item = topLevelItem(i)->child(j);
176 KAction *a = new KAction(KIcon(item->icon(0)), item->text(0), sub);
177 a->setData(item->data(0, IdRole));
178 a->setIconVisibleInMenu(false);
180 effectActions->addAction("video_effect_" + item->text(0), a);
185 void EffectsListWidget::loadEffects(const EffectsList *effectlist, KIcon icon, QTreeWidgetItem *defaultFolder, const QList<QTreeWidgetItem *> *folders, const QString type, const QString current, bool *found)
187 QStringList effectInfo, l;
188 QTreeWidgetItem *parentItem;
189 QTreeWidgetItem *item;
190 int ct = effectlist->count();
192 for (int ix = 0; ix < ct; ix ++) {
193 effectInfo = effectlist->effectIdInfo(ix);
194 effectInfo.append(type);
198 for (int i = 0; i < folders->count(); i++) {
199 l = folders->at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
200 if (l.contains(effectInfo.at(2))) {
201 parentItem = folders->at(i);
206 if (parentItem == NULL)
207 parentItem = defaultFolder;
209 if (!effectInfo.isEmpty()) {
210 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
211 item->setIcon(0, icon);
212 item->setData(0, TypeRole, type);
213 item->setData(0, IdRole, effectInfo);
214 item->setToolTip(0, effectlist->getInfo(effectInfo.at(0), effectInfo.at(1)));
215 if (item->text(0) == current) {
216 setCurrentItem(item);
223 QTreeWidgetItem *EffectsListWidget::findFolder(const QString name)
225 QTreeWidgetItem *item = NULL;
226 QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
227 if (!result.isEmpty()) {
228 for (int j = 0; j < result.count(); j++) {
229 if (result.at(j)->data(0, TypeRole) == EFFECT_FOLDER) {
238 const QDomElement EffectsListWidget::currentEffect() const
240 return itemEffect(currentItem());
243 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
246 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
247 QStringList effectInfo = item->data(0, IdRole).toStringList();
248 kDebug() << "// EFFECT SELECTED: " << effectInfo;
249 switch (item->data(0, TypeRole).toInt()) {
251 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
254 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
257 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
264 QString EffectsListWidget::currentInfo()
266 QTreeWidgetItem *item = currentItem();
267 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
269 QStringList effectInfo = item->data(0, IdRole).toStringList();
270 switch (item->data(0, TypeRole).toInt()) {
272 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
275 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
278 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
285 QMimeData * EffectsListWidget::mimeData(const QList<QTreeWidgetItem *> list) const
288 foreach(QTreeWidgetItem *item, list) {
289 if (item->flags() & Qt::ItemIsDragEnabled) {
290 const QDomElement e = itemEffect(item);
291 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
294 QMimeData *mime = new QMimeData;
296 data.append(doc.toString().toUtf8());
297 mime->setData("kdenlive/effectslist", data);
302 void EffectsListWidget::dragMoveEvent(QDragMoveEvent *event)
304 if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
305 event->acceptProposedAction();
313 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
315 QTreeWidgetItem *item = itemAt(event->pos());
316 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
319 #include "effectslistwidget.moc"