]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
Edit clip when double clicking on it's thumbnail in project tree
[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
46
47     setColumnCount(3);
48     QStringList headers;
49     headers << i18n("Clip") << i18n("Description") << i18n("Rating");
50     setHeaderLabels(headers);
51
52     QHeaderView* headerView = header();
53     headerView->setContextMenuPolicy(Qt::CustomContextMenu);
54     connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
55             this, SLOT(configureColumns(const QPoint&)));
56     headerView->setClickable(true);
57     headerView->setSortIndicatorShown(true);
58     headerView->setMovable(false);
59     sortByColumn(0, Qt::AscendingOrder);
60     setSortingEnabled(true);
61
62     if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(1);
63     if (!KdenliveSettings::showratingcolumn()) hideColumn(2);
64 }
65
66 ProjectListView::~ProjectListView()
67 {
68 }
69
70 void ProjectListView::configureColumns(const QPoint& pos)
71 {
72     KMenu popup(this);
73     popup.addTitle(i18nc("@title:menu", "Columns"));
74
75     QHeaderView* headerView = header();
76     for (int i = 1; i < headerView->count(); ++i) {
77         const QString text = model()->headerData(i, Qt::Horizontal).toString();
78         QAction* action = popup.addAction(text);
79         action->setCheckable(true);
80         action->setChecked(!headerView->isSectionHidden(i));
81         action->setData(i);
82     }
83
84     QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
85     if (activatedAction != 0) {
86         const bool show = activatedAction->isChecked();
87
88         // remember the changed column visibility in the settings
89         const int columnIndex = activatedAction->data().toInt();
90         switch (columnIndex) {
91         case 1:
92             KdenliveSettings::setShowdescriptioncolumn(show);
93             break;
94         case 2:
95             KdenliveSettings::setShowratingcolumn(show);
96             break;
97         default:
98             break;
99         }
100
101         // apply the changed column visibility
102         if (show) {
103             showColumn(columnIndex);
104         } else {
105             hideColumn(columnIndex);
106         }
107     }
108 }
109
110 // virtual
111 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
112 {
113     emit requestMenu(event->globalPos(), itemAt(event->pos()));
114 }
115
116 // virtual
117 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
118 {
119     QTreeWidgetItem *it = itemAt(event->pos());
120     if (!it) {
121         emit addClip();
122         return;
123     }
124     ProjectItem *item;
125     if (it->type() == PROJECTFOLDERTYPE) {
126         if ((columnAt(event->pos().x()) == 0)) QTreeWidget::mouseDoubleClickEvent(event);
127         return;
128     }
129     if (it->type() == PROJECTSUBCLIPTYPE) {
130         // subitem
131         if ((columnAt(event->pos().x()) == 1)) {
132             QTreeWidget::mouseDoubleClickEvent(event);
133             return;
134         }
135         item = static_cast <ProjectItem *>(it->parent());
136     } else item = static_cast <ProjectItem *>(it);
137
138     if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
139
140     int column = columnAt(event->pos().x());
141     if (column == 0 && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) {
142         QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
143         int offset = pix.width() + indentation();
144         if (item->parent()) offset += indentation();
145         if ((pix.isNull() || offset < event->pos().x())) {
146             QTreeWidget::mouseDoubleClickEvent(event);
147             return;
148         }
149     }
150     if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
151         QTreeWidget::mouseDoubleClickEvent(event);
152         return;
153     }
154     emit showProperties(item->referencedClip());
155 }
156
157 // virtual
158 void ProjectListView::dragEnterEvent(QDragEnterEvent *event)
159 {
160     if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
161         kDebug() << "////////////////  DRAG ENTR OK";
162     }
163     event->acceptProposedAction();
164 }
165
166 // virtual
167 void ProjectListView::dropEvent(QDropEvent *event)
168 {
169     FolderProjectItem *item = NULL;
170     QTreeWidgetItem *it = itemAt(event->pos());
171     while (it && it->type() != PROJECTFOLDERTYPE) {
172         it = it->parent();
173     }
174     if (it) item = static_cast <FolderProjectItem *>(it);
175     if (event->mimeData()->hasUrls()) {
176         QString groupName;
177         QString groupId;
178         if (item) {
179             groupName = item->groupName();
180             groupId = item->clipId();
181         }
182         emit addClip(event->mimeData()->urls(), groupName, groupId);
183         event->setDropAction(Qt::CopyAction);
184         event->accept();
185         return;
186     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
187         if (item) {
188             //emit addClip(event->mimeData->text());
189             const QList <QTreeWidgetItem *> list = selectedItems();
190             ProjectItem *clone;
191             QString parentId = item->clipId();
192             foreach(QTreeWidgetItem *it, list) {
193                 // TODO allow dragging of folders ?
194                 if (it->type() == PROJECTCLIPTYPE) {
195                     if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
196                     else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
197                     if (clone) {
198                         item->addChild(clone);
199                         QMap <QString, QString> props;
200                         props.insert("groupname", item->groupName());
201                         props.insert("groupid", parentId);
202                         clone->setProperties(props);
203                     }
204                 } else item = NULL;
205             }
206         } else {
207             // item dropped in empty zone, move it to top level
208             const QList <QTreeWidgetItem *> list = selectedItems();
209             ProjectItem *clone;
210             foreach(QTreeWidgetItem *it, list) {
211                 QTreeWidgetItem *parent = it->parent();
212                 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/)  {
213                     kDebug() << "++ item parent: " << parent->text(1);
214                     clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
215                     if (clone) {
216                         addTopLevelItem(clone);
217                         clone->clearProperty("groupname");
218                         clone->clearProperty("groupid");
219                     }
220                 }
221             }
222         }
223         emit projectModified();
224     } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
225         QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
226         emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
227     }
228     event->acceptProposedAction();
229 }
230
231 // virtual
232 void ProjectListView::mousePressEvent(QMouseEvent *event)
233 {
234     if (event->button() == Qt::LeftButton) {
235         m_DragStartPosition = event->pos();
236         m_dragStarted = true;
237         /*QTreeWidgetItem *underMouse = itemAt(event->pos());
238         if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
239     }
240     QTreeWidget::mousePressEvent(event);
241 }
242
243 // virtual
244 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
245 {
246     QTreeWidget::mouseReleaseEvent(event);
247     QTreeWidgetItem *underMouse = itemAt(event->pos());
248     if (underMouse) emit focusMonitor();
249 }
250
251 // virtual
252 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
253 {
254     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
255     if (!m_dragStarted) return;
256
257     if ((event->pos() - m_DragStartPosition).manhattanLength()
258             < QApplication::startDragDistance())
259         return;
260
261     QTreeWidgetItem *it = itemAt(m_DragStartPosition);
262     if (!it) return;
263     if (it->type() == PROJECTSUBCLIPTYPE) {
264         // subitem
265         SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
266         if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
267             ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
268             QDrag *drag = new QDrag(this);
269             QMimeData *mimeData = new QMimeData;
270
271             QStringList list;
272             list.append(clip->clipId());
273             QPoint p = clickItem->zone();
274             list.append(QString::number(p.x()));
275             list.append(QString::number(p.y()));
276             QByteArray data;
277             data.append(list.join(";").toUtf8());
278             mimeData->setData("kdenlive/clip", data);
279             drag->setMimeData(mimeData);
280             drag->setPixmap(clickItem->data(0, Qt::DecorationRole).value<QPixmap>());
281             drag->setHotSpot(QPoint(0, 50));
282             drag->exec();
283         }
284     } else {
285         if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
286             QDrag *drag = new QDrag(this);
287             QMimeData *mimeData = new QMimeData;
288             const QList <QTreeWidgetItem *> list = selectedItems();
289             QStringList ids;
290             foreach(const QTreeWidgetItem *item, list) {
291                 if (item->type() == PROJECTFOLDERTYPE) {
292                     const int children = item->childCount();
293                     for (int i = 0; i < children; i++) {
294                         ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
295                     }
296                 } else {
297                     const ProjectItem *clip = static_cast <const ProjectItem *>(item);
298                     ids.append(clip->clipId());
299                 }
300             }
301             if (ids.isEmpty()) return;
302             QByteArray data;
303             data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
304             mimeData->setData("kdenlive/producerslist", data);
305             //mimeData->setText(ids.join(";")); //doc.toString());
306             //mimeData->setImageData(image);
307             drag->setMimeData(mimeData);
308             drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
309             drag->setHotSpot(QPoint(0, 50));
310             drag->exec();
311         }
312         //event->accept();
313     }
314 }
315
316 // virtual
317 void ProjectListView::dragMoveEvent(QDragMoveEvent * event)
318 {
319     //event->setDropAction(Qt::MoveAction);
320     if (event->mimeData()->hasText()) {
321         event->acceptProposedAction();
322     }
323     // stop playing because we get a crash otherwise when fetching the thumbnails
324     emit pauseMonitor();
325 }
326
327 QStringList ProjectListView::mimeTypes() const
328 {
329     QStringList qstrList;
330     qstrList << QTreeWidget::mimeTypes();
331     // list of accepted mime types for drop
332     qstrList.append("text/uri-list");
333     qstrList.append("text/plain");
334     qstrList.append("kdenlive/producerslist");
335     qstrList.append("kdenlive/clip");
336     return qstrList;
337 }
338
339
340 Qt::DropActions ProjectListView::supportedDropActions() const
341 {
342     // returns what actions are supported when dropping
343     return Qt::MoveAction;
344 }
345
346 #include "projectlistview.moc"