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