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