]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
Fix proxy problem with too fast computers (item not in layout when trying to update it)
[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 <KLocale>
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(false);
47
48     setColumnCount(3);
49     QStringList headers;
50     headers << i18n("Clip") << i18n("Description") << i18n("Rating");
51     setHeaderLabels(headers);
52
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);
67 }
68
69 ProjectListView::~ProjectListView()
70 {
71 }
72
73 void ProjectListView::processLayout()
74 {
75     executeDelayedItemsLayout();
76 }
77
78 void ProjectListView::configureColumns(const QPoint& pos)
79 {
80     KMenu popup(this);
81     popup.addTitle(i18nc("@title:menu", "Columns"));
82
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));
89         action->setData(i);
90     }
91
92     QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
93     if (activatedAction != 0) {
94         const bool show = activatedAction->isChecked();
95
96         // remember the changed column visibility in the settings
97         const int columnIndex = activatedAction->data().toInt();
98         switch (columnIndex) {
99         case 1:
100             KdenliveSettings::setShowdescriptioncolumn(show);
101             break;
102         case 2:
103             KdenliveSettings::setShowratingcolumn(show);
104             break;
105         default:
106             break;
107         }
108
109         // apply the changed column visibility
110         if (show) {
111             showColumn(columnIndex);
112         } else {
113             hideColumn(columnIndex);
114         }
115     }
116 }
117
118 // virtual
119 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
120 {
121     emit requestMenu(event->globalPos(), itemAt(event->pos()));
122 }
123
124 void ProjectListView::slotCollapsed(QTreeWidgetItem *item)
125 {
126     if (item->type() == PROJECTFOLDERTYPE) {
127         blockSignals(true);
128         static_cast <FolderProjectItem *>(item)->switchIcon();
129         blockSignals(false);
130     }
131 }
132
133 void ProjectListView::slotExpanded(QTreeWidgetItem *item)
134 {
135     if (item->type() == PROJECTFOLDERTYPE) {
136         blockSignals(true);
137         static_cast <FolderProjectItem *>(item)->switchIcon();
138         blockSignals(false);
139     }
140 }
141
142 bool ProjectListView::eventFilter(QObject *obj, QEvent *event)
143 {
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);
148             event->accept();
149             return true;
150         } else if (ke->key() == Qt::Key_Minus) {
151             if (currentItem()) currentItem()->setExpanded(false);
152             event->accept();
153             return true;
154         } else {
155             return false;
156         }
157     } else {
158         // pass the event on to the parent class
159         return QTreeWidget::eventFilter(obj, event);
160     }
161 }
162
163 // virtual
164 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
165 {
166     QTreeWidgetItem *it = itemAt(event->pos());
167     if (!it) {
168         emit addClip();
169         return;
170     }
171     ProjectItem *item;
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());
178                 event->accept();
179             } else QTreeWidget::mouseDoubleClickEvent(event);
180         }
181         return;
182     }
183     if (it->type() == PROJECTSUBCLIPTYPE) {
184         // subitem
185         if ((columnAt(event->pos().x()) == 1)) {
186             QTreeWidget::mouseDoubleClickEvent(event);
187             return;
188         }
189         item = static_cast <ProjectItem *>(it->parent());
190     } else item = static_cast <ProjectItem *>(it);
191
192     if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
193
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());
202                 event->accept();
203                 return;
204             }
205         } else if (pix.isNull() || offset < event->pos().x()) {
206             QTreeWidget::mouseDoubleClickEvent(event);
207             return;
208         }
209     }
210     if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
211         QTreeWidget::mouseDoubleClickEvent(event);
212         return;
213     }
214     emit showProperties(item->referencedClip());
215 }
216
217
218 // virtual
219 void ProjectListView::dropEvent(QDropEvent *event)
220 {
221     FolderProjectItem *item = NULL;
222     QTreeWidgetItem *it = itemAt(event->pos());
223     while (it && it->type() != PROJECTFOLDERTYPE) {
224         it = it->parent();
225     }
226     if (it) item = static_cast <FolderProjectItem *>(it);
227     if (event->mimeData()->hasUrls()) {
228         QString groupName;
229         QString groupId;
230         if (item) {
231             groupName = item->groupName();
232             groupId = item->clipId();
233         }
234         emit addClip(event->mimeData()->urls(), groupName, groupId);
235         event->setDropAction(Qt::CopyAction);
236         event->accept();
237         QTreeWidget::dropEvent(event);
238         return;
239     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
240         if (item) {
241             //emit addClip(event->mimeData->text());
242             const QList <QTreeWidgetItem *> list = selectedItems();
243             ProjectItem *clone;
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));
250                     if (clone) {
251                         item->addChild(clone);
252                         QMap <QString, QString> props;
253                         props.insert("groupname", item->groupName());
254                         props.insert("groupid", parentId);
255                         clone->setProperties(props);
256                     }
257                 } else item = NULL;
258             }
259         } else {
260             // item dropped in empty zone, move it to top level
261             const QList <QTreeWidgetItem *> list = selectedItems();
262             ProjectItem *clone;
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)));
268                     if (clone) {
269                         addTopLevelItem(clone);
270                         clone->clearProperty("groupname");
271                         clone->clearProperty("groupid");
272                     }
273                 }
274             }
275         }
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());
280     }
281     event->acceptProposedAction();
282     QTreeWidget::dropEvent(event);
283 }
284
285 // virtual
286 void ProjectListView::mousePressEvent(QMouseEvent *event)
287 {
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();*/
293     }
294     QTreeWidget::mousePressEvent(event);
295 }
296
297 // virtual
298 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
299 {
300     QTreeWidget::mouseReleaseEvent(event);
301     QTreeWidgetItem *underMouse = itemAt(event->pos());
302     if (underMouse) emit focusMonitor();
303 }
304
305 // virtual
306 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
307 {
308     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
309     if (!m_dragStarted) return;
310
311     if ((event->pos() - m_DragStartPosition).manhattanLength()
312             < QApplication::startDragDistance())
313         return;
314
315     QTreeWidgetItem *it = itemAt(m_DragStartPosition);
316     if (!it) return;
317     if (it->type() == PROJECTSUBCLIPTYPE) {
318         // subitem
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;
324
325             QStringList list;
326             list.append(clip->clipId());
327             QPoint p = clickItem->zone();
328             list.append(QString::number(p.x()));
329             list.append(QString::number(p.y()));
330             QByteArray data;
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));
336             drag->exec();
337         }
338     } else {
339         if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
340             QDrag *drag = new QDrag(this);
341             QMimeData *mimeData = new QMimeData;
342             const QList <QTreeWidgetItem *> list = selectedItems();
343             QStringList ids;
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());
349                     }
350                 } else {
351                     const ProjectItem *clip = static_cast <const ProjectItem *>(item);
352                     ids.append(clip->clipId());
353                 }
354             }
355             if (ids.isEmpty()) return;
356             QByteArray data;
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));
364             drag->exec();
365         }
366         //event->accept();
367     }
368 }
369
370 // virtual
371 void ProjectListView::dragLeaveEvent(QDragLeaveEvent *event)
372 {
373     // stop playing because we get a crash otherwise when fetching the thumbnails
374     emit pauseMonitor();
375     QTreeWidget::dragLeaveEvent(event);
376 }
377
378 QStringList ProjectListView::mimeTypes() const
379 {
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");
387     return qstrList;
388 }
389
390
391 Qt::DropActions ProjectListView::supportedDropActions() const
392 {
393     // returns what actions are supported when dropping
394     return Qt::MoveAction | Qt::CopyAction;
395 }
396
397 #include "projectlistview.moc"