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);
45 setFrameShape(QFrame::NoFrame);
46 setRootIsDecorated(false);
50 headers << i18n("Clip") << i18n("Description") << i18n("Rating");
51 setHeaderLabels(headers);
53 QHeaderView* headerView = header();
54 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
55 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
56 this, SLOT(configureColumns(const QPoint&)));
57 headerView->setClickable(true);
58 headerView->setSortIndicatorShown(true);
59 headerView->setMovable(false);
60 sortByColumn(0, Qt::AscendingOrder);
61 setSortingEnabled(true);
63 if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(1);
64 if (!KdenliveSettings::showratingcolumn()) hideColumn(2);
67 ProjectListView::~ProjectListView()
71 void ProjectListView::configureColumns(const QPoint& pos)
74 popup.addTitle(i18nc("@title:menu", "Columns"));
76 QHeaderView* headerView = header();
77 for (int i = 1; i < headerView->count(); ++i) {
78 const QString text = model()->headerData(i, Qt::Horizontal).toString();
79 QAction* action = popup.addAction(text);
80 action->setCheckable(true);
81 action->setChecked(!headerView->isSectionHidden(i));
85 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
86 if (activatedAction != 0) {
87 const bool show = activatedAction->isChecked();
89 // remember the changed column visibility in the settings
90 const int columnIndex = activatedAction->data().toInt();
91 switch (columnIndex) {
93 KdenliveSettings::setShowdescriptioncolumn(show);
96 KdenliveSettings::setShowratingcolumn(show);
102 // apply the changed column visibility
104 showColumn(columnIndex);
106 hideColumn(columnIndex);
112 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
114 emit requestMenu(event->globalPos(), itemAt(event->pos()));
118 void ProjectListView::keyPressEvent(QKeyEvent * event)
120 if (event->key() == Qt::Key_Return) {
121 QTreeWidgetItem *it = currentItem();
122 if (it) it->setExpanded(!it->isExpanded());
123 } else QTreeWidget::keyPressEvent(event);
127 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
129 QTreeWidgetItem *it = itemAt(event->pos());
135 if (it->type() == PROJECTFOLDERTYPE) {
136 if ((columnAt(event->pos().x()) == 0)) {
137 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
138 int offset = pix.width() + indentation();
139 if (event->pos().x() < offset) {
140 it->setExpanded(!it->isExpanded());
142 } else QTreeWidget::mouseDoubleClickEvent(event);
146 if (it->type() == PROJECTSUBCLIPTYPE) {
148 if ((columnAt(event->pos().x()) == 1)) {
149 QTreeWidget::mouseDoubleClickEvent(event);
152 item = static_cast <ProjectItem *>(it->parent());
153 } else item = static_cast <ProjectItem *>(it);
155 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
157 int column = columnAt(event->pos().x());
158 if (column == 0 && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR || it->childCount() > 0)) {
159 QPixmap pix = qVariantValue<QPixmap>(it->data(0, Qt::DecorationRole));
160 int offset = pix.width() + indentation();
161 if (item->parent()) offset += indentation();
162 if (it->childCount() > 0) {
163 if (offset > event->pos().x()) {
164 it->setExpanded(!it->isExpanded());
168 } else if (pix.isNull() || offset < event->pos().x()) {
169 QTreeWidget::mouseDoubleClickEvent(event);
173 if ((column == 1) && it->type() != PROJECTSUBCLIPTYPE) {
174 QTreeWidget::mouseDoubleClickEvent(event);
177 emit showProperties(item->referencedClip());
181 void ProjectListView::dragEnterEvent(QDragEnterEvent *event)
183 if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
184 kDebug() << "//////////////// DRAG ENTR OK";
186 event->acceptProposedAction();
190 void ProjectListView::dropEvent(QDropEvent *event)
192 FolderProjectItem *item = NULL;
193 QTreeWidgetItem *it = itemAt(event->pos());
194 while (it && it->type() != PROJECTFOLDERTYPE) {
197 if (it) item = static_cast <FolderProjectItem *>(it);
198 if (event->mimeData()->hasUrls()) {
202 groupName = item->groupName();
203 groupId = item->clipId();
205 emit addClip(event->mimeData()->urls(), groupName, groupId);
206 event->setDropAction(Qt::CopyAction);
209 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
211 //emit addClip(event->mimeData->text());
212 const QList <QTreeWidgetItem *> list = selectedItems();
214 QString parentId = item->clipId();
215 foreach(QTreeWidgetItem *it, list) {
216 // TODO allow dragging of folders ?
217 if (it->type() == PROJECTCLIPTYPE) {
218 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
219 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
221 item->addChild(clone);
222 QMap <QString, QString> props;
223 props.insert("groupname", item->groupName());
224 props.insert("groupid", parentId);
225 clone->setProperties(props);
230 // item dropped in empty zone, move it to top level
231 const QList <QTreeWidgetItem *> list = selectedItems();
233 foreach(QTreeWidgetItem *it, list) {
234 QTreeWidgetItem *parent = it->parent();
235 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
236 kDebug() << "++ item parent: " << parent->text(1);
237 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
239 addTopLevelItem(clone);
240 clone->clearProperty("groupname");
241 clone->clearProperty("groupid");
246 emit projectModified();
247 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
248 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
249 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
251 event->acceptProposedAction();
255 void ProjectListView::mousePressEvent(QMouseEvent *event)
257 if (event->button() == Qt::LeftButton) {
258 m_DragStartPosition = event->pos();
259 m_dragStarted = true;
260 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
261 if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
263 QTreeWidget::mousePressEvent(event);
267 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
269 QTreeWidget::mouseReleaseEvent(event);
270 QTreeWidgetItem *underMouse = itemAt(event->pos());
271 if (underMouse) emit focusMonitor();
275 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
277 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
278 if (!m_dragStarted) return;
280 if ((event->pos() - m_DragStartPosition).manhattanLength()
281 < QApplication::startDragDistance())
284 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
286 if (it->type() == PROJECTSUBCLIPTYPE) {
288 SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
289 if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
290 ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
291 QDrag *drag = new QDrag(this);
292 QMimeData *mimeData = new QMimeData;
295 list.append(clip->clipId());
296 QPoint p = clickItem->zone();
297 list.append(QString::number(p.x()));
298 list.append(QString::number(p.y()));
300 data.append(list.join(";").toUtf8());
301 mimeData->setData("kdenlive/clip", data);
302 drag->setMimeData(mimeData);
303 drag->setPixmap(clickItem->data(0, Qt::DecorationRole).value<QPixmap>());
304 drag->setHotSpot(QPoint(0, 50));
308 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
309 QDrag *drag = new QDrag(this);
310 QMimeData *mimeData = new QMimeData;
311 const QList <QTreeWidgetItem *> list = selectedItems();
313 foreach(const QTreeWidgetItem *item, list) {
314 if (item->type() == PROJECTFOLDERTYPE) {
315 const int children = item->childCount();
316 for (int i = 0; i < children; i++) {
317 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
320 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
321 ids.append(clip->clipId());
324 if (ids.isEmpty()) return;
326 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
327 mimeData->setData("kdenlive/producerslist", data);
328 //mimeData->setText(ids.join(";")); //doc.toString());
329 //mimeData->setImageData(image);
330 drag->setMimeData(mimeData);
331 drag->setPixmap(it->data(0, Qt::DecorationRole).value<QPixmap>());
332 drag->setHotSpot(QPoint(0, 50));
340 void ProjectListView::dragMoveEvent(QDragMoveEvent * event)
342 //event->setDropAction(Qt::MoveAction);
343 if (event->mimeData()->hasText()) {
344 event->acceptProposedAction();
346 // stop playing because we get a crash otherwise when fetching the thumbnails
350 QStringList ProjectListView::mimeTypes() const
352 QStringList qstrList;
353 qstrList << QTreeWidget::mimeTypes();
354 // list of accepted mime types for drop
355 qstrList.append("text/uri-list");
356 qstrList.append("text/plain");
357 qstrList.append("kdenlive/producerslist");
358 qstrList.append("kdenlive/clip");
363 Qt::DropActions ProjectListView::supportedDropActions() const
365 // returns what actions are supported when dropping
366 return Qt::MoveAction;
369 #include "projectlistview.moc"