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);
50 headers << i18n("Clip") << i18n("Description") << i18n("Rating");
51 setHeaderLabels(headers);
54 QHeaderView* headerView = header();
55 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
56 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
57 this, SLOT(configureColumns(const QPoint&)));
58 connect(this, SIGNAL(itemCollapsed(QTreeWidgetItem *)), this, SLOT(slotCollapsed(QTreeWidgetItem *)));
59 connect(this, SIGNAL(itemExpanded(QTreeWidgetItem *)), this, SLOT(slotExpanded(QTreeWidgetItem *)));
60 headerView->setClickable(true);
61 headerView->setSortIndicatorShown(true);
62 headerView->setMovable(false);
63 sortByColumn(0, Qt::AscendingOrder);
64 setSortingEnabled(true);
65 installEventFilter(this);
66 if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(1);
67 if (!KdenliveSettings::showratingcolumn()) hideColumn(2);
70 ProjectListView::~ProjectListView()
74 void ProjectListView::processLayout()
76 executeDelayedItemsLayout();
79 void ProjectListView::configureColumns(const QPoint& pos)
82 popup.addTitle(i18nc("@title:menu", "Columns"));
84 QHeaderView* headerView = header();
85 for (int i = 1; i < headerView->count(); ++i) {
86 const QString text = model()->headerData(i, Qt::Horizontal).toString();
87 QAction* action = popup.addAction(text);
88 action->setCheckable(true);
89 action->setChecked(!headerView->isSectionHidden(i));
93 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
94 if (activatedAction != 0) {
95 const bool show = activatedAction->isChecked();
97 // remember the changed column visibility in the settings
98 const int columnIndex = activatedAction->data().toInt();
99 switch (columnIndex) {
101 KdenliveSettings::setShowdescriptioncolumn(show);
104 KdenliveSettings::setShowratingcolumn(show);
110 // apply the changed column visibility
112 showColumn(columnIndex);
114 hideColumn(columnIndex);
120 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
122 emit requestMenu(event->globalPos(), itemAt(event->pos()));
125 void ProjectListView::slotCollapsed(QTreeWidgetItem *item)
127 if (item->type() == PROJECTFOLDERTYPE) {
129 static_cast <FolderProjectItem *>(item)->switchIcon();
134 void ProjectListView::slotExpanded(QTreeWidgetItem *item)
136 if (item->type() == PROJECTFOLDERTYPE) {
138 static_cast <FolderProjectItem *>(item)->switchIcon();
143 bool ProjectListView::eventFilter(QObject *obj, QEvent *event)
145 if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
146 QKeyEvent* ke = (QKeyEvent*) event;
147 if (ke->key() == Qt::Key_Plus) {
148 if (currentItem()) currentItem()->setExpanded(true);
151 } else if (ke->key() == Qt::Key_Minus) {
152 if (currentItem()) currentItem()->setExpanded(false);
159 // pass the event on to the parent class
160 return QTreeWidget::eventFilter(obj, event);
165 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
167 QTreeWidgetItem *it = itemAt(event->pos());
173 if (it->type() == PROJECTFOLDERTYPE) {
174 if ((columnAt(event->pos().x()) == 0)) {
175 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
176 int offset = pix.width() + indentation();
177 if (event->pos().x() < offset) {
178 it->setExpanded(!it->isExpanded());
180 } else QTreeWidget::mouseDoubleClickEvent(event);
184 if (it->type() == PROJECTSUBCLIPTYPE) {
186 if ((columnAt(event->pos().x()) == 1)) {
187 QTreeWidget::mouseDoubleClickEvent(event);
190 item = static_cast <ProjectItem *>(it->parent());
191 } else item = static_cast <ProjectItem *>(it);
193 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
195 int column = columnAt(event->pos().x());
196 if (column == 0 && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR || it->childCount() > 0)) {
197 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
198 int offset = pix.width() + indentation();
199 if (item->parent()) offset += indentation();
200 if (it->childCount() > 0) {
201 if (offset > event->pos().x()) {
202 it->setExpanded(!it->isExpanded());
206 } else if (pix.isNull() || offset < event->pos().x()) {
207 QTreeWidget::mouseDoubleClickEvent(event);
211 if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
212 QTreeWidget::mouseDoubleClickEvent(event);
215 emit showProperties(item->referencedClip());
220 void ProjectListView::dropEvent(QDropEvent *event)
222 FolderProjectItem *item = NULL;
223 QTreeWidgetItem *it = itemAt(event->pos());
224 while (it && it->type() != PROJECTFOLDERTYPE) {
227 if (it) item = static_cast <FolderProjectItem *>(it);
228 if (event->mimeData()->hasUrls()) {
232 groupName = item->groupName();
233 groupId = item->clipId();
235 emit addClip(event->mimeData()->urls(), groupName, groupId);
236 event->setDropAction(Qt::CopyAction);
238 QTreeWidget::dropEvent(event);
240 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
242 //emit addClip(event->mimeData->text());
243 const QList <QTreeWidgetItem *> list = selectedItems();
245 QString parentId = item->clipId();
246 foreach(QTreeWidgetItem *it, list) {
247 // TODO allow dragging of folders ?
248 if (it->type() == PROJECTCLIPTYPE) {
249 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
250 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
252 item->addChild(clone);
253 QMap <QString, QString> props;
254 props.insert("groupname", item->groupName());
255 props.insert("groupid", parentId);
256 clone->setProperties(props);
261 // item dropped in empty zone, move it to top level
262 const QList <QTreeWidgetItem *> list = selectedItems();
264 foreach(QTreeWidgetItem *it, list) {
265 QTreeWidgetItem *parent = it->parent();
266 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
267 kDebug() << "++ item parent: " << parent->text(1);
268 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
270 addTopLevelItem(clone);
271 clone->clearProperty("groupname");
272 clone->clearProperty("groupid");
277 emit projectModified();
278 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
279 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
280 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
282 event->acceptProposedAction();
283 QTreeWidget::dropEvent(event);
287 void ProjectListView::mousePressEvent(QMouseEvent *event)
289 if (event->button() == Qt::LeftButton) {
290 m_DragStartPosition = event->pos();
291 m_dragStarted = true;
292 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
293 if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
295 QTreeWidget::mousePressEvent(event);
299 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
301 QTreeWidget::mouseReleaseEvent(event);
302 QTreeWidgetItem *underMouse = itemAt(event->pos());
303 if (underMouse) emit focusMonitor();
307 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
309 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
310 if (!m_dragStarted) return;
312 if ((event->pos() - m_DragStartPosition).manhattanLength()
313 < QApplication::startDragDistance())
316 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
318 if (it->type() == PROJECTSUBCLIPTYPE) {
320 SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
321 if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
322 ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
323 QDrag *drag = new QDrag(this);
324 QMimeData *mimeData = new QMimeData;
327 list.append(clip->clipId());
328 QPoint p = clickItem->zone();
329 list.append(QString::number(p.x()));
330 list.append(QString::number(p.y()));
332 data.append(list.join(";").toUtf8());
333 mimeData->setData("kdenlive/clip", data);
334 drag->setMimeData(mimeData);
335 drag->setPixmap(clickItem->data(0, Qt::DecorationRole).value<QPixmap>());
336 drag->setHotSpot(QPoint(0, 50));
340 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
341 QDrag *drag = new QDrag(this);
342 QMimeData *mimeData = new QMimeData;
343 const QList <QTreeWidgetItem *> list = selectedItems();
345 foreach(const QTreeWidgetItem *item, list) {
346 if (item->type() == PROJECTFOLDERTYPE) {
347 const int children = item->childCount();
348 for (int i = 0; i < children; i++) {
349 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
352 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
353 ids.append(clip->clipId());
356 if (ids.isEmpty()) return;
358 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
359 mimeData->setData("kdenlive/producerslist", data);
360 //mimeData->setText(ids.join(";")); //doc.toString());
361 //mimeData->setImageData(image);
362 drag->setMimeData(mimeData);
363 drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
364 drag->setHotSpot(QPoint(0, 50));
372 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
374 // stop playing because we get a crash otherwise when fetching the thumbnails
376 QTreeWidget::dragLeaveEvent(event);
379 QStringList ProjectListView::mimeTypes() const
381 QStringList qstrList;
382 qstrList << QTreeWidget::mimeTypes();
383 // list of accepted mime types for drop
384 qstrList.append("text/uri-list");
385 qstrList.append("text/plain");
386 qstrList.append("kdenlive/producerslist");
387 qstrList.append("kdenlive/clip");
392 Qt::DropActions ProjectListView::supportedDropActions() const
394 // returns what actions are supported when dropping
395 return Qt::MoveAction | Qt::CopyAction;
398 #include "projectlistview.moc"