]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
6b2b5abb2826365f8b781e57aa2b4c352d4e562c
[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 "kdenlivesettings.h"
24
25 #include <KDebug>
26 #include <KMenu>
27 #include <KLocale>
28
29 #include <QApplication>
30 #include <QHeaderView>
31 #include <QAction>
32
33 ProjectListView::ProjectListView(QWidget *parent) :
34         QTreeWidget(parent),
35         m_dragStarted(false)
36 {
37     setSelectionMode(QAbstractItemView::ExtendedSelection);
38     setDragDropMode(QAbstractItemView::DragDrop);
39     setDropIndicatorShown(true);
40     setAlternatingRowColors(true);
41     setDragEnabled(true);
42     setAcceptDrops(true);
43
44     setColumnCount(4);
45     QStringList headers;
46     headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description") << i18n("Rating");
47     setHeaderLabels(headers);
48     sortByColumn(1, Qt::AscendingOrder);
49
50     QHeaderView* headerView = header();
51     headerView->setContextMenuPolicy(Qt::CustomContextMenu);
52     connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
53             this, SLOT(configureColumns(const QPoint&)));
54
55     //connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), this, SLOT(slotFocusOut(QTreeWidgetItem *, QTreeWidgetItem *)));
56
57     if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(2);
58     if (!KdenliveSettings::showratingcolumn()) hideColumn(3);
59
60     setSortingEnabled(true);
61 }
62
63 ProjectListView::~ProjectListView()
64 {
65 }
66
67
68 void ProjectListView::configureColumns(const QPoint& pos)
69 {
70     KMenu popup(this);
71     popup.addTitle(i18nc("@title:menu", "Columns"));
72
73     QHeaderView* headerView = header();
74     for (int i = 2; i < headerView->count(); ++i) {
75         const QString text = model()->headerData(i, Qt::Horizontal).toString();
76         QAction* action = popup.addAction(text);
77         action->setCheckable(true);
78         action->setChecked(!headerView->isSectionHidden(i));
79         action->setData(i);
80     }
81
82     QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
83     if (activatedAction != 0) {
84         const bool show = activatedAction->isChecked();
85
86         // remember the changed column visibility in the settings
87         const int columnIndex = activatedAction->data().toInt();
88         switch (columnIndex) {
89         case 2:
90             KdenliveSettings::setShowdescriptioncolumn(show);
91             break;
92         case 3:
93             KdenliveSettings::setShowratingcolumn(show);
94             break;
95         default:
96             break;
97         }
98
99         // apply the changed column visibility
100         if (show) {
101             showColumn(columnIndex);
102         } else {
103             hideColumn(columnIndex);
104         }
105     }
106 }
107
108 // virtual
109 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
110 {
111     emit requestMenu(event->globalPos(), itemAt(event->pos()));
112 }
113
114 // virtual
115 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
116 {
117     ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
118     if (!item) emit addClip();
119     if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
120     else if (item->isGroup()) {
121         if ((columnAt(event->pos().x()) == 1)) QTreeWidget::mouseDoubleClickEvent(event);
122     } else {
123         if ((columnAt(event->pos().x()) == 1) && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) QTreeWidget::mouseDoubleClickEvent(event);
124         else if ((columnAt(event->pos().x()) == 2)) QTreeWidget::mouseDoubleClickEvent(event);
125         else emit showProperties(item->referencedClip());
126     }
127 }
128
129 // virtual
130 void ProjectListView::dragEnterEvent(QDragEnterEvent *event)
131 {
132     if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
133         kDebug() << "////////////////  DRAG ENTR OK";
134     }
135     event->acceptProposedAction();
136 }
137
138 // virtual
139 void ProjectListView::dropEvent(QDropEvent *event)
140 {
141     kDebug() << "////////////////  DROPPED EVENT";
142     if (event->mimeData()->hasUrls()) {
143         QTreeWidgetItem *item = itemAt(event->pos());
144         QString groupName;
145         if (item) {
146             if (((ProjectItem *) item)->isGroup()) groupName = item->text(1);
147             else if (item->parent() && ((ProjectItem *) item->parent())->isGroup())
148                 groupName = item->parent()->text(1);
149         }
150         emit addClip(event->mimeData()->urls(), groupName);
151
152     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
153         ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
154         if (item) {
155             if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
156             if (item->isGroup()) {
157                 //emit addClip(event->mimeData->text());
158                 kDebug() << "////////////////  DROPPED RIGHT 1 ";
159                 const QList <QTreeWidgetItem *> list = selectedItems();
160                 ProjectItem *clone;
161                 QString parentId = item->clipId();
162                 foreach(QTreeWidgetItem *it, list) {
163                     // TODO allow dragging of folders ?
164                     if (!((ProjectItem *) it)->isGroup()/* && ((ProjectItem *) it)->clipId() < 10000*/) {
165                         if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
166                         else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
167                         if (clone) {
168                             item->addChild(clone);
169                             QMap <QString, QString> props;
170                             props.insert("groupname", item->groupName());
171                             props.insert("groupid", parentId);
172                             clone->setProperties(props);
173                         }
174                     }
175                 }
176             } else item = NULL;
177         }
178         if (!item) {
179             kDebug() << "////////////////  DROPPED ON EMPTY ZONE";
180             // item dropped in empty zone, move it to top level
181             const QList <QTreeWidgetItem *> list = selectedItems();
182             ProjectItem *clone;
183             foreach(QTreeWidgetItem *it, list) {
184                 QTreeWidgetItem *parent = it->parent();
185                 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/)  {
186                     kDebug() << "++ item parent: " << parent->text(1);
187                     clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
188                     if (clone) {
189                         addTopLevelItem(clone);
190                         clone->clearProperty("groupname");
191                         clone->clearProperty("groupid");
192                     }
193                 }
194             }
195         }
196     }
197     event->acceptProposedAction();
198 }
199
200 // virtual
201 void ProjectListView::mousePressEvent(QMouseEvent *event)
202 {
203     if (event->button() == Qt::LeftButton) {
204         m_DragStartPosition = event->pos();
205         m_dragStarted = true;
206         QTreeWidgetItem *underMouse = itemAt(event->pos());
207         if (underMouse && underMouse->isSelected()) emit focusMonitor();
208     }
209     QTreeWidget::mousePressEvent(event);
210 }
211
212
213 // virtual
214 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
215 {
216     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
217     if (!m_dragStarted) return;
218
219     if ((event->pos() - m_DragStartPosition).manhattanLength()
220             < QApplication::startDragDistance())
221         return;
222
223     {
224         ProjectItem *clickItem = static_cast <ProjectItem *>(itemAt(m_DragStartPosition));
225         if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
226             QDrag *drag = new QDrag(this);
227             QMimeData *mimeData = new QMimeData;
228             const QList <QTreeWidgetItem *> list = selectedItems();
229             QStringList ids;
230             foreach(const QTreeWidgetItem *item, list) {
231                 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
232                 if (!clip->isGroup()) ids.append(clip->clipId());
233                 else {
234                     const int children = item->childCount();
235                     for (int i = 0; i < children; i++) {
236                         ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
237                     }
238                 }
239             }
240             if (ids.isEmpty()) return;
241             QByteArray data;
242             data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
243             mimeData->setData("kdenlive/producerslist", data);
244             //mimeData->setText(ids.join(";")); //doc.toString());
245             //mimeData->setImageData(image);
246             drag->setMimeData(mimeData);
247             drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
248             drag->setHotSpot(QPoint(0, 50));
249             drag->exec(Qt::MoveAction);
250         }
251         //event->accept();
252     }
253 }
254
255 // virtual
256 void ProjectListView::dragMoveEvent(QDragMoveEvent * event)
257 {
258     event->setDropAction(Qt::IgnoreAction);
259     event->setDropAction(Qt::MoveAction);
260     if (event->mimeData()->hasText()) {
261         event->acceptProposedAction();
262     }
263     // stop playing because we get a crash otherwise when fetching the thumbnails
264     emit pauseMonitor();
265 }
266
267 QStringList ProjectListView::mimeTypes() const
268 {
269     QStringList qstrList;
270     qstrList << QTreeWidget::mimeTypes();
271     // list of accepted mime types for drop
272     qstrList.append("text/uri-list");
273     qstrList.append("text/plain");
274     qstrList.append("kdenlive/producerslist");
275     return qstrList;
276 }
277
278
279 Qt::DropActions ProjectListView::supportedDropActions() const
280 {
281     // returns what actions are supported when dropping
282     return Qt::MoveAction;
283 }
284
285 #include "projectlistview.moc"