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;
76 QTreeWidgetItem *parentItem;
80 current = currentItem()->text(0);
81 if (currentItem()->parent()) currentFolder = currentItem()->parent()->text(0);
82 else if (currentItem()->data(0, TypeRole) == EFFECT_FOLDER) currentFolder = currentItem()->text(0);
86 QStringList effectInfo;
87 KIcon videoIcon("kdenlive-show-video");
88 KIcon audioIcon("kdenlive-show-audio");
89 KIcon customIcon("kdenlive-custom-effect");
90 KIcon folderIcon("folder");
92 QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
94 QFile file(effectCategory);
95 doc.setContent(&file, false);
97 QList <QTreeWidgetItem *> folders;
98 QStringList folderNames;
99 QDomNodeList groups = doc.documentElement().elementsByTagName("group");
100 for (int i = 0; i < groups.count(); i++) {
101 folderNames << groups.at(i).firstChild().firstChild().nodeValue();
103 for (int i = 0; i < topLevelItemCount(); i++) {
104 topLevelItem(i)->takeChildren();
105 QString currentName = topLevelItem(i)->text(0);
106 if (currentName != i18n("Misc") && currentName != i18n("Audio") && currentName != i18nc("Folder Name", "Custom") && !folderNames.contains(currentName)) {
112 for (int i = 0; i < groups.count(); i++) {
113 item = findFolder(folderNames.at(i));
115 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
117 item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(folderNames.at(i)));
118 item->setIcon(0, folderIcon);
119 item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
120 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
121 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
122 insertTopLevelItem(0, item);
124 folders.append(item);
127 QTreeWidgetItem *misc = findFolder(i18n("Misc"));
129 misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
130 misc->setIcon(0, folderIcon);
131 misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
132 misc->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
133 insertTopLevelItem(0, misc);
136 QTreeWidgetItem *audio = findFolder(i18n("Audio"));
138 audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
139 audio->setIcon(0, folderIcon);
140 audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
141 audio->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
142 insertTopLevelItem(0, audio);
145 QTreeWidgetItem *custom = findFolder(i18nc("Folder Name", "Custom"));
146 if (custom == NULL) {
147 custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18nc("Folder Name", "Custom")));
148 custom->setIcon(0, folderIcon);
149 custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
150 custom->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
151 insertTopLevelItem(0, custom);
154 //insertTopLevelItems(0, folders);
157 int ct = MainWindow::videoEffects.count();
158 for (int ix = 0; ix < ct; ix ++) {
159 effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
161 for (int i = 0; i < folders.count(); i++) {
162 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
163 if (l.contains(effectInfo.at(2))) {
164 parentItem = folders.at(i);
168 if (parentItem == NULL) parentItem = misc;
169 if (!effectInfo.isEmpty()) {
170 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
171 item->setIcon(0, videoIcon);
172 item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
173 item->setData(0, IdRole, effectInfo);
174 if (item->text(0) == current) {
175 setCurrentItem(item);
181 ct = MainWindow::audioEffects.count();
182 for (int ix = 0; ix < ct; ix ++) {
183 effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
185 for (int i = 0; i < folders.count(); i++) {
186 QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
187 if (l.contains(effectInfo.at(2))) {
188 parentItem = folders.at(i);
192 if (parentItem == NULL) parentItem = audio;
193 if (!effectInfo.isEmpty()) {
194 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
195 item->setIcon(0, audioIcon);
196 item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
197 item->setData(0, IdRole, effectInfo);
198 if (item->text(0) == current) {
199 setCurrentItem(item);
205 ct = MainWindow::customEffects.count();
206 kDebug() << "--- REBUILDING;: " << ct;
207 for (int ix = 0; ix < ct; ix ++) {
208 effectInfo = MainWindow::customEffects.effectIdInfo(ix);
209 if (!effectInfo.isEmpty()) {
210 item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
211 item->setIcon(0, customIcon);
212 item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
213 item->setData(0, IdRole, effectInfo);
214 if (item->text(0) == current) {
215 setCurrentItem(item);
220 if (!found && !currentFolder.isEmpty()) {
221 // previously selected effect was removed, focus on its parent folder
222 for (int i = 0; i < topLevelItemCount(); i++) {
223 if (topLevelItem(i)->text(0) == currentFolder) {
224 setCurrentItem(topLevelItem(i));
230 setSortingEnabled(true);
231 sortByColumn(0, Qt::AscendingOrder);
234 QTreeWidgetItem *EffectsListWidget::findFolder(const QString name)
236 QTreeWidgetItem *item = NULL;
237 QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
238 if (!result.isEmpty()) {
239 for (int j = 0; j < result.count(); j++) {
240 if (result.at(j)->data(0, TypeRole) == EFFECT_FOLDER) {
249 const QDomElement EffectsListWidget::currentEffect() const
251 return itemEffect(currentItem());
254 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
257 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
258 QStringList effectInfo = item->data(0, IdRole).toStringList();
259 kDebug() << "// EFFECT SELECTED: " << effectInfo;
260 switch (item->data(0, TypeRole).toInt()) {
262 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
265 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
268 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
275 QString EffectsListWidget::currentInfo()
277 QTreeWidgetItem *item = currentItem();
278 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
280 QStringList effectInfo = item->data(0, IdRole).toStringList();
281 switch (item->data(0, TypeRole).toInt()) {
283 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
286 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
289 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
296 QMimeData * EffectsListWidget::mimeData(const QList<QTreeWidgetItem *> list) const
299 foreach(QTreeWidgetItem *item, list) {
300 if (item->flags() & Qt::ItemIsDragEnabled) {
301 const QDomElement e = itemEffect(item);
302 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
305 QMimeData *mime = new QMimeData;
307 data.append(doc.toString().toUtf8());
308 mime->setData("kdenlive/effectslist", data);
313 void EffectsListWidget::dragMoveEvent(QDragMoveEvent *event)
315 if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
316 event->acceptProposedAction();
324 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
326 QTreeWidgetItem *item = itemAt(event->pos());
327 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
330 #include "effectslistwidget.moc"