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(true);
55 //setSelectionMode(QAbstractItemView::ExtendedSelection);
56 setDragDropMode(QAbstractItemView::DragOnly);
57 QPalette p = palette();
58 p.setBrush(QPalette::Base, Qt::NoBrush);
60 connect(this, SIGNAL(activated(const QModelIndex &)), this, SLOT(slotExpandItem(const QModelIndex &)));
63 EffectsListWidget::~EffectsListWidget()
67 void EffectsListWidget::slotExpandItem(const QModelIndex & index)
69 setExpanded(index, !isExpanded(index));
72 void EffectsListWidget::initList(QMenu *effectsMenu, KActionCategory *effectActions)
75 QString currentFolder;
76 QTreeWidgetItem *item = NULL;
81 current = currentItem()->text(0);
82 if (currentItem()->parent())
83 currentFolder = currentItem()->parent()->text(0);
84 else if (currentItem()->data(0, TypeRole) == EFFECT_FOLDER)
85 currentFolder = currentItem()->text(0);
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 << i18n(groups.at(i).firstChild().firstChild().nodeValue().toUtf8().constData());
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->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
115 item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
116 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
117 item->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicatorWhenChildless);
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->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
127 misc->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
128 insertTopLevelItem(0, misc);
131 QTreeWidgetItem *audio = findFolder(i18n("Audio"));
133 audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
134 audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
135 audio->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
136 insertTopLevelItem(0, audio);
139 QTreeWidgetItem *custom = findFolder(i18nc("Folder Name", "Custom"));
140 if (custom == NULL) {
141 custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18nc("Folder Name", "Custom")));
142 custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
143 custom->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
144 insertTopLevelItem(0, custom);
147 //insertTopLevelItems(0, folders);
149 loadEffects(&MainWindow::videoEffects, KIcon("kdenlive-show-video"), misc, &folders, EFFECT_VIDEO, current, &found);
150 loadEffects(&MainWindow::audioEffects, KIcon("kdenlive-show-audio"), audio, &folders, EFFECT_AUDIO, current, &found);
151 loadEffects(&MainWindow::customEffects, KIcon("kdenlive-custom-effect"), custom, static_cast<QList<QTreeWidgetItem *> *>(0), EFFECT_CUSTOM, current, &found);
153 if (!found && !currentFolder.isEmpty()) {
154 // previously selected effect was removed, focus on its parent folder
155 for (int i = 0; i < topLevelItemCount(); i++) {
156 if (topLevelItem(i)->text(0) == currentFolder) {
157 setCurrentItem(topLevelItem(i));
163 setSortingEnabled(true);
164 sortByColumn(0, Qt::AscendingOrder);
166 // 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 int effectsInCategory = topLevelItem(i)->childCount();
177 bool hasSubCategories = false;
178 if (effectsInCategory > 60) {
179 // create subcategories if there are too many effects
180 hasSubCategories = true;
181 sub1 = new QMenu(i18nc("menu name for effects names between these 2 letters", "0 - F"), sub);
183 sub2 = new QMenu(i18nc("menu name for effects names between these 2 letters", "G - L"), sub);
185 sub3 = new QMenu(i18nc("menu name for effects names between these 2 letters", "M - R"), sub);
187 sub4 = new QMenu(i18nc("menu name for effects names between these 2 letters", "S - Z"), sub);
190 for (int j = 0; j < effectsInCategory; j++) {
191 QTreeWidgetItem *item = topLevelItem(i)->child(j);
192 KAction *a = new KAction(KIcon(item->icon(0)), item->text(0), sub);
193 QStringList data = item->data(0, IdRole).toStringList();
194 QString id = data.at(1);
195 if (id.isEmpty()) id = data.at(0);
197 a->setIconVisibleInMenu(false);
198 if (hasSubCategories) {
199 // put action in sub category
200 QRegExp rx("^[s-z].+");
201 if (rx.exactMatch(item->text(0).toLower())) {
204 rx.setPattern("^[m-r].+");
205 if (rx.exactMatch(item->text(0).toLower())) {
209 rx.setPattern("^[g-l].+");
210 if (rx.exactMatch(item->text(0).toLower())) {
213 else sub1->addAction(a);
217 else sub->addAction(a);
218 effectActions->addAction("video_effect_" + id, a);
223 void EffectsListWidget::loadEffects(const EffectsList *effectlist, KIcon icon, QTreeWidgetItem *defaultFolder, const QList<QTreeWidgetItem *> *folders, int type, const QString current, bool *found)
225 QStringList effectInfo, l;
226 QTreeWidgetItem *parentItem;
227 QTreeWidgetItem *item;
228 int ct = effectlist->count();
231 for (int ix = 0; ix < ct; ix ++) {
232 effectInfo = effectlist->effectIdInfo(ix);
233 effectInfo.append(QString::number(type));
237 for (int i = 0; i < folders->count(); i++) {
238 l = folders->at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
239 if (l.contains(effectInfo.at(2))) {
240 parentItem = folders->at(i);
245 if (parentItem == NULL)
246 parentItem = defaultFolder;
248 if (!effectInfo.isEmpty()) {
249 item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
250 if (effectInfo.count() == 4) item->setIcon(0, KIcon("folder"));
251 else item->setIcon(0, icon);
252 item->setData(0, TypeRole, type);
253 item->setData(0, IdRole, effectInfo);
254 item->setToolTip(0, effectlist->getInfo(effectInfo.at(0), effectInfo.at(1)));
255 if (item->text(0) == current) {
256 setCurrentItem(item);
263 QTreeWidgetItem *EffectsListWidget::findFolder(const QString name)
265 QTreeWidgetItem *item = NULL;
266 QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
267 if (!result.isEmpty()) {
268 for (int j = 0; j < result.count(); j++) {
269 if (result.at(j)->data(0, TypeRole) == EFFECT_FOLDER) {
278 const QDomElement EffectsListWidget::currentEffect() const
280 return itemEffect(currentItem());
283 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
286 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
287 QStringList effectInfo = item->data(0, IdRole).toStringList();
288 switch (item->data(0, TypeRole).toInt()) {
290 effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
293 effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
296 effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
303 QString EffectsListWidget::currentInfo()
305 QTreeWidgetItem *item = currentItem();
306 if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
308 QStringList effectInfo = item->data(0, IdRole).toStringList();
309 switch (item->data(0, TypeRole).toInt()) {
311 info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
314 info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
317 info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
324 void EffectsListWidget::keyPressEvent(QKeyEvent *e)
326 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
327 emit applyEffect(currentEffect());
331 QTreeWidget::keyPressEvent(e);
335 QMimeData * EffectsListWidget::mimeData(const QList<QTreeWidgetItem *> list) const
338 foreach(QTreeWidgetItem *item, list) {
339 if (item->flags() & Qt::ItemIsDragEnabled) {
340 const QDomElement e = itemEffect(item);
341 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
344 QMimeData *mime = new QMimeData;
346 data.append(doc.toString().toUtf8());
347 mime->setData("kdenlive/effectslist", data);
352 void EffectsListWidget::dragMoveEvent(QDragMoveEvent *event)
354 if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
355 event->acceptProposedAction();
363 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
365 QTreeWidgetItem *item = itemAt(event->pos());
366 if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
369 #include "effectslistwidget.moc"