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);
52 headers << i18n("Clip") << i18n("Description") << i18n("Rating") << i18n("Date");
53 setHeaderLabels(headers);
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);
73 ProjectListView::~ProjectListView()
77 void ProjectListView::updateStyleSheet()
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);}";
89 void ProjectListView::processLayout()
91 executeDelayedItemsLayout();
94 void ProjectListView::configureColumns(const QPoint& pos)
97 popup.addTitle(i18nc("@title:menu", "Columns"));
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));
108 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
109 if (activatedAction != 0) {
110 const bool show = activatedAction->isChecked();
112 // remember the changed column visibility in the settings
113 const int columnIndex = activatedAction->data().toInt();
114 switch (columnIndex) {
116 KdenliveSettings::setShowdescriptioncolumn(show);
119 KdenliveSettings::setShowratingcolumn(show);
122 KdenliveSettings::setShowdatecolumn(show);
128 // apply the changed column visibility
130 showColumn(columnIndex);
132 hideColumn(columnIndex);
138 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
140 emit requestMenu(event->globalPos(), itemAt(event->pos()));
143 void ProjectListView::slotCollapsed(QTreeWidgetItem *item)
145 if (item->type() == PROJECTFOLDERTYPE) {
147 static_cast <FolderProjectItem *>(item)->switchIcon();
152 void ProjectListView::slotExpanded(QTreeWidgetItem *item)
154 if (item->type() == PROJECTFOLDERTYPE) {
156 static_cast <FolderProjectItem *>(item)->switchIcon();
161 bool ProjectListView::eventFilter(QObject *obj, QEvent *event)
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);
169 } else if (ke->key() == Qt::Key_Minus) {
170 if (currentItem()) currentItem()->setExpanded(false);
177 // pass the event on to the parent class
178 return QTreeWidget::eventFilter(obj, event);
183 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
185 QTreeWidgetItem *it = itemAt(event->pos());
192 if (it->type() == PROJECTFOLDERTYPE) {
193 if ((columnAt(event->pos().x()) == 0)) {
194 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
195 int offset = pix.width() + indentation();
196 if (event->pos().x() < offset) {
197 it->setExpanded(!it->isExpanded());
199 } else QTreeWidget::mouseDoubleClickEvent(event);
203 if (it->type() == PROJECTSUBCLIPTYPE) {
205 if ((columnAt(event->pos().x()) == 1)) {
206 QTreeWidget::mouseDoubleClickEvent(event);
209 item = static_cast <ProjectItem *>(it->parent());
210 } else item = static_cast <ProjectItem *>(it);
212 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
214 int column = columnAt(event->pos().x());
215 if (column == 0 && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR || it->childCount() > 0)) {
216 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
217 int offset = pix.width() + indentation();
218 if (item->parent()) offset += indentation();
219 if (it->childCount() > 0) {
220 if (offset > event->pos().x()) {
221 it->setExpanded(!it->isExpanded());
225 } else if (pix.isNull() || offset < event->pos().x()) {
226 QTreeWidget::mouseDoubleClickEvent(event);
230 if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
231 QTreeWidget::mouseDoubleClickEvent(event);
234 emit showProperties(item->referencedClip());
239 void ProjectListView::dropEvent(QDropEvent *event)
241 FolderProjectItem *item = NULL;
242 QTreeWidgetItem *it = itemAt(event->pos());
243 while (it && it->type() != PROJECTFOLDERTYPE) {
246 if (it) item = static_cast <FolderProjectItem *>(it);
247 if (event->mimeData()->hasUrls()) {
251 groupName = item->groupName();
252 groupId = item->clipId();
254 emit addClip(event->mimeData()->urls(), groupName, groupId);
255 event->setDropAction(Qt::CopyAction);
257 QTreeWidget::dropEvent(event);
259 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
261 //emit addClip(event->mimeData->text());
262 const QList <QTreeWidgetItem *> list = selectedItems();
264 QString parentId = item->clipId();
265 foreach(QTreeWidgetItem *it, list) {
266 // TODO allow dragging of folders ?
267 if (it->type() == PROJECTCLIPTYPE) {
268 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
269 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
271 item->addChild(clone);
272 QMap <QString, QString> props;
273 props.insert("groupname", item->groupName());
274 props.insert("groupid", parentId);
275 clone->setProperties(props);
280 // item dropped in empty zone, move it to top level
281 const QList <QTreeWidgetItem *> list = selectedItems();
283 foreach(QTreeWidgetItem *it, list) {
284 if (it->type() != PROJECTCLIPTYPE) continue;
285 QTreeWidgetItem *parent = it->parent();
286 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
287 kDebug() << "++ item parent: " << parent->text(1);
288 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
290 addTopLevelItem(clone);
291 clone->clearProperty("groupname");
292 clone->clearProperty("groupid");
297 emit projectModified();
298 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
299 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
300 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
302 if (event->source() == this) {
303 event->setDropAction(Qt::MoveAction);
306 event->acceptProposedAction();
308 QTreeWidget::dropEvent(event);
312 void ProjectListView::mousePressEvent(QMouseEvent *event)
314 if (event->button() == Qt::LeftButton) {
315 m_DragStartPosition = event->pos();
316 m_dragStarted = true;
317 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
318 ProjectItem *item = static_cast<ProjectItem *>(underMouse);
320 QRect itemRect = visualItemRect(item);
321 if (item->underJobMenu(itemRect, event->pos())) {
325 && underMouse->isSelected()) emit focusMonitor()
328 QTreeWidget::mousePressEvent(event);
332 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
334 QTreeWidget::mouseReleaseEvent(event);
335 QTreeWidgetItem *underMouse = itemAt(event->pos());
336 if (underMouse) emit focusMonitor(true);
340 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
342 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
343 if (!m_dragStarted) return;
345 if ((event->pos() - m_DragStartPosition).manhattanLength()
346 < QApplication::startDragDistance())
349 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
351 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
352 QDrag *drag = new QDrag(this);
353 QMimeData *mimeData = new QMimeData;
354 const QList <QTreeWidgetItem *> list = selectedItems();
356 foreach(const QTreeWidgetItem *item, list) {
357 if (item->type() == PROJECTFOLDERTYPE) {
358 const int children = item->childCount();
359 for (int i = 0; i < children; i++) {
360 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
362 } else if (item->type() == PROJECTSUBCLIPTYPE) {
363 const ProjectItem *parentclip = static_cast <const ProjectItem *>(item->parent());
364 const SubProjectItem *clickItem = static_cast <const SubProjectItem *>(item);
365 QPoint p = clickItem->zone();
366 QString data = parentclip->clipId();
367 data.append("/" + QString::number(p.x()));
368 data.append("/" + QString::number(p.y()));
371 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
372 ids.append(clip->clipId());
375 if (ids.isEmpty()) return;
377 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
378 mimeData->setData("kdenlive/producerslist", data);
379 //mimeData->setText(ids.join(";")); //doc.toString());
380 //mimeData->setImageData(image);
381 drag->setMimeData(mimeData);
382 drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
383 drag->setHotSpot(QPoint(0, 40));
384 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
389 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
391 // stop playing because we get a crash otherwise when fetching the thumbnails
393 QTreeWidget::dragLeaveEvent(event);
396 QStringList ProjectListView::mimeTypes() const
398 QStringList qstrList;
399 qstrList << QTreeWidget::mimeTypes();
400 // list of accepted mime types for drop
401 qstrList.append("text/uri-list");
402 qstrList.append("text/plain");
403 qstrList.append("kdenlive/producerslist");
404 qstrList.append("kdenlive/clip");
409 Qt::DropActions ProjectListView::supportedDropActions() const
411 // returns what actions are supported when dropping
412 return Qt::MoveAction | Qt::CopyAction;
415 #include "projectlistview.moc"