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::processLayout()
75 executeDelayedItemsLayout();
78 void ProjectListView::configureColumns(const QPoint& pos)
81 popup.addTitle(i18nc("@title:menu", "Columns"));
83 QHeaderView* headerView = header();
84 for (int i = 1; i < headerView->count(); ++i) {
85 const QString text = model()->headerData(i, Qt::Horizontal).toString();
86 QAction* action = popup.addAction(text);
87 action->setCheckable(true);
88 action->setChecked(!headerView->isSectionHidden(i));
92 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
93 if (activatedAction != 0) {
94 const bool show = activatedAction->isChecked();
96 // remember the changed column visibility in the settings
97 const int columnIndex = activatedAction->data().toInt();
98 switch (columnIndex) {
100 KdenliveSettings::setShowdescriptioncolumn(show);
103 KdenliveSettings::setShowratingcolumn(show);
109 // apply the changed column visibility
111 showColumn(columnIndex);
113 hideColumn(columnIndex);
119 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
121 emit requestMenu(event->globalPos(), itemAt(event->pos()));
124 void ProjectListView::slotCollapsed(QTreeWidgetItem *item)
126 if (item->type() == PROJECTFOLDERTYPE) {
128 static_cast <FolderProjectItem *>(item)->switchIcon();
133 void ProjectListView::slotExpanded(QTreeWidgetItem *item)
135 if (item->type() == PROJECTFOLDERTYPE) {
137 static_cast <FolderProjectItem *>(item)->switchIcon();
142 bool ProjectListView::eventFilter(QObject *obj, QEvent *event)
144 if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
145 QKeyEvent* ke = (QKeyEvent*) event;
146 if (ke->key() == Qt::Key_Plus) {
147 if (currentItem()) currentItem()->setExpanded(true);
150 } else if (ke->key() == Qt::Key_Minus) {
151 if (currentItem()) currentItem()->setExpanded(false);
158 // pass the event on to the parent class
159 return QTreeWidget::eventFilter(obj, event);
164 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
166 QTreeWidgetItem *it = itemAt(event->pos());
172 if (it->type() == PROJECTFOLDERTYPE) {
173 if ((columnAt(event->pos().x()) == 0)) {
174 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
175 int offset = pix.width() + indentation();
176 if (event->pos().x() < offset) {
177 it->setExpanded(!it->isExpanded());
179 } else QTreeWidget::mouseDoubleClickEvent(event);
183 if (it->type() == PROJECTSUBCLIPTYPE) {
185 if ((columnAt(event->pos().x()) == 1)) {
186 QTreeWidget::mouseDoubleClickEvent(event);
189 item = static_cast <ProjectItem *>(it->parent());
190 } else item = static_cast <ProjectItem *>(it);
192 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
194 int column = columnAt(event->pos().x());
195 if (column == 0 && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR || it->childCount() > 0)) {
196 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
197 int offset = pix.width() + indentation();
198 if (item->parent()) offset += indentation();
199 if (it->childCount() > 0) {
200 if (offset > event->pos().x()) {
201 it->setExpanded(!it->isExpanded());
205 } else if (pix.isNull() || offset < event->pos().x()) {
206 QTreeWidget::mouseDoubleClickEvent(event);
210 if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
211 QTreeWidget::mouseDoubleClickEvent(event);
214 emit showProperties(item->referencedClip());
219 void ProjectListView::dropEvent(QDropEvent *event)
221 FolderProjectItem *item = NULL;
222 QTreeWidgetItem *it = itemAt(event->pos());
223 while (it && it->type() != PROJECTFOLDERTYPE) {
226 if (it) item = static_cast <FolderProjectItem *>(it);
227 if (event->mimeData()->hasUrls()) {
231 groupName = item->groupName();
232 groupId = item->clipId();
234 emit addClip(event->mimeData()->urls(), groupName, groupId);
235 event->setDropAction(Qt::CopyAction);
237 QTreeWidget::dropEvent(event);
239 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
241 //emit addClip(event->mimeData->text());
242 const QList <QTreeWidgetItem *> list = selectedItems();
244 QString parentId = item->clipId();
245 foreach(QTreeWidgetItem *it, list) {
246 // TODO allow dragging of folders ?
247 if (it->type() == PROJECTCLIPTYPE) {
248 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
249 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
251 item->addChild(clone);
252 QMap <QString, QString> props;
253 props.insert("groupname", item->groupName());
254 props.insert("groupid", parentId);
255 clone->setProperties(props);
260 // item dropped in empty zone, move it to top level
261 const QList <QTreeWidgetItem *> list = selectedItems();
263 foreach(QTreeWidgetItem *it, list) {
264 QTreeWidgetItem *parent = it->parent();
265 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
266 kDebug() << "++ item parent: " << parent->text(1);
267 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
269 addTopLevelItem(clone);
270 clone->clearProperty("groupname");
271 clone->clearProperty("groupid");
276 emit projectModified();
277 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
278 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
279 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
281 event->acceptProposedAction();
282 QTreeWidget::dropEvent(event);
286 void ProjectListView::mousePressEvent(QMouseEvent *event)
288 if (event->button() == Qt::LeftButton) {
289 m_DragStartPosition = event->pos();
290 m_dragStarted = true;
291 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
292 if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
294 QTreeWidget::mousePressEvent(event);
298 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
300 QTreeWidget::mouseReleaseEvent(event);
301 QTreeWidgetItem *underMouse = itemAt(event->pos());
302 if (underMouse) emit focusMonitor();
306 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
308 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
309 if (!m_dragStarted) return;
311 if ((event->pos() - m_DragStartPosition).manhattanLength()
312 < QApplication::startDragDistance())
315 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
317 if (it->type() == PROJECTSUBCLIPTYPE) {
319 SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
320 if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
321 ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
322 QDrag *drag = new QDrag(this);
323 QMimeData *mimeData = new QMimeData;
326 list.append(clip->clipId());
327 QPoint p = clickItem->zone();
328 list.append(QString::number(p.x()));
329 list.append(QString::number(p.y()));
331 data.append(list.join(";").toUtf8());
332 mimeData->setData("kdenlive/clip", data);
333 drag->setMimeData(mimeData);
334 drag->setPixmap(clickItem->data(0, Qt::DecorationRole).value<QPixmap>());
335 drag->setHotSpot(QPoint(0, 50));
339 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
340 QDrag *drag = new QDrag(this);
341 QMimeData *mimeData = new QMimeData;
342 const QList <QTreeWidgetItem *> list = selectedItems();
344 foreach(const QTreeWidgetItem *item, list) {
345 if (item->type() == PROJECTFOLDERTYPE) {
346 const int children = item->childCount();
347 for (int i = 0; i < children; i++) {
348 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
351 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
352 ids.append(clip->clipId());
355 if (ids.isEmpty()) return;
357 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
358 mimeData->setData("kdenlive/producerslist", data);
359 //mimeData->setText(ids.join(";")); //doc.toString());
360 //mimeData->setImageData(image);
361 drag->setMimeData(mimeData);
362 drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
363 drag->setHotSpot(QPoint(0, 50));
371 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
373 // stop playing because we get a crash otherwise when fetching the thumbnails
375 QTreeWidget::dragLeaveEvent(event);
378 QStringList ProjectListView::mimeTypes() const
380 QStringList qstrList;
381 qstrList << QTreeWidget::mimeTypes();
382 // list of accepted mime types for drop
383 qstrList.append("text/uri-list");
384 qstrList.append("text/plain");
385 qstrList.append("kdenlive/producerslist");
386 qstrList.append("kdenlive/clip");
391 Qt::DropActions ProjectListView::supportedDropActions() const
393 // returns what actions are supported when dropping
394 return Qt::MoveAction | Qt::CopyAction;
397 #include "projectlistview.moc"