]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
Cleanup + fix another QDomDocument usage
[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
21 #include "projectlistview.h"
22 #include "projectitem.h"
23 #include "kdenlivesettings.h"
24
25 #include <KDebug>
26 #include <KMenu>
27 #include <KLocale>
28
29 #include <QApplication>
30 #include <QHeaderView>
31 #include <QAction>
32
33 ProjectListView::ProjectListView(QWidget *parent)
34         : QTreeWidget(parent), m_dragStarted(false) {
35     setSelectionMode(QAbstractItemView::ExtendedSelection);
36     setDragDropMode(QAbstractItemView::DragDrop);
37     setDropIndicatorShown(true);
38     setAlternatingRowColors(true);
39     setDragEnabled(true);
40     setAcceptDrops(true);
41
42     setColumnCount(4);
43     QStringList headers;
44     headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description") << i18n("Rating");
45     setHeaderLabels(headers);
46     sortByColumn(1, Qt::AscendingOrder);
47
48     QHeaderView* headerView = header();
49     headerView->setContextMenuPolicy(Qt::CustomContextMenu);
50     connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
51             this, SLOT(configureColumns(const QPoint&)));
52
53     //connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), this, SLOT(slotFocusOut(QTreeWidgetItem *, QTreeWidgetItem *)));
54
55     if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(2);
56     if (!KdenliveSettings::showratingcolumn()) hideColumn(3);
57
58     setSortingEnabled(true);
59 }
60
61 ProjectListView::~ProjectListView() {
62 }
63
64
65 void ProjectListView::configureColumns(const QPoint& pos) {
66     KMenu popup(this);
67     popup.addTitle(i18nc("@title:menu", "Columns"));
68
69     QHeaderView* headerView = header();
70     for (int i = 2; i < headerView->count(); ++i) {
71         const QString text = model()->headerData(i, Qt::Horizontal).toString();
72         QAction* action = popup.addAction(text);
73         action->setCheckable(true);
74         action->setChecked(!headerView->isSectionHidden(i));
75         action->setData(i);
76     }
77
78     QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
79     if (activatedAction != 0) {
80         const bool show = activatedAction->isChecked();
81
82         // remember the changed column visibility in the settings
83         const int columnIndex = activatedAction->data().toInt();
84         switch (columnIndex) {
85         case 2:
86             KdenliveSettings::setShowdescriptioncolumn(show);
87             break;
88         case 3:
89             KdenliveSettings::setShowratingcolumn(show);
90             break;
91         default:
92             break;
93         }
94
95         // apply the changed column visibility
96         if (show) {
97             showColumn(columnIndex);
98         } else {
99             hideColumn(columnIndex);
100         }
101     }
102 }
103
104 // virtual
105 void ProjectListView::contextMenuEvent(QContextMenuEvent * event) {
106     emit requestMenu(event->globalPos(), itemAt(event->pos()));
107 }
108
109 // virtual
110 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event) {
111     ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
112     if (!item) emit addClip();
113     else if (item->isGroup()) {
114         if ((columnAt(event->pos().x()) == 1)) QTreeWidget::mouseDoubleClickEvent(event);
115     } else {
116         if ((columnAt(event->pos().x()) == 1) && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) QTreeWidget::mouseDoubleClickEvent(event);
117         else if ((columnAt(event->pos().x()) == 2)) QTreeWidget::mouseDoubleClickEvent(event);
118         else emit showProperties(item->referencedClip());
119     }
120 }
121
122 // virtual
123 void ProjectListView::dragEnterEvent(QDragEnterEvent *event) {
124     if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
125         kDebug() << "////////////////  DRAG ENTR OK";
126     }
127     event->acceptProposedAction();
128 }
129
130 // virtual
131 void ProjectListView::dropEvent(QDropEvent *event) {
132     kDebug() << "////////////////  DROPPED EVENT";
133     if (event->mimeData()->hasUrls()) {
134         QTreeWidgetItem *item = itemAt(event->pos());
135         QString groupName;
136         if (item) {
137             if (((ProjectItem *) item)->isGroup()) groupName = item->text(1);
138             else if (item->parent() && ((ProjectItem *) item->parent())->isGroup())
139                 groupName = item->parent()->text(1);
140         }
141         emit addClip(event->mimeData()->urls(), groupName);
142
143     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
144         ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
145         if (item) {
146             if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
147             if (item->isGroup()) {
148                 //emit addClip(event->mimeData->text());
149                 kDebug() << "////////////////  DROPPED RIGHT 1 ";
150                 const QList <QTreeWidgetItem *> list = selectedItems();
151                 ProjectItem *clone;
152                 QString parentId = item->clipId();
153                 foreach(QTreeWidgetItem *it, list) {
154                     // TODO allow dragging of folders ?
155                     if (!((ProjectItem *) it)->isGroup()/* && ((ProjectItem *) it)->clipId() < 10000*/) {
156                         if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
157                         else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
158                         if (clone) {
159                             item->addChild(clone);
160                             QMap <QString, QString> props;
161                             props.insert("groupname", item->groupName());
162                             props.insert("groupid", parentId);
163                             clone->setProperties(props);
164                         }
165                     }
166                 }
167             } else item = NULL;
168         }
169         if (!item) {
170             kDebug() << "////////////////  DROPPED ON EMPTY ZONE";
171             // item dropped in empty zone, move it to top level
172             const QList <QTreeWidgetItem *> list = selectedItems();
173             ProjectItem *clone;
174             foreach(QTreeWidgetItem *it, list) {
175                 QTreeWidgetItem *parent = it->parent();
176                 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/)  {
177                     kDebug() << "++ item parent: " << parent->text(1);
178                     clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
179                     if (clone) {
180                         addTopLevelItem(clone);
181                         clone->clearProperty("groupname");
182                         clone->clearProperty("groupid");
183                     }
184                 }
185             }
186         }
187     }
188     event->acceptProposedAction();
189 }
190
191 // virtual
192 void ProjectListView::mousePressEvent(QMouseEvent *event) {
193     if (event->button() == Qt::LeftButton) {
194         m_DragStartPosition = event->pos();
195         m_dragStarted = true;
196         QTreeWidgetItem *underMouse = itemAt(event->pos());
197         if (underMouse && underMouse->isSelected()) emit focusMonitor();
198     }
199     QTreeWidget::mousePressEvent(event);
200 }
201
202
203 // virtual
204 void ProjectListView::mouseMoveEvent(QMouseEvent *event) {
205     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
206     if (!m_dragStarted) return;
207
208     if ((event->pos() - m_DragStartPosition).manhattanLength()
209             < QApplication::startDragDistance())
210         return;
211
212     {
213         ProjectItem *clickItem = (ProjectItem *) itemAt(m_DragStartPosition); //event->pos());
214         if (clickItem) {
215             QDrag *drag = new QDrag(this);
216             QMimeData *mimeData = new QMimeData;
217             const QList <QTreeWidgetItem *> list = selectedItems();
218             QStringList ids;
219             foreach(const QTreeWidgetItem *item, list) {
220                 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
221                 if (!clip->isGroup()) ids.append(clip->clipId());
222                 else {
223                     const int children = item->childCount();
224                     for (int i = 0; i < children; i++) {
225                         ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
226                     }
227                 }
228             }
229             if (ids.isEmpty()) return;
230             QByteArray data;
231             data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
232             mimeData->setData("kdenlive/producerslist", data);
233             //mimeData->setText(ids.join(";")); //doc.toString());
234             //mimeData->setImageData(image);
235             drag->setMimeData(mimeData);
236             drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
237             drag->setHotSpot(QPoint(0, 50));
238             drag->exec(Qt::MoveAction);
239         }
240         //event->accept();
241     }
242 }
243
244 // virtual
245 void ProjectListView::dragMoveEvent(QDragMoveEvent * event) {
246     event->setDropAction(Qt::IgnoreAction);
247     event->setDropAction(Qt::MoveAction);
248     if (event->mimeData()->hasText()) {
249         event->acceptProposedAction();
250     }
251     // stop playing because we get a crash otherwise when fetching the thumbnails
252     emit pauseMonitor();
253 }
254
255 QStringList ProjectListView::mimeTypes() const {
256     QStringList qstrList;
257     qstrList << QTreeWidget::mimeTypes();
258     // list of accepted mime types for drop
259     qstrList.append("text/uri-list");
260     qstrList.append("text/plain");
261     qstrList.append("kdenlive/producerslist");
262     return qstrList;
263 }
264
265
266 Qt::DropActions ProjectListView::supportedDropActions() const {
267     // returns what actions are supported when dropping
268     return Qt::MoveAction;
269 }
270
271 #include "projectlistview.moc"