]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
Reindent the codebase using 'linux' bracket placement.
[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
21 #include "projectlistview.h"
22 #include "projectitem.h"
23 #include "kdenlivesettings.h"
24
25 #include <KDebug>
26 #include <KMenu>
27 #include <KLocale>
28
29 #include <QApplication>
30 #include <QHeaderView>
31 #include <QAction>
32
33 ProjectListView::ProjectListView(QWidget *parent)
34         : QTreeWidget(parent), m_dragStarted(false)
35 {
36     setSelectionMode(QAbstractItemView::ExtendedSelection);
37     setDragDropMode(QAbstractItemView::DragDrop);
38     setDropIndicatorShown(true);
39     setAlternatingRowColors(true);
40     setDragEnabled(true);
41     setAcceptDrops(true);
42
43     setColumnCount(4);
44     QStringList headers;
45     headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description") << i18n("Rating");
46     setHeaderLabels(headers);
47     sortByColumn(1, Qt::AscendingOrder);
48
49     QHeaderView* headerView = header();
50     headerView->setContextMenuPolicy(Qt::CustomContextMenu);
51     connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
52             this, SLOT(configureColumns(const QPoint&)));
53
54     //connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), this, SLOT(slotFocusOut(QTreeWidgetItem *, QTreeWidgetItem *)));
55
56     if (!KdenliveSettings::showdescriptioncolumn()) hideColumn(2);
57     if (!KdenliveSettings::showratingcolumn()) hideColumn(3);
58
59     setSortingEnabled(true);
60 }
61
62 ProjectListView::~ProjectListView()
63 {
64 }
65
66
67 void ProjectListView::configureColumns(const QPoint& pos)
68 {
69     KMenu popup(this);
70     popup.addTitle(i18nc("@title:menu", "Columns"));
71
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));
78         action->setData(i);
79     }
80
81     QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
82     if (activatedAction != 0) {
83         const bool show = activatedAction->isChecked();
84
85         // remember the changed column visibility in the settings
86         const int columnIndex = activatedAction->data().toInt();
87         switch (columnIndex) {
88         case 2:
89             KdenliveSettings::setShowdescriptioncolumn(show);
90             break;
91         case 3:
92             KdenliveSettings::setShowratingcolumn(show);
93             break;
94         default:
95             break;
96         }
97
98         // apply the changed column visibility
99         if (show) {
100             showColumn(columnIndex);
101         } else {
102             hideColumn(columnIndex);
103         }
104     }
105 }
106
107 // virtual
108 void ProjectListView::contextMenuEvent(QContextMenuEvent * event)
109 {
110     emit requestMenu(event->globalPos(), itemAt(event->pos()));
111 }
112
113 // virtual
114 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event)
115 {
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);
120     } else {
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());
124     }
125 }
126
127 // virtual
128 void ProjectListView::dragEnterEvent(QDragEnterEvent *event)
129 {
130     if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
131         kDebug() << "////////////////  DRAG ENTR OK";
132     }
133     event->acceptProposedAction();
134 }
135
136 // virtual
137 void ProjectListView::dropEvent(QDropEvent *event)
138 {
139     kDebug() << "////////////////  DROPPED EVENT";
140     if (event->mimeData()->hasUrls()) {
141         QTreeWidgetItem *item = itemAt(event->pos());
142         QString groupName;
143         if (item) {
144             if (((ProjectItem *) item)->isGroup()) groupName = item->text(1);
145             else if (item->parent() && ((ProjectItem *) item->parent())->isGroup())
146                 groupName = item->parent()->text(1);
147         }
148         emit addClip(event->mimeData()->urls(), groupName);
149
150     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
151         ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
152         if (item) {
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();
158                 ProjectItem *clone;
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));
165                         if (clone) {
166                             item->addChild(clone);
167                             QMap <QString, QString> props;
168                             props.insert("groupname", item->groupName());
169                             props.insert("groupid", parentId);
170                             clone->setProperties(props);
171                         }
172                     }
173                 }
174             } else item = NULL;
175         }
176         if (!item) {
177             kDebug() << "////////////////  DROPPED ON EMPTY ZONE";
178             // item dropped in empty zone, move it to top level
179             const QList <QTreeWidgetItem *> list = selectedItems();
180             ProjectItem *clone;
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)));
186                     if (clone) {
187                         addTopLevelItem(clone);
188                         clone->clearProperty("groupname");
189                         clone->clearProperty("groupid");
190                     }
191                 }
192             }
193         }
194     }
195     event->acceptProposedAction();
196 }
197
198 // virtual
199 void ProjectListView::mousePressEvent(QMouseEvent *event)
200 {
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();
206     }
207     QTreeWidget::mousePressEvent(event);
208 }
209
210
211 // virtual
212 void ProjectListView::mouseMoveEvent(QMouseEvent *event)
213 {
214     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
215     if (!m_dragStarted) return;
216
217     if ((event->pos() - m_DragStartPosition).manhattanLength()
218             < QApplication::startDragDistance())
219         return;
220
221     {
222         ProjectItem *clickItem = (ProjectItem *) itemAt(m_DragStartPosition); //event->pos());
223         if (clickItem) {
224             QDrag *drag = new QDrag(this);
225             QMimeData *mimeData = new QMimeData;
226             const QList <QTreeWidgetItem *> list = selectedItems();
227             QStringList ids;
228             foreach(const QTreeWidgetItem *item, list) {
229                 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
230                 if (!clip->isGroup()) ids.append(clip->clipId());
231                 else {
232                     const int children = item->childCount();
233                     for (int i = 0; i < children; i++) {
234                         ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
235                     }
236                 }
237             }
238             if (ids.isEmpty()) return;
239             QByteArray data;
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);
248         }
249         //event->accept();
250     }
251 }
252
253 // virtual
254 void ProjectListView::dragMoveEvent(QDragMoveEvent * event)
255 {
256     event->setDropAction(Qt::IgnoreAction);
257     event->setDropAction(Qt::MoveAction);
258     if (event->mimeData()->hasText()) {
259         event->acceptProposedAction();
260     }
261     // stop playing because we get a crash otherwise when fetching the thumbnails
262     emit pauseMonitor();
263 }
264
265 QStringList ProjectListView::mimeTypes() const
266 {
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");
273     return qstrList;
274 }
275
276
277 Qt::DropActions ProjectListView::supportedDropActions() const
278 {
279     // returns what actions are supported when dropping
280     return Qt::MoveAction;
281 }
282
283 #include "projectlistview.moc"