]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
Allow editing of clip names again (for color, titles & slideshows), fix saving issue:
[kdenlive] / src / projectlistview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20 #include <QApplication>
21 #include <QHeaderView>
22 #include <QAction>
23
24 #include <KDebug>
25 #include <KMenu>
26 #include <KLocale>
27
28 #include "projectitem.h"
29 #include "projectlistview.h"
30 #include "kdenlivesettings.h"
31
32
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);
39     setDragEnabled(true);
40     setAcceptDrops(true);
41
42     setColumnCount(4);
43     QStringList headers;
44     headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description") << i18n("Rating");
45     setHeaderLabels(headers);
46     sortByColumn(1, Qt::AscendingOrder);
47
48     QHeaderView* headerView = header();
49     headerView->setContextMenuPolicy(Qt::CustomContextMenu);
50     connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
51             this, SLOT(configureColumns(const QPoint&)));
52
53     //connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), this, SLOT(slotFocusOut(QTreeWidgetItem *, QTreeWidgetItem *)));
54
55     if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(2);
56     if (!KdenliveSettings::showratingcolumn()) hideColumn(3);
57
58     setIconSize(QSize(60, 40));
59     setSortingEnabled(true);
60 }
61
62 ProjectListView::~ProjectListView() {
63 }
64
65
66 void ProjectListView::configureColumns(const QPoint& pos) {
67     KMenu popup(this);
68     popup.addTitle(i18nc("@title:menu", "Columns"));
69
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));
77         action->setData(i);
78     }
79
80     QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
81     if (activatedAction != 0) {
82         const bool show = activatedAction->isChecked();
83
84         // remember the changed column visibility in the settings
85         const int columnIndex = activatedAction->data().toInt();
86         switch (columnIndex) {
87         case 2:
88             KdenliveSettings::setShowdescriptioncolumn(show);
89             break;
90         case 3:
91             KdenliveSettings::setShowratingcolumn(show);
92             break;
93         default:
94             break;
95         }
96
97         // apply the changed column visibility
98         if (show) {
99             showColumn(columnIndex);
100         } else {
101             hideColumn(columnIndex);
102         }
103     }
104 }
105
106 // virtual
107 void ProjectListView::contextMenuEvent(QContextMenuEvent * event) {
108     emit requestMenu(event->globalPos(), itemAt(event->pos()));
109 }
110
111 // virtual
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);
117     } else {
118         if ((columnAt(event->pos().x()) == 1) && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) QTreeWidget::mouseDoubleClickEvent(event);
119         else if ((columnAt(event->pos().x()) == 2)) QTreeWidget::mouseDoubleClickEvent(event);
120         else emit showProperties(item->referencedClip());
121     }
122 }
123
124 // virtual
125 void ProjectListView::dragEnterEvent(QDragEnterEvent *event) {
126     if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
127         kDebug() << "////////////////  DRAG ENTR OK";
128     }
129     event->acceptProposedAction();
130 }
131
132 // virtual
133 void ProjectListView::dropEvent(QDropEvent *event) {
134     kDebug() << "////////////////  DROPPED EVENT";
135     if (event->mimeData()->hasUrls()) {
136         QTreeWidgetItem *item = itemAt(event->pos());
137         QString groupName;
138         if (item) {
139             if (((ProjectItem *) item)->isGroup()) groupName = item->text(1);
140             else if (item->parent() && ((ProjectItem *) item->parent())->isGroup())
141                 groupName = item->parent()->text(1);
142         }
143         const QList <QUrl> list = event->mimeData()->urls();
144         foreach(const QUrl &url, list) {
145             emit addClip(KUrl(url), groupName);
146         }
147
148     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
149         ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
150         if (item) {
151             if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
152             if (item->isGroup()) {
153                 //emit addClip(event->mimeData->text());
154                 kDebug() << "////////////////  DROPPED RIGHT 1 ";
155                 const QList <QTreeWidgetItem *> list = selectedItems();
156                 ProjectItem *clone;
157                 QString parentId = item->clipId();
158                 foreach(QTreeWidgetItem *it, list) {
159                     // TODO allow dragging of folders ?
160                     if (!((ProjectItem *) it)->isGroup()/* && ((ProjectItem *) it)->clipId() < 10000*/) {
161                         if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
162                         else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
163                         if (clone) {
164                             item->addChild(clone);
165                             QMap <QString, QString> props;
166                             props.insert("groupname", item->groupName());
167                             props.insert("groupid", parentId);
168                             clone->setProperties(props);
169                         }
170                     }
171                 }
172             } else item = NULL;
173         }
174         if (!item) {
175             kDebug() << "////////////////  DROPPED ON EMPTY ZONE";
176             // item dropped in empty zone, move it to top level
177             const QList <QTreeWidgetItem *> list = selectedItems();
178             ProjectItem *clone;
179             foreach(QTreeWidgetItem *it, list) {
180                 QTreeWidgetItem *parent = it->parent();
181                 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/)  {
182                     kDebug() << "++ item parent: " << parent->text(1);
183                     clone = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
184                     if (clone) {
185                         addTopLevelItem(clone);
186                         clone->clearProperty("groupname");
187                         clone->clearProperty("groupid");
188                     }
189                 }
190             }
191         }
192     }
193     event->acceptProposedAction();
194 }
195
196 // virtual
197 void ProjectListView::mousePressEvent(QMouseEvent *event) {
198     if (event->button() == Qt::LeftButton) {
199         this->m_DragStartPosition = event->pos();
200         m_dragStarted = true;
201         QTreeWidgetItem *underMouse = itemAt(event->pos());
202         if (underMouse && underMouse->isSelected()) emit focusMonitor();
203     }
204     QTreeWidget::mousePressEvent(event);
205 }
206
207
208 // virtual
209 void ProjectListView::mouseMoveEvent(QMouseEvent *event) {
210     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
211     if (!m_dragStarted) return;
212
213     if ((event->pos() - m_DragStartPosition).manhattanLength()
214             < QApplication::startDragDistance())
215         return;
216
217     {
218         ProjectItem *clickItem = (ProjectItem *) itemAt(m_DragStartPosition); //event->pos());
219         if (clickItem) {
220             QDrag *drag = new QDrag(this);
221             QMimeData *mimeData = new QMimeData;
222             QDomDocument doc;
223             const QList <QTreeWidgetItem *> list = selectedItems();
224             QStringList ids;
225             foreach(const QTreeWidgetItem *item, list) {
226                 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
227                 if (!clip->isGroup()) ids.append(clip->clipId());
228                 else {
229                     const int children = item->childCount();
230                     for (int i = 0; i < children; i++) {
231                         ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
232                     }
233                 }
234             }
235             if (ids.isEmpty()) return;
236             QByteArray data;
237             data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
238             mimeData->setData("kdenlive/producerslist", data);
239             //mimeData->setText(ids.join(";")); //doc.toString());
240             //mimeData->setImageData(image);
241             drag->setMimeData(mimeData);
242             drag->setPixmap(clickItem->icon(0).pixmap((int)(50 *16 / 9.0), 50));
243             drag->setHotSpot(QPoint(0, 50));
244             drag->start(Qt::MoveAction);
245
246             //Qt::DropAction dropAction;
247             //dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
248
249             //Qt::DropAction dropAction = drag->exec();
250
251         }
252         //event->accept();
253     }
254 }
255
256 // virtual
257 void ProjectListView::dragMoveEvent(QDragMoveEvent * event) {
258     event->setDropAction(Qt::IgnoreAction);
259     event->setDropAction(Qt::MoveAction);
260     if (event->mimeData()->hasText()) {
261         event->acceptProposedAction();
262     }
263     // stop playing because we get a crash otherwise when fetching the thumbnails
264     emit pauseMonitor();
265 }
266
267 QStringList ProjectListView::mimeTypes() const {
268     QStringList qstrList;
269     qstrList << QTreeWidget::mimeTypes();
270     // list of accepted mime types for drop
271     qstrList.append("text/uri-list");
272     qstrList.append("text/plain");
273     qstrList.append("kdenlive/producerslist");
274     return qstrList;
275 }
276
277
278 Qt::DropActions ProjectListView::supportedDropActions() const {
279     // returns what actions are supported when dropping
280     return Qt::MoveAction;
281 }
282
283 #include "projectlistview.moc"