| 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 |
//Copied from here: http://stackoverflow.com/questions/2536524/copy-directory-using-qt (ty roop) |
| 143 |
bool cpDir(const QString &srcPath, const QString &dstPath) |
| 144 |
{ |
| 145 |
rmDir(dstPath); |
| 146 |
|
| 147 |
QDir parentDstDir(QFileInfo(dstPath).path()); |
| 148 |
if (!parentDstDir.mkdir(QFileInfo(dstPath).fileName())) |
| 149 |
return false; |
| 150 |
|
| 151 |
QDir srcDir(srcPath); |
| 152 |
foreach(const QFileInfo &info, srcDir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)) { |
| 153 |
QString srcItemPath = srcPath + "/" + info.fileName(); |
| 154 |
QString dstItemPath = dstPath + "/" + info.fileName(); |
| 155 |
if (info.isDir()) { |
| 156 |
if (!cpDir(srcItemPath, dstItemPath)) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
} else if (info.isFile()) { |
| 160 |
if (!QFile::copy(srcItemPath, dstItemPath)) { |
| 161 |
return false; |
| 162 |
} |
| 163 |
} else { |
| 164 |
qDebug("Unhandled item" + info.filePath().toUtf8() + "in cpDir"); |
| 165 |
} |
| 166 |
} |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
//Copied from here: http://stackoverflow.com/questions/2536524/copy-directory-using-qt (ty roop) |
| 171 |
bool rmDir(const QString &dirPath) |
| 172 |
{ |
| 173 |
QDir dir(dirPath); |
| 174 |
if (!dir.exists()) |
| 175 |
return true; |
| 176 |
foreach(const QFileInfo &info, dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)) { |
| 177 |
if (info.isDir()) { |
| 178 |
if (!rmDir(info.filePath())) |
| 179 |
return false; |
| 180 |
} else { |
| 181 |
if (!dir.remove(info.fileName())) |
| 182 |
return false; |
| 183 |
} |
| 184 |
} |
| 185 |
QDir parentDir(QFileInfo(dirPath).path()); |
| 186 |
return parentDir.rmdir(QFileInfo(dirPath).fileName()); |
| 187 |
} |
| 188 |
|
| 189 |
QString fullTrim(QString str) { |
| 190 |
|
| 191 |
str = str.simplified(); //convert all invisible chars in normal whitespaces |
| 192 |
str.replace( " ", "" ); |
| 193 |
|
| 194 |
return str; |
| 195 |
} |
| 196 |
|
| 197 |
void openLogFile(){ |
| 198 |
QDesktopServices::openUrl(QUrl("file:///"+QDir::currentPath()+"/"+GlobalVars::AppLogName)); |
| 199 |
} |
| 200 |
|
| 201 |
//Searches for the QString "toSearch" in the "myString" variable backward |
| 202 |
//Returns the index of the first match or -1 if not found |
| 203 |
int indexOfBackward(QString myString, QString toSearch, int from){ |
| 204 |
int myStringSize=myString.size(); |
| 205 |
int toSearchSize=toSearch.size(); |
| 206 |
|
| 207 |
if(from==-1){ |
| 208 |
from=myStringSize; |
| 209 |
} |
| 210 |
|
| 211 |
int i=from; |
| 212 |
|
| 213 |
while(i>=0){ |
| 214 |
for(int j=toSearchSize-1; j>=0; j--){ |
| 215 |
i--; |
| 216 |
if(myString.at(i)!=toSearch.at(j)){ |
| 217 |
break; |
| 218 |
} |
| 219 |
if(j==0){ |
| 220 |
return i; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
return -1; |
| 226 |
} |
| 227 |
|
| 228 |
QStringList substring(QString myString,QString separator, Qt::CaseSensitivity cs){ |
| 229 |
QStringList result = QStringList(); |
| 230 |
int currIdx=0, nextIdx=0; |
| 231 |
|
| 232 |
while(true){ |
| 233 |
nextIdx=myString.indexOf(separator,currIdx,cs); |
| 234 |
result << myString.mid(currIdx,nextIdx-currIdx); |
| 235 |
if(nextIdx==-1) break; |
| 236 |
currIdx=nextIdx+1; |
| 237 |
} |
| 238 |
|
| 239 |
return result; |
| 240 |
} |
| 241 |
|
| 242 |
QString normalizeDecimalSeparator(QString value){ |
| 243 |
return value.replace(',','.'); |
| 244 |
} |
| 245 |
|
| 246 |
// From here: http://stackoverflow.com/questions/17893328/qt-getting-the-screen-resolution-without-the-extended-monitor ty Chris |
| 247 |
QRect getScreenResolution(){ |
| 248 |
QDesktopWidget widget; |
| 249 |
return widget.availableGeometry(widget.primaryScreen()); // or screenGeometry(), depending on your needs |
| 250 |
} |
| 251 |
|
| 252 |
} |