1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "projectlistview.h"
22 #include "projectitem.h"
23 #include "subprojectitem.h"
24 #include "folderprojectitem.h"
25 #include "kdenlivesettings.h"
31 #include <QApplication>
32 #include <QHeaderView>
35 ProjectListView::ProjectListView(QWidget *parent) :
39 setSelectionMode(QAbstractItemView::ExtendedSelection);
40 setDragDropMode(QAbstractItemView::DragDrop);
41 setDropIndicatorShown(true);
42 setAlternatingRowColors(true);
45 setFrameShape(QFrame::NoFrame);
46 setRootIsDecorated(true);
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);}";
60 headers << i18n("Clip") << i18n("Description") << i18n("Rating") << i18n("Date");
61 setHeaderLabels(headers);
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);
81 ProjectListView::~ProjectListView()
85 void ProjectListView::processLayout()
87 executeDelayedItemsLayout();
90 void ProjectListView::configureColumns(const QPoint& pos)
93 popup.addTitle(i18nc("@title:menu", "Columns"));
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));
104 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
105 if (activatedAction != 0) {
106 const bool show = activatedAction->isChecked();
108 // remember the changed column visibility in the settings
109 const int columnIndex = activatedAction->data().toInt();
110 switch (columnIndex) {
112 KdenliveSettings::setShowdescriptioncolumn(show);
115 KdenliveSettings::setShowratingcolumn(show);
118 KdenliveSettings::setShowdatecolumn(show);
124 // apply the changed column visibility
126 showColumn(columnIndex);
128 hideColumn(columnIndex);
134 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
136 emit requestMenu(event->globalPos(), itemAt(event->pos()));
139 void ProjectListView::slotCollapsed(QTreeWidgetItem *item)
141 if (item->type() == PROJECTFOLDERTYPE) {
143 static_cast <FolderProjectItem *>(item)->switchIcon();
148 void ProjectListView::slotExpanded(QTreeWidgetItem *item)
150 if (item->type() == PROJECTFOLDERTYPE) {
152 static_cast <FolderProjectItem *>(item)->switchIcon();
157 bool ProjectListView::eventFilter(QObject *obj, QEvent *event)
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);
165 } else if (ke->key() == Qt::Key_Minus) {
166 if (currentItem()) currentItem()->setExpanded(false);
173 // pass the event on to the parent class
174 return QTreeWidget::eventFilter(obj, event);
179 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
181 QTreeWidgetItem *it = itemAt(event->pos());
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());
194 } else QTreeWidget::mouseDoubleClickEvent(event);
198 if (it->type() == PROJECTSUBCLIPTYPE) {
200 if ((columnAt(event->pos().x()) == 1)) {
201 QTreeWidget::mouseDoubleClickEvent(event);
204 item = static_cast <ProjectItem *>(it->parent());
205 } else item = static_cast <ProjectItem *>(it);
207 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
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());
220 } else if (pix.isNull() || offset < event->pos().x()) {
221 QTreeWidget::mouseDoubleClickEvent(event);
225 if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
226 QTreeWidget::mouseDoubleClickEvent(event);
229 emit showProperties(item->referencedClip());
234 void ProjectListView::dropEvent(QDropEvent *event)
236 FolderProjectItem *item = NULL;
237 QTreeWidgetItem *it = itemAt(event->pos());
238 while (it && it->type() != PROJECTFOLDERTYPE) {
241 if (it) item = static_cast <FolderProjectItem *>(it);
242 if (event->mimeData()->hasUrls()) {
246 groupName = item->groupName();
247 groupId = item->clipId();
249 emit addClip(event->mimeData()->urls(), groupName, groupId);
250 event->setDropAction(Qt::CopyAction);
252 QTreeWidget::dropEvent(event);
254 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
256 //emit addClip(event->mimeData->text());
257 const QList <QTreeWidgetItem *> list = selectedItems();
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));
266 item->addChild(clone);
267 QMap <QString, QString> props;
268 props.insert("groupname", item->groupName());
269 props.insert("groupid", parentId);
270 clone->setProperties(props);
275 // item dropped in empty zone, move it to top level
276 const QList <QTreeWidgetItem *> list = selectedItems();
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)));
284 addTopLevelItem(clone);
285 clone->clearProperty("groupname");
286 clone->clearProperty("groupid");
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());
296 event->acceptProposedAction();
297 QTreeWidget::dropEvent(event);
301 void ProjectListView::mousePressEvent(QMouseEvent *event)
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();*/
309 QTreeWidget::mousePressEvent(event);
313 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
315 QTreeWidget::mouseReleaseEvent(event);
316 QTreeWidgetItem *underMouse = itemAt(event->pos());
317 if (underMouse) emit focusMonitor();
321 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
323 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
324 if (!m_dragStarted) return;
326 if ((event->pos() - m_DragStartPosition).manhattanLength()
327 < QApplication::startDragDistance())
330 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
332 if (it->type() == PROJECTSUBCLIPTYPE) {
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;
341 list.append(clip->clipId());
342 QPoint p = clickItem->zone();
343 list.append(QString::number(p.x()));
344 list.append(QString::number(p.y()));
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));
354 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
355 QDrag *drag = new QDrag(this);
356 QMimeData *mimeData = new QMimeData;
357 const QList <QTreeWidgetItem *> list = selectedItems();
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());
366 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
367 ids.append(clip->clipId());
370 if (ids.isEmpty()) return;
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));
386 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
388 // stop playing because we get a crash otherwise when fetching the thumbnails
390 QTreeWidget::dragLeaveEvent(event);
393 QStringList ProjectListView::mimeTypes() const
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");
406 Qt::DropActions ProjectListView::supportedDropActions() const
408 // returns what actions are supported when dropping
409 return Qt::MoveAction | Qt::CopyAction;
412 #include "projectlistview.moc"