1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "projectlistview.h"
22 #include "projectitem.h"
23 #include "subprojectitem.h"
24 #include "folderprojectitem.h"
25 #include "kdenlivesettings.h"
31 #include <QApplication>
32 #include <QHeaderView>
35 ProjectListView::ProjectListView(QWidget *parent) :
39 setSelectionMode(QAbstractItemView::ExtendedSelection);
40 setDragDropMode(QAbstractItemView::DragDrop);
41 setDropIndicatorShown(true);
42 setAlternatingRowColors(true);
49 headers << i18n("Clip") << i18n("Description") << i18n("Rating");
50 setHeaderLabels(headers);
52 QHeaderView* headerView = header();
53 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
54 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
55 this, SLOT(configureColumns(const QPoint&)));
56 headerView->setClickable(true);
57 headerView->setSortIndicatorShown(true);
58 headerView->setMovable(false);
59 sortByColumn(0, Qt::AscendingOrder);
60 setSortingEnabled(true);
62 if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(1);
63 if (!KdenliveSettings::showratingcolumn()) hideColumn(2);
66 ProjectListView::~ProjectListView()
70 void ProjectListView::configureColumns(const QPoint& pos)
73 popup.addTitle(i18nc("@title:menu", "Columns"));
75 QHeaderView* headerView = header();
76 for (int i = 1; i < headerView->count(); ++i) {
77 const QString text = model()->headerData(i, Qt::Horizontal).toString();
78 QAction* action = popup.addAction(text);
79 action->setCheckable(true);
80 action->setChecked(!headerView->isSectionHidden(i));
84 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
85 if (activatedAction != 0) {
86 const bool show = activatedAction->isChecked();
88 // remember the changed column visibility in the settings
89 const int columnIndex = activatedAction->data().toInt();
90 switch (columnIndex) {
92 KdenliveSettings::setShowdescriptioncolumn(show);
95 KdenliveSettings::setShowratingcolumn(show);
101 // apply the changed column visibility
103 showColumn(columnIndex);
105 hideColumn(columnIndex);
111 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
113 emit requestMenu(event->globalPos(), itemAt(event->pos()));
117 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
119 QTreeWidgetItem *it = itemAt(event->pos());
125 if (it->type() == PROJECTFOLDERTYPE) {
126 if ((columnAt(event->pos().x()) == 0)) QTreeWidget::mouseDoubleClickEvent(event);
129 if (it->type() == PROJECTSUBCLIPTYPE) {
131 if ((columnAt(event->pos().x()) == 1)) {
132 QTreeWidget::mouseDoubleClickEvent(event);
135 item = static_cast <ProjectItem *>(it->parent());
136 } else item = static_cast <ProjectItem *>(it);
138 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
139 if ((columnAt(event->pos().x()) == 0) && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) QTreeWidget::mouseDoubleClickEvent(event);
140 else if ((columnAt(event->pos().x()) == 1) && it->type() != PROJECTSUBCLIPTYPE) QTreeWidget::mouseDoubleClickEvent(event);
141 else emit showProperties(item->referencedClip());
145 void ProjectListView::dragEnterEvent(QDragEnterEvent *event)
147 if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
148 kDebug() << "//////////////// DRAG ENTR OK";
150 event->acceptProposedAction();
154 void ProjectListView::dropEvent(QDropEvent *event)
156 FolderProjectItem *item = NULL;
157 QTreeWidgetItem *it = itemAt(event->pos());
158 while (it && it->type() != PROJECTFOLDERTYPE) {
161 if (it) item = static_cast <FolderProjectItem *>(it);
162 if (event->mimeData()->hasUrls()) {
166 groupName = item->groupName();
167 groupId = item->clipId();
169 emit addClip(event->mimeData()->urls(), groupName, groupId);
170 event->setDropAction(Qt::CopyAction);
173 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
175 //emit addClip(event->mimeData->text());
176 const QList <QTreeWidgetItem *> list = selectedItems();
178 QString parentId = item->clipId();
179 foreach(QTreeWidgetItem *it, list) {
180 // TODO allow dragging of folders ?
181 if (it->type() == PROJECTCLIPTYPE) {
182 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
183 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
185 item->addChild(clone);
186 QMap <QString, QString> props;
187 props.insert("groupname", item->groupName());
188 props.insert("groupid", parentId);
189 clone->setProperties(props);
194 // item dropped in empty zone, move it to top level
195 const QList <QTreeWidgetItem *> list = selectedItems();
197 foreach(QTreeWidgetItem *it, list) {
198 QTreeWidgetItem *parent = it->parent();
199 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
200 kDebug() << "++ item parent: " << parent->text(1);
201 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
203 addTopLevelItem(clone);
204 clone->clearProperty("groupname");
205 clone->clearProperty("groupid");
210 emit projectModified();
211 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
212 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
213 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
215 event->acceptProposedAction();
219 void ProjectListView::mousePressEvent(QMouseEvent *event)
221 if (event->button() == Qt::LeftButton) {
222 m_DragStartPosition = event->pos();
223 m_dragStarted = true;
224 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
225 if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
227 QTreeWidget::mousePressEvent(event);
231 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
233 QTreeWidget::mouseReleaseEvent(event);
234 QTreeWidgetItem *underMouse = itemAt(event->pos());
235 if (underMouse) emit focusMonitor();
239 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
241 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
242 if (!m_dragStarted) return;
244 if ((event->pos() - m_DragStartPosition).manhattanLength()
245 < QApplication::startDragDistance())
248 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
250 if (it->type() == PROJECTSUBCLIPTYPE) {
252 SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
253 if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
254 ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
255 QDrag *drag = new QDrag(this);
256 QMimeData *mimeData = new QMimeData;
259 list.append(clip->clipId());
260 QPoint p = clickItem->zone();
261 list.append(QString::number(p.x()));
262 list.append(QString::number(p.y()));
264 data.append(list.join(";").toUtf8());
265 mimeData->setData("kdenlive/clip", data);
266 drag->setMimeData(mimeData);
267 drag->setPixmap(clickItem->data(0, Qt::DecorationRole).value<QPixmap>());
268 drag->setHotSpot(QPoint(0, 50));
272 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
273 QDrag *drag = new QDrag(this);
274 QMimeData *mimeData = new QMimeData;
275 const QList <QTreeWidgetItem *> list = selectedItems();
277 foreach(const QTreeWidgetItem *item, list) {
278 if (item->type() == PROJECTFOLDERTYPE) {
279 const int children = item->childCount();
280 for (int i = 0; i < children; i++) {
281 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
284 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
285 ids.append(clip->clipId());
288 if (ids.isEmpty()) return;
290 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
291 mimeData->setData("kdenlive/producerslist", data);
292 //mimeData->setText(ids.join(";")); //doc.toString());
293 //mimeData->setImageData(image);
294 drag->setMimeData(mimeData);
295 drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
296 drag->setHotSpot(QPoint(0, 50));
304 void ProjectListView::dragMoveEvent(QDragMoveEvent * event)
306 //event->setDropAction(Qt::MoveAction);
307 if (event->mimeData()->hasText()) {
308 event->acceptProposedAction();
310 // stop playing because we get a crash otherwise when fetching the thumbnails
314 QStringList ProjectListView::mimeTypes() const
316 QStringList qstrList;
317 qstrList << QTreeWidget::mimeTypes();
318 // list of accepted mime types for drop
319 qstrList.append("text/uri-list");
320 qstrList.append("text/plain");
321 qstrList.append("kdenlive/producerslist");
322 qstrList.append("kdenlive/clip");
327 Qt::DropActions ProjectListView::supportedDropActions() const
329 // returns what actions are supported when dropping
330 return Qt::MoveAction;
333 #include "projectlistview.moc"