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