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