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