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