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