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