| 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, QMessageBox::StandardButton standardButton){ |
| 71 |
return QMessageBox::question (parent, "Are you sure?", message, QMessageBox::Yes | QMessageBox::No, standardButton)==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 checkIfIntegers(QStringList toCheck){ |
| 109 |
foreach (QString current, toCheck){ |
| 110 |
if(!isStringInteger(current)){ |
| 111 |
return true; // Some aren't valid integers |
| 112 |
} |
| 113 |
} |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
bool checkIfDoubles(QStringList toCheck){ |
| 118 |
foreach (QString current, toCheck){ |
| 119 |
if(!isStringDouble(current)){ |
| 120 |
return true; // Some aren't valid doubles |
| 121 |
} |
| 122 |
} |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
bool isStringInteger(QString myString){ |
| 127 |
bool isNumber; |
| 128 |
|
| 129 |
myString.toInt(&isNumber); //convert to int and see if it succeeds |
| 130 |
|
| 131 |
return isNumber; |
| 132 |
} |
| 133 |
|
| 134 |
bool isStringDouble(QString myString){ |
| 135 |
bool isDouble; |
| 136 |
|
| 137 |
myString.toDouble(&isDouble); //convert to double and see if it succeeds |
| 138 |
|
| 139 |
return isDouble; |
| 140 |
} |
| 141 |
|
| 142 |
// from here: https://gzeki.com/blog/view/Recursive_copy_files_from_one_directory_to_another_in_C++_(Qt_5) |
| 143 |
bool copyDir(QString from_dir, QString to_dir, bool replace_on_conflit) |
| 144 |
{ |
| 145 |
QDir dir; |
| 146 |
dir.setPath(from_dir); |
| 147 |
|
| 148 |
from_dir += QDir::separator(); |
| 149 |
to_dir += QDir::separator(); |
| 150 |
|
| 151 |
foreach (QString copy_file, dir.entryList(QDir::Files)) |
| 152 |
{ |
| 153 |
QString from = from_dir + copy_file; |
| 154 |
QString to = to_dir + copy_file; |
| 155 |
|
| 156 |
if (QFile::exists(to)) |
| 157 |
{ |
| 158 |
if (replace_on_conflit) |
| 159 |
{ |
| 160 |
if (QFile::remove(to) == false) |
| 161 |
{ |
| 162 |
return false; |
| 163 |
} |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
continue; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if (QFile::copy(from, to) == false) |
| 172 |
{ |
| 173 |
return false; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
foreach (QString copy_dir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) |
| 178 |
{ |
| 179 |
QString from = from_dir + copy_dir; |
| 180 |
QString to = to_dir + copy_dir; |
| 181 |
|
| 182 |
if (dir.mkpath(to) == false) |
| 183 |
{ |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
if (copyDir(from, to, replace_on_conflit) == false) |
| 188 |
{ |
| 189 |
return false; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
//Copied from here: http://stackoverflow.com/questions/2536524/copy-directory-using-qt (ty roop) |
| 197 |
bool rmDir(const QString &dirPath) |
| 198 |
{ |
| 199 |
QDir dir(dirPath); |
| 200 |
if (!dir.exists()) |
| 201 |
return true; |
| 202 |
foreach(const QFileInfo &info, dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)) { |
| 203 |
if (info.isDir()) { |
| 204 |
if (!rmDir(info.filePath())) |
| 205 |
return false; |
| 206 |
} else { |
| 207 |
if (!dir.remove(info.fileName())) |
| 208 |
return false; |
| 209 |
} |
| 210 |
} |
| 211 |
QDir parentDir(QFileInfo(dirPath).path()); |
| 212 |
return parentDir.rmdir(QFileInfo(dirPath).fileName()); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
QString fullTrim(QString str) { |
| 217 |
|
| 218 |
str = str.simplified(); //convert all invisible chars in normal whitespaces |
| 219 |
str.replace( " ", "" ); |
| 220 |
|
| 221 |
return str; |
| 222 |
} |
| 223 |
|
| 224 |
void openLogFile(){ |
| 225 |
QDesktopServices::openUrl(QUrl("file:///"+Util::getAppPath()+"/"+GlobalVars::AppLogName)); |
| 226 |
} |
| 227 |
|
| 228 |
//Searches for the QString "toSearch" in the "myString" variable backward |
| 229 |
//Returns the index of the first match or -1 if not found |
| 230 |
int indexOfBackward(QString myString, QString toSearch, int from){ |
| 231 |
int myStringSize=myString.size(); |
| 232 |
int toSearchSize=toSearch.size(); |
| 233 |
|
| 234 |
if(from==-1){ |
| 235 |
from=myStringSize; |
| 236 |
} |
| 237 |
|
| 238 |
int i=from; |
| 239 |
|
| 240 |
while(i>=0){ |
| 241 |
for(int j=toSearchSize-1; j>=0; j--){ |
| 242 |
i--; |
| 243 |
if(myString.at(i)!=toSearch.at(j)){ |
| 244 |
break; |
| 245 |
} |
| 246 |
if(j==0){ |
| 247 |
return i; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return -1; |
| 253 |
} |
| 254 |
|
| 255 |
QStringList substring(QString myString,QString separator, Qt::CaseSensitivity cs){ |
| 256 |
QStringList result = QStringList(); |
| 257 |
int currIdx=0, nextIdx=0; |
| 258 |
|
| 259 |
while(true){ |
| 260 |
nextIdx=myString.indexOf(separator,currIdx,cs); |
| 261 |
result << myString.mid(currIdx,nextIdx-currIdx); |
| 262 |
if(nextIdx==-1) break; |
| 263 |
currIdx=nextIdx+1; |
| 264 |
} |
| 265 |
|
| 266 |
return result; |
| 267 |
} |
| 268 |
|
| 269 |
QString normalizeDecimalSeparator(QString value){ |
| 270 |
return value.replace(',','.'); |
| 271 |
} |
| 272 |
|
| 273 |
// From here: http://stackoverflow.com/questions/17893328/qt-getting-the-screen-resolution-without-the-extended-monitor ty Chris |
| 274 |
QRect getScreenResolution(){ |
| 275 |
QDesktopWidget widget; |
| 276 |
return widget.availableGeometry(widget.primaryScreen()); // or screenGeometry(), depending on your needs |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
Gets application directory. In mac os gets the .app directory |
| 281 |
**/ |
| 282 |
QString getOSIndependentAppPath(){ |
| 283 |
#ifdef Q_OS_MAC |
| 284 |
QDir dir = QDir(QCoreApplication::applicationDirPath()); |
| 285 |
if(dir.absolutePath().contains(".app")){ // include bundle, but we don't want it |
| 286 |
dir.cdUp(); |
| 287 |
dir.cdUp(); |
| 288 |
dir.cdUp(); |
| 289 |
} |
| 290 |
return dir.absolutePath(); |
| 291 |
#else |
| 292 |
return QDir::currentPath(); |
| 293 |
#endif |
| 294 |
} |
| 295 |
|
| 296 |
QString getAppPath(){ |
| 297 |
return getOSIndependentAppPath(); |
| 298 |
} |
| 299 |
|
| 300 |
QString getOniSplitExeName(){ |
| 301 |
|
| 302 |
#ifdef Q_OS_MAC |
| 303 |
return getMonoExecutablePath() + " " + GlobalVars::OniSplitString; |
| 304 |
#elif |
| 305 |
return GlobalVars::OniSplitString; |
| 306 |
#endif |
| 307 |
} |
| 308 |
|
| 309 |
QString getXmlToolsExeName(){ |
| 310 |
|
| 311 |
#ifdef Q_OS_MAC |
| 312 |
return getMonoExecutablePath() + " " + GlobalVars::XmlToolsString; |
| 313 |
#elif |
| 314 |
return GlobalVars::XmlToolsString; |
| 315 |
#endif |
| 316 |
} |
| 317 |
|
| 318 |
#ifdef Q_OS_MAC |
| 319 |
QString getMonoExecutablePath(){ |
| 320 |
|
| 321 |
// Only way that I found to get mono working in 10.11 |
| 322 |
QString possibleMonoDir = "/usr/local/bin/mono"; |
| 323 |
QFileInfo checkFile(possibleMonoDir); |
| 324 |
|
| 325 |
if (checkFile.exists() && checkFile.isFile()) { |
| 326 |
return possibleMonoDir; |
| 327 |
} else { |
| 328 |
return "mono"; |
| 329 |
} |
| 330 |
|
| 331 |
} |
| 332 |
#endif |
| 333 |
|
| 334 |
} |