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