]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
Make folders appear on top of the 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
46
47     setColumnCount(4);
48     QStringList headers;
49     headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description") << i18n("Rating");
50     setHeaderLabels(headers);
51
52     QHeaderView* headerView = header();
53     headerView->setContextMenuPolicy(Qt::CustomContextMenu);
54     connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
55             this, SLOT(configureColumns(const QPoint&)));
56     headerView->setClickable(true);
57     headerView->setSortIndicatorShown(true);
58     headerView->setMovable(false);
59     sortByColumn(1, Qt::AscendingOrder);
60     setSortingEnabled(true);
61
62     if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(2);
63     if (!KdenliveSettings::showratingcolumn()) hideColumn(3);
64 }
65
66 ProjectListView::~ProjectListView()
67 {
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 = 2; 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 2:
93             KdenliveSettings::setShowdescriptioncolumn(show);
94             break;
95         case 3:
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::mouseDoubleClickEvent(QMouseEvent * event)
119 {
120     QTreeWidgetItem *it = itemAt(event->pos());
121     if (!it) {
122         emit addClip();
123         return;
124     }
125     ProjectItem *item;
126     if (it->type() == PROJECTFOLDERTYPE) {
127         if ((columnAt(event->pos().x()) == 1)) QTreeWidget::mouseDoubleClickEvent(event);
128         return;
129     }
130     if (it->type() == PROJECTSUBCLIPTYPE) {
131         // subitem
132         item = static_cast <ProjectItem *>(it->parent());
133     } else item = static_cast <ProjectItem *>(it);
134
135     if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
136     if ((columnAt(event->pos().x()) == 1) && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) QTreeWidget::mouseDoubleClickEvent(event);
137     else if ((columnAt(event->pos().x()) == 2) && it->type() != PROJECTSUBCLIPTYPE) QTreeWidget::mouseDoubleClickEvent(event);
138     else emit showProperties(item->referencedClip());
139 }
140
141 // virtual
142 void ProjectListView::dragEnterEvent(QDragEnterEvent *event)
143 {
144     if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
145         kDebug() << "////////////////  DRAG ENTR OK";
146     }
147     event->acceptProposedAction();
148 }
149
150 // virtual
151 void ProjectListView::dropEvent(QDropEvent *event)
152 {
153     FolderProjectItem *item = NULL;
154     QTreeWidgetItem *it = itemAt(event->pos());
155     while (it && it->type() != PROJECTFOLDERTYPE) {
156         it = it->parent();
157     }
158     if (it) item = static_cast <FolderProjectItem *>(it);
159     if (event->mimeData()->hasUrls()) {
160         QString groupName;
161         QString groupId;
162         if (item) {
163             groupName = item->groupName();
164             groupId = item->clipId();
165         }
166         emit addClip(event->mimeData()->urls(), groupName, groupId);
167         event->setDropAction(Qt::CopyAction);
168         event->accept();
169         return;
170     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
171         if (item) {
172             //emit addClip(event->mimeData->text());
173             const QList <QTreeWidgetItem *> list = selectedItems();
174             ProjectItem *clone;
175             QString parentId = item->clipId();
176             foreach(QTreeWidgetItem *it, list) {
177                 // TODO allow dragging of folders ?
178                 if (it->type() == PROJECTCLIPTYPE) {
179                     if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
180                     else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
181                     if (clone) {
182                         item->addChild(clone);
183                         QMap <QString, QString> props;
184                         props.insert("groupname", item->groupName());
185                         props.insert("groupid", parentId);
186                         clone->setProperties(props);
187                     }
188                 } else item = NULL;
189             }
190         } else {
191             // item dropped in empty zone, move it to top level
192             const QList <QTreeWidgetItem *> list = selectedItems();
193             ProjectItem *clone;
194             foreach(QTreeWidgetItem *it, list) {
195                 QTreeWidgetItem *parent = it->parent();
196                 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/)  {
197                     kDebug() << "++ item parent: " << parent->text(1);
198                     clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
199                     if (clone) {
200                         addTopLevelItem(clone);
201                         clone->clearProperty("groupname");
202                         clone->clearProperty("groupid");
203                     }
204                 }
205             }
206         }
207     } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
208         QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
209         emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
210     }
211     event->acceptProposedAction();
212 }
213
214 // virtual
215 void ProjectListView::mousePressEvent(QMouseEvent *event)
216 {
217     if (event->button() == Qt::LeftButton) {
218         m_DragStartPosition = event->pos();
219         m_dragStarted = true;
220         /*QTreeWidgetItem *underMouse = itemAt(event->pos());
221         if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
222     }
223     QTreeWidget::mousePressEvent(event);
224 }
225
226 // virtual
227 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
228 {
229     QTreeWidget::mouseReleaseEvent(event);
230     QTreeWidgetItem *underMouse = itemAt(event->pos());
231     if (underMouse) emit focusMonitor();
232 }
233
234 // virtual
235 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
236 {
237     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
238     if (!m_dragStarted) return;
239
240     if ((event->pos() - m_DragStartPosition).manhattanLength()
241             < QApplication::startDragDistance())
242         return;
243
244     QTreeWidgetItem *it = itemAt(m_DragStartPosition);
245     if (!it) return;
246     if (it->type() == PROJECTSUBCLIPTYPE) {
247         // subitem
248         SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
249         if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
250             ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
251             QDrag *drag = new QDrag(this);
252             QMimeData *mimeData = new QMimeData;
253
254             QStringList list;
255             list.append(clip->clipId());
256             QPoint p = clickItem->zone();
257             list.append(QString::number(p.x()));
258             list.append(QString::number(p.y()));
259             QByteArray data;
260             data.append(list.join(";").toUtf8());
261             mimeData->setData("kdenlive/clip", data);
262             drag->setMimeData(mimeData);
263             drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
264             drag->setHotSpot(QPoint(0, 50));
265             drag->exec();
266         }
267     } else {
268         if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
269             QDrag *drag = new QDrag(this);
270             QMimeData *mimeData = new QMimeData;
271             const QList <QTreeWidgetItem *> list = selectedItems();
272             QStringList ids;
273             foreach(const QTreeWidgetItem *item, list) {
274                 if (item->type() == PROJECTFOLDERTYPE) {
275                     const int children = item->childCount();
276                     for (int i = 0; i < children; i++) {
277                         ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
278                     }
279                 } else {
280                     const ProjectItem *clip = static_cast <const ProjectItem *>(item);
281                     ids.append(clip->clipId());
282                 }
283             }
284             if (ids.isEmpty()) return;
285             QByteArray data;
286             data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
287             mimeData->setData("kdenlive/producerslist", data);
288             //mimeData->setText(ids.join(";")); //doc.toString());
289             //mimeData->setImageData(image);
290             drag->setMimeData(mimeData);
291             drag->setPixmap(it->icon(0).pixmap(iconSize()));
292             drag->setHotSpot(QPoint(0, 50));
293             drag->exec();
294         }
295         //event->accept();
296     }
297 }
298
299 // virtual
300 void ProjectListView::dragMoveEvent(QDragMoveEvent * event)
301 {
302     //event->setDropAction(Qt::MoveAction);
303     if (event->mimeData()->hasText()) {
304         event->acceptProposedAction();
305     }
306     // stop playing because we get a crash otherwise when fetching the thumbnails
307     emit pauseMonitor();
308 }
309
310 QStringList ProjectListView::mimeTypes() const
311 {
312     QStringList qstrList;
313     qstrList << QTreeWidget::mimeTypes();
314     // list of accepted mime types for drop
315     qstrList.append("text/uri-list");
316     qstrList.append("text/plain");
317     qstrList.append("kdenlive/producerslist");
318     qstrList.append("kdenlive/clip");
319     return qstrList;
320 }
321
322
323 Qt::DropActions ProjectListView::supportedDropActions() const
324 {
325     // returns what actions are supported when dropping
326     return Qt::MoveAction;
327 }
328
329 #include "projectlistview.moc"