]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
Clip cuts in project tree can now have a description
[kdenlive] / src / effectslistwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include "effectslistwidget.h"
22 #include "effectslist.h"
23 #include "mainwindow.h"
24
25 #include "KDebug"
26
27 #include "QApplication"
28 #include "QMouseEvent"
29 #include <QMenu>
30
31
32 static const int EFFECT_VIDEO = 1;
33 static const int EFFECT_AUDIO = 2;
34 static const int EFFECT_CUSTOM = 3;
35 static const int EFFECT_FOLDER = 4;
36
37 const int TypeRole = Qt::UserRole;
38 const int IdRole = TypeRole + 1;
39
40 EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent) :
41         QTreeWidget(parent),
42         m_menu(menu)
43 {
44     //setSelectionMode(QAbstractItemView::ExtendedSelection);
45     //setDragDropMode(QAbstractItemView::DragDrop);
46     setColumnCount(1);
47     setDropIndicatorShown(true);
48     setAlternatingRowColors(true);
49     setDragEnabled(true);
50     setAcceptDrops(true);
51     setHeaderHidden(true);
52     initList();
53 }
54
55 EffectsListWidget::~EffectsListWidget()
56 {
57 }
58
59 void EffectsListWidget::initList()
60 {
61     clear();
62     QTreeWidgetItem *item;
63     QTreeWidgetItem *parentItem;
64     QString effectName;
65     QStringList effectInfo;
66     KIcon videoIcon("kdenlive-show-video");
67     KIcon audioIcon("kdenlive-show-audio");
68     KIcon customIcon("kdenlive-custom-effect");
69     KIcon folderIcon("folder");
70
71     KSharedConfigPtr config = KSharedConfig::openConfig("kdenliveeffectscategoryrc");
72     KConfigGroup transConfig(config, "Category");
73     // read the entries
74     QMap< QString, QString > profiles = transConfig.entryMap();
75     QMapIterator<QString, QString> i(profiles);
76     QList <QTreeWidgetItem *> folders;
77     while (i.hasNext()) {
78         i.next();
79         item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n(i.key().toUtf8().data())));
80         item->setIcon(0, folderIcon);
81         item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
82         item->setData(0, IdRole, i.value());
83         folders.append(item);
84     }
85     QTreeWidgetItem *misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
86     misc->setIcon(0, folderIcon);
87     misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
88
89     QTreeWidgetItem *audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
90     audio->setIcon(0, folderIcon);
91     audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
92
93     QTreeWidgetItem *custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
94     custom->setIcon(0, folderIcon);
95     custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
96
97     insertTopLevelItem(0, custom);
98     insertTopLevelItem(0, misc);
99     insertTopLevelItem(0, audio);
100     insertTopLevelItems(0, folders);
101
102
103     int ct = MainWindow::videoEffects.count();
104     for (int ix = 0; ix < ct; ix ++) {
105         effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
106         parentItem = NULL;
107         for (int i = 0; i < folders.count(); i++) {
108             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
109             if (l.contains(effectInfo.at(2))) {
110                 parentItem = folders.at(i);
111                 break;
112             }
113         }
114         if (parentItem == NULL) parentItem = misc;
115         if (!effectInfo.isEmpty()) {
116             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
117             item->setIcon(0, videoIcon);
118             item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
119             item->setData(0, IdRole, effectInfo);
120         }
121     }
122
123     ct = MainWindow::audioEffects.count();
124     for (int ix = 0; ix < ct; ix ++) {
125         effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
126         parentItem = NULL;
127         for (int i = 0; i < folders.count(); i++) {
128             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
129             if (l.contains(effectInfo.at(2))) {
130                 parentItem = folders.at(i);
131                 break;
132             }
133         }
134         if (parentItem == NULL) parentItem = audio;
135         if (!effectInfo.isEmpty()) {
136             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
137             item->setIcon(0, audioIcon);
138             item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
139             item->setData(0, IdRole, effectInfo);
140         }
141     }
142
143     ct = MainWindow::customEffects.count();
144     for (int ix = 0; ix < ct; ix ++) {
145         effectInfo = MainWindow::customEffects.effectIdInfo(ix);
146         if (!effectInfo.isEmpty()) {
147             item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
148             item->setIcon(0, customIcon);
149             item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
150             item->setData(0, IdRole, effectInfo);
151         }
152     }
153     setSortingEnabled(true);
154     sortByColumn(0, Qt::AscendingOrder);
155 }
156
157 const QDomElement EffectsListWidget::currentEffect() const
158 {
159     return itemEffect(currentItem());
160 }
161
162 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
163 {
164     QDomElement effect;
165     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
166     QStringList effectInfo = item->data(0, IdRole).toStringList();
167     kDebug() << "// EFFECT SELECTED: " << effectInfo;
168     switch (item->data(0, TypeRole).toInt()) {
169     case 1:
170         effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
171         break;
172     case 2:
173         effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
174         break;
175     default:
176         effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
177         break;
178     }
179     return effect;
180 }
181
182
183 QString EffectsListWidget::currentInfo()
184 {
185     QTreeWidgetItem *item = currentItem();
186     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
187     QString info;
188     QStringList effectInfo = item->data(0, IdRole).toStringList();
189     switch (item->data(0, TypeRole).toInt()) {
190     case 1:
191         info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
192         break;
193     case 2:
194         info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
195         break;
196     default:
197         info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
198         break;
199     }
200     return info;
201 }
202
203 // virtual
204 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
205 {
206     if (event->button() == Qt::LeftButton) {
207         m_DragStartPosition = event->pos();
208         m_dragStarted = true;
209     }
210     QTreeWidget::mousePressEvent(event);
211 }
212
213 // virtual
214 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
215 {
216     if (!m_dragStarted) return;
217     if ((event->pos() - m_DragStartPosition).manhattanLength()
218             < QApplication::startDragDistance())
219         return;
220
221     {
222         QTreeWidgetItem *clickItem = itemAt(event->pos());
223         if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) {
224             QDrag *drag = new QDrag(this);
225             QMimeData *mimeData = new QMimeData;
226             const QList <QTreeWidgetItem *>list = selectedItems();
227             QDomDocument doc;
228             foreach(QTreeWidgetItem *item, list) {
229                 const QDomElement e = itemEffect(item);
230                 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
231             }
232             QByteArray data;
233             data.append(doc.toString().toUtf8());
234             mimeData->setData("kdenlive/effectslist", data);
235             drag->setMimeData(mimeData);
236             //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
237             //drag->setPixmap(pix);
238             //drag->setHotSpot(QPoint(0, 50));
239             drag->start(Qt::CopyAction);
240         }
241         event->accept();
242     }
243 }
244
245 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
246 {
247     event->setDropAction(Qt::CopyAction);
248     if (event->mimeData()->hasText()) {
249         event->acceptProposedAction();
250     }
251     //}
252 }
253
254 //virtual
255 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
256 {
257     QTreeWidgetItem *item = itemAt(event->pos());
258     if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
259 }
260
261 #include "effectslistwidget.moc"