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(false);
50 headers << i18n("Clip") << i18n("Description") << i18n("Rating");
51 setHeaderLabels(headers);
53 QHeaderView* headerView = header();
54 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
55 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
56 this, SLOT(configureColumns(const QPoint&)));
57 connect(this, SIGNAL(itemCollapsed(QTreeWidgetItem *)), this, SLOT(slotCollapsed(QTreeWidgetItem *)));
58 connect(this, SIGNAL(itemExpanded(QTreeWidgetItem *)), this, SLOT(slotExpanded(QTreeWidgetItem *)));
59 headerView->setClickable(true);
60 headerView->setSortIndicatorShown(true);
61 headerView->setMovable(false);
62 sortByColumn(0, Qt::AscendingOrder);
63 setSortingEnabled(true);
64 installEventFilter(this);
65 if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(1);
66 if (!KdenliveSettings::showratingcolumn()) hideColumn(2);
69 ProjectListView::~ProjectListView()
73 void ProjectListView::configureColumns(const QPoint& pos)
76 popup.addTitle(i18nc("@title:menu", "Columns"));
78 QHeaderView* headerView = header();
79 for (int i = 1; i < headerView->count(); ++i) {
80 const QString text = model()->headerData(i, Qt::Horizontal).toString();
81 QAction* action = popup.addAction(text);
82 action->setCheckable(true);
83 action->setChecked(!headerView->isSectionHidden(i));
87 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
88 if (activatedAction != 0) {
89 const bool show = activatedAction->isChecked();
91 // remember the changed column visibility in the settings
92 const int columnIndex = activatedAction->data().toInt();
93 switch (columnIndex) {
95 KdenliveSettings::setShowdescriptioncolumn(show);
98 KdenliveSettings::setShowratingcolumn(show);
104 // apply the changed column visibility
106 showColumn(columnIndex);
108 hideColumn(columnIndex);
114 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
116 emit requestMenu(event->globalPos(), itemAt(event->pos()));
119 void ProjectListView::slotCollapsed(QTreeWidgetItem *item)
121 if (item->type() == PROJECTFOLDERTYPE) {
123 static_cast <FolderProjectItem *>(item)->switchIcon();
128 void ProjectListView::slotExpanded(QTreeWidgetItem *item)
130 if (item->type() == PROJECTFOLDERTYPE) {
132 static_cast <FolderProjectItem *>(item)->switchIcon();
137 bool ProjectListView::eventFilter(QObject *obj, QEvent *event)
139 if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
140 QKeyEvent* ke = (QKeyEvent*) event;
141 if (ke->key() == Qt::Key_Plus) {
142 if (currentItem()) currentItem()->setExpanded(true);
145 } else if (ke->key() == Qt::Key_Minus) {
146 if (currentItem()) currentItem()->setExpanded(false);
153 // pass the event on to the parent class
154 return QTreeWidget::eventFilter(obj, event);
159 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
161 QTreeWidgetItem *it = itemAt(event->pos());
167 if (it->type() == PROJECTFOLDERTYPE) {
168 if ((columnAt(event->pos().x()) == 0)) {
169 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
170 int offset = pix.width() + indentation();
171 if (event->pos().x() < offset) {
172 it->setExpanded(!it->isExpanded());
174 } else QTreeWidget::mouseDoubleClickEvent(event);
178 if (it->type() == PROJECTSUBCLIPTYPE) {
180 if ((columnAt(event->pos().x()) == 1)) {
181 QTreeWidget::mouseDoubleClickEvent(event);
184 item = static_cast <ProjectItem *>(it->parent());
185 } else item = static_cast <ProjectItem *>(it);
187 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
189 int column = columnAt(event->pos().x());
190 if (column == 0 && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR || it->childCount() > 0)) {
191 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
192 int offset = pix.width() + indentation();
193 if (item->parent()) offset += indentation();
194 if (it->childCount() > 0) {
195 if (offset > event->pos().x()) {
196 it->setExpanded(!it->isExpanded());
200 } else if (pix.isNull() || offset < event->pos().x()) {
201 QTreeWidget::mouseDoubleClickEvent(event);
205 if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
206 QTreeWidget::mouseDoubleClickEvent(event);
209 emit showProperties(item->referencedClip());
214 void ProjectListView::dropEvent(QDropEvent *event)
216 FolderProjectItem *item = NULL;
217 QTreeWidgetItem *it = itemAt(event->pos());
218 while (it && it->type() != PROJECTFOLDERTYPE) {
221 if (it) item = static_cast <FolderProjectItem *>(it);
222 if (event->mimeData()->hasUrls()) {
226 groupName = item->groupName();
227 groupId = item->clipId();
229 emit addClip(event->mimeData()->urls(), groupName, groupId);
230 event->setDropAction(Qt::CopyAction);
232 QTreeWidget::dropEvent(event);
234 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
236 //emit addClip(event->mimeData->text());
237 const QList <QTreeWidgetItem *> list = selectedItems();
239 QString parentId = item->clipId();
240 foreach(QTreeWidgetItem *it, list) {
241 // TODO allow dragging of folders ?
242 if (it->type() == PROJECTCLIPTYPE) {
243 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
244 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
246 item->addChild(clone);
247 QMap <QString, QString> props;
248 props.insert("groupname", item->groupName());
249 props.insert("groupid", parentId);
250 clone->setProperties(props);
255 // item dropped in empty zone, move it to top level
256 const QList <QTreeWidgetItem *> list = selectedItems();
258 foreach(QTreeWidgetItem *it, list) {
259 QTreeWidgetItem *parent = it->parent();
260 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
261 kDebug() << "++ item parent: " << parent->text(1);
262 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
264 addTopLevelItem(clone);
265 clone->clearProperty("groupname");
266 clone->clearProperty("groupid");
271 emit projectModified();
272 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
273 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
274 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
276 event->acceptProposedAction();
277 QTreeWidget::dropEvent(event);
281 void ProjectListView::mousePressEvent(QMouseEvent *event)
283 if (event->button() == Qt::LeftButton) {
284 m_DragStartPosition = event->pos();
285 m_dragStarted = true;
286 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
287 if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
289 QTreeWidget::mousePressEvent(event);
293 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
295 QTreeWidget::mouseReleaseEvent(event);
296 QTreeWidgetItem *underMouse = itemAt(event->pos());
297 if (underMouse) emit focusMonitor();
301 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
303 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
304 if (!m_dragStarted) return;
306 if ((event->pos() - m_DragStartPosition).manhattanLength()
307 < QApplication::startDragDistance())
310 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
312 if (it->type() == PROJECTSUBCLIPTYPE) {
314 SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
315 if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
316 ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
317 QDrag *drag = new QDrag(this);
318 QMimeData *mimeData = new QMimeData;
321 list.append(clip->clipId());
322 QPoint p = clickItem->zone();
323 list.append(QString::number(p.x()));
324 list.append(QString::number(p.y()));
326 data.append(list.join(";").toUtf8());
327 mimeData->setData("kdenlive/clip", data);
328 drag->setMimeData(mimeData);
329 drag->setPixmap(clickItem->data(0, Qt::DecorationRole).value<QPixmap>());
330 drag->setHotSpot(QPoint(0, 50));
334 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
335 QDrag *drag = new QDrag(this);
336 QMimeData *mimeData = new QMimeData;
337 const QList <QTreeWidgetItem *> list = selectedItems();
339 foreach(const QTreeWidgetItem *item, list) {
340 if (item->type() == PROJECTFOLDERTYPE) {
341 const int children = item->childCount();
342 for (int i = 0; i < children; i++) {
343 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
346 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
347 ids.append(clip->clipId());
350 if (ids.isEmpty()) return;
352 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
353 mimeData->setData("kdenlive/producerslist", data);
354 //mimeData->setText(ids.join(";")); //doc.toString());
355 //mimeData->setImageData(image);
356 drag->setMimeData(mimeData);
357 drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
358 drag->setHotSpot(QPoint(0, 50));
366 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
368 // stop playing because we get a crash otherwise when fetching the thumbnails
370 QTreeWidget::dragLeaveEvent(event);
373 QStringList ProjectListView::mimeTypes() const
375 QStringList qstrList;
376 qstrList << QTreeWidget::mimeTypes();
377 // list of accepted mime types for drop
378 qstrList.append("text/uri-list");
379 qstrList.append("text/plain");
380 qstrList.append("kdenlive/producerslist");
381 qstrList.append("kdenlive/clip");
386 Qt::DropActions ProjectListView::supportedDropActions() const
388 // returns what actions are supported when dropping
389 return Qt::MoveAction | Qt::CopyAction;
392 #include "projectlistview.moc"