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