]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
Add icon for custom effects
[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
36 const int TypeRole = Qt::UserRole;
37 const int IdRole = TypeRole + 1;
38
39 EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent) :
40         KListWidget(parent),
41         m_menu(menu)
42 {
43     //setSelectionMode(QAbstractItemView::ExtendedSelection);
44     //setDragDropMode(QAbstractItemView::DragDrop);
45     setDropIndicatorShown(true);
46     setAlternatingRowColors(true);
47     setSortingEnabled(true);
48     setDragEnabled(true);
49     setAcceptDrops(true);
50     initList();
51 }
52
53 EffectsListWidget::~EffectsListWidget()
54 {
55 }
56
57 void EffectsListWidget::initList()
58 {
59     clear();
60     QListWidgetItem *item;
61     QString effectName;
62     QStringList effectInfo;
63     KIcon videoIcon("kdenlive-show-video");
64     KIcon audioIcon("kdenlive-show-audio");
65     KIcon customIcon("kdenlive-custom-effect");
66     int ct = MainWindow::videoEffects.count();
67     for (int ix = 0; ix < ct; ix ++) {
68         effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
69         if (!effectInfo.isEmpty()) {
70             item = new QListWidgetItem(videoIcon, effectInfo.takeFirst(), this);
71             item->setData(TypeRole, QString::number((int) EFFECT_VIDEO));
72             item->setData(IdRole, effectInfo);
73         }
74     }
75
76     ct = MainWindow::audioEffects.count();
77     for (int ix = 0; ix < ct; ix ++) {
78         effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
79         if (!effectInfo.isEmpty()) {
80             item = new QListWidgetItem(audioIcon, effectInfo.takeFirst(), this);
81             item->setData(TypeRole, QString::number((int) EFFECT_AUDIO));
82             item->setData(IdRole, effectInfo);
83         }
84     }
85
86     ct = MainWindow::customEffects.count();
87     for (int ix = 0; ix < ct; ix ++) {
88         effectInfo = MainWindow::customEffects.effectIdInfo(ix);
89         if (!effectInfo.isEmpty()) {
90             item = new QListWidgetItem(customIcon, effectInfo.takeFirst(), this);
91             item->setData(TypeRole, QString::number((int) EFFECT_CUSTOM));
92             item->setData(IdRole, effectInfo);
93         }
94     }
95 }
96
97 const QDomElement EffectsListWidget::currentEffect() const
98 {
99     return itemEffect(currentItem());
100 }
101
102 const QDomElement EffectsListWidget::itemEffect(QListWidgetItem *item) const
103 {
104     QDomElement effect;
105     if (!item) return effect;
106     QStringList effectInfo = item->data(IdRole).toStringList();
107     kDebug() << "// EFFECT SELECTED: " << effectInfo;
108     switch (item->data(TypeRole).toInt()) {
109     case 1:
110         effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
111         break;
112     case 2:
113         effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
114         break;
115     default:
116         effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
117         break;
118     }
119     return effect;
120 }
121
122
123 QString EffectsListWidget::currentInfo()
124 {
125     QListWidgetItem *item = currentItem();
126     if (!item) return QString();
127     QString info;
128     QStringList effectInfo = item->data(IdRole).toStringList();
129     switch (item->data(TypeRole).toInt()) {
130     case 1:
131         info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
132         break;
133     case 2:
134         info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
135         break;
136     default:
137         info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
138         break;
139     }
140     return info;
141 }
142
143 // virtual
144 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
145 {
146     if (event->button() == Qt::LeftButton) {
147         m_DragStartPosition = event->pos();
148         m_dragStarted = true;
149     }
150     KListWidget::mousePressEvent(event);
151 }
152
153 // virtual
154 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
155 {
156     if (!m_dragStarted) return;
157     if ((event->pos() - m_DragStartPosition).manhattanLength()
158             < QApplication::startDragDistance())
159         return;
160
161     {
162         QListWidgetItem *clickItem = itemAt(event->pos());
163         if (clickItem) {
164             QDrag *drag = new QDrag(this);
165             QMimeData *mimeData = new QMimeData;
166             const QList <QListWidgetItem *>list = selectedItems();
167             QDomDocument doc;
168             foreach(QListWidgetItem *item, list) {
169                 const QDomElement e = itemEffect(item);
170                 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
171             }
172             QByteArray data;
173             data.append(doc.toString().toUtf8());
174             mimeData->setData("kdenlive/effectslist", data);
175             drag->setMimeData(mimeData);
176             //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
177             //drag->setPixmap(pix);
178             //drag->setHotSpot(QPoint(0, 50));
179             drag->start(Qt::CopyAction);
180         }
181         event->accept();
182     }
183 }
184
185 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
186 {
187     event->setDropAction(Qt::CopyAction);
188     if (event->mimeData()->hasText()) {
189         event->acceptProposedAction();
190     }
191     //}
192 }
193
194 //virtual
195 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
196 {
197     QListWidgetItem *item = itemAt(event->pos());
198     if (item && item->data(TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
199 }
200
201 #include "effectslistwidget.moc"