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 "kdenlivesettings.h"
29 #include <QApplication>
30 #include <QHeaderView>
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 setSortingEnabled(true);
61 ProjectListView::~ProjectListView() {
65 void ProjectListView::configureColumns(const QPoint& pos) {
67 popup.addTitle(i18nc("@title:menu", "Columns"));
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));
78 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
79 if (activatedAction != 0) {
80 const bool show = activatedAction->isChecked();
82 // remember the changed column visibility in the settings
83 const int columnIndex = activatedAction->data().toInt();
84 switch (columnIndex) {
86 KdenliveSettings::setShowdescriptioncolumn(show);
89 KdenliveSettings::setShowratingcolumn(show);
95 // apply the changed column visibility
97 showColumn(columnIndex);
99 hideColumn(columnIndex);
105 void ProjectListView::contextMenuEvent(QContextMenuEvent * event) {
106 emit requestMenu(event->globalPos(), itemAt(event->pos()));
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);
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());
123 void ProjectListView::dragEnterEvent(QDragEnterEvent *event) {
124 if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
125 kDebug() << "//////////////// DRAG ENTR OK";
127 event->acceptProposedAction();
131 void ProjectListView::dropEvent(QDropEvent *event) {
132 kDebug() << "//////////////// DROPPED EVENT";
133 if (event->mimeData()->hasUrls()) {
134 QTreeWidgetItem *item = itemAt(event->pos());
137 if (((ProjectItem *) item)->isGroup()) groupName = item->text(1);
138 else if (item->parent() && ((ProjectItem *) item->parent())->isGroup())
139 groupName = item->parent()->text(1);
141 const QList <QUrl> list = event->mimeData()->urls();
142 foreach(const QUrl &url, list) {
143 emit addClip(KUrl(url), groupName);
146 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
147 ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
149 if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
150 if (item->isGroup()) {
151 //emit addClip(event->mimeData->text());
152 kDebug() << "//////////////// DROPPED RIGHT 1 ";
153 const QList <QTreeWidgetItem *> list = selectedItems();
155 QString parentId = item->clipId();
156 foreach(QTreeWidgetItem *it, list) {
157 // TODO allow dragging of folders ?
158 if (!((ProjectItem *) it)->isGroup()/* && ((ProjectItem *) it)->clipId() < 10000*/) {
159 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
160 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
162 item->addChild(clone);
163 QMap <QString, QString> props;
164 props.insert("groupname", item->groupName());
165 props.insert("groupid", parentId);
166 clone->setProperties(props);
173 kDebug() << "//////////////// DROPPED ONÂ EMPTY ZONE";
174 // item dropped in empty zone, move it to top level
175 const QList <QTreeWidgetItem *> list = selectedItems();
177 foreach(QTreeWidgetItem *it, list) {
178 QTreeWidgetItem *parent = it->parent();
179 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
180 kDebug() << "++ item parent: " << parent->text(1);
181 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
183 addTopLevelItem(clone);
184 clone->clearProperty("groupname");
185 clone->clearProperty("groupid");
191 event->acceptProposedAction();
195 void ProjectListView::mousePressEvent(QMouseEvent *event) {
196 if (event->button() == Qt::LeftButton) {
197 m_DragStartPosition = event->pos();
198 m_dragStarted = true;
199 QTreeWidgetItem *underMouse = itemAt(event->pos());
200 if (underMouse && underMouse->isSelected()) emit focusMonitor();
202 QTreeWidget::mousePressEvent(event);
207 void ProjectListView::mouseMoveEvent(QMouseEvent *event) {
208 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
209 if (!m_dragStarted) return;
211 if ((event->pos() - m_DragStartPosition).manhattanLength()
212 < QApplication::startDragDistance())
216 ProjectItem *clickItem = (ProjectItem *) itemAt(m_DragStartPosition); //event->pos());
218 QDrag *drag = new QDrag(this);
219 QMimeData *mimeData = new QMimeData;
221 const QList <QTreeWidgetItem *> list = selectedItems();
223 foreach(const QTreeWidgetItem *item, list) {
224 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
225 if (!clip->isGroup()) ids.append(clip->clipId());
227 const int children = item->childCount();
228 for (int i = 0; i < children; i++) {
229 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
233 if (ids.isEmpty()) return;
235 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
236 mimeData->setData("kdenlive/producerslist", data);
237 //mimeData->setText(ids.join(";")); //doc.toString());
238 //mimeData->setImageData(image);
239 drag->setMimeData(mimeData);
240 drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
241 drag->setHotSpot(QPoint(0, 50));
242 drag->exec(Qt::MoveAction);
249 void ProjectListView::dragMoveEvent(QDragMoveEvent * event) {
250 event->setDropAction(Qt::IgnoreAction);
251 event->setDropAction(Qt::MoveAction);
252 if (event->mimeData()->hasText()) {
253 event->acceptProposedAction();
255 // stop playing because we get a crash otherwise when fetching the thumbnails
259 QStringList ProjectListView::mimeTypes() const {
260 QStringList qstrList;
261 qstrList << QTreeWidget::mimeTypes();
262 // list of accepted mime types for drop
263 qstrList.append("text/uri-list");
264 qstrList.append("text/plain");
265 qstrList.append("kdenlive/producerslist");
270 Qt::DropActions ProjectListView::supportedDropActions() const {
271 // returns what actions are supported when dropping
272 return Qt::MoveAction;
275 #include "projectlistview.moc"