aboutsummaryrefslogtreecommitdiff
path: root/lumina-fm/widgets/DDListWidgets.h
blob: c211206b5f31cdac3c0ffb8f356e724c05c0589d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2015, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
// This is a couple simple widget subclasses to enable drag and drop functionality
// NOTE: The "whatsThis()" item information needs to correspond to the "[cut/copy]::::<file path>" syntax
//NOTE2: The "whatsThis()" information on the widget itself should be the current dir path *if* it can accept drops
//===========================================
#ifndef _LUMINA_FM_DRAG_DROP_WIDGETS_H
#define _LUMINA_FM_DRAG_DROP_WIDGETS_H

#define MIME QString("x-special/lumina-copied-files")

#include <QListWidget>
#include <QTreeWidget>
#include <QDropEvent>
#include <QMimeData>
#include <QDrag>
#include <QFileInfo>
#include <QDebug>

//==============
//  LIST WIDGET
//==============
class DDListWidget : public QListWidget{
	Q_OBJECT
public:
	DDListWidget(QWidget *parent=0) : QListWidget(parent){
	  //Drag and Drop Properties
	  this->setDragDropMode(QAbstractItemView::DragDrop);
	  this->setDefaultDropAction(Qt::MoveAction); //prevent any built-in Qt actions - the class handles it
	  //Other custom properties necessary for the FM
	  this->setFocusPolicy(Qt::StrongFocus);
	  this->setContextMenuPolicy(Qt::CustomContextMenu);
	  this->setSelectionMode(QAbstractItemView::ExtendedSelection);
	  this->setSelectionBehavior(QAbstractItemView::SelectRows);
	  this->setFlow(QListView::TopToBottom);
	  this->setWrapping(true);
	  this->setMouseTracking(true);
	  //this->setSortingEnabled(true); //This sorts *only* by name - type is not preserved
	}
	~DDListWidget(){}

signals:
	void DataDropped(QString, QStringList); //Dir path, List of commands
		
protected:
	void startDrag(Qt::DropActions act){
	  QList<QListWidgetItem*> items = this->selectedItems();
	  if(items.length()<1){ return; }
	  QStringList info;
	  for(int i=0; i<items.length(); i++){ info << items[i]->whatsThis(); }
	  //Create the mime data
	  QMimeData *mime = new QMimeData;
	  mime->setData(MIME,info.join("\n").toLocal8Bit());
	  //Create the drag structure
	  QDrag *drag = new QDrag(this);
	  drag->setMimeData(mime);
	  drag->exec(act | Qt::MoveAction | Qt::CopyAction);
	}

	void dragEnterEvent(QDragEnterEvent *ev){
	  //qDebug() << "Drag Enter Event:" << ev->mimeData()->hasFormat(MIME);
	  if(ev->mimeData()->hasFormat(MIME) && !this->whatsThis().isEmpty() ){
	    //qDebug() << "Accepted:" << ev->mimeData()->data(MIME);
	    if(QString(ev->mimeData()->data(MIME)).section("::::",0,0)=="cut"){
		ev->setDropAction(Qt::MoveAction);
	    }else{
		ev->setDropAction(Qt::CopyAction);
	    }
	    ev->accept(); //allow this to be dropped here
	  }		  
	}
	
	void dragMoveEvent(QDragMoveEvent *ev){
	  //qDebug() << "Drag Move Event:" << ev->mimeData()->hasFormat(MIME);
	  if(ev->mimeData()->hasFormat(MIME) && !this->whatsThis().isEmpty()){
	    //qDebug() << "Accepted:" << ev->mimeData()->data(MIME);
	    if(QString(ev->mimeData()->data(MIME)).section("::::",0,0)=="cut"){
		ev->setDropAction(Qt::MoveAction);
	    }else{
		ev->setDropAction(Qt::CopyAction);
	    }
	    ev->accept(); //allow this to be dropped here
	  }				
	}
	
