]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
Disable comments on project folders since it is currently not supported
[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 emit showProperties(item->referencedClip());
118 }
119
120 // virtual
121 void ProjectListView::dragEnterEvent(QDragEnterEvent *event) {
122     if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
123         kDebug() << "////////////////  DRAG ENTR OK";
124     }
125     event->acceptProposedAction();
126 }
127
128 // virtual
129 void ProjectListView::dropEvent(QDropEvent *event) {
130     kDebug() << "////////////////  DROPPED EVENT";
131     if (event->mimeData()->hasUrls()) {
132         QTreeWidgetItem *item = itemAt(event->pos());
133         QString groupName;
134         if (item) {
135             if (((ProjectItem *) item)->isGroup()) groupName = item->text(1);
136             else if (item->parent() && ((ProjectItem *) item->parent())->isGroup())
137                 groupName = item->parent()->text(1);
138         }
139         const QList <QUrl> list = event->mimeData()->urls();
140         foreach(const QUrl &url, list) {
141             emit addClip(url, groupName);
142         }
143
144     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
145         ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
146         if (item) {
147             if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
148             if (item->isGroup()) {
149                 //emit addClip(event->mimeData->text());
150                 kDebug() << "////////////////  DROPPED RIGHT 1 ";
151                 const QList <QTreeWidgetItem *> list = selectedItems();
152                 ProjectItem *clone;
153                 QString parentId = item->clipId();
154                 foreach(QTreeWidgetItem *it, list) {
155                     // TODO allow dragging of folders ?
156                     if (!((ProjectItem *) it)->isGroup()/* && ((ProjectItem *) it)->clipId() < 10000*/) {
157                         if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
158                         else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
159                         if (clone) {
160                             item->addChild(clone);
161                             QMap <QString, QString> props;
162                             props.insert("groupname", item->groupName());
163                             props.insert("groupid", parentId);
164                             clone->setProperties(props);
165                         }
166                     }
167                 }
168             } else item = NULL;
169         }
170         if (!item) {
171             kDebug() << "////////////////  DROPPED ON EMPTY ZONE";
172             // item dropped in empty zone, move it to top level
173             const QList <QTreeWidgetItem *> list = selectedItems();
174             ProjectItem *clone;
175             foreach(QTreeWidgetItem *it, list) {
176                 QTreeWidgetItem *parent = it->parent();
177                 if (parent/* && ((ProjectItem *) it)->clipId() < 10000*/)  {
178                     kDebug() << "++ item parent: " << parent->text(1);
179                     clone = (ProjectItem*) parent->takeChild(parent->indexOfChild(it));
180                     if (clone) addTopLevelItem(clone);
181                 }
182             }
183         }
184     }
185     event->acceptProposedAction();
186 }
187
188 // virtual
189 void ProjectListView::mousePressEvent(QMouseEvent *event) {
190     if (event->button() == Qt::LeftButton) {
191         this->m_DragStartPosition = event->pos();
192         m_dragStarted = true;
193         QTreeWidgetItem *underMouse = itemAt(event->pos());
194         if (underMouse && underMouse->isSelected()) emit focusMonitor();
195     }
196     QTreeWidget::mousePressEvent(event);
197 }
198
199
200 // virtual
201 void ProjectListView::mouseMoveEvent(QMouseEvent *event) {
202     kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
203     if (!m_dragStarted) return;
204
205     if ((event->pos() - m_DragStartPosition).manhattanLength()
206             < QApplication::startDragDistance())
207         return;
208
209     {
210         ProjectItem *clickItem = (ProjectItem *) itemAt(m_DragStartPosition); //event->pos());
211         if (clickItem) {
212             QDrag *drag = new QDrag(this);
213             QMimeData *mimeData = new QMimeData;
214             QDomDocument doc;
215             const QList <QTreeWidgetItem *> list = selectedItems();
216             QStringList ids;
217             foreach(const QTreeWidgetItem *item, list) {
218                 // TODO allow dragging of folders
219                 ids.append(((ProjectItem *) item)->clipId());
220             }
221             QByteArray data;
222             data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
223             mimeData->setData("kdenlive/producerslist", data);
224             //mimeData->setText(ids.join(";")); //doc.toString());
225             //mimeData->setImageData(image);
226             drag->setMimeData(mimeData);
227             drag->setPixmap(clickItem->icon(0).pixmap((int)(50 *16 / 9.0), 50));
228             drag->setHotSpot(QPoint(0, 50));
229             drag->start(Qt::MoveAction);
230
231             //Qt::DropAction dropAction;
232             //dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
233
234             //Qt::DropAction dropAction = drag->exec();
235
236         }
237         //event->accept();
238     }
239 }
240
241 // virtual
242 void ProjectListView::dragMoveEvent(QDragMoveEvent * event) {
243     QTreeWidgetItem * item = itemAt(event->pos());
244     event->setDropAction(Qt::IgnoreAction);
245     //if (item) {
246     event->setDropAction(Qt::MoveAction);
247     if (event->mimeData()->hasText()) {
248         event->acceptProposedAction();
249     }
250     //}
251 }
252
253 QStringList ProjectListView::mimeTypes() const {
254     QStringList qstrList;
255     qstrList << QTreeWidget::mimeTypes();
256     // list of accepted mime types for drop
257     qstrList.append("text/uri-list");
258     qstrList.append("text/plain");
259     qstrList.append("kdenlive/producerslist");
260     return qstrList;
261 }
262
263
264 Qt::DropActions ProjectListView::supportedDropActions() const {
265     // returns what actions are supported when dropping
266     return Qt::MoveAction;
267 }
268
269 #include "projectlistview.moc"