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 if (!topLevelItem(i)->childCount())
174 QMenu *sub = new QMenu(topLevelItem(i)->text(0), effectsMenu);
175 effectsMenu->addMenu(sub);
176 for (int j = 0; j < topLevelItem(i)->childCount(); j++) {
177 QTreeWidgetItem *item = topLevelItem(i)->child(j);
178 KAction *a = new KAction(KIcon(item->icon(0)), item->text(0), sub);
179 QStringList data = item->data(0, IdRole).toStringList();
180 QString id = data.at(1);
181 if (id.isEmpty()) id = data.at(0);
183 a->setIconVisibleInMenu(false);
185 effectActions->addAction("video_effect_" + id, a);
190 void EffectsListWidget::loadEffects(const EffectsList *effectlist, KIcon icon, QTreeWidgetItem *defaultFolder, const QList<QTreeWidgetItem *> *folders, const QString type, const QString current, bool *found)
192 QStringList effectInfo, l;
193 QTreeWidgetItem *parentItem;
194 QTreeWidgetItem *item;
195 int ct = effectlist->count();
197 for (int ix = 0; ix < ct; ix ++) {
198 effectInfo = effectlist->effectIdInfo(ix);
199 effectInfo.append(type);
203 for (int i = 0; i < folders->count(); i++) {
204 l = folders->at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
205 if (l.contains(effectInfo.at(2))) {
206 parentItem = folders->at(i);
211 if (parentItem == NULL)
212 parentItem = defaultFolder;
214 if (!effectInfo.isEmpty()) {
215 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
216 item->setIcon(0, icon);
217 item->setData(0, TypeRole, type);
218 item->setData(0, IdRole, effectInfo);
219 item->setToolTip(0, effectlist->getInfo(effectInfo.at(0), effectInfo.at(1)));
220 if (item->text(0) == current) {
221 setCurrentItem(item);
228 QTreeWidgetItem *EffectsListWidget::findFolder(const QString name)
230 QTreeWidgetItem *item = NULL;
231 QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
232 if (!result.isEmpty()) {
233 for (int j = 0; j < result.count(); j++) {
234 if (result.at(j)->data(0, TypeRole) == EFFECT_FOLDER) {
243 const QDomElement EffectsListWidget::currentEffect() const
245 return itemEffect(currentItem());
248 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
251 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
252 QStringList effectInfo = item->data(0, IdRole).toStringList();
253 kDebug() << "// EFFECT SELECTED: " << effectInfo;
254 switch (item->data(0, TypeRole).toInt()) {
256 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
259 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
262 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
269 QString EffectsListWidget::currentInfo()
271 QTreeWidgetItem *item = currentItem();
272 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
274 QStringList effectInfo = item->data(0, IdRole).toStringList();
275 switch (item->data(0, TypeRole).toInt()) {
277 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
280 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
283 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
290 QMimeData * EffectsListWidget::mimeData(const QList<QTreeWidgetItem *> list) const
293 foreach(QTreeWidgetItem *item, list) {
294 if (item->flags() & Qt::ItemIsDragEnabled) {
295 const QDomElement e = itemEffect(item);
296 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
299 QMimeData *mime = new QMimeData;
301 data.append(doc.toString().toUtf8());
302 mime->setData("kdenlive/effectslist", data);
307 void EffectsListWidget::dragMoveEvent(QDragMoveEvent *event)
309 if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
310 event->acceptProposedAction();
318 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
320 QTreeWidgetItem *item = itemAt(event->pos());
321 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
324 #include "effectslistwidget.moc"