	void dropEvent(QDropEvent *ev){
	  if(this->whatsThis().isEmpty()){ return; } //not supported
	  ev->accept(); //handled here
	  QString dirpath = this->whatsThis();
	  //See if the item under the drop point is a directory or not
	  QListWidgetItem *it = this->itemAt( ev->pos());
	  if(it!=0){
	    QFileInfo info(it->whatsThis().section("::::",1,100));
	    if(info.isDir() && info.isWritable()){
	      dirpath = info.absoluteFilePath();
	    }
	  }
	  //qDebug() << "Drop Event:" << dirpath;
	  emit DataDropped( dirpath, QString(ev->mimeData()->data(MIME)).split("\n") );
	}
};

//================
//     TreeWidget
//================
class DDTreeWidget : public QTreeWidget{
	Q_OBJECT
public:
	DDTreeWidget(QWidget *parent=0) : QTreeWidget(parent){
	  //Drag and Drop Properties
	  this->setDragDropMode(QAbstractItemView::DragDrop);
	  this->setDefaultDropAction(Qt::MoveAction); //prevent any built-in Qt actions - the class handles it
	  //Other custom properties necessary for the FM
	  this->setFocusPolicy(Qt::StrongFocus);
	  this->setContextMenuPolicy(Qt::CustomContextMenu);
	  this->setSelectionMode(QAbstractItemView::ExtendedSelection);
	  this->setSelectionBehavior(QAbstractItemView::SelectRows);
	  this->setMouseTracking(true);
	  this->setSortingEnabled(true);
	  this->setIndentation(0);
	  this->setItemsExpandable(false);
	}
	~DDTreeWidget(){}

signals:
	void DataDropped(QString, QStringList); //Dir path, List of commands
		
protected:
	void startDrag(Qt::DropActions act){
	  QList<QTreeWidgetItem*> items = this->selectedItems();
	  if(items.length()<1){ return; }
	  QStringList info;
	  for(int i=0; i<items.length(); i++){ info << items[i]->whatsThis(0); }
	  //Create the mime data
	  QMimeData *mime = new QMimeData;
	  mime->setData(MIME,info.join("\n").toLocal8Bit());
	  //Create the drag structure
	  QDrag *drag = new QDrag(this);
	  drag->setMimeData(mime);
	  drag->exec(act | Qt::MoveAction | Qt::CopyAction);
	}

	void dragEnterEvent(QDragEnterEvent *ev){
	  //qDebug() << "Drag Enter Event:" << ev->mimeData()->hasFormat(MIME);
	  if(ev->mimeData()->hasFormat(MIME) && !this->whatsThis().isEmpty() ){
	    //qDebug() << "Accepted:" << ev->mimeData()->data(MIME);
	    if(QString(ev->mimeData()->data(MIME)).section("::::",0,0)=="cut"){
		ev->setDropAction(Qt::MoveAction);
	    }else{
		ev->setDropAction(Qt::CopyAction);
	    }
	    ev->accept(); //allow this to be dropped here
	  }		  
	}
	
	void dragMoveEvent(QDragMoveEvent *ev){
	  //qDebug() << "Drag Move Event:" << ev->mimeData()->hasFormat(MIME);
	  if(ev->mimeData()->hasFormat(MIME) && !this->whatsThis().isEmpty()){
	    //qDebug() << "Accepted:" << ev->mimeData()->data(MIME);
	    if(QString(ev->mimeData()->data(MIME)).section("::::",0,0)=="cut"){
		ev->setDropAction(Qt::MoveAction);
	    }else{
		ev->setDropAction(Qt::CopyAction);
	    }
	    ev->accept(); //allow this to be dropped here
	  }				
	}
	
	void dropEvent(QDropEvent *ev){
	  if(this->whatsThis().isEmpty()){ return; } //not supported
	  ev->accept(); //handled here
	  QString dirpath = this->whatsThis();
	  //See if the item under the drop point is a directory or not
	  QTreeWidgetItem *it = this->itemAt( ev->pos());
	  if(it!=0){
	    QFileInfo info(it->whatsThis(0).section("::::",1,100));
	    if(info.isDir() && info.isWritable()){
	      dirpath = info.absoluteFilePath();
	    }
	  }
	  //qDebug() << "Drop Event:" << dirpath;
	  emit DataDropped( dirpath, QString(ev->mimeData()->data(MIME)).split("\n") );
	}
};
#endif
bgstack15