dndfilesystemlistview.cpp
1 //------------------------------------------------------------------------------
2 // dndfilesystemlistview.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2011 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "dndfilesystemlistview.h"
24 
25 #include <QDebug>
26 #include <QDragEnterEvent>
27 #include <QFileInfo>
28 #include <QList>
29 #include <QUrl>
30 
31 DndFileSystemListView::DndFileSystemListView(QWidget* pParent)
32 : QListView(pParent)
33 {
34 }
35 
36 void DndFileSystemListView::dragEnterEvent(QDragEnterEvent* pEvent)
37 {
38  if (pEvent->mimeData()->hasUrls())
39  {
40  pEvent->acceptProposedAction();
41  }
42  else
43  {
44  QListView::dragEnterEvent(pEvent);
45  }
46 }
47 
48 void DndFileSystemListView::dropEvent(QDropEvent* pEvent)
49 {
50  if (pEvent->mimeData()->hasUrls())
51  {
52  QList<QUrl> droppedUrls = pEvent->mimeData()->urls();
53  for(int i = 0; i < droppedUrls.size(); ++i)
54  {
55  QString localPath = droppedUrls[i].toLocalFile();
56  QFileInfo fileInfo(localPath);
57  if(!localPath.isEmpty() && fileInfo.exists())
58  {
59  emit fileSystemPathDropped(fileInfo.absoluteFilePath());
60  }
61  }
62 
63  pEvent->acceptProposedAction();
64  }
65  else
66  {
67  QListView::dropEvent(pEvent);
68  }
69 }
70