-
Notifications
You must be signed in to change notification settings - Fork 0
/
srcdiritemmodel.cpp
147 lines (121 loc) · 3.82 KB
/
srcdiritemmodel.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
#include <QDir>
#include <QFileInfo>
#include <QMimeData>
#include <QUrl>
#include "srcdiritemmodel.h"
#include "additionalpath.h"
SrcDirItemModel::SrcDirItemModel(QObject *parent) :
QAbstractListModel(parent)
{
dirLevel = 0;
}
SrcDirItemModel::~SrcDirItemModel() {
}
//public
void SrcDirItemModel::addDir(QString dir, QString path) {
dirs.append(dir);
additionalPath.append(path);
QModelIndex index;
emit dataChanged(index,index);
}
QVariant SrcDirItemModel::data(const QModelIndex &index, int role) const {
if (index.isValid() and index.row() < dirs.size() and index.row() >= 0) {
return role == Qt::DisplayRole? dirs.at(index.row()): QVariant();
} else
return QVariant();
}
void SrcDirItemModel::delDir(const QModelIndex &index) {
dirs.removeAt(index.row());
additionalPath.removeAt(index.row());
emit dataChanged(index,index);
}
QString SrcDirItemModel::dirAt(int i) {
if (i < dirs.count())
return dirs.at(i);
else
return QString();
}
QString SrcDirItemModel::getAdditionalPath(const int index) const {
return additionalPath.at(index);
}
QString SrcDirItemModel::getAdditionalPath(const QModelIndex &index) const {
return additionalPath.at(index.row());
}
QString SrcDirItemModel::getDir(const int index) const {
return dirs.at(index);
}
QString SrcDirItemModel::lastDir() {
if (dirs.size()) {
return dirs.at(dirs.size() - 1);
} else {
return QString();
}
}
void SrcDirItemModel::load(QString dirsStr, QString pathStr) {
dirs << dirsStr.split("////");
additionalPath << pathStr.split("////");
QModelIndex index;
emit dataChanged(index,index);
if (dirs.at(0).isEmpty()) {
dirs.clear();
additionalPath.clear();
}
}
int SrcDirItemModel::rowCount(const QModelIndex &parent) const {
return dirs.size();
}
QString SrcDirItemModel::serializeDirs() const {
return dirs.join("////");
}
QString SrcDirItemModel::serializePaths() const {
return additionalPath.join("////");
}
void SrcDirItemModel::updateDir(QString dir, QString path,
const QModelIndex &index) {
if (dirs.size() > index.row()) {
dirs[index.row()] = dir;
additionalPath[index.row()] = path;
emit dataChanged(index,index);
}
}
//protected
bool SrcDirItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column,
const QModelIndex &parent) {
if (action == Qt::IgnoreAction)
return true;
if (!data->hasFormat("text/uri-list"))
return false;
if (column > 0)
return false;
QList<QUrl> urlList = data->urls();
foreach (QUrl url, urlList) {
QString dirName = url.toLocalFile();
if (QFileInfo(dirName).isDir())
addDir(QDir::toNativeSeparators(dirName),
QDir::toNativeSeparators(
getAdditonalPath(dirName, dirLevel)));
}
return true;
}
Qt::ItemFlags SrcDirItemModel::flags(const QModelIndex &index) const {
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
if (index.isValid())
return Qt::ItemIsDropEnabled | defaultFlags;
else
return Qt::ItemIsDropEnabled | defaultFlags;
}
Qt::DropActions SrcDirItemModel::supportedDropActions() const {
return Qt::CopyAction;
}
QMimeData *SrcDirItemModel::mimeData(const QModelIndexList &indexes) const {
QMimeData *mimeData = new QMimeData();
return mimeData;
}
QStringList SrcDirItemModel::mimeTypes() const {
return QStringList("text/uri-list");
}
//public slots
void SrcDirItemModel::setDirLevel(int level) {
dirLevel = level;
}