]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
Implement clipmanager
[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       QStringList ids;
160       foreach (QTreeWidgetItem *item, list) {
161         // TODO allow dragging of folders
162         if (!((ProjectItem *) item)->isGroup())
163           ids.append(QString::number(((ProjectItem *) item)->clipId()));
164       }
165       //QByteArray data;
166       //data.append(doc.toString().toUtf8());
167       //mimeData->setData("kdenlive/westley",data );
168       mimeData->setText(ids.join(";")); //doc.toString());
169       //mimeData->setImageData(image);
170       drag->setMimeData(mimeData);
171       drag->setPixmap(clickItem->icon(0).pixmap(50 *16/9.0, 50));
172       drag->setHotSpot(QPoint(0, 50));
173       drag->start(Qt::MoveAction);
174
175       //Qt::DropAction dropAction;
176       //dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
177
178       //Qt::DropAction dropAction = drag->exec();
179       
180     }
181     //event->accept(); 
182   }
183 }
184
185 void ProjectListView::dragMoveEvent(QDragMoveEvent * event) {
186         QTreeWidgetItem * item = itemAt(event->pos());
187         event->setDropAction(Qt::IgnoreAction);
188         //if (item) {
189                 event->setDropAction(Qt::MoveAction);
190                 if (event->mimeData()->hasText()) {
191                         event->acceptProposedAction();
192                 }
193         //}
194 }
195
196 QStringList ProjectListView::mimeTypes () const
197 {
198     QStringList qstrList;
199     // list of accepted mime types for drop
200     qstrList.append("text/uri-list");
201     qstrList.append("text/plain");
202     return qstrList;
203 }
204  
205  
206 Qt::DropActions ProjectListView::supportedDropActions () const
207 {
208     // returns what actions are supported when dropping
209     return Qt::MoveAction;
210 }
211
212 #include "projectlistview.moc"