]> git.sesse.net Git - kdenlive/blob - src/projectlistview.cpp
[PATCH by Ray Lehtiniem] Delete some unused variables
[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     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     setSortingEnabled(true);
59 }
60
61 ProjectListView::~ProjectListView() {
62 }
63
64
65 void ProjectListView::configureColumns(const QPoint& pos) {
66     KMenu popup(this);
67     popup.addTitle(i18nc("@title:menu", "Columns"));
68
69     QHeaderView* headerView = header();
70     for (int i = 2; i < headerView->count(); ++i) {
71         const QString text = model()->headerData(i, Qt::Horizontal).toString();
72         QAction* action = popup.addAction(text);
73         action->setCheckable(true);
74         action->setChecked(!headerView->isSectionHidden(i));
75         action->setData(i);
76     }
77
78     QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
79     if (activatedAction != 0) {
80         const bool show = activatedAction->isChecked();
81
82         // remember the changed column visibility in the settings
83         const int columnIndex = activatedAction->data().toInt();
84         switch (columnIndex) {
85         case 2:
86             KdenliveSettings::setShowdescriptioncolumn(show);
87             break;
88         case 3:
89             KdenliveSettings::setShowratingcolumn(show);
90             break;
91         default:
92             break;
93         }
94
95         // apply the changed column visibility
96         if (show) {
97             showColumn(columnIndex);
98         } else {
99             hideColumn(columnIndex);
100         }
101     }
102 }
103
104 // virtual
105 void ProjectListView::contextMenuEvent(QContextMenuEvent * event) {
106     emit requestMenu(event->globalPos(), itemAt(event->pos()));
107 }
108
109 // virtual
110 void ProjectListView::mouseDoubleClickEvent(QMouseEvent * event) {
111     ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
112     if (!item) emit addClip();
113     else if (item->isGroup()) {
114         if ((columnAt(event->pos().x()) == 1)) QTreeWidget::mouseDoubleClickEvent(event);
115     } else {
116         if ((columnAt(event->pos().x()) == 1) && (item->clipType() == SLIDESHOW || item->clipType() == TEXT || item->clipType() == COLOR)) QTreeWidget::mouseDoubleClickEvent(event);
117         else if ((columnAt(event->pos().x()) == 2)) QTreeWidget::mouseDoubleClickEvent(event);
118         else emit showProperties(item->referencedClip());
119     }
120 }
121
122 // virtual
123 void ProjectListView::dragEnterEvent(QDragEnterEvent *event) {
124     if (event->mimeData()->hasUrls() || event->mimeData()->hasText()) {
125         kDebug() << "////////////////  DRAG ENTR OK";
126     }
127     event->acceptProposedAction();
128 }
129
130 // virtual
131 void ProjectListView::dropEvent(QDropEvent *event) {
132     kDebug() << "////////////////  DROPPED EVENT";
133     if (event->mimeData()->hasUrls()) {
134         QTreeWidgetItem *item = itemAt(event->pos());
135         QString groupName;
136         if (item) {
137             if (((ProjectItem *) item)->isGroup()) groupName = item->text(1);
138             else if (item->parent() && ((ProjectItem *) item->parent())->isGroup())
139                 groupName = item->parent()->text(1);
140         }
141         const QList <QUrl> list = event->mimeData()->urls();
142         foreach(const QUrl &url, list) {
143             emit addClip(KUrl(url), groupName);
144         }
145
146     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
147         ProjectItem *item = static_cast <ProjectItem *>(itemAt(event->pos()));
148         if (item) {
149             if (item->parent()) item = static_cast <ProjectItem *>(item->parent());
150             if (item->isGroup()) {
151                 //emit addClip(event->mimeData->text());
152                 kDebug() << "////////////////  DROPPED RIGHT 1 ";
153                 const QList <QTreeWidgetItem *> list = selectedItems();
154                 ProjectItem *clone;
155                 QString parentId = item->clipId();
156                 foreach(QTreeWidgetItem *it, list) {
157                     // TODO allow dragging of folders ?
158                     if (!((ProjectItem *) it)->isGroup()/* && ((ProjectItem *) it)->clipId() < 10000*/) {
159                         if (it->parent()) clone = (ProjectItem*) it->parent()->takeChild(it->parent()->indexOfChild(it));
160                         else clone = (ProjectItem*) takeTopLevelItem(indexOfTopLevelItem(it));
161                         if (clone) {
162                             item->addChild(clone);
163                             QMap <QString, QString> props;
164                             props.insert("groupname", item->groupName());
165                             props.insert("groupid", parentId);
166                             clone->setProperties(props);
167                         }
168                     }
169                 }
170             } else item = NULL;
171         }
172         if (!item) {
173             kDebug() << "////////////////  DROPPED ON EMPTY ZONE";
174             // item dropped in empty zone, move it to top level
175             const QList <QTreeWidgetItem *> 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 = static_cast <ProjectItem*>(parent->takeChild(parent->indexOfChild(it)));
182                     if (clone) {
183                         addTopLevelItem(clone);
184                         clone->clearProperty("groupname");
185                         clone->clearProperty("groupid");
186                     }
187                 }
188             }
189         }
190     }
191     event->acceptProposedAction();
192 }
193
194 // virtual
195 void ProjectListView::mousePressEvent(QMouseEvent *event) {
196     if (event->button() == Qt::LeftButton) {
197         this->m_DragStartPosition = event->pos();
198         m_dragStarted = true;
199         QTreeWidgetItem *underMouse = itemAt(event->pos());
200         if (underMouse && underMouse->isSelected()) emit focusMonitor();
201     }
202     QTreeWidget::mousePressEvent(event);
203 }
204
205
206 // virtual
207 void ProjectListView::mouseMoveEvent(QMouseEvent *event) {
208     //kDebug() << "// DRAG STARTED, MOUSE MOVED: ";
209     if (!m_dragStarted) return;
210
211     if ((event->pos() - m_DragStartPosition).manhattanLength()
212             < QApplication::startDragDistance())
213         return;
214
215     {
216         ProjectItem *clickItem = (ProjectItem *) itemAt(m_DragStartPosition); //event->pos());
217         if (clickItem) {
218             QDrag *drag = new QDrag(this);
219             QMimeData *mimeData = new QMimeData;
220             QDomDocument doc;
221             const QList <QTreeWidgetItem *> list = selectedItems();
222             QStringList ids;
223             foreach(const QTreeWidgetItem *item, list) {
224                 const ProjectItem *clip = static_cast <const ProjectItem *>(item);
225                 if (!clip->isGroup()) ids.append(clip->clipId());
226                 else {
227                     const int children = item->childCount();
228                     for (int i = 0; i < children; i++) {
229                         ids.append(static_cast <ProjectItem *>(item->child(i))->clipId());
230                     }
231                 }
232             }
233             if (ids.isEmpty()) return;
234             QByteArray data;
235             data.append(ids.join(";").toUtf8()); //doc.toString().toUtf8());
236             mimeData->setData("kdenlive/producerslist", data);
237             //mimeData->setText(ids.join(";")); //doc.toString());
238             //mimeData->setImageData(image);
239             drag->setMimeData(mimeData);
240             drag->setPixmap(clickItem->icon(0).pixmap(iconSize()));
241             drag->setHotSpot(QPoint(0, 50));
242             Qt::DropAction dropAction = drag->exec(Qt::MoveAction);
243         }
244         //event->accept();
245     }
246 }
247
248 // virtual
249 void ProjectListView::dragMoveEvent(QDragMoveEvent * event) {
250     event->setDropAction(Qt::IgnoreAction);
251     event->setDropAction(Qt::MoveAction);
252     if (event->mimeData()->hasText()) {
253         event->acceptProposedAction();
254     }
255     // stop playing because we get a crash otherwise when fetching the thumbnails
256     emit pauseMonitor();
257 }
258
259 QStringList ProjectListView::mimeTypes() const {
260     QStringList qstrList;
261     qstrList << QTreeWidget::mimeTypes();
262     // list of accepted mime types for drop
263     qstrList.append("text/uri-list");
264     qstrList.append("text/plain");
265     qstrList.append("kdenlive/producerslist");
266     return qstrList;
267 }
268
269
270 Qt::DropActions ProjectListView::supportedDropActions() const {
271     // returns what actions are supported when dropping
272     return Qt::MoveAction;
273 }
274
275 #include "projectlistview.moc"