]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
d96f6a7b2145246144432a7617ce42af35689e5a
[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) {
119         emit addClip();
120         return;
121     }
122     if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
123     if (item->isGroup()) {
124         if ((columnAt(event->pos().x()) == 1)) QTreeWidget::mouseDoubleClickEvent(event);
125     } else {
126         if ((columnAt(event->pos().x()) == 1) && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) QTreeWidget::mouseDoubleClickEvent(event);
127         else if ((columnAt(event->pos().x()) == 2)) QTreeWidget::mouseDoubleClickEvent(event);
128         else emit showProperties(item->referencedClip());
129     }
130 }
131
132 // virtual
133 void ProjectListView::dragEnterEvent(QDragEnterEvent *event)
134 {
135     if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
136         kDebug() << "////////////////  DRAG ENTR OK";
137     }
138     event->acceptProposedAction();
139 }
140
141 // virtual
142 void ProjectListView::dropEvent(QDropEvent *event)
143 {
144     kDebug() << "////////////////  DROPPED EVENT";
145     if (event->mimeData()->hasUrls()) {
146         ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
147         QString groupName;
148         QString groupId;
149         if (item) {
150             if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
151             if (item->isGroup()) {
152                 groupName = item->groupName();
153                 groupId = item->clipId();
154             }
155         }
156         emit addClip(event->mimeData()->urls(), groupName, groupId);
157         event->setDropAction(Qt::CopyAction);
158         event->accept();
159         return;
160     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
161         ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
162         if (item) {
163             if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
164             if (item->isGroup()) {
165                 //emit addClip(event->mimeData->text());
166                 kDebug() << "////////////////  DROPPED RIGHT 1 ";
167                 const QList <QTreeWidgetItem *> list = selectedItems();
168                 ProjectItem *clone;
169                 QString parentId = item->clipId();
170                 foreach(QTreeWidgetItem *it, list) {
171                     // TODO allow dragging of folders ?
172                     if (!((ProjectItem *) it)->isGroup()/* && ((ProjectItem *) it)->clipId() < 10000*/) {
173                         if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
174                         else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
175                         if (clone) {
176                             item->addChild(clone);
177                             QMap <QString, QString> props;
178                             props.insert("groupname", item->groupName());
179                             props.insert("groupid", parentId);
180                             clone->setProperties(props);
181                         }
182                     }
183                 }
184             } else item = NULL;
185         }
186         if (!item) {
187             kDebug() << "////////////////  DROPPED ON EMPTY ZONE";
188             // item dropped in empty zone, move it to top level
189             const QList <QTreeWidgetItem *> list = selectedItems();
190             ProjectItem *clone;
191             foreach(QTreeWidgetItem *it, list) {
192                 QTreeWidgetItem *parent = it->parent();
193                 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/)  {
194                     kDebug() << "++ item parent: " << parent->text(1);
195                     clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
196                     if (clone) {
197                         addTopLevelItem(clone);
198                         clone->clearProperty("groupname");
199                         clone->clearProperty("groupid");
200                     }
201                 }
202             }
203         }
204     }
205     event->acceptProposedAction();
206 }
207
208 // virtual
209 void ProjectListView::mousePressEvent(QMouseEvent *event)
210 {
211     if (event->button() == Qt::LeftButton) {
212         m_DragStartPosition = event->pos();
213         m_dragStarted = true;
214         /*QTreeWidgetItem *underMouse = itemAt(event->pos());
215         if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
216     }
217     QTreeWidget::mousePressEvent(event);
218 }
219
220 // virtual
221 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
222 {
223     QTreeWidget::mouseReleaseEvent(event);
224     QTreeWidgetItem *underMouse = itemAt(event->pos());
225     if (underMouse) emit focusMonitor();
226 }
227
228 // virtual
229 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
230 {
231     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
232     if (!m_dragStarted) return;
233
234     if ((event->pos() - m_DragStartPosition).manhattanLength()
235             < QApplication::startDragDistance())
236         return;
237
238     {
239         ProjectItem *clickItem = static_cast <ProjectItem *>(itemAt(m_DragStartPosition));
240         if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
241             QDrag *drag = new QDrag(this);
242             QMimeData *mimeData = new QMimeData;
243             const QList <QTreeWidgetItem *> list = selectedItems();
244             QStringList ids;
245             foreach(const QTreeWidgetItem *item, list) {
246                 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
247                 if (!clip->isGroup()) ids.append(clip->clipId());
248                 else {
249                     const int children = item->childCount();
250                     for (int i = 0; i < children; i++) {
251                         ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
252                     }
253                 }
254             }
255             if (ids.isEmpty()) return;
256             QByteArray data;
257             data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
258             mimeData->setData("kdenlive/producerslist", data);
259             //mimeData->setText(ids.join(";")); //doc.toString());
260             //mimeData->setImageData(image);
261             drag->setMimeData(mimeData);
262             drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
263             drag->setHotSpot(QPoint(0, 50));
264             drag->exec();
265         }
266         //event->accept();
267     }
268 }
269
270 // virtual
271 void ProjectListView::dragMoveEvent(QDragMoveEvent * event)
272 {
273     //event->setDropAction(Qt::MoveAction);
274     if (event->mimeData()->hasText()) {
275         event->acceptProposedAction();
276     }
277     // stop playing because we get a crash otherwise when fetching the thumbnails
278     emit pauseMonitor();
279 }
280
281 QStringList ProjectListView::mimeTypes() const
282 {
283     QStringList qstrList;
284     qstrList << QTreeWidget::mimeTypes();
285     // list of accepted mime types for drop
286     qstrList.append("text/uri-list");
287     qstrList.append("text/plain");
288     qstrList.append("kdenlive/producerslist");
289     return qstrList;
290 }
291
292
293 Qt::DropActions ProjectListView::supportedDropActions() const
294 {
295     // returns what actions are supported when dropping
296     return Qt::MoveAction;
297 }
298
299 #include "projectlistview.moc"