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"
24 #include <QApplication>
25 #include <QHeaderView>
31 #include <KLocalizedString>
34 #include "projectlistview.h"
35 #include "projectitem.h"
36 #include "subprojectitem.h"
37 #include "folderprojectitem.h"
38 #include "kdenlivesettings.h"
40 ProjectListView::ProjectListView(QWidget *parent)
42 , m_dragStarted(false)
44 setSelectionMode(QAbstractItemView::ExtendedSelection);
45 setDragDropMode(QAbstractItemView::DragDrop);
46 setDropIndicatorShown(true);
47 setAlternatingRowColors(true);
50 setFrameShape(QFrame::NoFrame);
51 setRootIsDecorated(true);
57 headers << i18n("Clip") << i18n("Description") << i18n("Rating") << i18n("Date");
58 setHeaderLabels(headers);
61 QHeaderView* headerView = header();
62 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
63 connect(headerView, SIGNAL(customContextMenuRequested(QPoint)),
64 this, SLOT(configureColumns(QPoint)));
65 connect(this, SIGNAL(itemCollapsed(QTreeWidgetItem*)), this, SLOT(slotCollapsed(QTreeWidgetItem*)));
66 connect(this, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(slotExpanded(QTreeWidgetItem*)));
67 headerView->setClickable(true);
68 headerView->setSortIndicatorShown(true);
69 headerView->setMovable(false);
70 sortByColumn(0, Qt::AscendingOrder);
71 setSortingEnabled(true);
72 installEventFilter(this);
74 if (!KdenliveSettings::showdescriptioncolumn()) {
77 if (!KdenliveSettings::showratingcolumn()) {
80 if (!KdenliveSettings::showdatecolumn()) {
85 ProjectListView::~ProjectListView()
89 void ProjectListView::updateStyleSheet()
91 QString style = "QTreeView::branch:has-siblings:!adjoins-item{border-image: none;border:0px} \
92 QTreeView::branch:has-siblings:adjoins-item {border-image: none;border:0px} \
93 QTreeView::branch:!has-children:!has-siblings:adjoins-item {border-image: none;border:0px} \
94 QTreeView::branch:has-children:!has-siblings:closed,QTreeView::branch:closed:has-children:has-siblings { \
95 border-image: none;image: url(:/images/stylesheet-branch-closed.png);} \
96 QTreeView::branch:open:has-children:!has-siblings,QTreeView::branch:open:has-children:has-siblings { \
97 border-image: none;image: url(:/images/stylesheet-branch-open.png);}";
101 void ProjectListView::processLayout()
103 executeDelayedItemsLayout();
106 void ProjectListView::configureColumns(const QPoint& pos)
109 popup.addTitle(i18nc("@title:menu", "Columns"));
111 QHeaderView* headerView = header();
112 for (int i = 1; i < headerView->count(); ++i) {
113 const QString text = model()->headerData(i, Qt::Horizontal).toString();
114 QAction* action = popup.addAction(text);
115 action->setCheckable(true);
116 action->setChecked(!headerView->isSectionHidden(i));
120 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
121 if (activatedAction != 0) {
122 const bool show = activatedAction->isChecked();
124 // remember the changed column visibility in the settings
125 const int columnIndex = activatedAction->data().toInt();
126 switch (columnIndex) {
128 KdenliveSettings::setShowdescriptioncolumn(show);
131 KdenliveSettings::setShowratingcolumn(show);
134 KdenliveSettings::setShowdatecolumn(show);
140 // apply the changed column visibility
142 showColumn(columnIndex);
144 hideColumn(columnIndex);
150 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
152 emit requestMenu(event->globalPos(), itemAt(event->pos()));
155 void ProjectListView::slotCollapsed(QTreeWidgetItem *item)
157 if (item->type() == ProjectFoldeType) {
159 static_cast <FolderProjectItem *>(item)->switchIcon();
164 void ProjectListView::slotExpanded(QTreeWidgetItem *item)
166 if (item->type() == ProjectFoldeType) {
168 static_cast <FolderProjectItem *>(item)->switchIcon();
173 bool ProjectListView::eventFilter(QObject *obj, QEvent *event)
175 if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
176 QKeyEvent* ke = (QKeyEvent*) event;
177 if (ke->key() == Qt::Key_Plus) {
178 if (currentItem()) currentItem()->setExpanded(true);
181 } else if (ke->key() == Qt::Key_Minus) {
182 if (currentItem()) currentItem()->setExpanded(false);
189 // pass the event on to the parent class
190 return QTreeWidget::eventFilter(obj, event);
195 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
197 QTreeWidgetItem *it = itemAt(event->pos());
204 if (it->type() == ProjectFoldeType) {
205 if ((columnAt(event->pos().x()) == 0)) {
206 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
207 int offset = pix.width() + indentation();
208 if (event->pos().x() < offset) {
209 it->setExpanded(!it->isExpanded());
211 } else QTreeWidget::mouseDoubleClickEvent(event);
215 if (it->type() == ProjectSubclipType) {
217 if ((columnAt(event->pos().x()) == 1)) {
218 QTreeWidget::mouseDoubleClickEvent(event);
221 item = static_cast <ProjectItem *>(it->parent());
222 } else item = static_cast <ProjectItem *>(it);
224 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
226 int column = columnAt(event->pos().x());
227 if (column == 0 && (item->clipType() == SlideShow || item->clipType() == Text || item->clipType() == Color || it->childCount() > 0)) {
228 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
229 int offset = pix.width() + indentation();
230 if (item->parent()) offset += indentation();
231 if (it->childCount() > 0) {
232 if (offset > event->pos().x()) {
233 it->setExpanded(!it->isExpanded());
237 } else if (pix.isNull() || offset < event->pos().x()) {
238 QTreeWidget::mouseDoubleClickEvent(event);
242 if ((column == 1) && it->type() != ProjectSubclipType) {
243 QTreeWidget::mouseDoubleClickEvent(event);
246 emit showProperties(item->referencedClip());
251 void ProjectListView::dropEvent(QDropEvent *event)
253 FolderProjectItem *item = NULL;
254 QTreeWidgetItem *it = itemAt(event->pos());
255 while (it && it->type() != ProjectFoldeType) {
258 if (it) item = static_cast <FolderProjectItem *>(it);
259 if (event->mimeData()->hasUrls()) {
263 groupName = item->groupName();
264 groupId = item->clipId();
266 emit addClip(event->mimeData()->urls(), groupName, groupId);
267 event->setDropAction(Qt::CopyAction);
269 QTreeWidget::dropEvent(event);
271 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
273 //emit addClip(event->mimeData->text());
274 const QList <QTreeWidgetItem *> list = selectedItems();
276 QString parentId = item->clipId();
277 foreach(QTreeWidgetItem *it, list) {
278 // TODO allow dragging of folders ?
279 if (it->type() == ProjectClipType) {
280 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
281 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
283 item->addChild(clone);
284 QMap <QString, QString> props;
285 props.insert("groupname", item->groupName());
286 props.insert("groupid", parentId);
287 clone->setProperties(props);
292 // item dropped in empty zone, move it to top level
293 const QList <QTreeWidgetItem *> list = selectedItems();
295 foreach(QTreeWidgetItem *it, list) {
296 if (it->type() != ProjectClipType) continue;
297 QTreeWidgetItem *parent = it->parent();
298 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
299 kDebug() << "++ item parent: " << parent->text(1);
300 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
302 addTopLevelItem(clone);
303 clone->clearProperty("groupname");
304 clone->clearProperty("groupid");
309 emit projectModified();
310 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
311 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
312 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
314 if (event->source() == this) {
315 event->setDropAction(Qt::MoveAction);
318 event->acceptProposedAction();
320 QTreeWidget::dropEvent(event);
324 void ProjectListView::mousePressEvent(QMouseEvent *event)
326 if (event->button() == Qt::LeftButton) {
327 m_DragStartPosition = event->pos();
328 m_dragStarted = true;
329 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
330 ProjectItem *item = static_cast<ProjectItem *>(underMouse);
332 QRect itemRect = visualItemRect(item);
333 if (item->underJobMenu(itemRect, event->pos())) {
337 && underMouse->isSelected()) emit focusMonitor()
340 QTreeWidget::mousePressEvent(event);
344 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
346 QTreeWidget::mouseReleaseEvent(event);
347 QTreeWidgetItem *underMouse = itemAt(event->pos());
348 if (underMouse) emit focusMonitor(true);
352 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
354 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
355 if (!m_dragStarted) return;
357 if ((event->pos() - m_DragStartPosition).manhattanLength()
358 < QApplication::startDragDistance())
361 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
363 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
364 QDrag *drag = new QDrag(this);
365 QMimeData *mimeData = new QMimeData;
366 const QList <QTreeWidgetItem *> list = selectedItems();
368 foreach(const QTreeWidgetItem *item, list) {
369 if (item->type() == ProjectFoldeType) {
370 const int children = item->childCount();
371 for (int i = 0; i < children; ++i) {
372 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
374 } else if (item->type() == ProjectSubclipType) {
375 const ProjectItem *parentclip = static_cast <const ProjectItem *>(item->parent());
376 const SubProjectItem *clickItem = static_cast <const SubProjectItem *>(item);
377 QPoint p = clickItem->zone();
378 QString data = parentclip->clipId();
379 data.append("/" + QString::number(p.x()));
380 data.append("/" + QString::number(p.y()));
383 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
384 ids.append(clip->clipId());
387 if (ids.isEmpty()) return;
389 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
390 mimeData->setData("kdenlive/producerslist", data);
391 //mimeData->setText(ids.join(";")); //doc.toString());
392 //mimeData->setImageData(image);
393 drag->setMimeData(mimeData);
394 drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
395 drag->setHotSpot(QPoint(0, 40));
396 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
401 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
403 // stop playing because we get a crash otherwise when fetching the thumbnails
405 QTreeWidget::dragLeaveEvent(event);
408 QStringList ProjectListView::mimeTypes() const
410 QStringList qstrList;
411 qstrList << QTreeWidget::mimeTypes();
412 // list of accepted mime types for drop
413 qstrList.append("text/uri-list");
414 qstrList.append("text/plain");
415 qstrList.append("kdenlive/producerslist");
416 qstrList.append("kdenlive/clip");
421 Qt::DropActions ProjectListView::supportedDropActions() const
423 // returns what actions are supported when dropping
424 return Qt::MoveAction | Qt::CopyAction;
427 void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
429 if (index.column() == 0 && !index.data(ItemDelegate::DurationRole).isNull()) {
430 QRect r1 = option.rect;
432 QStyleOptionViewItemV4 opt(option);
433 QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
434 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
436 if (option.state & QStyle::State_Selected) {
437 painter->setPen(option.palette.highlightedText().color());
439 const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
440 QPixmap pixmap = qVariantValue<QPixmap>(index.data(Qt::DecorationRole));
441 QPoint pixmapPoint(r1.left() + textMargin, r1.top() + (r1.height() - pixmap.height()) / 2);
442 painter->drawPixmap(pixmapPoint, pixmap);
443 int decoWidth = pixmap.width() + 2 * textMargin;
445 QFont font = painter->font();
447 painter->setFont(font);
448 int mid = (int)((r1.height() / 2));
449 r1.adjust(decoWidth, 0, 0, -mid);
450 QRect r2 = option.rect;
451 r2.adjust(decoWidth, mid, 0, 0);
452 painter->drawText(r1, Qt::AlignLeft | Qt::AlignBottom, index.data().toString());
454 painter->setFont(font);
455 QString subText = index.data(ItemDelegate::DurationRole).toString();
456 int usage = index.data(ItemDelegate::UsageRole).toInt();
458 subText.append(QString::fromLatin1(" (%1)").arg(usage));
462 painter->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter , subText, &bounding);
463 int jobProgress = index.data(Qt::UserRole + 5).toInt();
464 if (jobProgress != 0 && jobProgress != JobDone && jobProgress != JobAborted) {
465 if (jobProgress != JobCrashed) {
466 // Draw job progress bar
467 QColor color = option.palette.alternateBase().color();
469 painter->setPen(option.palette.link().color());
470 QRect progress(pixmapPoint.x() + 2, pixmapPoint.y() + pixmap.height() - 9, pixmap.width() - 4, 7);
471 painter->setBrush(QBrush(color));
472 painter->drawRect(progress);
473 painter->setBrush(option.palette.link());
474 progress.adjust(2, 2, -2, -2);
475 if (jobProgress == JobWaiting) {
476 progress.setLeft(progress.right() - 2);
477 painter->drawRect(progress);
478 progress.moveLeft(progress.left() - 5);
479 painter->drawRect(progress);
481 else if (jobProgress > 0) {
482 progress.setWidth(progress.width() * jobProgress / 100);
483 painter->drawRect(progress);
485 } else if (jobProgress == JobCrashed) {
486 QString jobText = index.data(Qt::UserRole + 7).toString();
487 if (!jobText.isEmpty()) {
488 QRectF txtBounding = painter->boundingRect(r2, Qt::AlignRight | Qt::AlignVCenter, QLatin1Char(' ') + jobText + QLatin1Char(' ') );
489 painter->setPen(Qt::NoPen);
490 painter->setBrush(option.palette.highlight());
491 painter->drawRoundedRect(txtBounding, 2, 2);
492 painter->setPen(option.palette.highlightedText().color());
493 painter->drawText(txtBounding, Qt::AlignCenter, jobText);
499 } else if (index.column() == 2 && KdenliveSettings::activate_nepomuk()) {
500 if (index.data().toString().isEmpty()) {
501 QStyledItemDelegate::paint(painter, option, index);
504 QRect r1 = option.rect;
505 if (option.state & (QStyle::State_Selected)) {
506 painter->fillRect(r1, option.palette.highlight());
509 KRatingPainter::paintRating(painter, r1, Qt::AlignCenter, index.data().toInt());
512 KRatingPainter::paintRating(painter, r1, Qt::AlignCenter, index.data().toInt());
516 QStyledItemDelegate::paint(painter, option, index);
520 #include "projectlistview.moc"