]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
Use KLocalizedString (for i18n only, in kf5 it will necessary => use a script for...
[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
21 #include "projectlistview.h"
22 #include "projectitem.h"
23 #include "subprojectitem.h"
24 #include "folderprojectitem.h"
25 #include "kdenlivesettings.h"
26
27 #include <KDebug>
28 #include <KMenu>
29 #include <KLocalizedString>
30
31 #include <QApplication>
32 #include <QHeaderView>
33 #include <QAction>
34
35 ProjectListView::ProjectListView(QWidget *parent) :
36         QTreeWidget(parent),
37         m_dragStarted(false)
38 {
39     setSelectionMode(QAbstractItemView::ExtendedSelection);
40     setDragDropMode(QAbstractItemView::DragDrop);
41     setDropIndicatorShown(true);
42     setAlternatingRowColors(true);
43     setDragEnabled(true);
44     setAcceptDrops(true);
45     setFrameShape(QFrame::NoFrame);
46     setRootIsDecorated(true);
47
48     updateStyleSheet();
49
50     setColumnCount(4);
51     QStringList headers;
52     headers << i18n("Clip") << i18n("Description") << i18n("Rating") << i18n("Date");
53     setHeaderLabels(headers);
54     setIndentation(12);
55     
56     QHeaderView* headerView = header();
57     headerView->setContextMenuPolicy(Qt::CustomContextMenu);
58     connect(headerView, SIGNAL(customContextMenuRequested(QPoint)),
59             this, SLOT(configureColumns(QPoint)));
60     connect(this, SIGNAL(itemCollapsed(QTreeWidgetItem*)), this, SLOT(slotCollapsed(QTreeWidgetItem*)));
61     connect(this, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(slotExpanded(QTreeWidgetItem*)));
62     headerView->setClickable(true);
63     headerView->setSortIndicatorShown(true);
64     headerView->setMovable(false);
65     sortByColumn(0, Qt::AscendingOrder);
66     setSortingEnabled(true);
67     installEventFilter(this);
68     if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(1);
69     if (!KdenliveSettings::showratingcolumn()) hideColumn(2);
70     if (!KdenliveSettings::showdatecolumn()) hideColumn(3);
71 }
72
73 ProjectListView::~ProjectListView()
74 {
75 }
76
77 void ProjectListView::updateStyleSheet()
78 {
79     QString style = "QTreeView::branch:has-siblings:!adjoins-item{border-image: none;border:0px} \
80     QTreeView::branch:has-siblings:adjoins-item {border-image: none;border:0px}      \
81     QTreeView::branch:!has-children:!has-siblings:adjoins-item {border-image: none;border:0px} \
82     QTreeView::branch:has-children:!has-siblings:closed,QTreeView::branch:closed:has-children:has-siblings {   \
83          border-image: none;image: url(:/images/stylesheet-branch-closed.png);}      \
84     QTreeView::branch:open:has-children:!has-siblings,QTreeView::branch:open:has-children:has-siblings  {    \
85          border-image: none;image: url(:/images/stylesheet-branch-open.png);}";
86     setStyleSheet(style);
87 }
88
89 void ProjectListView::processLayout()
90 {
91     executeDelayedItemsLayout();
92 }
93
94 void ProjectListView::configureColumns(const QPoint& pos)
95 {
96     KMenu popup(this);
97     popup.addTitle(i18nc("@title:menu", "Columns"));
98
99     QHeaderView* headerView = header();
100     for (int i = 1; i < headerView->count(); ++i) {
101         const QString text = model()->headerData(i, Qt::Horizontal).toString();
102         QAction* action = popup.addAction(text);
103         action->setCheckable(true);
104         action->setChecked(!headerView->isSectionHidden(i));
105         action->setData(i);
106     }
107
108     QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
109     if (activatedAction != 0) {
110         const bool show = activatedAction->isChecked();
111
112         // remember the changed column visibility in the settings
113         const int columnIndex = activatedAction->data().toInt();
114         switch (columnIndex) {
115         case 1:
116             KdenliveSettings::setShowdescriptioncolumn(show);
117             break;
118         case 2:
119             KdenliveSettings::setShowratingcolumn(show);
120             break;
121         case 3:
122             KdenliveSettings::setShowdatecolumn(show);
123             break;
124         default:
125             break;
126         }
127
128         // apply the changed column visibility
129         if (show) {
130             showColumn(columnIndex);
131         } else {
132             hideColumn(columnIndex);
133         }
134     }
135 }
136
137 // virtual
138 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
139 {
140     emit requestMenu(event->globalPos(), itemAt(event->pos()));
141 }
142
143 void ProjectListView::slotCollapsed(QTreeWidgetItem *item)
144 {
145     if (item->type() == PROJECTFOLDERTYPE) {
146         blockSignals(true);
147         static_cast <FolderProjectItem *>(item)->switchIcon();
148         blockSignals(false);
149     }
150 }
151
152 void ProjectListView::slotExpanded(QTreeWidgetItem *item)
153 {
154     if (item->type() == PROJECTFOLDERTYPE) {
155         blockSignals(true);
156         static_cast <FolderProjectItem *>(item)->switchIcon();
157         blockSignals(false);
158     }
159 }
160
161 bool ProjectListView::eventFilter(QObject *obj, QEvent *event)
162 {
163     if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
164         QKeyEvent* ke = (QKeyEvent*) event;
165         if (ke->key() == Qt::Key_Plus) {
166             if (currentItem()) currentItem()->setExpanded(true);
167             event->accept();
168             return true;
169         } else if (ke->key() == Qt::Key_Minus) {
170             if (currentItem()) currentItem()->setExpanded(false);
171             event->accept();
172             return true;
173         } else {
174             return false;
175         }
176     } else {
177         // pass the event on to the parent class
178         return QTreeWidget::eventFilter(obj, event);
179     }
180 }
181
182 // virtual
183 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
184 {
185     QTreeWidgetItem *it = itemAt(event->pos());
186     if (!it) {
187         emit pauseMonitor();
188         emit addClip();
189         return;
190     }
191     ProjectItem *item;
192     if (it->type() == PROJECTFOLDERTYPE) {
193         if ((columnAt(event->pos().x()) == 0)) {
194             QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
195             int offset = pix.width() + indentation();
196             if (event->pos().x() < offset) {
197                 it->setExpanded(!it->isExpanded());
198                 event->accept();
199             } else QTreeWidget::mouseDoubleClickEvent(event);
200         }
201         return;
202     }
203     if (it->type() == PROJECTSUBCLIPTYPE) {
204         // subitem
205         if ((columnAt(event->pos().x()) == 1)) {
206             QTreeWidget::mouseDoubleClickEvent(event);
207             return;
208         }
209         item = static_cast <ProjectItem *>(it->parent());
210     } else item = static_cast <ProjectItem *>(it);
211
212     if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
213
214     int column = columnAt(event->pos().x());
215     if (column == 0 && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR || it->childCount() > 0)) {
216         QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
217         int offset = pix.width() + indentation();
218         if (item->parent()) offset += indentation();
219         if (it->childCount() > 0) {
220             if (offset > event->pos().x()) {
221                 it->setExpanded(!it->isExpanded());
222                 event->accept();
223                 return;
224             }
225         } else if (pix.isNull() || offset < event->pos().x()) {
226             QTreeWidget::mouseDoubleClickEvent(event);
227             return;
228         }
229     }
230     if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
231         QTreeWidget::mouseDoubleClickEvent(event);
232         return;
233     }
234     emit showProperties(item->referencedClip());
235 }
236
237
238 // virtual
239 void ProjectListView::dropEvent(QDropEvent *event)
240 {
241     FolderProjectItem *item = NULL;
242     QTreeWidgetItem *it = itemAt(event->pos());
243     while (it && it->type() != PROJECTFOLDERTYPE) {
244         it = it->parent();
245     }
246     if (it) item = static_cast <FolderProjectItem *>(it);
247     if (event->mimeData()->hasUrls()) {
248         QString groupName;
249         QString groupId;
250         if (item) {
251             groupName = item->groupName();
252             groupId = item->clipId();
253         }
254         emit addClip(event->mimeData()->urls(), groupName, groupId);
255         event->setDropAction(Qt::CopyAction);
256         event->accept();
257         QTreeWidget::dropEvent(event);
258         return;
259     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
260         if (item) {
261             //emit addClip(event->mimeData->text());
262             const QList <QTreeWidgetItem *> list = selectedItems();
263             ProjectItem *clone;
264             QString parentId = item->clipId();
265             foreach(QTreeWidgetItem *it, list) {
266                 // TODO allow dragging of folders ?
267                 if (it->type() == PROJECTCLIPTYPE) {
268                     if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
269                     else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
270                     if (clone && item) {
271                         item->addChild(clone);
272                         QMap <QString, QString> props;
273                         props.insert("groupname", item->groupName());
274                         props.insert("groupid", parentId);
275                         clone->setProperties(props);
276                     }
277                 } else item = NULL;
278             }
279         } else {
280             // item dropped in empty zone, move it to top level
281             const QList <QTreeWidgetItem *> list = selectedItems();
282             ProjectItem *clone;
283             foreach(QTreeWidgetItem *it, list) {
284                 if (it->type() != PROJECTCLIPTYPE) continue;
285                 QTreeWidgetItem *parent = it->parent();
286                 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/)  {
287                     kDebug() << "++ item parent: " << parent->text(1);
288                     clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
289                     if (clone) {
290                         addTopLevelItem(clone);
291                         clone->clearProperty("groupname");
292                         clone->clearProperty("groupid");
293                     }
294                 }
295             }
296         }
297         emit projectModified();
298     } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
299         QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
300         emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
301     }
302     if (event->source() == this) {
303         event->setDropAction(Qt::MoveAction);
304         event->accept();
305     } else {
306         event->acceptProposedAction();
307     }
308     QTreeWidget::dropEvent(event);
309 }
310
311 // virtual
312 void ProjectListView::mousePressEvent(QMouseEvent *event)
313 {
314     if (event->button() == Qt::LeftButton) {
315         m_DragStartPosition = event->pos();
316         m_dragStarted = true;
317         /*QTreeWidgetItem *underMouse = itemAt(event->pos());
318         ProjectItem *item = static_cast<ProjectItem *>(underMouse);
319         if (item) {
320             QRect itemRect = visualItemRect(item);
321             if (item->underJobMenu(itemRect, event->pos())) {
322                 emit display
323             }
324             
325             && underMouse->isSelected()) emit focusMonitor()
326         }*/
327     }
328     QTreeWidget::mousePressEvent(event);
329 }
330
331 // virtual
332 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
333 {
334     QTreeWidget::mouseReleaseEvent(event);
335     QTreeWidgetItem *underMouse = itemAt(event->pos());
336     if (underMouse) emit focusMonitor(true);
337 }
338
339 // virtual
340 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
341 {
342     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
343     if (!m_dragStarted) return;
344
345     if ((event->pos() - m_DragStartPosition).manhattanLength()
346             < QApplication::startDragDistance())
347         return;
348
349     QTreeWidgetItem *it = itemAt(m_DragStartPosition);
350     if (!it) return;
351     if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
352         QDrag *drag = new QDrag(this);
353         QMimeData *mimeData = new QMimeData;
354         const QList <QTreeWidgetItem *> list = selectedItems();
355         QStringList ids;
356         foreach(const QTreeWidgetItem *item, list) {
357             if (item->type() == PROJECTFOLDERTYPE) {
358                 const int children = item->childCount();
359                 for (int i = 0; i < children; ++i) {
360                     ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
361                 }
362             } else if (item->type() == PROJECTSUBCLIPTYPE) {
363                 const ProjectItem *parentclip = static_cast <const ProjectItem *>(item->parent());
364                 const SubProjectItem *clickItem = static_cast <const SubProjectItem *>(item);
365                 QPoint p = clickItem->zone();
366                 QString data = parentclip->clipId();
367                 data.append("/" + QString::number(p.x()));
368                 data.append("/" + QString::number(p.y()));
369                 ids.append(data);
370             } else {
371                 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
372                 ids.append(clip->clipId());
373             }
374         }
375         if (ids.isEmpty()) return;
376         QByteArray data;
377         data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
378         mimeData->setData("kdenlive/producerslist", data);
379         //mimeData->setText(ids.join(";")); //doc.toString());
380         //mimeData->setImageData(image);
381         drag->setMimeData(mimeData);
382         drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
383         drag->setHotSpot(QPoint(0, 40));
384         drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
385     }
386 }
387
388 // virtual
389 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
390 {
391     // stop playing because we get a crash otherwise when fetching the thumbnails
392     emit pauseMonitor();
393     QTreeWidget::dragLeaveEvent(event);
394 }
395
396 QStringList ProjectListView::mimeTypes() const
397 {
398     QStringList qstrList;
399     qstrList << QTreeWidget::mimeTypes();
400     // list of accepted mime types for drop
401     qstrList.append("text/uri-list");
402     qstrList.append("text/plain");
403     qstrList.append("kdenlive/producerslist");
404     qstrList.append("kdenlive/clip");
405     return qstrList;
406 }
407
408
409 Qt::DropActions ProjectListView::supportedDropActions() const
410 {
411     // returns what actions are supported when dropping
412     return Qt::MoveAction | Qt::CopyAction;
413 }
414
415 #include "projectlistview.moc"