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());
191 if (it->type() == PROJECTFOLDERTYPE) {
192 if ((columnAt(event->pos().x()) == 0)) {
193 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
194 int offset = pix.width() + indentation();
195 if (event->pos().x() < offset) {
196 it->setExpanded(!it->isExpanded());
198 } else QTreeWidget::mouseDoubleClickEvent(event);
202 if (it->type() == PROJECTSUBCLIPTYPE) {
204 if ((columnAt(event->pos().x()) == 1)) {
205 QTreeWidget::mouseDoubleClickEvent(event);
208 item = static_cast <ProjectItem *>(it->parent());
209 } else item = static_cast <ProjectItem *>(it);
211 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
213 int column = columnAt(event->pos().x());
214 if (column == 0 && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR || it->childCount() > 0)) {
215 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
216 int offset = pix.width() + indentation();
217 if (item->parent()) offset += indentation();
218 if (it->childCount() > 0) {
219 if (offset > event->pos().x()) {
220 it->setExpanded(!it->isExpanded());
224 } else if (pix.isNull() || offset < event->pos().x()) {
225 QTreeWidget::mouseDoubleClickEvent(event);
229 if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
230 QTreeWidget::mouseDoubleClickEvent(event);
233 emit showProperties(item->referencedClip());
238 void ProjectListView::dropEvent(QDropEvent *event)
240 FolderProjectItem *item = NULL;
241 QTreeWidgetItem *it = itemAt(event->pos());
242 while (it && it->type() != PROJECTFOLDERTYPE) {
245 if (it) item = static_cast <FolderProjectItem *>(it);
246 if (event->mimeData()->hasUrls()) {
250 groupName = item->groupName();
251 groupId = item->clipId();
253 emit addClip(event->mimeData()->urls(), groupName, groupId);
254 event->setDropAction(Qt::CopyAction);
256 QTreeWidget::dropEvent(event);
258 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
260 //emit addClip(event->mimeData->text());
261 const QList <QTreeWidgetItem *> list = selectedItems();
263 QString parentId = item->clipId();
264 foreach(QTreeWidgetItem *it, list) {
265 // TODO allow dragging of folders ?
266 if (it->type() == PROJECTCLIPTYPE) {
267 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
268 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
270 item->addChild(clone);
271 QMap <QString, QString> props;
272 props.insert("groupname", item->groupName());
273 props.insert("groupid", parentId);
274 clone->setProperties(props);
279 // item dropped in empty zone, move it to top level
280 const QList <QTreeWidgetItem *> list = selectedItems();
282 foreach(QTreeWidgetItem *it, list) {
283 if (it->type() != PROJECTCLIPTYPE) continue;
284 QTreeWidgetItem *parent = it->parent();
285 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
286 kDebug() << "++ item parent: " << parent->text(1);
287 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
289 addTopLevelItem(clone);
290 clone->clearProperty("groupname");
291 clone->clearProperty("groupid");
296 emit projectModified();
297 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
298 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
299 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
301 if (event->source() == this) {
302 event->setDropAction(Qt::MoveAction);
305 event->acceptProposedAction();
307 QTreeWidget::dropEvent(event);
311 void ProjectListView::mousePressEvent(QMouseEvent *event)
313 if (event->button() == Qt::LeftButton) {
314 m_DragStartPosition = event->pos();
315 m_dragStarted = true;
316 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
317 ProjectItem *item = static_cast<ProjectItem *>(underMouse);
319 QRect itemRect = visualItemRect(item);
320 if (item->underJobMenu(itemRect, event->pos())) {
324 && underMouse->isSelected()) emit focusMonitor()
327 QTreeWidget::mousePressEvent(event);
331 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
333 QTreeWidget::mouseReleaseEvent(event);
334 QTreeWidgetItem *underMouse = itemAt(event->pos());
335 if (underMouse) emit focusMonitor();
339 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
341 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
342 if (!m_dragStarted) return;
344 if ((event->pos() - m_DragStartPosition).manhattanLength()
345 < QApplication::startDragDistance())
348 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
350 if (it->type() == PROJECTSUBCLIPTYPE) {
352 SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
353 if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
354 ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
355 QDrag *drag = new QDrag(this);
356 QMimeData *mimeData = new QMimeData;
359 list.append(clip->clipId());
360 QPoint p = clickItem->zone();
361 list.append(QString::number(p.x()));
362 list.append(QString::number(p.y()));
364 data.append(list.join(";").toUtf8());
365 mimeData->setData("kdenlive/clip", data);
366 drag->setMimeData(mimeData);
367 drag->setPixmap(clickItem->data(0, Qt::DecorationRole).value<QPixmap>());
368 drag->setHotSpot(QPoint(0, 50));
369 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
372 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
373 QDrag *drag = new QDrag(this);
374 QMimeData *mimeData = new QMimeData;
375 const QList <QTreeWidgetItem *> list = selectedItems();
377 foreach(const QTreeWidgetItem *item, list) {
378 if (item->type() == PROJECTFOLDERTYPE) {
379 const int children = item->childCount();
380 for (int i = 0; i < children; i++) {
381 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
384 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
385 ids.append(clip->clipId());
388 if (ids.isEmpty()) return;
390 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
391 mimeData->setData("kdenlive/producerslist", data);
392 //mimeData->setText(ids.join(";")); //doc.toString());
393 //mimeData->setImageData(image);
394 drag->setMimeData(mimeData);
395 drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
396 drag->setHotSpot(QPoint(0, 50));
397 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
404 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
406 // stop playing because we get a crash otherwise when fetching the thumbnails
408 QTreeWidget::dragLeaveEvent(event);
411 QStringList ProjectListView::mimeTypes() const
413 QStringList qstrList;
414 qstrList << QTreeWidget::mimeTypes();
415 // list of accepted mime types for drop
416 qstrList.append("text/uri-list");
417 qstrList.append("text/plain");
418 qstrList.append("kdenlive/producerslist");
419 qstrList.append("kdenlive/clip");
424 Qt::DropActions ProjectListView::supportedDropActions() const
426 // returns what actions are supported when dropping
427 return Qt::MoveAction | Qt::CopyAction;
430 #include "projectlistview.moc"