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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
//===========================================
// 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>
#include <QMouseEvent>
#include <QUrl>
#include <QDir>
#include <LuminaUtils.h>
//==============
// 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; }
QList<QUrl> urilist;
for(int i=0; i<items.length(); i++){
urilist << QUrl::fromLocalFile(items[i]->whatsThis().section("::::",1,100));
}
//Create the mime data
QMimeData *mime = new QMimeData;
mime->setUrls(urilist);
//Create the drag structure
QDrag *drag = new QDrag(this);
drag->setMimeData(mime);
/*if(info.first().section("::::",0,0)=="cut"){
drag->exec(act | Qt::MoveAction);
}else{*/
drag->exec(act | Qt::CopyAction);
//}
}
void dragEnterEvent(QDragEnterEvent *ev){
//qDebug() << "Drag Enter Event:" << ev->mimeData()->hasFormat(MIME);
if(ev->mimeData()->hasUrls() && !this->whatsThis().isEmpty() ){
ev->acceptProposedAction(); //allow this to be dropped here
}else{
ev->ignore();
}
}
void dragMoveEvent(QDragMoveEvent *ev){
if(ev->mimeData()->hasUrls() && !this->whatsThis().isEmpty() ){
//Change the drop type depending on the data/dir
QString home = QDir::homePath();
if( this->whatsThis().startsWith(home) ){ ev->setDropAction(Qt::MoveAction); }
else{ ev->setDropAction(Qt::CopyAction); }
ev->acceptProposedAction(); //allow this to be dropped here
}else{
ev->ignore();
}
}
void dropEvent(QDropEvent *ev){
if(this->whatsThis().isEmpty() || !ev->mimeData()->hasUrls() ){ ev->ignore(); return; } //not supported
//qDebug() << "Drop Event:";
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();
}
}
//Now turn the input urls into local file paths
QStringList files;
QString home = QDir::homePath();
foreach(const QUrl &url, ev->mimeData()->urls()){
const QString filepath = url.toLocalFile();
//If the target file is modifiable, assume a move - otherwise copy
if(QFileInfo(filepath).isWritable() && (filepath.startsWith(home) && dirpath.startsWith(home))){ files << "cut::::"+filepath; }
else{ files << "copy::::"+filepath; }
}
//qDebug() << "Drop Event:" << dirpath;
emit DataDropped( dirpath, files );
}
void mouseReleaseEvent(QMouseEvent *ev){
if(ev->button() != Qt::RightButton && ev->button() != Qt::LeftButton){ ev->ignore(); }
else{ QListWidget::mouseReleaseEvent(ev); } //pass it along to the widget
}
void mousePressEvent(QMouseEvent *ev){
if(ev->button() != Qt::RightButton && ev->button() != Qt::LeftButton){ ev->ignore(); }
else{ QListWidget::mousePressEvent(ev); } //pass it along to the widget
}
/*void mouseMoveEvent(QMouseEvent *ev){
if(ev->button() != Qt::RightButton && ev->button() != Qt::LeftButton){ ev->ignore(); }
else{ QListWidget::mouseMoveEvent(ev); } //pass it along to the widget
}*/
};
//================
// 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; }
QList<QUrl> urilist;
for(int i=0; i<items.length(); i++){
urilist << QUrl::fromLocalFile(items[i]->whatsThis(0).section("::::",1,100));
}
//Create the mime data
QMimeData *mime = new QMimeData;
mime->setUrls(urilist);
//Create the drag structure
QDrag *drag = new QDrag(this);
drag->setMimeData(mime);
/*if(info.first().section("::::",0,0)=="cut"){
drag->exec(act | Qt::MoveAction);
}else{*/
drag->exec(act | Qt::CopyAction| Qt::MoveAction);
//}
}
void dragEnterEvent(QDragEnterEvent *ev){
//qDebug() << "Drag Enter Event:" << ev->mimeData()->hasFormat(MIME);
if(ev->mimeData()->hasUrls() && !this->whatsThis().isEmpty() ){
ev->acceptProposedAction(); //allow this to be dropped here
}else{
ev->ignore();
}
}
void dragMoveEvent(QDragMoveEvent *ev){
if(ev->mimeData()->hasUrls() && !this->whatsThis().isEmpty() ){
//Change the drop type depending on the data/dir
QString home = QDir::homePath();
if( this->whatsThis().startsWith(home) ){ ev->setDropAction(Qt::MoveAction); }
else{ ev->setDropAction(Qt::CopyAction); }
ev->accept(); //allow this to be dropped here
}else{
ev->ignore();
}
}
void dropEvent(QDropEvent *ev){
if(this->whatsThis().isEmpty() || !ev->mimeData()->hasUrls() ){ ev->ignore(); 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;
//Now turn the input urls into local file paths
QStringList files;
QString home = QDir::homePath();
foreach(const QUrl &url, ev->mimeData()->urls()){
const QString filepath = url.toLocalFile();
//If the target file is modifiable, assume a move - otherwise copy
if(QFileInfo(filepath).isWritable() && (filepath.startsWith(home) && dirpath.startsWith(home)) ){ files << "cut::::"+filepath; }
else{ files << "copy::::"+filepath; }
}
//qDebug() << "Drop Event:" << dirpath;
emit DataDropped( dirpath, files );
}
void mouseReleaseEvent(QMouseEvent *ev){
if(ev->button() != Qt::RightButton && ev->button() != Qt::LeftButton){ ev->ignore(); }
else{ QTreeWidget::mouseReleaseEvent(ev); } //pass it along to the widget
}
void mousePressEvent(QMouseEvent *ev){
if(ev->button() != Qt::RightButton && ev->button() != Qt::LeftButton){ ev->ignore(); }
else{ QTreeWidget::mousePressEvent(ev); } //pass it along to the widget
}
/*void mouseMoveEvent(QMouseEvent *ev){
if(ev->button() != Qt::RightButton && ev->button() != Qt::LeftButton){ ev->ignore(); }
else{ QTreeWidget::mouseMoveEvent(ev); } //pass it along to the widget
}*/
};
/*
* Virtual class for managing the sort of folders/files items. The problem with base class is that it only manages texts fields and
* we have dates and sizes.
*
* On this class, we overwrite the function operator<.
*/
class CQTreeWidgetItem : public QTreeWidgetItem {
public:
CQTreeWidgetItem(int type = Type) : QTreeWidgetItem(type) {}
CQTreeWidgetItem(const QStringList & strings, int type = Type) : QTreeWidgetItem(strings, type) {}
CQTreeWidgetItem(QTreeWidget * parent, int type = Type) : QTreeWidgetItem(parent, type) {}
CQTreeWidgetItem(QTreeWidget * parent, const QStringList & strings, int type = Type) : QTreeWidgetItem(parent, strings, type) {}
CQTreeWidgetItem(QTreeWidget * parent, QTreeWidgetItem * preceding, int type = Type) : QTreeWidgetItem(parent, preceding, type) {}
CQTreeWidgetItem(QTreeWidgetItem * parent, int type = Type) : QTreeWidgetItem(parent, type) {}
CQTreeWidgetItem(QTreeWidgetItem * parent, const QStringList & strings, int type = Type) : QTreeWidgetItem(parent, strings, type) {}
CQTreeWidgetItem(QTreeWidgetItem * parent, QTreeWidgetItem * preceding, int type = Type) : QTreeWidgetItem(parent, preceding, type) {}
virtual ~CQTreeWidgetItem() {}
inline virtual bool operator<(const QTreeWidgetItem &tmp) const {
int column = this->treeWidget()->sortColumn();
// We are in date text
if(column == 3 || column == 4)
return this->whatsThis(column) < tmp.whatsThis(column);
// We are in size text
else if(column == 1) {
QString text = this->text(column);
QString text_tmp = tmp.text(column);
double filesize, filesize_tmp;
// On folders, text is empty so we check for that
// In case we are in folders, we put -1 for differentiate of regular files with 0 bytes.
// Doing so, all folders we'll be together instead of mixing with files with 0 bytes.
if(text.isEmpty())
filesize = -1;
else
filesize = LUtils::DisplaySizeToBytes(text);
if(text_tmp.isEmpty())
filesize_tmp = -1;
else
filesize_tmp = LUtils::DisplaySizeToBytes(text_tmp);
return filesize < filesize_tmp;
}
// In other cases, we trust base class implementation
return QTreeWidgetItem::operator<(tmp);
}
};
#endif
|