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 "kdenlivesettings.h"
30 #include <QApplication>
31 #include <QHeaderView>
34 ProjectListView::ProjectListView(QWidget *parent) :
38 setSelectionMode(QAbstractItemView::ExtendedSelection);
39 setDragDropMode(QAbstractItemView::DragDrop);
40 setDropIndicatorShown(true);
41 setAlternatingRowColors(true);
47 headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description") << i18n("Rating");
48 setHeaderLabels(headers);
49 sortByColumn(1, Qt::AscendingOrder);
51 QHeaderView* headerView = header();
52 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
53 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
54 this, SLOT(configureColumns(const QPoint&)));
56 //connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), this, SLOT(slotFocusOut(QTreeWidgetItem *, QTreeWidgetItem *)));
58 if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(2);
59 if (!KdenliveSettings::showratingcolumn()) hideColumn(3);
61 setSortingEnabled(true);
64 ProjectListView::~ProjectListView()
69 void ProjectListView::configureColumns(const QPoint& pos)
72 popup.addTitle(i18nc("@title:menu", "Columns"));
74 QHeaderView* headerView = header();
75 for (int i = 2; i < headerView->count(); ++i) {
76 const QString text = model()->headerData(i, Qt::Horizontal).toString();
77 QAction* action = popup.addAction(text);
78 action->setCheckable(true);
79 action->setChecked(!headerView->isSectionHidden(i));
83 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
84 if (activatedAction != 0) {
85 const bool show = activatedAction->isChecked();
87 // remember the changed column visibility in the settings
88 const int columnIndex = activatedAction->data().toInt();
89 switch (columnIndex) {
91 KdenliveSettings::setShowdescriptioncolumn(show);
94 KdenliveSettings::setShowratingcolumn(show);
100 // apply the changed column visibility
102 showColumn(columnIndex);
104 hideColumn(columnIndex);
110 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
112 emit requestMenu(event->globalPos(), itemAt(event->pos()));
116 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
118 QTreeWidgetItem *it = itemAt(event->pos());
121 if (it->type() == QTreeWidgetItem::UserType + 1) {
123 item = static_cast <ProjectItem *>(it->parent());
124 } else item = static_cast <ProjectItem *>(it);
129 if (!(item->flags() & Qt::ItemIsDragEnabled)) return;
130 if (item->isGroup()) {
131 if ((columnAt(event->pos().x()) == 1)) QTreeWidget::mouseDoubleClickEvent(event);
133 if ((columnAt(event->pos().x()) == 1) && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) QTreeWidget::mouseDoubleClickEvent(event);
134 else if ((columnAt(event->pos().x()) == 2) && it->type() != QTreeWidgetItem::UserType + 1) QTreeWidget::mouseDoubleClickEvent(event);
135 else emit showProperties(item->referencedClip());
140 void ProjectListView::dragEnterEvent(QDragEnterEvent *event)
142 if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
143 kDebug() << "//////////////// DRAG ENTR OK";
145 event->acceptProposedAction();
149 void ProjectListView::dropEvent(QDropEvent *event)
151 ProjectItem *item = NULL;
152 QTreeWidgetItem *it = itemAt(event->pos());
154 if (it->type() == QTreeWidgetItem::UserType + 1) {
156 item = static_cast <ProjectItem *>(it->parent());
157 } else item = static_cast <ProjectItem *>(it);
160 if (event->mimeData()->hasUrls()) {
164 if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
165 if (item->isGroup()) {
166 groupName = item->groupName();
167 groupId = item->clipId();
170 emit addClip(event->mimeData()->urls(), groupName, groupId);
171 event->setDropAction(Qt::CopyAction);
174 } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
176 if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
177 if (item->isGroup()) {
178 //emit addClip(event->mimeData->text());
179 const QList <QTreeWidgetItem *> list = selectedItems();
181 QString parentId = item->clipId();
182 foreach(QTreeWidgetItem *it, list) {
183 // TODO allow dragging of folders ?
184 if (!((ProjectItem *) it)->isGroup()/* && ((ProjectItem *) it)->clipId() < 10000*/) {
185 if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
186 else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
188 item->addChild(clone);
189 QMap <QString, QString> props;
190 props.insert("groupname", item->groupName());
191 props.insert("groupid", parentId);
192 clone->setProperties(props);
199 // item dropped in empty zone, move it to top level
200 const QList <QTreeWidgetItem *> list = selectedItems();
202 foreach(QTreeWidgetItem *it, list) {
203 QTreeWidgetItem *parent = it->parent();
204 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/) {
205 kDebug() << "++ item parent: " << parent->text(1);
206 clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
208 addTopLevelItem(clone);
209 clone->clearProperty("groupname");
210 clone->clearProperty("groupid");
215 } else if (event->mimeData()->hasFormat("kdenlive/clip")) {
216 QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
217 emit addClipCut(list.at(0), list.at(1).toInt(), list.at(2).toInt());
219 event->acceptProposedAction();
223 void ProjectListView::mousePressEvent(QMouseEvent *event)
225 if (event->button() == Qt::LeftButton) {
226 m_DragStartPosition = event->pos();
227 m_dragStarted = true;
228 /*QTreeWidgetItem *underMouse = itemAt(event->pos());
229 if (underMouse && underMouse->isSelected()) emit focusMonitor();*/
231 QTreeWidget::mousePressEvent(event);
235 void ProjectListView::mouseReleaseEvent(QMouseEvent *event)
237 QTreeWidget::mouseReleaseEvent(event);
238 QTreeWidgetItem *underMouse = itemAt(event->pos());
239 if (underMouse) emit focusMonitor();
243 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
245 //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
246 if (!m_dragStarted) return;
248 if ((event->pos() - m_DragStartPosition).manhattanLength()
249 < QApplication::startDragDistance())
252 QTreeWidgetItem *it = itemAt(m_DragStartPosition);
254 if (it->type() == QTreeWidgetItem::UserType + 1) {
256 SubProjectItem *clickItem = static_cast <SubProjectItem *>(it);
257 if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
258 ProjectItem *clip = static_cast <ProjectItem *>(it->parent());
259 QDrag *drag = new QDrag(this);
260 QMimeData *mimeData = new QMimeData;
263 list.append(clip->clipId());
264 QPoint p = clickItem->zone();
265 list.append(QString::number(p.x()));
266 list.append(QString::number(p.y()));
268 data.append(list.join(";").toUtf8());
269 mimeData->setData("kdenlive/clip", data);
270 drag->setMimeData(mimeData);
271 drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
272 drag->setHotSpot(QPoint(0, 50));
276 ProjectItem *clickItem = static_cast <ProjectItem *>(it);
277 if (clickItem && (clickItem->flags() & Qt::ItemIsDragEnabled)) {
278 QDrag *drag = new QDrag(this);
279 QMimeData *mimeData = new QMimeData;
280 const QList <QTreeWidgetItem *> list = selectedItems();
282 foreach(const QTreeWidgetItem *item, list) {
283 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
284 if (!clip->isGroup()) ids.append(clip->clipId());
286 const int children = item->childCount();
287 for (int i = 0; i < children; i++) {
288 ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
292 if (ids.isEmpty()) return;
294 data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
295 mimeData->setData("kdenlive/producerslist", data);
296 //mimeData->setText(ids.join(";")); //doc.toString());
297 //mimeData->setImageData(image);
298 drag->setMimeData(mimeData);
299 drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
300 drag->setHotSpot(QPoint(0, 50));
308 void ProjectListView::dragMoveEvent(QDragMoveEvent * event)
310 //event->setDropAction(Qt::MoveAction);
311 if (event->mimeData()->hasText()) {
312 event->acceptProposedAction();
314 // stop playing because we get a crash otherwise when fetching the thumbnails
318 QStringList ProjectListView::mimeTypes() const
320 QStringList qstrList;
321 qstrList << QTreeWidget::mimeTypes();
322 // list of accepted mime types for drop
323 qstrList.append("text/uri-list");
324 qstrList.append("text/plain");
325 qstrList.append("kdenlive/producerslist");
326 qstrList.append("kdenlive/clip");
331 Qt::DropActions ProjectListView::supportedDropActions() const
333 // returns what actions are supported when dropping
334 return Qt::MoveAction;
337 #include "projectlistview.moc"