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("Thumbnail") << i18n("Filename") << 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(1, Qt::AscendingOrder);
60 setSortingEnabled(true);
62 if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(2);
63 if (!KdenliveSettings::showratingcolumn()) hideColumn(3);
66 ProjectListView::~ProjectListView()
71 void ProjectListView::configureColumns(const QPoint& pos)
74 popup.addTitle(i18nc("@title:menu", "Columns"));
76 QHeaderView* headerView = header();
77 for (int i = 2; 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::mouseDoubleClickEvent(QMouseEvent * event)
120 QTreeWidgetItem *it = itemAt(event->pos());
126 if (it->type() == PROJECTFOLDERTYPE) {
127 if ((columnAt(event->pos().x()) == 1)) QTreeWidget::mouseDoubleClickEvent(event);
130 if (it->type() == PROJECTSUBCLIPTYPE) {
132 item = static_cast <ProjectItem *>(it->parent());
133 } else item = static_cast <ProjectItem *>(it);
135 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
136 if ((columnAt(event->pos().x()) == 1) && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) QTreeWidget::mouseDoubleClickEvent(event);
137 else if ((columnAt(event->pos().x()) == 2) && it->type() != PROJECTSUBCLIPTYPE) QTreeWidget::mouseDoubleClickEvent(event);
138 else emit showProperties(item->referencedClip());
142 void ProjectListView::dragEnterEvent(QDragEnterEvent *event)
144 if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
145 kDebug() << "//////////////// DRAG ENTR OK";
147 event->acceptProposedAction();
151 void ProjectListView::dropEvent(QDropEvent *event)
153 FolderProjectItem *item = NULL;
154 QTreeWidgetItem *it = itemAt(event->pos());
155 while (it && it->type() != PROJECTFOLDERTYPE) {
158 if (it) item = static_cast <FolderProjectItem *>(it);
159 if (event->mimeData()->hasUrls()) {
163 groupName = item->groupName();
164 groupId = item->clipId();
166 emit addClip(event->mimeData()->urls(), groupName, groupId);
167 event->setDropAction(Qt::CopyAction);
170 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
172 //emit addClip(event->mimeData->text());
173 const QList <QTreeWidgetItem *> list = selectedItems();
175 QString parentId = item->clipId();
176 foreach(QTreeWidgetItem *it, list) {
177 // TODO allow dragging of folders ?
178 if (it->type() == PROJECTCLIPTYPE) {
179 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
180 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
182 item->addChild(clone);
183 QMap <QString, QString> props;
184 props.insert("groupname", item->groupName());
185 props.insert("groupid", parentId);
186 clone->setProperties(props);
191 // item dropped in empty zone, move it to top level
192 const QList <QTreeWidgetItem *> list = selectedItems();
194 foreach(QTreeWidgetItem *it, list) {
195 QTreeWidgetItem *parent = it->parent();
196 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
197 kDebug() << "++ item parent: " << parent->text(1);
198 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
200 addTopLevelItem(clone);
201 clone->clearProperty("groupname");
202 clone->clearProperty("groupid");
207 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
208 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
209 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
211 event->acceptProposedAction();
215 void ProjectListView::mousePressEvent(QMouseEvent *event)
217 if (event->button() == Qt::LeftButton) {
218 m_DragStartPosition = event->pos();
219 m_dragStarted = true;
220 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
221 if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
223 QTreeWidget::mousePressEvent(event);
227 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
229 QTreeWidget::mouseReleaseEvent(event);
230 QTreeWidgetItem *underMouse = itemAt(event->pos());
231 if (underMouse) emit focusMonitor();
235 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
237 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
238 if (!m_dragStarted) return;
240 if ((event->pos() - m_DragStartPosition).manhattanLength()
241 < QApplication::startDragDistance())
244 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
246 if (it->type() == PROJECTSUBCLIPTYPE) {
248 SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
249 if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
250 ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
251 QDrag *drag = new QDrag(this);
252 QMimeData *mimeData = new QMimeData;
255 list.append(clip->clipId());
256 QPoint p = clickItem->zone();
257 list.append(QString::number(p.x()));
258 list.append(QString::number(p.y()));
260 data.append(list.join(";").toUtf8());
261 mimeData->setData("kdenlive/clip", data);
262 drag->setMimeData(mimeData);
263 drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
264 drag->setHotSpot(QPoint(0, 50));
268 if (it && (it->flags() & Qt::ItemIsDragEnabled)) {
269 QDrag *drag = new QDrag(this);
270 QMimeData *mimeData = new QMimeData;
271 const QList <QTreeWidgetItem *> list = selectedItems();
273 foreach(const QTreeWidgetItem *item, list) {
274 if (item->type() == PROJECTFOLDERTYPE) {
275 const int children = item->childCount();
276 for (int i = 0; i < children; i++) {
277 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
280 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
281 ids.append(clip->clipId());
284 if (ids.isEmpty()) return;
286 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
287 mimeData->setData("kdenlive/producerslist", data);
288 //mimeData->setText(ids.join(";")); //doc.toString());
289 //mimeData->setImageData(image);
290 drag->setMimeData(mimeData);
291 drag->setPixmap(it->icon(0).pixmap(iconSize()));
292 drag->setHotSpot(QPoint(0, 50));
300 void ProjectListView::dragMoveEvent(QDragMoveEvent * event)
302 //event->setDropAction(Qt::MoveAction);
303 if (event->mimeData()->hasText()) {
304 event->acceptProposedAction();
306 // stop playing because we get a crash otherwise when fetching the thumbnails
310 QStringList ProjectListView::mimeTypes() const
312 QStringList qstrList;
313 qstrList << QTreeWidget::mimeTypes();
314 // list of accepted mime types for drop
315 qstrList.append("text/uri-list");
316 qstrList.append("text/plain");
317 qstrList.append("kdenlive/producerslist");
318 qstrList.append("kdenlive/clip");
323 Qt::DropActions ProjectListView::supportedDropActions() const
325 // returns what actions are supported when dropping
326 return Qt::MoveAction;
329 #include "projectlistview.moc"