]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
folders in project view
[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 #include "QApplication"
21
22 #include "KDebug"
23
24 #include "projectitem.h"
25 #include "projectlistview.h"
26
27
28 ProjectListView::ProjectListView(QWidget *parent)
29         : QTreeWidget(parent), m_dragStarted(false) {
30     setSelectionMode(QAbstractItemView::ExtendedSelection);
31     setDragDropMode(QAbstractItemView::DragDrop);
32     setDropIndicatorShown(true);
33     setAlternatingRowColors(true);
34     setDragEnabled(true);
35     setAcceptDrops(true);
36 }
37
38 ProjectListView::~ProjectListView() {
39 }
40
41 void ProjectListView::editItem(QTreeWidgetItem * item, int column) {
42     kDebug() << "////////////////  EDIT ITEM, COL: " << column;
43 }
44
45 // virtual
46 void ProjectListView::contextMenuEvent(QContextMenuEvent * event) {
47     emit requestMenu(event->globalPos(), itemAt(event->pos()));
48 }
49
50 // virtual
51 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event) {
52     ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
53     if (!item) emit addClip();
54     else if ((item->clipType() == FOLDER && columnAt(event->pos().x()) == 1) || columnAt(event->pos().x()) == 2) QTreeWidget::mouseDoubleClickEvent(event);
55 }
56
57 // virtual
58 void ProjectListView::dragEnterEvent(QDragEnterEvent *event) {
59     if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
60         kDebug() << "////////////////  DRAG ENTR OK";
61     }
62     event->acceptProposedAction();
63 }
64
65 // virtual
66 void ProjectListView::dropEvent(QDropEvent *event) {
67     kDebug() << "////////////////  DROPPED EVENT";
68     if (event->mimeData()->hasUrls()) {
69         QTreeWidgetItem *item = itemAt(event->pos());
70         QString groupName;
71         if (item) {
72             if (((ProjectItem *) item)->isGroup()) groupName = item->text(1);
73             else if (item->parent() && ((ProjectItem *) item->parent())->isGroup())
74                 groupName = item->parent()->text(1);
75         }
76         QList <QUrl> list;
77         list = event->mimeData()->urls();
78         foreach(QUrl url, list) {
79             emit addClip(url, groupName);
80         }
81
82     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
83         ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
84         if (item) {
85             if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
86             if (item->isGroup()) {
87                 //emit addClip(event->mimeData->text());
88                 kDebug() << "////////////////  DROPPED RIGHT 1 ";
89                 QList <QTreeWidgetItem *> list;
90                 list = selectedItems();
91                 ProjectItem *clone;
92                 int parentId = item->clipId();
93                 foreach(QTreeWidgetItem *it, list) {
94                     // TODO allow dragging of folders ?
95                     if (!((ProjectItem *) it)->isGroup() && ((ProjectItem *) it)->clipId() < 10000) {
96                         if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
97                         else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
98                         if (clone) {
99                             item->addChild(clone);
100                             clone->setGroup(item->groupName(), QString::number(parentId));
101                         }
102                     }
103                 }
104             } else item = NULL;
105         }
106         if (!item) {
107             kDebug() << "////////////////  DROPPED ON EMPTY ZONE";
108             // item dropped in empty zone, move it to top level
109             QList <QTreeWidgetItem *> list;
110             list = selectedItems();
111             ProjectItem *clone;
112             foreach(QTreeWidgetItem *it, list) {
113                 QTreeWidgetItem *parent = it->parent();
114                 if (parent && ((ProjectItem *) it)->clipId() < 10000)  {
115                     kDebug() << "++ item parent: " << parent->text(1);
116                     clone = (ProjectItem*) parent->takeChild(parent->indexOfChild(it));
117                     if (clone) addTopLevelItem(clone);
118                 }
119             }
120         }
121     }
122     event->acceptProposedAction();
123 }
124
125 // virtual
126 void ProjectListView::mousePressEvent(QMouseEvent *event) {
127     if (event->button() == Qt::LeftButton) {
128         this->m_DragStartPosition = event->pos();
129         m_dragStarted = true;
130     }
131     QTreeWidget::mousePressEvent(event);
132 }
133
134
135 // virtual
136 void ProjectListView::mouseMoveEvent(QMouseEvent *event) {
137     kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
138     if (!m_dragStarted) return;
139
140     if ((event->pos() - m_DragStartPosition).manhattanLength()
141             < QApplication::startDragDistance())
142         return;
143
144     {
145         ProjectItem *clickItem = (ProjectItem *) itemAt(event->pos());
146         if (clickItem) {
147             QDrag *drag = new QDrag(this);
148             QMimeData *mimeData = new QMimeData;
149             QDomDocument doc;
150             QList <QTreeWidgetItem *> list;
151             list = selectedItems();
152             QStringList ids;
153             foreach(QTreeWidgetItem *item, list) {
154                 // TODO allow dragging of folders
155                 ids.append(QString::number(((ProjectItem *) item)->clipId()));
156             }
157             QByteArray data;
158             data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
159             mimeData->setData("kdenlive/producerslist", data);
160             //mimeData->setText(ids.join(";")); //doc.toString());
161             //mimeData->setImageData(image);
162             drag->setMimeData(mimeData);
163             drag->setPixmap(clickItem->icon(0).pixmap((int)(50 *16 / 9.0), 50));
164             drag->setHotSpot(QPoint(0, 50));
165             drag->start(Qt::MoveAction);
166
167             //Qt::DropAction dropAction;
168             //dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
169
170             //Qt::DropAction dropAction = drag->exec();
171
172         }
173         //event->accept();
174     }
175 }
176
177 void ProjectListView::dragMoveEvent(QDragMoveEvent * event) {
178     QTreeWidgetItem * item = itemAt(event->pos());
179     event->setDropAction(Qt::IgnoreAction);
180     //if (item) {
181     event->setDropAction(Qt::MoveAction);
182     if (event->mimeData()->hasText()) {
183         event->acceptProposedAction();
184     }
185     //}
186 }
187
188 QStringList ProjectListView::mimeTypes() const {
189     QStringList qstrList;
190     qstrList << QTreeWidget::mimeTypes();
191     // list of accepted mime types for drop
192     qstrList.append("text/uri-list");
193     qstrList.append("text/plain");
194     qstrList.append("kdenlive/producerslist");
195     return qstrList;
196 }
197
198
199 Qt::DropActions ProjectListView::supportedDropActions() const {
200     // returns what actions are supported when dropping
201     return Qt::MoveAction;
202 }
203
204 #include "projectlistview.moc"