]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
cleanup
[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 EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent)
35         : KListWidget(parent), m_menu(menu) {
36     //setSelectionMode(QAbstractItemView::ExtendedSelection);
37     //setDragDropMode(QAbstractItemView::DragDrop);
38     setDropIndicatorShown(true);
39     setAlternatingRowColors(true);
40     setSortingEnabled(true);
41     setDragEnabled(true);
42     setAcceptDrops(true);
43     initList();
44 }
45
46 EffectsListWidget::~EffectsListWidget() {
47 }
48
49 void EffectsListWidget::initList() {
50     clear();
51     QStringList names = MainWindow::videoEffects.effectNames();
52     QListWidgetItem *item;
53     foreach(const QString &str, names) {
54         item = new QListWidgetItem(str, this);
55         item->setData(Qt::UserRole, QString::number((int) EFFECT_VIDEO));
56     }
57
58     names = MainWindow::audioEffects.effectNames();
59     foreach(const QString &str, names) {
60         item = new QListWidgetItem(str, this);
61         item->setData(Qt::UserRole, QString::number((int) EFFECT_AUDIO));
62     }
63
64     names = MainWindow::customEffects.effectNames();
65     foreach(const QString &str, names) {
66         item = new QListWidgetItem(str, this);
67         item->setData(Qt::UserRole, QString::number((int) EFFECT_CUSTOM));
68     }
69 }
70
71 QDomElement EffectsListWidget::currentEffect() {
72     return itemEffect(currentItem());
73 }
74
75 QDomElement EffectsListWidget::itemEffect(QListWidgetItem *item) {
76     QDomElement effect;
77     if (!item) return effect;
78     switch (item->data(Qt::UserRole).toInt()) {
79     case 1:
80         effect =  MainWindow::videoEffects.getEffectByName(item->text());
81         break;
82     case 2:
83         effect = MainWindow::audioEffects.getEffectByName(item->text());
84         break;
85     default:
86         effect = MainWindow::customEffects.getEffectByName(item->text());
87         break;
88     }
89     return effect;
90 }
91
92
93 QString EffectsListWidget::currentInfo() {
94     QListWidgetItem *item = currentItem();
95     if (!item) return QString();
96     QString info;
97     switch (item->data(Qt::UserRole).toInt()) {
98     case 1:
99         info = MainWindow::videoEffects.getInfo(item->text());
100         break;
101     case 2:
102         info = MainWindow::audioEffects.getInfo(item->text());
103         break;
104     default:
105         info = MainWindow::customEffects.getInfo(item->text());
106         break;
107     }
108     return info;
109 }
110
111 // virtual
112 void EffectsListWidget::mousePressEvent(QMouseEvent *event) {
113     if (event->button() == Qt::LeftButton) {
114         this->m_DragStartPosition = event->pos();
115         m_dragStarted = true;
116     }
117     KListWidget::mousePressEvent(event);
118 }
119
120 // virtual
121 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event) {
122     if (!m_dragStarted) return;
123     if ((event->pos() - m_DragStartPosition).manhattanLength()
124             < QApplication::startDragDistance())
125         return;
126
127     {
128         QListWidgetItem *clickItem = itemAt(event->pos());
129         if (clickItem) {
130             QDrag *drag = new QDrag(this);
131             QMimeData *mimeData = new QMimeData;
132             const QList <QListWidgetItem *>list = selectedItems();
133             QDomDocument doc;
134             foreach(QListWidgetItem *item, list) {
135                 doc.appendChild(doc.importNode(itemEffect(item), true));
136             }
137             QByteArray data;
138             data.append(doc.toString().toUtf8());
139             mimeData->setData("kdenlive/effectslist", data);
140             drag->setMimeData(mimeData);
141             //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
142             //drag->setPixmap(pix);
143             //drag->setHotSpot(QPoint(0, 50));
144             drag->start(Qt::MoveAction);
145         }
146         //event->accept();
147     }
148 }
149
150 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event) {
151     event->setDropAction(Qt::IgnoreAction);
152     //if (item) {
153     event->setDropAction(Qt::MoveAction);
154     if (event->mimeData()->hasText()) {
155         event->acceptProposedAction();
156     }
157     //}
158 }
159
160 //virtual
161 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event) {
162     QListWidgetItem *item = itemAt(event->pos());
163     if (item && item->data(Qt::UserRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
164 }
165
166 #include "effectslistwidget.moc"