]> git.sesse.net Git - kdenlive/blob - src/effectslistwidget.cpp
Make effect groups translatable
[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 #include "KStandardDirs"
27
28 #include "QApplication"
29 #include "QMouseEvent"
30 #include <QMenu>
31
32
33 static const int EFFECT_VIDEO = 1;
34 static const int EFFECT_AUDIO = 2;
35 static const int EFFECT_CUSTOM = 3;
36 static const int EFFECT_FOLDER = 4;
37
38 const int TypeRole = Qt::UserRole;
39 const int IdRole = TypeRole + 1;
40
41 EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent) :
42         QTreeWidget(parent),
43         m_menu(menu)
44 {
45     //setSelectionMode(QAbstractItemView::ExtendedSelection);
46     //setDragDropMode(QAbstractItemView::DragDrop);
47     setColumnCount(1);
48     setDropIndicatorShown(true);
49     setAlternatingRowColors(true);
50     setDragEnabled(true);
51     setAcceptDrops(true);
52     setHeaderHidden(true);
53     initList();
54 }
55
56 EffectsListWidget::~EffectsListWidget()
57 {
58 }
59
60 void EffectsListWidget::initList()
61 {
62     clear();
63     QTreeWidgetItem *item;
64     QTreeWidgetItem *parentItem;
65     QString effectName;
66     QStringList effectInfo;
67     KIcon videoIcon("kdenlive-show-video");
68     KIcon audioIcon("kdenlive-show-audio");
69     KIcon customIcon("kdenlive-custom-effect");
70     KIcon folderIcon("folder");
71
72     QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc");
73     QDomDocument doc;
74     QFile file(effectCategory);
75     doc.setContent(&file, false);
76     file.close();
77     QList <QTreeWidgetItem *> folders;
78     QDomNodeList groups = doc.documentElement().elementsByTagName("group");
79     for (int i = 0; i < groups.count(); i++) {
80         item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(groups.at(i).firstChild().firstChild().nodeValue()));
81         item->setIcon(0, folderIcon);
82         item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
83         item->setData(0, IdRole, groups.at(i).toElement().attribute("list"));
84         folders.append(item);
85     }
86
87     QTreeWidgetItem *misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
88     misc->setIcon(0, folderIcon);
89     misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
90
91     QTreeWidgetItem *audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
92     audio->setIcon(0, folderIcon);
93     audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
94
95     QTreeWidgetItem *custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom")));
96     custom->setIcon(0, folderIcon);
97     custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
98
99     insertTopLevelItem(0, custom);
100     insertTopLevelItem(0, misc);
101     insertTopLevelItem(0, audio);
102     insertTopLevelItems(0, folders);
103
104
105     int ct = MainWindow::videoEffects.count();
106     for (int ix = 0; ix < ct; ix ++) {
107         effectInfo = MainWindow::videoEffects.effectIdInfo(ix);
108         parentItem = NULL;
109         for (int i = 0; i < folders.count(); i++) {
110             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
111             if (l.contains(effectInfo.at(2))) {
112                 parentItem = folders.at(i);
113                 break;
114             }
115         }
116         if (parentItem == NULL) parentItem = misc;
117         if (!effectInfo.isEmpty()) {
118             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
119             item->setIcon(0, videoIcon);
120             item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO));
121             item->setData(0, IdRole, effectInfo);
122         }
123     }
124
125     ct = MainWindow::audioEffects.count();
126     for (int ix = 0; ix < ct; ix ++) {
127         effectInfo = MainWindow::audioEffects.effectIdInfo(ix);
128         parentItem = NULL;
129         for (int i = 0; i < folders.count(); i++) {
130             QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts);
131             if (l.contains(effectInfo.at(2))) {
132                 parentItem = folders.at(i);
133                 break;
134             }
135         }
136         if (parentItem == NULL) parentItem = audio;
137         if (!effectInfo.isEmpty()) {
138             item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
139             item->setIcon(0, audioIcon);
140             item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO));
141             item->setData(0, IdRole, effectInfo);
142         }
143     }
144
145     ct = MainWindow::customEffects.count();
146     for (int ix = 0; ix < ct; ix ++) {
147         effectInfo = MainWindow::customEffects.effectIdInfo(ix);
148         if (!effectInfo.isEmpty()) {
149             item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst()));
150             item->setIcon(0, customIcon);
151             item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM));
152             item->setData(0, IdRole, effectInfo);
153         }
154     }
155     setSortingEnabled(true);
156     sortByColumn(0, Qt::AscendingOrder);
157 }
158
159 const QDomElement EffectsListWidget::currentEffect() const
160 {
161     return itemEffect(currentItem());
162 }
163
164 const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const
165 {
166     QDomElement effect;
167     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect;
168     QStringList effectInfo = item->data(0, IdRole).toStringList();
169     kDebug() << "// EFFECT SELECTED: " << effectInfo;
170     switch (item->data(0, TypeRole).toInt()) {
171     case 1:
172         effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
173         break;
174     case 2:
175         effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
176         break;
177     default:
178         effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
179         break;
180     }
181     return effect;
182 }
183
184
185 QString EffectsListWidget::currentInfo()
186 {
187     QTreeWidgetItem *item = currentItem();
188     if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
189     QString info;
190     QStringList effectInfo = item->data(0, IdRole).toStringList();
191     switch (item->data(0, TypeRole).toInt()) {
192     case 1:
193         info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
194         break;
195     case 2:
196         info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
197         break;
198     default:
199         info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
200         break;
201     }
202     return info;
203 }
204
205 // virtual
206 void EffectsListWidget::mousePressEvent(QMouseEvent *event)
207 {
208     if (event->button() == Qt::LeftButton) {
209         m_DragStartPosition = event->pos();
210         m_dragStarted = true;
211     }
212     QTreeWidget::mousePressEvent(event);
213 }
214
215 // virtual
216 void EffectsListWidget::mouseMoveEvent(QMouseEvent *event)
217 {
218     if (!m_dragStarted) return;
219     if ((event->pos() - m_DragStartPosition).manhattanLength()
220             < QApplication::startDragDistance())
221         return;
222
223     {
224         QTreeWidgetItem *clickItem = itemAt(event->pos());
225         if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) {
226             QDrag *drag = new QDrag(this);
227             QMimeData *mimeData = new QMimeData;
228             const QList <QTreeWidgetItem *>list = selectedItems();
229             QDomDocument doc;
230             foreach(QTreeWidgetItem *item, list) {
231                 const QDomElement e = itemEffect(item);
232                 if (!e.isNull()) doc.appendChild(doc.importNode(e, true));
233             }
234             QByteArray data;
235             data.append(doc.toString().toUtf8());
236             mimeData->setData("kdenlive/effectslist", data);
237             drag->setMimeData(mimeData);
238             //QPixmap pix = qVariantValue<QPixmap>(clickItem->data(Qt::DecorationRole));
239             //drag->setPixmap(pix);
240             //drag->setHotSpot(QPoint(0, 50));
241             drag->start(Qt::CopyAction);
242         }
243         event->accept();
244     }
245 }
246
247 void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event)
248 {
249     event->setDropAction(Qt::CopyAction);
250     if (event->mimeData()->hasText()) {
251         event->acceptProposedAction();
252     }
253     //}
254 }
255
256 //virtual
257 void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
258 {
259     QTreeWidgetItem *item = itemAt(event->pos());
260     if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos());
261 }
262
263 #include "effectslistwidget.moc"