-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfmiconview.cpp
487 lines (416 loc) · 16.6 KB
/
fmiconview.cpp
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include "fmiconview.h"
#include "fmmodel.h"
#include "fmfileiconprovider.h"
//#include "titlebar.h"
#include <QSettings>
#include <QDebug>
#include <QMimeData>
#include <QUrl>
#include <QList>
#include <QPalette>
#include <QtX11Extras/QX11Info>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <QDrag>
#include <QDropEvent>
#include <QDesktopServices>
#include <QApplication>
#include <QMenu>
#include <QAction>
#include <QListWidgetItem>
#include <QFileIconProvider>
#include <QIcon>
#include <QPalette>
#include <QKeyEvent>
#include <QPainter>
#include <QPaintEvent>
#include <QVBoxLayout>
#include <QApplication>
#include <QClipboard>
#include <QMimeData>
#include <QDir>
FMIconView::~FMIconView(){
delete fileSystemModel;
delete itemSelectionModel;
delete blankMenu;
delete oneSelectedMenu;
delete pluralitySelectedMenu;
}
void FMIconView::closeEvent(QCloseEvent *e) {
//writeSettings();
QListView::closeEvent(e);
}
FMIconView::FMIconView(int id)
{
mId = id;
setAutoFillBackground(true);
FMModel *model = new FMModel;
//model->myComputer();
//QFileSystemModel *model = new QFileSystemModel;
FMFileIconProvider *fileIconProvider = new FMFileIconProvider;
model->setIconProvider(fileIconProvider);
//setAttribute(Qt::WA_TranslucentBackground,true);
/*********************transparent test********************************/
//setAttribute(Qt::WA_TranslucentBackground, true);
//setWindowState (Qt::WindowMaximized);
//QPalette pal = palette();
//pal.setColor(QPalette::Foreground, QColor(0x00,0xff,0x00,0x00));
//setPalette(pal);
//setFixedSize(600,600);
//setWindowOpacity(0.5);
//move(1000,200);
//setStyleSheet("background-color:transparent,white");
//setStyleSheet("color:white;background-color:rgb(0,0,0)");
//setStyleSheet("background-color:rgba(200,200,200,0.6)");
//setStyleSheet("background-color:rgba(244,244,244,2)");
setStyleSheet("color: white;"
"gridline-color: black;"
//"{"
"background-color: rgba(100, 100, 100, 0.5);"
//"opacity:0.5;"
//"}"
//"background-color: transparent;"
"alternate-background-color: rgba(64, 64, 64, 0.5);"
"selection-color: white;"
//"selection-background-color: rgba(77, 77, 77, 0.5);"
"border: 3px groove gray;"
"border-radius: 0px;"
"font-size:16px;"
//"word-wrap: break-word;"
//"break-word:normal;"
"padding: 2px 4px;");
/*********************transparent test********************************/
//model->setRootPath("/");
model->setRootPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.iconviews_on_desktop_test/"+QString::number(id));
setModel(model);
QItemSelectionModel *selections = new QItemSelectionModel(model);
setViewMode(QListView::IconMode);
setRootIndex(model->index(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.iconviews_on_desktop_test/"+QString::number(id)));
//setRootIndex(model->index("/"));
setSelectionMode(QAbstractItemView::ExtendedSelection);
setGridSize(QSize(108,108));
//setIconSize(QSize(48,48));
setDragEnabled(true);
//setDropIndicatorShown(true);
setAcceptDrops(true);
//setDefaultDropAction(Qt::MoveAction);
setDragDropOverwriteMode(true);
setDragDropMode(QListView::DragDrop);
setResizeMode(QListView::Adjust);
connect(this, &FMIconView::doubleClicked, this, &FMIconView::openFileOrDirectory);
qDebug()<<QApplication::desktopFileName();
setContextMenuPolicy(Qt::CustomContextMenu);
blankMenu = new QMenu;
QAction *updir = new QAction(blankMenu);
updirAction = updir;
updir->setText("up");
connect(updir, &QAction::triggered, this, &FMIconView::backToUp);
QAction *paste = new QAction(blankMenu);
pasteAction = paste;
paste->setText("paste");
connect(paste, &QAction::triggered, this, &FMIconView::pasteFilesInClipboard);
blankMenu->addAction(updir);
blankMenu->addAction(paste);
oneSelectedMenu = new QMenu;
QAction *moveToDesktopAction = new QAction(oneSelectedMenu);
moveToDesktopAction->setText("move to desktop");
connect(moveToDesktopAction, &QAction::triggered, this, &FMIconView::moveSelectionToDesktop);
QAction *copyAction = new QAction(oneSelectedMenu);
copyAction->setText("copy to clipboard");
connect(copyAction, &QAction::triggered, this, &FMIconView::copySelectionToClipboard);
QAction *deleteAction = new QAction(oneSelectedMenu);
deleteAction->setText("delete, not to trash");
connect(deleteAction, &QAction::triggered, this, &FMIconView::deleteSelection);
oneSelectedMenu->addAction(moveToDesktopAction);
oneSelectedMenu->addAction(copyAction);
oneSelectedMenu->addAction(deleteAction);
//TODO: add pluralitySelectedMenu actions
pluralitySelectedMenu = new QMenu;
//connect(this, &FMIconView::customContextMenuRequested, this, &FMIconView::showMenu);
fileSystemModel = model;
itemSelectionModel = selections;
//setEditTriggers(QAbstractItemView::AnyKeyPressed);
//setTextElideMode();
//setFocusPolicy(Qt::StrongFocus);
//keyPressEvent();
//setFlow(QListView::TopToBottom);
//setSpacing(100);
//setTextElideMode(Qt::ElideMiddle);
setUniformItemSizes(true);
setMovement(QListView::Snap);
//model->setData(model->index("test"), model->myComputer());
}
void FMIconView::dragEnterEvent(QDragEnterEvent *e){
qDebug()<<"drag enter event";
if (e->mimeData()->hasUrls()){
QList<QUrl> urls = e->mimeData()->urls();
qDebug()<<urls<<e->mimeData()->formats();
if (e->source()==this){
e->setDropAction(Qt::MoveAction);
e->accept();
} else {
e->acceptProposedAction();
}
} else {
e->ignore();
}
//QListView::dragEnterEvent(e);
}
/* NOTE:
* we must override the dragMoveEvent(),
* because the default event would ignore the drag event
* if which is from other widgets,
* and drop event could not happend.
*/
void FMIconView::dragMoveEvent(QDragMoveEvent *e) {
e->accept();
QModelIndex index = indexAt(e->pos());
if (index.isValid()) {
//qDebug()<<e->pos()<<index.model()->itemData(index);
//qDebug()<<fileSystemModel->filePath(index);
//highlight the index when drag move at.
}
}
void FMIconView::dropEvent(QDropEvent *e){
qDebug()<<"drop event";
/* NOTE:
* the QModeIndex var index presents the index that current file mouse hanging on.
* we need to consider under what circumstances to perform the files move operation.
*/
QModelIndex index = indexAt(e->pos());
qDebug()<<fileSystemModel->filePath(index);
if (this==(e->source())){
//TODO: when drop uppon to a directory icon, move selection in to this.
if (index.isValid()) {
QUrl tmpUrl = QUrl("file://"+fileSystemModel->filePath(index));
if (!e->mimeData()->urls().contains(tmpUrl)&&fileSystemModel->fileInfo(index).isDir()){
e->setDropAction(Qt::MoveAction);
qDebug()<<"files move action";
//May be we need a thread job to handle lots of files move action?
}
e->accept();
} else {
e->ignore();
}
return;
} else {
//drag form other widget
QUrl tmpUrl = QUrl("file://"+fileSystemModel->filePath(index));
qDebug()<<tmpUrl;
if (e->mimeData()->hasUrls()){
QList<QUrl> urls = e->mimeData()->urls();
qDebug()<<urls;
/* NOTE:
* when accept drop from other widget, we aslo need consider how to deal with the files opreation.
* probably there is a better way to handle drop event.
*/
//QUrl tmpUrl = QUrl("file://"+fileSystemModel->fileName(index));
if (!index.isValid()) {
QUrl currentDirUrl = QUrl("file://"+fileSystemModel->filePath(this->rootIndex()));
qDebug()<<currentDirUrl<<endl<<"!valid";
if (!e->mimeData()->urls().contains(currentDirUrl)) {
e->setDropAction(Qt::MoveAction);
qDebug()<<"move files to current dir";
for(int i = 0; i<urls.count(); i++) {
QUrl tmpUrl = urls.at(i);
qDebug()<<"tmpUrl: "<<tmpUrl;
QFileInfo tmpInfo = QFileInfo(tmpUrl.path());
QString filename = tmpInfo.fileName();
qDebug()<<filename;
qDebug()<<QString(QString(".iconviews_on_desktop_test/")+QString::number(mId)+QString("/")+filename);
QString sourcePath = tmpInfo.filePath();
//this is same to fileSystemModel->filePath(this->rootIndex()) here, and more fast.
QString destPath = QString(QString(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.iconviews_on_desktop_test/")+QString::number(mId)+QString("/")+filename);
if (tmpInfo.isFile()) {
qDebug()<<"is file";
qDebug()<<QFile::rename(sourcePath, destPath);
} else if (tmpInfo.isDir()) {
qDebug()<<"is dir";
qDebug()<<QDir(tmpInfo.filePath()).rename(sourcePath, destPath);
} else {
qDebug()<<"tmpInfo has not type?";
}
}
//May be we need a thread job to handle lots of files move action?
} else {
qDebug()<<"warning dialog, can't copy/move dir into itself";
}
} else if (!e->mimeData()->urls().contains(tmpUrl)&&fileSystemModel->fileInfo(index).isDir()) {
e->setDropAction(Qt::MoveAction);
qDebug()<<"files move action";
//May be we need a thread job to handle lots of files move action?
} else {
e->setDropAction(Qt::CopyAction);
qDebug()<<"files copy action";
//May be we need a thread job to handle lots of files move action?
}
e->acceptProposedAction();
QListView::dropEvent(e); //this may be useless.
} else {
e->ignore();
}
}
}
void FMIconView::trayIconAction(QSystemTrayIcon::ActivationReason reason){
if (reason){
}
qDebug()<<"trayIconAction";
}
void FMIconView::openFileOrDirectory(QModelIndex m){
qDebug()<<m<<fileSystemModel->fileInfo(m);
if (fileSystemModel->isDir(m)){
//setRootIndex(m);
QDesktopServices::openUrl(QUrl("file://"+fileSystemModel->filePath(m)));
} else {
QDesktopServices::openUrl(QUrl("file://"+fileSystemModel->filePath(m)));
}
}
void FMIconView::showMenu(){
blankMenu->exec(QCursor::pos());
}
void FMIconView::backToUp(){
qDebug()<<"backToUp?";
if (rootIndex().parent().isValid()) {
qDebug()<<"backToUp";
setRootIndex(rootIndex().parent());
}
}
void FMIconView::mouseReleaseEvent(QMouseEvent *e){
qDebug()<<"mouseReleaseEvent";
if (fileSystemModel->filePath(rootIndex()) == fileSystemModel->rootPath()) {
updirAction->setEnabled(false);
} else {
updirAction->setEnabled(true);
}
if (e->button()==Qt::RightButton) {
if (!this->indexAt(e->pos()).isValid()) {
if (!QApplication::clipboard()->mimeData()->hasUrls()) {
pasteAction->setEnabled(false);
} else {
pasteAction->setEnabled(true);
}
blankMenu->exec(QCursor::pos());
} else {
if (this->selectedIndexes().count()==1) {
oneSelectedMenu->exec(QCursor::pos());
} else {
//TODO: use pluralitySelectedMenu here
oneSelectedMenu->exec(QCursor::pos());
}
}
}
QListView::mouseReleaseEvent(e);
}
void FMIconView::mouseMoveEvent(QMouseEvent *e) {
QListView::mouseMoveEvent(e);
}
void FMIconView::keyReleaseEvent(QKeyEvent *e){
qDebug()<<"keyReleaseEvent";
QListView::keyReleaseEvent(e);
}
void FMIconView::paintEvent(QPaintEvent *e){
/*
QPainter p(this);
p.setCompositionMode(QPainter::CompositionMode_Clear);
p.fillRect( 10, 10, 300, 300, Qt::SolidPattern );
*/
QListView::paintEvent(e);
}
void FMIconView::moveSelectionToDesktop() {
QModelIndexList selectionList = this->selectedIndexes();
for (int i = 0; i < selectionList.count(); i++) {
QModelIndex tmpIndex = selectionList.at(i);
QString sourcePath = fileSystemModel->filePath(tmpIndex);
QString destPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QString("/") + fileSystemModel->fileName(tmpIndex);
if (fileSystemModel->isDir(tmpIndex)) {
QDir dirHelper;
dirHelper.rename(sourcePath, destPath);
} else {
QFile fileHelper;
fileHelper.rename(sourcePath, destPath);
}
}
}
void FMIconView::copySelectionToClipboard() {
QModelIndexList selectionList = this->selectedIndexes();
QMimeData *selectionMimeData = fileSystemModel->mimeData(selectionList);
QApplication::clipboard()->setMimeData(selectionMimeData);
}
void FMIconView::deleteSelection() {
QModelIndexList selectionList = this->selectedIndexes();
for (int i = 0; i < selectionList.count(); i++) {
QModelIndex tmpIndex = selectionList.at(i);
QString sourcePath = fileSystemModel->filePath(tmpIndex);
if (fileSystemModel->isDir(tmpIndex)) {
QDir dirHelper;
dirHelper.setPath(sourcePath);
dirHelper.removeRecursively();
} else {
QFile fileHelper;
fileHelper.remove(sourcePath);
}
}
}
//TODO: we need support over one widget, so every settings should have a diffrent name.
void FMIconView::readSettings() {
/*
QSettings settings;
settings.beginGroup("my_listview1");
restoreGeometry(settings.value("geometry").toByteArray());
qDebug()<<settings.value("geometry");
settings.endGroup();
*/
}
void FMIconView::writeSettings() {
/*
qDebug()<<"write settings";
QSettings settings;
settings.beginGroup("my_listview1");
settings.setValue("geometry",saveGeometry());
settings.endGroup();
*/
}
bool copyRecursively(const QString &srcFilePath, const QString &tgtFilePath)
{
QFileInfo srcFileInfo(srcFilePath);
if (srcFileInfo.isDir()) {
QDir targetDir(tgtFilePath);
bool ok = targetDir.cdUp();
if (ok && !targetDir.mkdir(QFileInfo(tgtFilePath).fileName())) return false;
QDir sourceDir(srcFilePath);
QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
for (int i = 0; i < fileNames.count(); i++) {
const QString newSrcFilePath = srcFilePath + QLatin1Char('/') + fileNames.at(i);
const QString newTgtFilePath = tgtFilePath + QLatin1Char('/') + fileNames.at(i);
if (!copyRecursively(newSrcFilePath, newTgtFilePath)) return false;
}
}
else {
if (!QFile::copy(srcFilePath, tgtFilePath)) return false;
}
return true;
}
void FMIconView::pasteFilesInClipboard() {
qDebug()<<"pasteFilesInClipboard";
QList<QUrl> urls = QApplication::clipboard()->mimeData()->urls();
qDebug()<<urls;
for(int i = 0 ; i<urls.count(); i++) {
QUrl tmpUrl = urls.at(i);
qDebug()<<"pasteFilesInClipboard tmpUrl: "<<tmpUrl;
QFileInfo tmpInfo = QFileInfo(tmpUrl.path());
QString filename = tmpInfo.fileName();
qDebug()<<filename;
qDebug()<<QString(QString(".iconviews_on_desktop_test/")+QString::number(mId)+QString("/")+filename);
QString sourcePath = tmpInfo.filePath();
//this is same to fileSystemModel->filePath(this->rootIndex()) here, and more fast.
QString destPath = QString(QString(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.iconviews_on_desktop_test/")+QString::number(mId)+QString("/")+filename);
copyRecursively(sourcePath, destPath);
}
}
void FMIconView::focusOutEvent(QFocusEvent *e) {
if (blankMenu->isHidden() && oneSelectedMenu->isHidden() && pluralitySelectedMenu->isHidden()){
this->clearSelection();
}
QListView::focusOutEvent(e);
}