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 insertQuotes(QString path){ |
14 |
return "\""+path+"\""; |
15 |
} |
16 |
|
17 |
QString normalizeAndQuote(QString path){ |
18 |
return insertQuotes(normalizePath(path)); |
19 |
} |
20 |
|
21 |
void showPopUp(QString message){ |
22 |
QMessageBox msgBox; |
23 |
msgBox.setIcon(QMessageBox::Information); |
24 |
msgBox.setText(message); |
25 |
msgBox.exec(); |
26 |
} |
27 |
|
28 |
void showRichPopUp(QString message){ |
29 |
QMessageBox msgBox; |
30 |
msgBox.setTextFormat(Qt::RichText); |
31 |
msgBox.setIcon(QMessageBox::Information); |
32 |
msgBox.setText(message); |
33 |
msgBox.exec(); |
34 |
} |
35 |
|
36 |
void showWarningPopUp(QString message){ |
37 |
QMessageBox msgBox; |
38 |
msgBox.setIcon(QMessageBox::Warning); |
39 |
msgBox.setText(message); |
40 |
msgBox.exec(); |
41 |
} |
42 |
|
43 |
void showErrorPopUp(QString message){ |
44 |
QMessageBox msgBox; |
45 |
msgBox.setIcon(QMessageBox::Critical); |
46 |
msgBox.setText(message); |
47 |
msgBox.exec(); |
48 |
} |
49 |
|
50 |
//Same of above but allow open log file (doesn't right in log file!!) |
51 |
void showErrorLogPopUp(QString message){ |
52 |
QMessageBox msgBox; |
53 |
msgBox.setIcon(QMessageBox::Critical); |
54 |
msgBox.setText(message); |
55 |
QPushButton *viewb = msgBox.addButton("View log", QMessageBox::ActionRole); |
56 |
msgBox.setStandardButtons(QMessageBox::Close); |
57 |
msgBox.exec(); |
58 |
if(msgBox.clickedButton() == (QAbstractButton*) viewb){ |
59 |
openLogFile(); |
60 |
} |
61 |
} |
62 |
|
63 |
void showRichErrorPopUp(QString message){ |
64 |
QMessageBox msgBox; |
65 |
msgBox.setIcon(QMessageBox::Critical); |
66 |
msgBox.setText(message); |
67 |
msgBox.exec(); |
68 |
} |
69 |
|
70 |
bool showQuestionPopUp(QWidget * parent, QString message){ |
71 |
return QMessageBox::question (parent, "Are you sure?", message, QMessageBox::Yes, QMessageBox::No)==QMessageBox::Yes; |
72 |
} |
73 |
|
74 |
QStringList multipleDirDialog(QString title){ |
75 |
QFileDialog w; |
76 |
|
77 |
w.setFileMode(QFileDialog::DirectoryOnly); |
78 |
|
79 |
w.setWindowTitle(title); |
80 |
|
81 |
QListView *l = w.findChild<QListView*>("listView"); |
82 |
|
83 |
if (l) { |
84 |
l->setSelectionMode(QAbstractItemView::MultiSelection); |
85 |
} |
86 |
|
87 |
QTreeView *t = w.findChild<QTreeView*>(); |
88 |
|
89 |
if (t) { |
90 |
t->setSelectionMode(QAbstractItemView::MultiSelection); |
91 |
} |
92 |
|
93 |
if(w.exec()){ //if accepted |
94 |
return w.selectedFiles(); |
95 |
} |
96 |
return QStringList(); //return empty |
97 |
} |
98 |
|
99 |
bool checkEmptySpaces(QStringList toCheck){ |
100 |
foreach (QString current, toCheck){ |
101 |
if(current.trimmed().isEmpty()){ |
102 |
return true; //There are empty spaces |
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 |
//Copied from here: http://stackoverflow.com/questions/2536524/copy-directory-using-qt (ty roop) |
134 |
bool cpDir(const QString &srcPath, const QString &dstPath) |
135 |
{ |
136 |
rmDir(dstPath); |
137 |
|
138 |
QDir parentDstDir(QFileInfo(dstPath).path()); |
139 |
if (!parentDstDir.mkdir(QFileInfo(dstPath).fileName())) |
140 |
return false; |
141 |
|
142 |
QDir srcDir(srcPath); |
143 |
foreach(const QFileInfo &info, srcDir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)) { |
144 |
QString srcItemPath = srcPath + "/" + info.fileName(); |
145 |
QString dstItemPath = dstPath + "/" + info.fileName(); |
146 |
if (info.isDir()) { |
147 |
if (!cpDir(srcItemPath, dstItemPath)) { |
148 |
return false; |
149 |
} |
150 |
} else if (info.isFile()) { |
151 |
if (!QFile::copy(srcItemPath, dstItemPath)) { |
152 |
return false; |
153 |
} |
154 |
} else { |
155 |
qDebug("Unhandled item" + info.filePath().toAscii() + "in cpDir"); |
156 |
} |
157 |
} |
158 |
return true; |
159 |
} |
160 |
|
161 |
//Copied from here: http://stackoverflow.com/questions/2536524/copy-directory-using-qt (ty roop) |
162 |
bool rmDir(const QString &dirPath) |
163 |
{ |
164 |
QDir dir(dirPath); |
165 |
if (!dir.exists()) |
166 |
return true; |
167 |
foreach(const QFileInfo &info, dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)) { |
168 |
if (info.isDir()) { |
169 |
if (!rmDir(info.filePath())) |
170 |
return false; |
171 |
} else { |
172 |
if (!dir.remove(info.fileName())) |
173 |
return false; |
174 |
} |
175 |
} |
176 |
QDir parentDir(QFileInfo(dirPath).path()); |
177 |
return parentDir.rmdir(QFileInfo(dirPath).fileName()); |
178 |
} |
179 |
|
180 |
QString fullTrim(QString str) { |
181 |
|
182 |
str = str.simplified(); //convert all invisible chars in normal whitespaces |
183 |
str.replace( " ", "" ); |
184 |
|
185 |
return str; |
186 |
} |
187 |
|
188 |
void openLogFile(){ |
189 |
QDesktopServices::openUrl(QUrl("file:///"+QDir::currentPath()+"/"+GlobalVars::AppLogName)); |
190 |
} |
191 |
|
192 |
//Searches for the QString "toSearch" in the "myString" variable backward |
193 |
//Returns the index of the first match or -1 if not found |
194 |
int indexOfBackward(QString myString, QString toSearch, int from){ |
195 |
int myStringSize=myString.size(); |
196 |
int toSearchSize=toSearch.size(); |
197 |
|
198 |
if(from==-1){ |
199 |
from=myStringSize; |
200 |
} |
201 |
|
202 |
int i=from; |
203 |
|
204 |
while(i>=0){ |
205 |
for(int j=toSearchSize-1; j>=0; j--){ |
206 |
i--; |
207 |
if(myString.at(i)!=toSearch.at(j)){ |
208 |
break; |
209 |
} |
210 |
if(j==0){ |
211 |
return i; |
212 |
} |
213 |
} |
214 |
} |
215 |
|
216 |
return -1; |
217 |
} |
218 |
|
219 |
QStringList substring(QString myString,QString separator, Qt::CaseSensitivity cs){ |
220 |
QStringList result = QStringList(); |
221 |
int currIdx=0, nextIdx=0; |
222 |
|
223 |
while(true){ |
224 |
nextIdx=myString.indexOf(separator,currIdx,cs); |
225 |
result << myString.mid(currIdx,nextIdx-currIdx); |
226 |
if(nextIdx==-1) break; |
227 |
currIdx=nextIdx+1; |
228 |
} |
229 |
|
230 |
return result; |
231 |
} |
232 |
|
233 |
QString normalizeDecimalSeparator(QString value){ |
234 |
return value.replace(',','.'); |
235 |
} |
236 |
|
237 |
} |