-
Notifications
You must be signed in to change notification settings - Fork 4
/
globals.cpp
169 lines (147 loc) · 4.53 KB
/
globals.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
#include "globals.h"
#include <QDir>
#include <QCryptographicHash>
#include <QJsonDocument>
#include <QPainter>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QJsonArray>
QStringList findFilesRecursively ( QStringList paths, QStringList fileTypes )
{
if(fileTypes.isEmpty()) fileTypes << "*";
QStringList result, more;
QStringList::Iterator it;
for ( uint i = 0 ; i < paths.size() ; i++ )
{
QDir dir( paths[i] );
more = dir.entryList( fileTypes, QDir::Files );
for ( it = more.begin() ; it != more.end() ; ++it )
result.append( paths[i] + "/" + *it );
more = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot );
for ( it = more.begin() ; it != more.end() ; ++it )
*it = paths[i] + "/" + *it;
more = findFilesRecursively( more, fileTypes );
for ( it = more.begin() ; it != more.end() ; ++it )
result.append( *it );
}
return result; // yields absolute paths
}
bool gfRemoveFolder(QString path)
{
bool result = true;
QDir dir(path);
if (dir.exists(path)) {
Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
if (info.isDir()) {
result = gfRemoveFolder(info.absoluteFilePath());
}
else {
result = QFile::remove(info.absoluteFilePath());
}
if (!result) {
return result;
}
}
result = dir.rmdir(path);
}
return result;
}
bool gfRemoveFiles(QString path, QString mask)
{
QDir d(path);
QFileInfoList files = d.entryInfoList(QStringList()<<mask);
foreach(QFileInfo i, files)
{
QFile file;
file.setFileName(i.absoluteFilePath());
file.remove();
}
return true;
}
QString UuidFromMd5(QString source)
{
auto src = source.toUtf8();
auto hash = QCryptographicHash::hash(src, QCryptographicHash::Md5);
auto stringified = QString(hash.toHex());
auto uuid = stringified;
return uuid;
}
QString gfReadFile(QString path)
{
QFile f(path);
if(f.open(QIODevice::ReadOnly)) return f.readAll();
return QString();
}
void gfSaveJson(QString file, QJsonObject val)
{
QJsonDocument d; d.setObject(val);
QFile f(file);
if(f.open(QIODevice::WriteOnly)) f.write(d.toJson());
}
void gfSaveJson(QString file, QJsonArray val)
{
QJsonDocument d; d.setArray(val);
QFile f(file);
if(f.open(QIODevice::WriteOnly)) f.write(d.toJson());
}
void gfSaveTextFile(QString file, QString text)
{
QFile f(file);
if(f.open(QIODevice::WriteOnly)) f.write(text.toUtf8());
}
QJsonObject gfReadJson(QString filePath)
{
QFile f(filePath);
if(f.open(QIODevice::ReadOnly))
{
return QJsonDocument::fromJson(f.readAll()).object();
}
return QJsonObject();
}
QPixmap applyEffectToImage(QPixmap src, QGraphicsEffect *effect, int extent)
{
if(src.isNull()) return QPixmap(); //No need to do anything else!
if(!effect) return src; //No need to do anything else!
QGraphicsScene scene;
QGraphicsPixmapItem item;
item.setPixmap(src);
item.setGraphicsEffect(effect);
scene.addItem(&item);
QImage res(src.size()+QSize(extent*2, extent*2), QImage::Format_ARGB32);
res.fill(Qt::transparent);
QPainter ptr(&res);
scene.render(&ptr, QRectF(), QRectF( -extent, -extent, src.width()+extent*2, src.height()+extent*2 ) );
return QPixmap::fromImage(res);
}
QString insertUniqueSuffixedNameInList(QStringList namesAlreadyThere, QString nameToInsert, QString suffixFormat)
{
// obviously there
if(!namesAlreadyThere.contains(nameToInsert)) return nameToInsert;
// Format?
bool isFormatValid = suffixFormat.contains("%1");
assert(isFormatValid);
if(!isFormatValid) suffixFormat = "%1";
QString regex = QString(suffixFormat).replace("%1", "(\\d+)");
// is there any number already prefixed to the nameToInsert?
QRegExp rx(regex);
int pos = rx.lastIndexIn(nameToInsert);
int nextId = 1;
if(pos != -1)
{
nextId = rx.cap(1).toInt();
nameToInsert = nameToInsert.mid(0, pos);
}
QString newName;
do
{
newName = nameToInsert + suffixFormat.arg(nextId);
nextId++;
}
while(namesAlreadyThere.contains(newName));
return newName;
}
bool isSuitableForFileSystem(const QString &s)
{
QRegExp rx("[\\/<>|\":\?*]");
return !s.contains(rx);
}