]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
af26e4caafc75f1f9231ead0e1b44f8277e79984
[kdenlive] / src / projectlistview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20 // Self
21 #include "projectlistview.h"
22
23 // Qt
24 #include <QApplication>
25 #include <QHeaderView>
26 #include <QAction>
27
28 // KDE
29 #include <KDebug>
30 #include <KMenu>
31 #include <KLocalizedString>
32
33 // KDEnlive
34 #include "projectlistview.h"
35 #include "projectitem.h"
36 #include "subprojectitem.h"
37 #include "folderprojectitem.h"
38 #include "kdenlivesettings.h"
39
40 ProjectListView::ProjectListView(QWidget *parent)
41     : QTreeWidget(parent)
42     , m_dragStarted(false)
43 {
44     setSelectionMode(QAbstractItemView::ExtendedSelection);
45     setDragDropMode(QAbstractItemView::DragDrop);
46     setDropIndicatorShown(true);
47     setAlternatingRowColors(true);
48     setDragEnabled(true);
49     setAcceptDrops(true);
50     setFrameShape(QFrame::NoFrame);
51     setRootIsDecorated(true);
52
53     updateStyleSheet();
54
55     setColumnCount(4);
56     QStringList headers;
57     headers << i18n("Clip") << i18n("Description") << i18n("Rating") << i18n("Date");
58     setHeaderLabels(headers);
59     setIndentation(12);
60     
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);
73
74     if (!KdenliveSettings::showdescriptioncolumn()) {
75         hideColumn(1);
76     }
77     if (!KdenliveSettings::showratingcolumn()) {
78         hideColumn(2);
79     }
80     if (!KdenliveSettings::showdatecolumn()) {
81         hideColumn(3);
82     }
83 }
84
85 ProjectListView::~ProjectListView()
86 {
87 }
88
89 void ProjectListView::updateStyleSheet()
90 {
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);}";
98     setStyleSheet(style);
99 }
100
101 void ProjectListView::processLayout()
102 {
103     executeDelayedItemsLayout();
104 }
105
106 void ProjectListView::configureColumns(const QPoint& pos)
107 {
108     KMenu popup(this);
109     popup.addTitle(i18nc("@title:menu", "Columns"));
110
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));
117         action->setData(i);
118     }
119
120     QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
121     if (activatedAction != 0) {
122         const bool show = activatedAction->isChecked();
123
124         // remember the changed column visibility in the settings
125         const int columnIndex = activatedAction->data().toInt();
126         switch (columnIndex) {
127         case 1:
128             KdenliveSettings::setShowdescriptioncolumn(show);
129             break;
130         case 2:
131             KdenliveSettings::setShowratingcolumn(show);
132             break;
133         case 3:
134             KdenliveSettings::setShowdatecolumn(show);
135             break;
136         default:
137             break;
138         }
139
140         // apply the changed column visibility
141         if (show) {
142             showColumn(columnIndex);
143         } else {
144             hideColumn(columnIndex);
145         }
146     }
147 }
148
149 // virtual
150 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
151 {
152     emit requestMenu(event->globalPos(), itemAt(event->pos()));
153 }
154
155 void ProjectListView::slotCollapsed(QTreeWidgetItem *item)
156 {
157     if (item->type() == PROJECTFOLDERTYPE) {
158         blockSignals(true);
159         static_cast <FolderProjectItem *>(item)->switchIcon();
160         blockSignals(false);
161     }
162 }
163
164 void ProjectListView::slotExpanded(QTreeWidgetItem *item)
165 {
166     if (item->type() == PROJECTFOLDERTYPE) {
167         blockSignals(true);
168         static_cast <FolderProjectItem *>(item)->switchIcon();
169         blockSignals(false);
170     }
171 }
172
173 bool ProjectListView::eventFilter(QObject *obj, QEvent *event)
174 {
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);
179             event->accept();
180             return true;
181         } else if (ke->key() == Qt::Key_Minus) {
182             if (currentItem()) currentItem()->setExpanded(false);
183             event->accept();
184             return true;
185         } else {
186             return false;
187         }
188     } else {
189         // pass the event on to the parent class
190         return QTreeWidget::eventFilter(obj, event);
191     }
192 }
193
194 // virtual
195 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
196 {
197     QTreeWidgetItem *it = itemAt(event->pos());
198     if (!it) {
199         emit pauseMonitor();
200         emit addClip();
201         return;
202     }
203     ProjectItem *item;
204     if (it->type() == PROJECTFOLDERTYPE) {
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());
210                 event->accept();
211             } else QTreeWidget::mouseDoubleClickEvent(event);
212         }
213         return;
214     }
215     if (it->type() == PROJECTSUBCLIPTYPE) {
216         // subitem
217         if ((columnAt(event->pos().x()) == 1)) {
218             QTreeWidget::mouseDoubleClickEvent(event);
219             return;
220         }
221         item = static_cast <ProjectItem *>(it->parent());
222     } else item = static_cast <ProjectItem *>(it);
223
224     if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
225
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());
234                 event->accept();
235                 return;
236             }
237         } else if (pix.isNull() || offset < event->pos().x()) {
238             QTreeWidget::mouseDoubleClickEvent(event);
239             return;
240         }
241     }
242     if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
243         QTreeWidget::mouseDoubleClickEvent(event);
244         return;
245     }
246     emit showProperties(item->referencedClip());
247 }
248
249
250 // virtual
251 void ProjectListView::dropEvent(QDropEvent *event)
252 {
253     FolderProjectItem *item = NULL;
254     QTreeWidgetItem *it = itemAt(event->pos());
255     while (it && it->type() != PROJECTFOLDERTYPE) {
256         it = it->parent();
257     }
258     if (it) item = static_cast <FolderProjectItem *>(it);
259     if (event->mimeData()->hasUrls()) {
260         QString groupName;
261         QString groupId;
262         if (item) {
263             groupName = item->groupName();
264             groupId = item->clipId();
265         }
266         emit addClip(event->mimeData()->urls(), groupName, groupId);
267         event->setDropAction(Qt::CopyAction);
268         event->accept();
269         QTreeWidget::dropEvent(event);
270         return;
271     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
272         if (item) {
273             //emit addClip(event->mimeData->text());
274             const QList <QTreeWidgetItem *> list = selectedItems();
275             ProjectItem *clone;
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));
282                     if (clone && item) {
283                         item->addChild(clone);
284                         QMap <QString, QString> props;
285                         props.insert("groupname", item->groupName());
286                         props.insert("groupid", parentId);
287                         clone->setProperties(props);
288                     }
289                 } else item = NULL;
290             }
291         } else {
292             // item dropped in empty zone, move it to top level
293             const QList <QTreeWidgetItem *> list = selectedItems();
294             ProjectItem *clone;
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)));
301                     if (clone) {
302                         addTopLevelItem(clone);
303                         clone->clearProperty("groupname");
304                         clone->clearProperty("groupid");
305                     }
306                 }
307             }
308         }
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());
313     }
314     if (event->source() == this) {
315         event->setDropAction(Qt::MoveAction);
316         event->accept();
317     } else {
318         event->acceptProposedAction();
319     }
320     QTreeWidget::dropEvent(event);
321 }
322
323 // virtual
324 void ProjectListView::mousePressEvent(QMouseEvent *event)
325 {
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);
331         if (item) {
332             QRect itemRect = visualItemRect(item);
333             if (item->underJobMenu(itemRect, event->pos())) {
334                 emit display
335             }
336             
337             && underMouse->isSelected()) emit focusMonitor()
338         }*/
339     }
340     QTreeWidget::mousePressEvent(event);
341 }
342
343 // virtual
344 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
345 {
346     QTreeWidget::mouseReleaseEvent(event);
347     QTreeWidgetItem *underMouse = itemAt(event->pos());
348     if (underMouse) emit focusMonitor(true);
349 }
350
351 // virtual
352 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
353 {
354     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
355     if (!m_dragStarted) return;
356
357     if ((event->pos() - m_DragStartPosition).manhattanLength()
358             < QApplication::startDragDistance())
359         return;
360
361     QTreeWidgetItem *it = itemAt(m_DragStartPosition);
362     if (!it) return;
363     if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
364         QDrag *drag = new QDrag(this);
365         QMimeData *mimeData = new QMimeData;
366         const QList <QTreeWidgetItem *> list = selectedItems();
367         QStringList ids;
368         foreach(const QTreeWidgetItem *item, list) {
369             if (item->type() == PROJECTFOLDERTYPE) {
370                 const int children = item->childCount();
371                 for (int i = 0; i < children; ++i) {
372                     ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
373                 }
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()));
381                 ids.append(data);
382             } else {
383                 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
384                 ids.append(clip->clipId());
385             }
386         }
387         if (ids.isEmpty()) return;
388         QByteArray data;
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);
397     }
398 }
399
400 // virtual
401 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
402 {
403     // stop playing because we get a crash otherwise when fetching the thumbnails
404     emit pauseMonitor();
405     QTreeWidget::dragLeaveEvent(event);
406 }
407
408 QStringList ProjectListView::mimeTypes() const
409 {
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");
417     return qstrList;
418 }
419
420
421 Qt::DropActions ProjectListView::supportedDropActions() const
422 {
423     // returns what actions are supported when dropping
424     return Qt::MoveAction | Qt::CopyAction;
425 }
426
427 void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
428 {
429     if (index.column() == 0 && !index.data(ItemDelegate::DurationRole).isNull()) {
430         QRect r1 = option.rect;
431         painter->save();
432         QStyleOptionViewItemV4 opt(option);
433         QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
434         style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
435
436         if (option.state & QStyle::State_Selected) {
437             painter->setPen(option.palette.highlightedText().color());
438         }
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;
444
445         QFont font = painter->font();
446         font.setBold(true);
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());
453         font.setBold(false);
454         painter->setFont(font);
455         QString subText = index.data(ItemDelegate::DurationRole).toString();
456         int usage = index.data(ItemDelegate::UsageRole).toInt();
457         if (usage != 0) {
458             subText.append(QString::fromLatin1(" (%1)").arg(usage));
459         }
460
461         QRectF bounding;
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();
468                 color.setAlpha(150);
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);
480                 }
481                 else if (jobProgress > 0) {
482                     progress.setWidth(progress.width() * jobProgress / 100);
483                     painter->drawRect(progress);
484                 }
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);
494                 }
495             }
496         }
497
498         painter->restore();
499     } else if (index.column() == 2 && KdenliveSettings::activate_nepomuk()) {
500         if (index.data().toString().isEmpty()) {
501             QStyledItemDelegate::paint(painter, option, index);
502             return;
503         }
504         QRect r1 = option.rect;
505         if (option.state & (QStyle::State_Selected)) {
506             painter->fillRect(r1, option.palette.highlight());
507         }
508 #ifdef NEPOMUK
509         KRatingPainter::paintRating(painter, r1, Qt::AlignCenter, index.data().toInt());
510 #endif
511 #ifdef NEPOMUKCORE
512         KRatingPainter::paintRating(painter, r1, Qt::AlignCenter, index.data().toInt());
513 #endif
514
515     } else {
516         QStyledItemDelegate::paint(painter, option, index);
517     }
518 }
519
520 #include "projectlistview.moc"