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