-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTreeView.cpp
More file actions
178 lines (141 loc) · 5.4 KB
/
MyTreeView.cpp
File metadata and controls
178 lines (141 loc) · 5.4 KB
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
#include "MyTreeView.h"
MyTreeView::MyTreeView(QWidget *parent)
:QTreeView(parent){
setDragEnabled(true);
setAcceptDrops(true);
setDropIndicatorShown(true);
setDefaultDropAction(Qt::CopyAction);
setSelectionMode(QAbstractItemView::ExtendedSelection);
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(ShowContextMenu(const QPoint &)));
}
bool MyTreeView::CopyFile(const QString& sourceFile, const QString& destinationDir)
{
QFileInfo fileInfo(sourceFile);
QString destinationFile = destinationDir + QDir::separator() + fileInfo.fileName();
bool result = QFile::copy(sourceFile, destinationFile);
return result;
}
void MyTreeView::ShowContextMenu(const QPoint &pos)
{
QMenu contextMenu(tr("Context menu"), this);
QAction action1("New Folder", this);
QAction action2("Remove Folder", this);
connect(&action1, SIGNAL(triggered()), this, SLOT(addFolder()));
connect(&action2, SIGNAL(triggered()), this, SLOT(removeFolder()));
contextMenu.addAction(&action1);
contextMenu.addAction(&action2);
contextMenu.exec(mapToGlobal(pos));
}
void MyTreeView::addFolder()
{
QModelIndex index = this->currentIndex();
QFileSystemModel * data = static_cast<QFileSystemModel *>(this ->model());
if(data)
if(data->isDir(index))
data->mkdir(index, "New Folder");
else
{
index = index.parent();
if(data->isDir(index))
data->mkdir(index, "New Folder");
}
}
void MyTreeView::removeFolder()
{
QModelIndex index = this->currentIndex();
QFileSystemModel * data = static_cast<QFileSystemModel *>(this ->model());
if(!data )return;
if(data->isDir(index))
{
data->rmdir(index);
}
}
void MyTreeView::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist")) {
event->acceptProposedAction();
} else if (event->mimeData()->hasFormat("text/uri-list")) {
event->acceptProposedAction();
} else {
std::cout<<RED<<"Drag enter Event ignored"<<RESET<<std::endl;
event->ignore();
}
}
void MyTreeView::dragMoveEvent(QDragMoveEvent *event)
{
std::cout<<RED<<"DragMove Event"<<RESET<<std::endl;
if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist")
|| event->mimeData()->hasFormat("application/x-qstandarditemmodeldatalist")
|| event->mimeData()->hasFormat("Qt/QStandardItemArray")) {
event->acceptProposedAction();
} else if(event->mimeData()->hasUrls()){
event->acceptProposedAction();
}else {
std::cout<<RED<<"DragMove Event ignored"<<RESET<<std::endl;
event->ignore();
}
setCurrentIndex(indexAt(mapFromGlobal(QCursor::pos())));
}
void MyTreeView::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key::Key_Delete )
{
QModelIndex index = this->currentIndex();
QFileSystemModel * data = static_cast<QFileSystemModel *>(this ->model());
if(data)
{
QMessageBox msgBox(this);
msgBox.setInformativeText("Are you sure you want to delete: " + data->fileName(index) + "?");
msgBox.setIcon(QMessageBox::Icon::Question);
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Yes);
int ret = msgBox.exec();
if(ret == QMessageBox::Yes)
if(data->isDir(index))removeFolder();
else
remove(data->filePath(index).toStdString().c_str());
}
}
}
void MyTreeView::dropEvent(QDropEvent *event)
{
std::cout<<RED<<"Drop Event"<<RESET<<std::endl;
if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
{
// Fetch the encoded bytes, create a data stream for decoding,
// and variables to store the output.
QByteArray indexListBytes = event->mimeData()->data("Qt/QStandardItemArray");
QDataStream ds(&indexListBytes, QIODevice::ReadOnly);
quint32 numItems = 0;
// Get the number of items, allocate memory to store pointers to
// them and read the pointer data into that memory.
ds >> numItems;
int byteLen = numItems*sizeof(QStandardItem*);
QStandardItem** stdItems = (QStandardItem**)malloc(byteLen);
ds.readRawData((char*)stdItems, byteLen);
auto model = static_cast<QFileSystemModel *>(this->model());
QString target_path = model->filePath(currentIndex());
// Add items to the target index if requested,
for (unsigned i = 0; i < numItems; i++)
{
QString source_path = stdItems[i]->text();
CopyFile(source_path,target_path);
}
}
else if (event->mimeData()->hasUrls())
{
QFileSystemModel * model = static_cast<QFileSystemModel *>(this ->model());
if(!model )return;
QString target_path = model->filePath(currentIndex());
QList<QUrl> urlList = event->mimeData()->urls();
// move the paths of the files
for (int i = 0; i < urlList.size() ; ++i)
{
QString source_path = urlList.at(i).path();
source_path.remove(0, 1); //removing slash from begining of path
CopyFile(source_path ,target_path);
}
}
}