]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
Remember render settings for project (zone, guides):
[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     QString current;
76     QString currentFolder;
77     QTreeWidgetItem *item;
78     QTreeWidgetItem *parentItem;
79     bool found = false;
80
81     if (currentItem()) {
82         current = currentItem()->text(0);
83         if (currentItem()->parent()) currentFolder = currentItem()->parent()->text(0);
84     }
85
86     QString effectName;
87     QStringList effectInfo;
88     KIcon videoIcon("kdenlive-show-video");
89     KIcon audioIcon("kdenlive-show-audio");
90     KIcon customIcon("kdenlive-custom-effect");
91     KIcon folderIcon("folder");
92
93     QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
94     QDomDocument doc;
95     QFile file(effectCategory);
96     doc.setContent(&file, false);
97     file.close();
98     QList <QTreeWidgetItem *> folders;
99     QStringList folderNames;
100     QDomNodeList groups = doc.documentElement().elementsByTagName("group");
101     for (int i = 0; i < groups.count(); i++) {
102         folderNames << groups.at(i).firstChild().firstChild().nodeValue();
103     }
104     for (int i = 0; i < topLevelItemCount(); i++) {
105         topLevelItem(i)->takeChildren();
106         if (!folderNames.contains(topLevelItem(i)->text(0))) {
107             takeTopLevelItem(i);
108             i--;
109         }
110     }
111
112     for (int i = 0; i < groups.count(); i++) {
113         QList<QTreeWidgetItem *> result = findItems(folderNames.at(i), Qt::MatchExactly);
114         if (!result.isEmpty()) item = result.at(0);
115         else item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(folderNames.at(i)));
116         item->setIcon(0, folderIcon);
117         item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
118         item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
119         folders.append(item);
120     }
121
122     QTreeWidgetItem *misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
123     misc->setIcon(0, folderIcon);
124     misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
125
126     QTreeWidgetItem *audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
127     audio->setIcon(0, folderIcon);
128     audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
129
130     QTreeWidgetItem *custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
131     custom->setIcon(0, folderIcon);
132     custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
133
134     insertTopLevelItem(0, custom);
135     insertTopLevelItem(0, misc);
136     insertTopLevelItem(0, audio);
137     insertTopLevelItems(0, folders);
138
139
140     int ct = MainWindow::videoEffects.count();
141     for (int ix = 0; ix < ct; ix ++) {
142         effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
143         parentItem = NULL;
144         for (int i = 0; i < folders.count(); i++) {
145             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
146             if (l.contains(effectInfo.at(2))) {
147                 parentItem = folders.at(i);
148                 break;
149             }
150         }
151         if (parentItem == NULL) parentItem = misc;
152         if (!effectInfo.isEmpty()) {
153             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
154             item->setIcon(0, videoIcon);
155             item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
156             item->setData(0, IdRole, effectInfo);
157             if (item->text(0) == current) {
158                 setCurrentItem(item);
159                 found = true;
160             }
161         }
162     }
163
164     ct = MainWindow::audioEffects.count();
165     for (int ix = 0; ix < ct; ix ++) {
166         effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
167         parentItem = NULL;
168         for (int i = 0; i < folders.count(); i++) {
169             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
170             if (l.contains(effectInfo.at(2))) {
171                 parentItem = folders.at(i);
172                 break;
173             }
174         }
175         if (parentItem == NULL) parentItem = audio;
176         if (!effectInfo.isEmpty()) {
177             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
178             item->setIcon(0, audioIcon);
179             item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
180             item->setData(0, IdRole, effectInfo);
181             if (item->text(0) == current) {
182                 setCurrentItem(item);
183                 found = true;
184             }
185         }
186     }
187
188     ct = MainWindow::customEffects.count();
189     kDebug() << "--- REBUILDING;: " << ct;
190     for (int ix = 0; ix < ct; ix ++) {
191         effectInfo = MainWindow::customEffects.effectIdInfo(ix);
192         if (!effectInfo.isEmpty()) {
193             item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
194             item->setIcon(0, customIcon);
195             item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
196             item->setData(0, IdRole, effectInfo);
197             if (item->text(0) == current) {
198                 setCurrentItem(item);
199                 found = true;
200             }
201         }
202     }
203     if (!found && !currentFolder.isEmpty()) {
204         // previously selected effect was removed, focus on its parent folder
205         for (int i = 0; i < topLevelItemCount(); i++) {
206             if (topLevelItem(i)->text(0) == currentFolder) {
207                 setCurrentItem(topLevelItem(i));
208                 topLevelItem(i)->setExpanded(true);
209                 break;
210             }
211         }
212
213     }
214     setSortingEnabled(true);
215     sortByColumn(0, Qt::AscendingOrder);
216 }
217
218 const QDomElement EffectsListWidget::currentEffect() const
219 {
220     return itemEffect(currentItem());
221 }
222
223 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
224 {
225     QDomElement effect;
226     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
227     QStringList effectInfo = item->data(0, IdRole).toStringList();
228     kDebug() << "// EFFECT SELECTED: " << effectInfo;
229     switch (item->data(0, TypeRole).toInt()) {
230     case 1:
231         effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
232         break;
233     case 2:
234         effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
235         break;
236     default:
237         effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
238         break;
239     }
240     return effect;
241 }
242
243
244 QString EffectsListWidget::currentInfo()
245 {
246     QTreeWidgetItem *item = currentItem();
247     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
248     QString info;
249     QStringList effectInfo = item->data(0, IdRole).toStringList();
250     switch (item->data(0, TypeRole).toInt()) {
251     case 1:
252         info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
253         break;
254     case 2:
255         info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
256         break;
257     default:
258         info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
259         break;
260     }
261     return info;
262 }
263
264 // virtual
265 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
266 {
267     if (event->button() == Qt::LeftButton) {
268         m_DragStartPosition = event->pos();
269         m_dragStarted = true;
270     }
271     QTreeWidget::mousePressEvent(event);
272 }
273
274 // virtual
275 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
276 {
277     if (!m_dragStarted) return;
278     if ((event->pos() - m_DragStartPosition).manhattanLength()
279             < QApplication::startDragDistance())
280         return;
281
282     {
283         QTreeWidgetItem *clickItem = itemAt(event->pos());
284         if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) {
285             QDrag *drag = new QDrag(this);
286             QMimeData *mimeData = new QMimeData;
287             const QList <QTreeWidgetItem *>list = selectedItems();
288             QDomDocument doc;
289             foreach(QTreeWidgetItem *item, list) {
290                 const QDomElement e = itemEffect(item);
291                 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
292             }
293             QByteArray data;
294             data.append(doc.toString().toUtf8());
295             mimeData->setData("kdenlive/effectslist", data);
296             drag->setMimeData(mimeData);
297             //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
298             //drag->setPixmap(pix);
299             //drag->setHotSpot(QPoint(0, 50));
300             drag->start(Qt::CopyAction);
301         }
302         event->accept();
303     }
304 }
305
306 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
307 {
308     event->setDropAction(Qt::CopyAction);
309     if (event->mimeData()->hasText()) {
310         event->acceptProposedAction();
311     }
312     //}
313 }
314
315 //virtual
316 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
317 {
318     QTreeWidgetItem *item = itemAt(event->pos());
319     if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
320 }
321
322 #include "effectslistwidget.moc"