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