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