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 ***************************************************************************/
20 #include <QApplication>
21 #include <QHeaderView>
28 #include "projectitem.h"
29 #include "projectlistview.h"
30 #include "kdenlivesettings.h"
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);
44 headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description") << i18n("Rating");
45 setHeaderLabels(headers);
46 sortByColumn(1, Qt::AscendingOrder);
48 QHeaderView* headerView = header();
49 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
50 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
51 this, SLOT(configureColumns(const QPoint&)));
53 //connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), this, SLOT(slotFocusOut(QTreeWidgetItem *, QTreeWidgetItem *)));
55 if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(2);
56 if (!KdenliveSettings::showratingcolumn()) hideColumn(3);
58 setIconSize(QSize(60, 40));
59 setSortingEnabled(true);
62 ProjectListView::~ProjectListView() {
66 void ProjectListView::configureColumns(const QPoint& pos) {
68 popup.addTitle(i18nc("@title:menu", "Columns"));
70 QHeaderView* headerView = header();
71 for (int i = 2; i < headerView->count(); ++i) {
72 const int logicalIndex = headerView->logicalIndex(i);
73 const QString text = model()->headerData(i, Qt::Horizontal).toString();
74 QAction* action = popup.addAction(text);
75 action->setCheckable(true);
76 action->setChecked(!headerView->isSectionHidden(logicalIndex));
80 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
81 if (activatedAction != 0) {
82 const bool show = activatedAction->isChecked();
84 // remember the changed column visibility in the settings
85 const int columnIndex = activatedAction->data().toInt();
86 switch (columnIndex) {
88 KdenliveSettings::setShowdescriptioncolumn(show);
91 KdenliveSettings::setShowratingcolumn(show);
97 // apply the changed column visibility
99 showColumn(columnIndex);
101 hideColumn(columnIndex);
107 void ProjectListView::contextMenuEvent(QContextMenuEvent * event) {
108 emit requestMenu(event->globalPos(), itemAt(event->pos()));
112 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event) {
113 ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
114 if (!item) emit addClip();
115 else if (item->isGroup()) {
116 if ((columnAt(event->pos().x()) == 1)) QTreeWidget::mouseDoubleClickEvent(event);
118 if ((columnAt(event->pos().x()) == 2)) QTreeWidget::mouseDoubleClickEvent(event);
119 else emit showProperties(item->referencedClip());
124 void ProjectListView::dragEnterEvent(QDragEnterEvent *event) {
125 if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
126 kDebug() << "//////////////// DRAG ENTR OK";
128 event->acceptProposedAction();
132 void ProjectListView::dropEvent(QDropEvent *event) {
133 kDebug() << "//////////////// DROPPED EVENT";
134 if (event->mimeData()->hasUrls()) {
135 QTreeWidgetItem *item = itemAt(event->pos());
138 if (((ProjectItem *) item)->isGroup()) groupName = item->text(1);
139 else if (item->parent() && ((ProjectItem *) item->parent())->isGroup())
140 groupName = item->parent()->text(1);
142 const QList <QUrl> list = event->mimeData()->urls();
143 foreach(const QUrl &url, list) {
144 emit addClip(KUrl(url), groupName);
147 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
148 ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
150 if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
151 if (item->isGroup()) {
152 //emit addClip(event->mimeData->text());
153 kDebug() << "//////////////// DROPPED RIGHT 1 ";
154 const QList <QTreeWidgetItem *> list = selectedItems();
156 QString parentId = item->clipId();
157 foreach(QTreeWidgetItem *it, list) {
158 // TODO allow dragging of folders ?
159 if (!((ProjectItem *) it)->isGroup()/* && ((ProjectItem *) it)->clipId() < 10000*/) {
160 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
161 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
163 item->addChild(clone);
164 QMap <QString, QString> props;
165 props.insert("groupname", item->groupName());
166 props.insert("groupid", parentId);
167 clone->setProperties(props);
174 kDebug() << "//////////////// DROPPED ONÂ EMPTY ZONE";
175 // item dropped in empty zone, move it to top level
176 const QList <QTreeWidgetItem *> list = selectedItems();
178 foreach(QTreeWidgetItem *it, list) {
179 QTreeWidgetItem *parent = it->parent();
180 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
181 kDebug() << "++ item parent: " << parent->text(1);
182 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
184 addTopLevelItem(clone);
185 clone->clearProperty("groupname");
186 clone->clearProperty("groupid");
192 event->acceptProposedAction();
196 void ProjectListView::mousePressEvent(QMouseEvent *event) {
197 if (event->button() == Qt::LeftButton) {
198 this->m_DragStartPosition = event->pos();
199 m_dragStarted = true;
200 QTreeWidgetItem *underMouse = itemAt(event->pos());
201 if (underMouse && underMouse->isSelected()) emit focusMonitor();
203 QTreeWidget::mousePressEvent(event);
208 void ProjectListView::mouseMoveEvent(QMouseEvent *event) {
209 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
210 if (!m_dragStarted) return;
212 if ((event->pos() - m_DragStartPosition).manhattanLength()
213 < QApplication::startDragDistance())
217 ProjectItem *clickItem = (ProjectItem *) itemAt(m_DragStartPosition); //event->pos());
219 QDrag *drag = new QDrag(this);
220 QMimeData *mimeData = new QMimeData;
222 const QList <QTreeWidgetItem *> list = selectedItems();
224 foreach(const QTreeWidgetItem *item, list) {
225 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
226 if (!clip->isGroup()) ids.append(clip->clipId());
228 const int children = item->childCount();
229 for (int i = 0; i < children; i++) {
230 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
234 if (ids.isEmpty()) return;
236 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
237 mimeData->setData("kdenlive/producerslist", data);
238 //mimeData->setText(ids.join(";")); //doc.toString());
239 //mimeData->setImageData(image);
240 drag->setMimeData(mimeData);
241 drag->setPixmap(clickItem->icon(0).pixmap((int)(50 *16 / 9.0), 50));
242 drag->setHotSpot(QPoint(0, 50));
243 drag->start(Qt::MoveAction);
245 //Qt::DropAction dropAction;
246 //dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
248 //Qt::DropAction dropAction = drag->exec();
256 void ProjectListView::dragMoveEvent(QDragMoveEvent * event) {
257 event->setDropAction(Qt::IgnoreAction);
258 event->setDropAction(Qt::MoveAction);
259 if (event->mimeData()->hasText()) {
260 event->acceptProposedAction();
262 // stop playing because we get a crash otherwise when fetching the thumbnails
266 QStringList ProjectListView::mimeTypes() const {
267 QStringList qstrList;
268 qstrList << QTreeWidget::mimeTypes();
269 // list of accepted mime types for drop
270 qstrList.append("text/uri-list");
271 qstrList.append("text/plain");
272 qstrList.append("kdenlive/producerslist");
277 Qt::DropActions ProjectListView::supportedDropActions() const {
278 // returns what actions are supported when dropping
279 return Qt::MoveAction;
282 #include "projectlistview.moc"