]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
Prepare checking of removed / deleted files in a project:
[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 #include <KStandardDirs>
27
28 #include <QApplication>
29 #include <QMouseEvent>
30 #include <QMenu>
31
32
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;
37
38 const int TypeRole = Qt::UserRole;
39 const int IdRole = TypeRole + 1;
40
41
42 EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent) :
43         QTreeWidget(parent),
44         m_menu(menu)
45 {
46     //setSelectionMode(QAbstractItemView::ExtendedSelection);
47     //setDragDropMode(QAbstractItemView::DragDrop);
48     setColumnCount(1);
49     setDropIndicatorShown(true);
50     //setAlternatingRowColors(true);
51     setDragEnabled(true);
52     setAcceptDrops(true);
53     setHeaderHidden(true);
54     setFrameShape(QFrame::NoFrame);
55     setAutoFillBackground(false);
56     setRootIsDecorated(false);
57     QPalette p = palette();
58     p.setBrush(QPalette::Base, Qt::NoBrush);
59     setPalette(p);
60     initList();
61     connect(this, SIGNAL(activated(const QModelIndex &)), this, SLOT(slotExpandItem(const QModelIndex &)));
62 }
63
64 EffectsListWidget::~EffectsListWidget()
65 {
66 }
67
68 void EffectsListWidget::slotExpandItem(const QModelIndex & index)
69 {
70     setExpanded(index, !isExpanded(index));
71 }
72
73 void EffectsListWidget::initList()
74 {
75     clear();
76     QTreeWidgetItem *item;
77     QTreeWidgetItem *parentItem;
78     QString effectName;
79     QStringList effectInfo;
80     KIcon videoIcon("kdenlive-show-video");
81     KIcon audioIcon("kdenlive-show-audio");
82     KIcon customIcon("kdenlive-custom-effect");
83     KIcon folderIcon("folder");
84
85     QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
86     QDomDocument doc;
87     QFile file(effectCategory);
88     doc.setContent(&file, false);
89     file.close();
90     QList <QTreeWidgetItem *> folders;
91     QDomNodeList groups = doc.documentElement().elementsByTagName("group");
92     for (int i = 0; i < groups.count(); i++) {
93         item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(groups.at(i).firstChild().firstChild().nodeValue()));
94         item->setIcon(0, folderIcon);
95         item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
96         item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
97         folders.append(item);
98     }
99
100     QTreeWidgetItem *misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
101     misc->setIcon(0, folderIcon);
102     misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
103
104     QTreeWidgetItem *audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
105     audio->setIcon(0, folderIcon);
106     audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
107
108     QTreeWidgetItem *custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
109     custom->setIcon(0, folderIcon);
110     custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
111
112     insertTopLevelItem(0, custom);
113     insertTopLevelItem(0, misc);
114     insertTopLevelItem(0, audio);
115     insertTopLevelItems(0, folders);
116
117
118     int ct = MainWindow::videoEffects.count();
119     for (int ix = 0; ix < ct; ix ++) {
120         effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
121         parentItem = NULL;
122         for (int i = 0; i < folders.count(); i++) {
123             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
124             if (l.contains(effectInfo.at(2))) {
125                 parentItem = folders.at(i);
126                 break;
127             }
128         }
129         if (parentItem == NULL) parentItem = misc;
130         if (!effectInfo.isEmpty()) {
131             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
132             item->setIcon(0, videoIcon);
133             item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
134             item->setData(0, IdRole, effectInfo);
135         }
136     }
137
138     ct = MainWindow::audioEffects.count();
139     for (int ix = 0; ix < ct; ix ++) {
140         effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
141         parentItem = NULL;
142         for (int i = 0; i < folders.count(); i++) {
143             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
144             if (l.contains(effectInfo.at(2))) {
145                 parentItem = folders.at(i);
146                 break;
147             }
148         }
149         if (parentItem == NULL) parentItem = audio;
150         if (!effectInfo.isEmpty()) {
151             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
152             item->setIcon(0, audioIcon);
153             item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
154             item->setData(0, IdRole, effectInfo);
155         }
156     }
157
158     ct = MainWindow::customEffects.count();
159     for (int ix = 0; ix < ct; ix ++) {
160         effectInfo = MainWindow::customEffects.effectIdInfo(ix);
161         if (!effectInfo.isEmpty()) {
162             item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
163             item->setIcon(0, customIcon);
164             item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
165             item->setData(0, IdRole, effectInfo);
166         }
167     }
168     setSortingEnabled(true);
169     sortByColumn(0, Qt::AscendingOrder);
170 }
171
172 const QDomElement EffectsListWidget::currentEffect() const
173 {
174     return itemEffect(currentItem());
175 }
176
177 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
178 {
179     QDomElement effect;
180     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
181     QStringList effectInfo = item->data(0, IdRole).toStringList();
182     kDebug() << "// EFFECT SELECTED: " << effectInfo;
183     switch (item->data(0, TypeRole).toInt()) {
184     case 1:
185         effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
186         break;
187     case 2:
188         effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
189         break;
190     default:
191         effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
192         break;
193     }
194     return effect;
195 }
196
197
198 QString EffectsListWidget::currentInfo()
199 {
200     QTreeWidgetItem *item = currentItem();
201     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
202     QString info;
203     QStringList effectInfo = item->data(0, IdRole).toStringList();
204     switch (item->data(0, TypeRole).toInt()) {
205     case 1:
206         info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
207         break;
208     case 2:
209         info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
210         break;
211     default:
212         info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
213         break;
214     }
215     return info;
216 }
217
218 // virtual
219 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
220 {
221     if (event->button() == Qt::LeftButton) {
222         m_DragStartPosition = event->pos();
223         m_dragStarted = true;
224     }
225     QTreeWidget::mousePressEvent(event);
226 }
227
228 // virtual
229 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
230 {
231     if (!m_dragStarted) return;
232     if ((event->pos() - m_DragStartPosition).manhattanLength()
233             < QApplication::startDragDistance())
234         return;
235
236     {
237         QTreeWidgetItem *clickItem = itemAt(event->pos());
238         if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) {
239             QDrag *drag = new QDrag(this);
240             QMimeData *mimeData = new QMimeData;
241             const QList <QTreeWidgetItem *>list = selectedItems();
242             QDomDocument doc;
243             foreach(QTreeWidgetItem *item, list) {
244                 const QDomElement e = itemEffect(item);
245                 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
246             }
247             QByteArray data;
248             data.append(doc.toString().toUtf8());
249             mimeData->setData("kdenlive/effectslist", data);
250             drag->setMimeData(mimeData);
251             //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
252             //drag->setPixmap(pix);
253             //drag->setHotSpot(QPoint(0, 50));
254             drag->start(Qt::CopyAction);
255         }
256         event->accept();
257     }
258 }
259
260 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
261 {
262     event->setDropAction(Qt::CopyAction);
263     if (event->mimeData()->hasText()) {
264         event->acceptProposedAction();
265     }
266     //}
267 }
268
269 //virtual
270 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
271 {
272     QTreeWidgetItem *item = itemAt(event->pos());
273     if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
274 }
275
276 #include "effectslistwidget.moc"