]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
Add subclips (dragging a zone from clip monitor to project tree)
[kdenlive] / src / projectlistview.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 "projectlistview.h"
22 #include "projectitem.h"
23 #include "subprojectitem.h"
24 #include "kdenlivesettings.h"
25
26 #include <KDebug>
27 #include <KMenu>
28 #include <KLocale>
29
30 #include <QApplication>
31 #include <QHeaderView>
32 #include <QAction>
33
34 ProjectListView::ProjectListView(QWidget *parent) :
35         QTreeWidget(parent),
36         m_dragStarted(false)
37 {
38     setSelectionMode(QAbstractItemView::ExtendedSelection);
39     setDragDropMode(QAbstractItemView::DragDrop);
40     setDropIndicatorShown(true);
41     setAlternatingRowColors(true);
42     setDragEnabled(true);
43     setAcceptDrops(true);
44
45     setColumnCount(4);
46     QStringList headers;
47     headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description") << i18n("Rating");
48     setHeaderLabels(headers);
49     sortByColumn(1, Qt::AscendingOrder);
50
51     QHeaderView* headerView = header();
52     headerView->setContextMenuPolicy(Qt::CustomContextMenu);
53     connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
54             this, SLOT(configureColumns(const QPoint&)));
55
56     //connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), this, SLOT(slotFocusOut(QTreeWidgetItem *, QTreeWidgetItem *)));
57
58     if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(2);
59     if (!KdenliveSettings::showratingcolumn()) hideColumn(3);
60
61     setSortingEnabled(true);
62 }
63
64 ProjectListView::~ProjectListView()
65 {
66 }
67
68
69 void ProjectListView::configureColumns(const QPoint& pos)
70 {
71     KMenu popup(this);
72     popup.addTitle(i18nc("@title:menu", "Columns"));
73
74     QHeaderView* headerView = header();
75     for (int i = 2; i < headerView->count(); ++i) {
76         const QString text = model()->headerData(i, Qt::Horizontal).toString();
77         QAction* action = popup.addAction(text);
78         action->setCheckable(true);
79         action->setChecked(!headerView->isSectionHidden(i));
80         action->setData(i);
81     }
82
83     QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
84     if (activatedAction != 0) {
85         const bool show = activatedAction->isChecked();
86
87         // remember the changed column visibility in the settings
88         const int columnIndex = activatedAction->data().toInt();
89         switch (columnIndex) {
90         case 2:
91             KdenliveSettings::setShowdescriptioncolumn(show);
92             break;
93         case 3:
94             KdenliveSettings::setShowratingcolumn(show);
95             break;
96         default:
97             break;
98         }
99
100         // apply the changed column visibility
101         if (show) {
102             showColumn(columnIndex);
103         } else {
104             hideColumn(columnIndex);
105         }
106     }
107 }
108
109 // virtual
110 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
111 {
112     emit requestMenu(event->globalPos(), itemAt(event->pos()));
113 }
114
115 // virtual
116 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
117 {
118     QTreeWidgetItem *it = itemAt(event->pos());
119     if (!it) return;
120     ProjectItem *item;
121     if (it->type() == QTreeWidgetItem::UserType + 1) {
122         // subitem
123         item = static_cast <ProjectItem *>(it->parent());
124     } else item = static_cast <ProjectItem *>(it);
125     if (!item) {
126         emit addClip();
127         return;
128     }
129     if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
130     if (item->isGroup()) {
131         if ((columnAt(event->pos().x()) == 1)) QTreeWidget::mouseDoubleClickEvent(event);
132     } else {
133         if ((columnAt(event->pos().x()) == 1) && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) QTreeWidget::mouseDoubleClickEvent(event);
134         else if ((columnAt(event->pos().x()) == 2)) QTreeWidget::mouseDoubleClickEvent(event);
135         else emit showProperties(item->referencedClip());
136     }
137 }
138
139 // virtual
140 void ProjectListView::dragEnterEvent(QDragEnterEvent *event)
141 {
142     if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
143         kDebug() << "////////////////  DRAG ENTR OK";
144     }
145     event->acceptProposedAction();
146 }
147
148 // virtual
149 void ProjectListView::dropEvent(QDropEvent *event)
150 {
151     ProjectItem *item = NULL;
152     QTreeWidgetItem *it = itemAt(event->pos());
153     if (it) {
154         if (it->type() == QTreeWidgetItem::UserType + 1) {
155             // subitem
156             item = static_cast <ProjectItem *>(it->parent());
157         } else item = static_cast <ProjectItem *>(it);
158     }
159
160     if (event->mimeData()->hasUrls()) {
161         QString groupName;
162         QString groupId;
163         if (item) {
164             if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
165             if (item->isGroup()) {
166                 groupName = item->groupName();
167                 groupId = item->clipId();
168             }
169         }
170         emit addClip(event->mimeData()->urls(), groupName, groupId);
171         event->setDropAction(Qt::CopyAction);
172         event->accept();
173         return;
174     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
175         if (item) {
176             if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
177             if (item->isGroup()) {
178                 //emit addClip(event->mimeData->text());
179                 const QList <QTreeWidgetItem *> list = selectedItems();
180                 ProjectItem *clone;
181                 QString parentId = item->clipId();
182                 foreach(QTreeWidgetItem *it, list) {
183                     // TODO allow dragging of folders ?
184                     if (!((ProjectItem *) it)->isGroup()/* && ((ProjectItem *) it)->clipId() < 10000*/) {
185                         if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
186                         else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
187                         if (clone) {
188                             item->addChild(clone);
189                             QMap <QString, QString> props;
190                             props.insert("groupname", item->groupName());
191                             props.insert("groupid", parentId);
192                             clone->setProperties(props);
193                         }
194                     }
195                 }
196             } else item = NULL;
197         }
198         if (!item) {
199             // item dropped in empty zone, move it to top level
200             const QList <QTreeWidgetItem *> list = selectedItems();
201             ProjectItem *clone;
202             foreach(QTreeWidgetItem *it, list) {
203                 QTreeWidgetItem *parent = it->parent();
204                 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/)  {
205                     kDebug() << "++ item parent: " << parent->text(1);
206                     clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
207                     if (clone) {
208                         addTopLevelItem(clone);
209                         clone->clearProperty("groupname");
210                         clone->clearProperty("groupid");
211                     }
212                 }
213             }
214         }
215     } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
216         QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
217         emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
218     }
219     event->acceptProposedAction();
220 }
221
222 // virtual
223 void ProjectListView::mousePressEvent(QMouseEvent *event)
224 {
225     if (event->button() == Qt::LeftButton) {
226         m_DragStartPosition = event->pos();
227         m_dragStarted = true;
228         /*QTreeWidgetItem *underMouse = itemAt(event->pos());
229         if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
230     }
231     QTreeWidget::mousePressEvent(event);
232 }
233
234 // virtual
235 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
236 {
237     QTreeWidget::mouseReleaseEvent(event);
238     QTreeWidgetItem *underMouse = itemAt(event->pos());
239     if (underMouse) emit focusMonitor();
240 }
241
242 // virtual
243 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
244 {
245     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
246     if (!m_dragStarted) return;
247
248     if ((event->pos() - m_DragStartPosition).manhattanLength()
249             < QApplication::startDragDistance())
250         return;
251
252     QTreeWidgetItem *it = itemAt(m_DragStartPosition);
253     if (!it) return;
254     if (it->type() == QTreeWidgetItem::UserType + 1) {
255         // subitem
256         SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
257         if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
258             ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
259             QDrag *drag = new QDrag(this);
260             QMimeData *mimeData = new QMimeData;
261
262             QStringList list;
263             list.append(clip->clipId());
264             QPoint p = clickItem->zone();
265             list.append(QString::number(p.x()));
266             list.append(QString::number(p.y()));
267             QByteArray data;
268             data.append(list.join(";").toUtf8());
269             mimeData->setData("kdenlive/clip", data);
270             drag->setMimeData(mimeData);
271             drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
272             drag->setHotSpot(QPoint(0, 50));
273             drag->exec();
274         }
275     } else {
276         ProjectItem *clickItem = static_cast <ProjectItem *>(it);
277         if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
278             QDrag *drag = new QDrag(this);
279             QMimeData *mimeData = new QMimeData;
280             const QList <QTreeWidgetItem *> list = selectedItems();
281             QStringList ids;
282             foreach(const QTreeWidgetItem *item, list) {
283                 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
284                 if (!clip->isGroup()) ids.append(clip->clipId());
285                 else {
286                     const int children = item->childCount();
287                     for (int i = 0; i < children; i++) {
288                         ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
289                     }
290                 }
291             }
292             if (ids.isEmpty()) return;
293             QByteArray data;
294             data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
295             mimeData->setData("kdenlive/producerslist", data);
296             //mimeData->setText(ids.join(";")); //doc.toString());
297             //mimeData->setImageData(image);
298             drag->setMimeData(mimeData);
299             drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
300             drag->setHotSpot(QPoint(0, 50));
301             drag->exec();
302         }
303         //event->accept();
304     }
305 }
306
307 // virtual
308 void ProjectListView::dragMoveEvent(QDragMoveEvent * event)
309 {
310     //event->setDropAction(Qt::MoveAction);
311     if (event->mimeData()->hasText()) {
312         event->acceptProposedAction();
313     }
314     // stop playing because we get a crash otherwise when fetching the thumbnails
315     emit pauseMonitor();
316 }
317
318 QStringList ProjectListView::mimeTypes() const
319 {
320     QStringList qstrList;
321     qstrList << QTreeWidget::mimeTypes();
322     // list of accepted mime types for drop
323     qstrList.append("text/uri-list");
324     qstrList.append("text/plain");
325     qstrList.append("kdenlive/producerslist");
326     qstrList.append("kdenlive/clip");
327     return qstrList;
328 }
329
330
331 Qt::DropActions ProjectListView::supportedDropActions() const
332 {
333     // returns what actions are supported when dropping
334     return Qt::MoveAction;
335 }
336
337 #include "projectlistview.moc"