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