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 0;} \
49 QTreeView::branch:has-siblings:adjoins-item {border-image: none 0;} \
50 QTreeView::branch:!has-children:!has-siblings:adjoins-item {border-image: none 0;} \
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");
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);
80 ProjectListView::~ProjectListView()
84 void ProjectListView::processLayout()
86 executeDelayedItemsLayout();
89 void ProjectListView::configureColumns(const QPoint& pos)
92 popup.addTitle(i18nc("@title:menu", "Columns"));
94 QHeaderView* headerView = header();
95 for (int i = 1; i < headerView->count(); ++i) {
96 const QString text = model()->headerData(i, Qt::Horizontal).toString();
97 QAction* action = popup.addAction(text);
98 action->setCheckable(true);
99 action->setChecked(!headerView->isSectionHidden(i));
103 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
104 if (activatedAction != 0) {
105 const bool show = activatedAction->isChecked();
107 // remember the changed column visibility in the settings
108 const int columnIndex = activatedAction->data().toInt();
109 switch (columnIndex) {
111 KdenliveSettings::setShowdescriptioncolumn(show);
114 KdenliveSettings::setShowratingcolumn(show);
120 // apply the changed column visibility
122 showColumn(columnIndex);
124 hideColumn(columnIndex);
130 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
132 emit requestMenu(event->globalPos(), itemAt(event->pos()));
135 void ProjectListView::slotCollapsed(QTreeWidgetItem *item)
137 if (item->type() == PROJECTFOLDERTYPE) {
139 static_cast <FolderProjectItem *>(item)->switchIcon();
144 void ProjectListView::slotExpanded(QTreeWidgetItem *item)
146 if (item->type() == PROJECTFOLDERTYPE) {
148 static_cast <FolderProjectItem *>(item)->switchIcon();
153 bool ProjectListView::eventFilter(QObject *obj, QEvent *event)
155 if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
156 QKeyEvent* ke = (QKeyEvent*) event;
157 if (ke->key() == Qt::Key_Plus) {
158 if (currentItem()) currentItem()->setExpanded(true);
161 } else if (ke->key() == Qt::Key_Minus) {
162 if (currentItem()) currentItem()->setExpanded(false);
169 // pass the event on to the parent class
170 return QTreeWidget::eventFilter(obj, event);
175 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
177 QTreeWidgetItem *it = itemAt(event->pos());
183 if (it->type() == PROJECTFOLDERTYPE) {
184 if ((columnAt(event->pos().x()) == 0)) {
185 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
186 int offset = pix.width() + indentation();
187 if (event->pos().x() < offset) {
188 it->setExpanded(!it->isExpanded());
190 } else QTreeWidget::mouseDoubleClickEvent(event);
194 if (it->type() == PROJECTSUBCLIPTYPE) {
196 if ((columnAt(event->pos().x()) == 1)) {
197 QTreeWidget::mouseDoubleClickEvent(event);
200 item = static_cast <ProjectItem *>(it->parent());
201 } else item = static_cast <ProjectItem *>(it);
203 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
205 int column = columnAt(event->pos().x());
206 if (column == 0 && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR || it->childCount() > 0)) {
207 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
208 int offset = pix.width() + indentation();
209 if (item->parent()) offset += indentation();
210 if (it->childCount() > 0) {
211 if (offset > event->pos().x()) {
212 it->setExpanded(!it->isExpanded());
216 } else if (pix.isNull() || offset < event->pos().x()) {
217 QTreeWidget::mouseDoubleClickEvent(event);
221 if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
222 QTreeWidget::mouseDoubleClickEvent(event);
225 emit showProperties(item->referencedClip());
230 void ProjectListView::dropEvent(QDropEvent *event)
232 FolderProjectItem *item = NULL;
233 QTreeWidgetItem *it = itemAt(event->pos());
234 while (it && it->type() != PROJECTFOLDERTYPE) {
237 if (it) item = static_cast <FolderProjectItem *>(it);
238 if (event->mimeData()->hasUrls()) {
242 groupName = item->groupName();
243 groupId = item->clipId();
245 emit addClip(event->mimeData()->urls(), groupName, groupId);
246 event->setDropAction(Qt::CopyAction);
248 QTreeWidget::dropEvent(event);
250 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
252 //emit addClip(event->mimeData->text());
253 const QList <QTreeWidgetItem *> list = selectedItems();
255 QString parentId = item->clipId();
256 foreach(QTreeWidgetItem *it, list) {
257 // TODO allow dragging of folders ?
258 if (it->type() == PROJECTCLIPTYPE) {
259 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
260 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
262 item->addChild(clone);
263 QMap <QString, QString> props;
264 props.insert("groupname", item->groupName());
265 props.insert("groupid", parentId);
266 clone->setProperties(props);
271 // item dropped in empty zone, move it to top level
272 const QList <QTreeWidgetItem *> list = selectedItems();
274 foreach(QTreeWidgetItem *it, list) {
275 QTreeWidgetItem *parent = it->parent();
276 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
277 kDebug() << "++ item parent: " << parent->text(1);
278 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
280 addTopLevelItem(clone);
281 clone->clearProperty("groupname");
282 clone->clearProperty("groupid");
287 emit projectModified();
288 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
289 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
290 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
292 event->acceptProposedAction();
293 QTreeWidget::dropEvent(event);
297 void ProjectListView::mousePressEvent(QMouseEvent *event)
299 if (event->button() == Qt::LeftButton) {
300 m_DragStartPosition = event->pos();
301 m_dragStarted = true;
302 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
303 if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
305 QTreeWidget::mousePressEvent(event);
309 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
311 QTreeWidget::mouseReleaseEvent(event);
312 QTreeWidgetItem *underMouse = itemAt(event->pos());
313 if (underMouse) emit focusMonitor();
317 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
319 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
320 if (!m_dragStarted) return;
322 if ((event->pos() - m_DragStartPosition).manhattanLength()
323 < QApplication::startDragDistance())
326 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
328 if (it->type() == PROJECTSUBCLIPTYPE) {
330 SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
331 if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
332 ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
333 QDrag *drag = new QDrag(this);
334 QMimeData *mimeData = new QMimeData;
337 list.append(clip->clipId());
338 QPoint p = clickItem->zone();
339 list.append(QString::number(p.x()));
340 list.append(QString::number(p.y()));
342 data.append(list.join(";").toUtf8());
343 mimeData->setData("kdenlive/clip", data);
344 drag->setMimeData(mimeData);
345 drag->setPixmap(clickItem->data(0, Qt::DecorationRole).value<QPixmap>());
346 drag->setHotSpot(QPoint(0, 50));
350 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
351 QDrag *drag = new QDrag(this);
352 QMimeData *mimeData = new QMimeData;
353 const QList <QTreeWidgetItem *> list = selectedItems();
355 foreach(const QTreeWidgetItem *item, list) {
356 if (item->type() == PROJECTFOLDERTYPE) {
357 const int children = item->childCount();
358 for (int i = 0; i < children; i++) {
359 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
362 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
363 ids.append(clip->clipId());
366 if (ids.isEmpty()) return;
368 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
369 mimeData->setData("kdenlive/producerslist", data);
370 //mimeData->setText(ids.join(";")); //doc.toString());
371 //mimeData->setImageData(image);
372 drag->setMimeData(mimeData);
373 drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
374 drag->setHotSpot(QPoint(0, 50));
382 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
384 // stop playing because we get a crash otherwise when fetching the thumbnails
386 QTreeWidget::dragLeaveEvent(event);
389 QStringList ProjectListView::mimeTypes() const
391 QStringList qstrList;
392 qstrList << QTreeWidget::mimeTypes();
393 // list of accepted mime types for drop
394 qstrList.append("text/uri-list");
395 qstrList.append("text/plain");
396 qstrList.append("kdenlive/producerslist");
397 qstrList.append("kdenlive/clip");
402 Qt::DropActions ProjectListView::supportedDropActions() const
404 // returns what actions are supported when dropping
405 return Qt::MoveAction | Qt::CopyAction;
408 #include "projectlistview.moc"