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)
36 setSelectionMode(QAbstractItemView::ExtendedSelection);
37 setDragDropMode(QAbstractItemView::DragDrop);
38 setDropIndicatorShown(true);
39 setAlternatingRowColors(true);
45 headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description") << i18n("Rating");
46 setHeaderLabels(headers);
47 sortByColumn(1, Qt::AscendingOrder);
49 QHeaderView* headerView = header();
50 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
51 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
52 this, SLOT(configureColumns(const QPoint&)));
54 //connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), this, SLOT(slotFocusOut(QTreeWidgetItem *, QTreeWidgetItem *)));
56 if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(2);
57 if (!KdenliveSettings::showratingcolumn()) hideColumn(3);
59 setSortingEnabled(true);
62 ProjectListView::~ProjectListView()
67 void ProjectListView::configureColumns(const QPoint& pos)
70 popup.addTitle(i18nc("@title:menu", "Columns"));
72 QHeaderView* headerView = header();
73 for (int i = 2; i < headerView->count(); ++i) {
74 const QString text = model()->headerData(i, Qt::Horizontal).toString();
75 QAction* action = popup.addAction(text);
76 action->setCheckable(true);
77 action->setChecked(!headerView->isSectionHidden(i));
81 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
82 if (activatedAction != 0) {
83 const bool show = activatedAction->isChecked();
85 // remember the changed column visibility in the settings
86 const int columnIndex = activatedAction->data().toInt();
87 switch (columnIndex) {
89 KdenliveSettings::setShowdescriptioncolumn(show);
92 KdenliveSettings::setShowratingcolumn(show);
98 // apply the changed column visibility
100 showColumn(columnIndex);
102 hideColumn(columnIndex);
108 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
110 emit requestMenu(event->globalPos(), itemAt(event->pos()));
114 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
116 ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
117 if (!item) emit addClip();
118 else if (item->isGroup()) {
119 if ((columnAt(event->pos().x()) == 1)) QTreeWidget::mouseDoubleClickEvent(event);
121 if ((columnAt(event->pos().x()) == 1) && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) QTreeWidget::mouseDoubleClickEvent(event);
122 else if ((columnAt(event->pos().x()) == 2)) QTreeWidget::mouseDoubleClickEvent(event);
123 else emit showProperties(item->referencedClip());
128 void ProjectListView::dragEnterEvent(QDragEnterEvent *event)
130 if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
131 kDebug() << "//////////////// DRAG ENTR OK";
133 event->acceptProposedAction();
137 void ProjectListView::dropEvent(QDropEvent *event)
139 kDebug() << "//////////////// DROPPED EVENT";
140 if (event->mimeData()->hasUrls()) {
141 QTreeWidgetItem *item = itemAt(event->pos());
144 if (((ProjectItem *) item)->isGroup()) groupName = item->text(1);
145 else if (item->parent() && ((ProjectItem *) item->parent())->isGroup())
146 groupName = item->parent()->text(1);
148 emit addClip(event->mimeData()->urls(), groupName);
150 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
151 ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
153 if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
154 if (item->isGroup()) {
155 //emit addClip(event->mimeData->text());
156 kDebug() << "//////////////// DROPPED RIGHT 1 ";
157 const QList <QTreeWidgetItem *> list = selectedItems();
159 QString parentId = item->clipId();
160 foreach(QTreeWidgetItem *it, list) {
161 // TODO allow dragging of folders ?
162 if (!((ProjectItem *) it)->isGroup()/* && ((ProjectItem *) it)->clipId() < 10000*/) {
163 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
164 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
166 item->addChild(clone);
167 QMap <QString, QString> props;
168 props.insert("groupname", item->groupName());
169 props.insert("groupid", parentId);
170 clone->setProperties(props);
177 kDebug() << "//////////////// DROPPED ONÂ EMPTY ZONE";
178 // item dropped in empty zone, move it to top level
179 const QList <QTreeWidgetItem *> list = selectedItems();
181 foreach(QTreeWidgetItem *it, list) {
182 QTreeWidgetItem *parent = it->parent();
183 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
184 kDebug() << "++ item parent: " << parent->text(1);
185 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
187 addTopLevelItem(clone);
188 clone->clearProperty("groupname");
189 clone->clearProperty("groupid");
195 event->acceptProposedAction();
199 void ProjectListView::mousePressEvent(QMouseEvent *event)
201 if (event->button() == Qt::LeftButton) {
202 m_DragStartPosition = event->pos();
203 m_dragStarted = true;
204 QTreeWidgetItem *underMouse = itemAt(event->pos());
205 if (underMouse && underMouse->isSelected()) emit focusMonitor();
207 QTreeWidget::mousePressEvent(event);
212 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
214 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
215 if (!m_dragStarted) return;
217 if ((event->pos() - m_DragStartPosition).manhattanLength()
218 < QApplication::startDragDistance())
222 ProjectItem *clickItem = (ProjectItem *) itemAt(m_DragStartPosition); //event->pos());
224 QDrag *drag = new QDrag(this);
225 QMimeData *mimeData = new QMimeData;
226 const QList <QTreeWidgetItem *> list = selectedItems();
228 foreach(const QTreeWidgetItem *item, list) {
229 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
230 if (!clip->isGroup()) ids.append(clip->clipId());
232 const int children = item->childCount();
233 for (int i = 0; i < children; i++) {
234 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
238 if (ids.isEmpty()) return;
240 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
241 mimeData->setData("kdenlive/producerslist", data);
242 //mimeData->setText(ids.join(";")); //doc.toString());
243 //mimeData->setImageData(image);
244 drag->setMimeData(mimeData);
245 drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
246 drag->setHotSpot(QPoint(0, 50));
247 drag->exec(Qt::MoveAction);
254 void ProjectListView::dragMoveEvent(QDragMoveEvent * event)
256 event->setDropAction(Qt::IgnoreAction);
257 event->setDropAction(Qt::MoveAction);
258 if (event->mimeData()->hasText()) {
259 event->acceptProposedAction();
261 // stop playing because we get a crash otherwise when fetching the thumbnails
265 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
279 // returns what actions are supported when dropping
280 return Qt::MoveAction;
283 #include "projectlistview.moc"