1 |
#include "util.h" |
2 |
|
3 |
namespace Util{ |
4 |
|
5 |
QString normalizePath(QString path){ |
6 |
return path.replace("\\","/"); |
7 |
} |
8 |
|
9 |
QString cutName(QString path){ |
10 |
return path.remove(0,path.lastIndexOf('/')).remove('"'); |
11 |
} |
12 |
|
13 |
QString cutNameWithoutBackSlash(QString path){ |
14 |
return cutName(path).remove('/'); |
15 |
} |
16 |
|
17 |
QString insertQuotes(const QString &currString){ |
18 |
return "\""+currString+"\""; |
19 |
} |
20 |
|
21 |
QString normalizeAndQuote(QString path){ |
22 |
return insertQuotes(normalizePath(path)); |
23 |
} |
24 |
|
25 |
void showPopUp(const QString &message){ |
26 |
QMessageBox msgBox; |
27 |
msgBox.setIcon(QMessageBox::Information); |
28 |
msgBox.setText(message); |
29 |
msgBox.exec(); |
30 |
} |
31 |
|
32 |
void showRichPopUp(const QString &message){ |
33 |
QMessageBox msgBox; |
34 |
msgBox.setTextFormat(Qt::RichText); |
35 |
msgBox.setIcon(QMessageBox::Information); |
36 |
msgBox.setText(message); |
37 |
msgBox.exec(); |
38 |
} |
39 |
|
40 |
void showWarningPopUp(const QString &message){ |
41 |
QMessageBox msgBox; |
42 |
msgBox.setIcon(QMessageBox::Warning); |
43 |
msgBox.setText(message); |
44 |
msgBox.exec(); |
45 |
} |
46 |
|
47 |
void showErrorPopUp(const QString &message){ |
48 |
QMessageBox msgBox; |
49 |
msgBox.setIcon(QMessageBox::Critical); |
50 |
msgBox.setText(message); |
51 |
msgBox.exec(); |
52 |
} |
53 |
|
54 |
void showRichErrorPopUp(const QString &message){ |
55 |
QMessageBox msgBox; |
56 |
msgBox.setIcon(QMessageBox::Critical); |
57 |
msgBox.setText(message); |
58 |
msgBox.exec(); |
59 |
} |
60 |
|
61 |
bool showQuestionPopUp(QWidget * parent, QString message, QMessageBox::StandardButton standardButton){ |
62 |
return QMessageBox::question (parent, "Are you sure?", message, QMessageBox::Yes | QMessageBox::No, standardButton)==QMessageBox::Yes; |
63 |
} |
64 |
|
65 |
QStringList multipleDirDialog(QString title){ |
66 |
QFileDialog w; |
67 |
|
68 |
w.setFileMode(QFileDialog::DirectoryOnly); |
69 |
|
70 |
w.setWindowTitle(title); |
71 |
|
72 |
QListView *l = w.findChild<QListView*>("listView"); |
73 |
|
74 |
if (l) { |
75 |
l->setSelectionMode(QAbstractItemView::MultiSelection); |
76 |
} |
77 |
|
78 |
QTreeView *t = w.findChild<QTreeView*>(); |
79 |
|
80 |
if (t) { |
81 |
t->setSelectionMode(QAbstractItemView::MultiSelection); |
82 |
} |
83 |
|
84 |
if(w.exec()){ //if accepted |
85 |
return w.selectedFiles(); |
86 |
} |
87 |
return QStringList(); //return empty |
88 |
} |
89 |
|
90 |
bool checkEmptySpaces(QStringList toCheck){ |
91 |
foreach (QString current, toCheck){ |
92 |
if(current.trimmed().isEmpty()){ |
93 |
return true; //There are empty spaces |
94 |
} |
95 |
} |
96 |
return false; |
97 |
} |
98 |
|
99 |
bool checkIfIntegers(QStringList toCheck){ |
100 |
foreach (QString current, toCheck){ |
101 |
if(!isStringInteger(current)){ |
102 |
return true; // Some aren't valid integers |
103 |
} |
104 |
} |
105 |
return false; |
106 |
} |
107 |
|
108 |
bool checkIfDoubles(QStringList toCheck){ |
109 |
foreach (QString current, toCheck){ |
110 |
if(!isStringDouble(current)){ |
111 |
return true; // Some aren't valid doubles |
112 |
} |
113 |
} |
114 |
return false; |
115 |
} |
116 |
|
117 |
bool isStringInteger(QString myString){ |
118 |
bool isNumber; |
119 |
|
120 |
myString.toInt(&isNumber); //convert to int and see if it succeeds |
121 |
|
122 |
return isNumber; |
123 |
} |
124 |
|
125 |
bool isStringDouble(QString myString){ |
126 |
bool isDouble; |
127 |
|
128 |
myString.toDouble(&isDouble); //convert to double and see if it succeeds |
129 |
|
130 |
return isDouble; |
131 |
} |
132 |
|
133 |
// Created from scratch |
134 |
bool copyDir(const QString &fromPath, QString toPath, const bool isRecursive){ |
135 |
QDir fromDir(fromPath); |
136 |
QDir toDir(toPath); |
137 |
|
138 |
if(!toDir.mkdir(fromDir.dirName())){ // create the folder in the destination |
139 |
return false; |
140 |
} |
141 |
|
142 |
// Update toPath to include the folder from "fromPath" |
143 |
toPath = toPath + "/" + fromDir.dirName(); |
144 |
toDir = QDir(toPath); |
145 |
|
146 |
for(const QFileInfo &currFileInfo : fromDir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)){ |
147 |
|
148 |
if(currFileInfo.isFile()){ |
149 |
|
150 |
QFile destFile(toPath + "/" + currFileInfo.fileName()); |
151 |
|
152 |
if(!QFile::copy(currFileInfo.absoluteFilePath(),toPath + "/" + currFileInfo.fileName())){ |
153 |
return false; |
154 |
} |
155 |
} |
156 |
else if(isRecursive && currFileInfo.isDir() && currFileInfo.absoluteFilePath() != fromDir.absolutePath()){ |
157 |
|
158 |
if(!copyDir(currFileInfo.absoluteFilePath(), toPath, isRecursive)){ |
159 |
return false; |
160 |
} |
161 |
} |
162 |
} |
163 |
|
164 |
return true; |
165 |
} |
166 |
|
167 |
//Copied from here: http://stackoverflow.com/questions/2536524/copy-directory-using-qt (ty roop) |
168 |
bool rmDir(const QString &dirPath) |
169 |
{ |
170 |
QDir dir(dirPath); |
171 |
if (!dir.exists()) |
172 |
return true; |
173 |
foreach(const QFileInfo &info, dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)) { |
174 |
if (info.isDir()) { |
175 |
if (!rmDir(info.filePath())) |
176 |
return false; |
177 |
} else { |
178 |
if (!dir.remove(info.fileName())) |
179 |
return false; |
180 |
} |
181 |
} |
182 |
QDir parentDir(QFileInfo(dirPath).path()); |
183 |
return parentDir.rmdir(QFileInfo(dirPath).fileName()); |
184 |
} |
185 |
|
186 |
|
187 |
QString fullTrim(QString str) { |
188 |
|
189 |
str = str.simplified(); //convert all invisible chars in normal whitespaces |
190 |
str.replace( " ", "" ); |
191 |
|
192 |
return str; |
193 |
} |
194 |
|
195 |
//Searches for the QString "toSearch" in the "myString" variable backward |
196 |
//Returns the index of the first match or -1 if not found |
197 |
int indexOfBackward(QString myString, QString toSearch, int from){ |
198 |
int myStringSize=myString.size(); |
199 |
int toSearchSize=toSearch.size(); |
200 |
|
201 |
if(from==-1){ |
202 |
from=myStringSize; |
203 |
} |
204 |
|
205 |
int i=from; |
206 |
|
207 |
while(i>=0){ |
208 |
for(int j=toSearchSize-1; j>=0; j--){ |
209 |
i--; |
210 |
if(myString.at(i)!=toSearch.at(j)){ |
211 |
break; |
212 |
} |
213 |
if(j==0){ |
214 |
return i; |
215 |
} |
216 |
} |
217 |
} |
218 |
|
219 |
return -1; |
220 |
} |
221 |
|
222 |
QStringList substring(QString myString,QString separator, Qt::CaseSensitivity cs){ |
223 |
QStringList result = QStringList(); |
224 |
int currIdx=0, nextIdx=0; |
225 |
|
226 |
while(true){ |
227 |
nextIdx=myString.indexOf(separator,currIdx,cs); |
228 |
result << myString.mid(currIdx,nextIdx-currIdx); |
229 |
if(nextIdx==-1) break; |
230 |
currIdx=nextIdx+1; |
231 |
} |
232 |
|
233 |
return result; |
234 |
} |
235 |
|
236 |
QString normalizeDecimalSeparator(QString value){ |
237 |
return value.replace(',','.'); |
238 |
} |
239 |
|
240 |
// From here: http://stackoverflow.com/questions/17893328/qt-getting-the-screen-resolution-without-the-extended-monitor ty Chris |
241 |
QRect getScreenResolution(){ |
242 |
QDesktopWidget widget; |
243 |
return widget.availableGeometry(widget.primaryScreen()); // or screenGeometry(), depending on your needs |
244 |
} |
245 |
|
246 |
} |