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 if (it->type() != PROJECTCLIPTYPE) continue;
280 QTreeWidgetItem *parent = it->parent();
281 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
282 kDebug() << "++ item parent: " << parent->text(1);
283 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
285 addTopLevelItem(clone);
286 clone->clearProperty("groupname");
287 clone->clearProperty("groupid");
292 emit projectModified();
293 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
294 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
295 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
297 if (event->source() == this) {
298 event->setDropAction(Qt::MoveAction);
301 event->acceptProposedAction();
303 QTreeWidget::dropEvent(event);
307 void ProjectListView::mousePressEvent(QMouseEvent *event)
309 if (event->button() == Qt::LeftButton) {
310 m_DragStartPosition = event->pos();
311 m_dragStarted = true;
312 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
313 ProjectItem *item = static_cast<ProjectItem *>(underMouse);
315 QRect itemRect = visualItemRect(item);
316 if (item->underJobMenu(itemRect, event->pos())) {
320 && underMouse->isSelected()) emit focusMonitor()
323 QTreeWidget::mousePressEvent(event);
327 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
329 QTreeWidget::mouseReleaseEvent(event);
330 QTreeWidgetItem *underMouse = itemAt(event->pos());
331 if (underMouse) emit focusMonitor();
335 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
337 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
338 if (!m_dragStarted) return;
340 if ((event->pos() - m_DragStartPosition).manhattanLength()
341 < QApplication::startDragDistance())
344 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
346 if (it->type() == PROJECTSUBCLIPTYPE) {
348 SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
349 if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
350 ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
351 QDrag *drag = new QDrag(this);
352 QMimeData *mimeData = new QMimeData;
355 list.append(clip->clipId());
356 QPoint p = clickItem->zone();
357 list.append(QString::number(p.x()));
358 list.append(QString::number(p.y()));
360 data.append(list.join(";").toUtf8());
361 mimeData->setData("kdenlive/clip", data);
362 drag->setMimeData(mimeData);
363 drag->setPixmap(clickItem->data(0, Qt::DecorationRole).value<QPixmap>());
364 drag->setHotSpot(QPoint(0, 50));
365 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
368 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
369 QDrag *drag = new QDrag(this);
370 QMimeData *mimeData = new QMimeData;
371 const QList <QTreeWidgetItem *> list = selectedItems();
373 foreach(const QTreeWidgetItem *item, list) {
374 if (item->type() == PROJECTFOLDERTYPE) {
375 const int children = item->childCount();
376 for (int i = 0; i < children; i++) {
377 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
380 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
381 ids.append(clip->clipId());
384 if (ids.isEmpty()) return;
386 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
387 mimeData->setData("kdenlive/producerslist", data);
388 //mimeData->setText(ids.join(";")); //doc.toString());
389 //mimeData->setImageData(image);
390 drag->setMimeData(mimeData);
391 drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
392 drag->setHotSpot(QPoint(0, 50));
393 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
400 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
402 // stop playing because we get a crash otherwise when fetching the thumbnails
404 QTreeWidget::dragLeaveEvent(event);
407 QStringList ProjectListView::mimeTypes() const
409 QStringList qstrList;
410 qstrList << QTreeWidget::mimeTypes();
411 // list of accepted mime types for drop
412 qstrList.append("text/uri-list");
413 qstrList.append("text/plain");
414 qstrList.append("kdenlive/producerslist");
415 qstrList.append("kdenlive/clip");
420 Qt::DropActions ProjectListView::supportedDropActions() const
422 // returns what actions are supported when dropping
423 return Qt::MoveAction | Qt::CopyAction;
426 #include "projectlistview.moc"