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