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