| 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 | QMessageBox::StandardButton showQuestionPopUpWithCancel(QWidget * parent, QString message, QMessageBox::StandardButton standardButton){
|
|---|
| 66 | return QMessageBox::question (parent, "Are you sure?", message, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, standardButton);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | QStringList multipleDirDialog(QString title){
|
|---|
| 70 | QFileDialog w;
|
|---|
| 71 |
|
|---|
| 72 | w.setFileMode(QFileDialog::DirectoryOnly);
|
|---|
| 73 |
|
|---|
| 74 | w.setWindowTitle(title);
|
|---|
| 75 |
|
|---|
| 76 | QListView *l = w.findChild<QListView*>("listView");
|
|---|
| 77 |
|
|---|
| 78 | if (l) {
|
|---|
| 79 | l->setSelectionMode(QAbstractItemView::MultiSelection);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | QTreeView *t = w.findChild<QTreeView*>();
|
|---|
| 83 |
|
|---|
| 84 | if (t) {
|
|---|
| 85 | t->setSelectionMode(QAbstractItemView::MultiSelection);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | if(w.exec()){ //if accepted
|
|---|
| 89 | return w.selectedFiles();
|
|---|
| 90 | }
|
|---|
| 91 | return QStringList(); //return empty
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | bool checkEmptySpaces(QStringList toCheck){
|
|---|
| 95 | foreach (QString current, toCheck){
|
|---|
| 96 | if(current.trimmed().isEmpty()){
|
|---|
| 97 | return true; //There are empty spaces
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | return false;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | bool checkIfIntegers(QStringList toCheck){
|
|---|
| 104 | foreach (QString current, toCheck){
|
|---|
| 105 | if(!isStringInteger(current)){
|
|---|
| 106 | return true; // Some aren't valid integers
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | return false;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | bool checkIfDoubles(QStringList toCheck){
|
|---|
| 113 | foreach (QString current, toCheck){
|
|---|
| 114 | if(!isStringDouble(current)){
|
|---|
| 115 | return true; // Some aren't valid doubles
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | return false;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | bool isStringInteger(QString myString){
|
|---|
| 122 | bool isNumber;
|
|---|
| 123 |
|
|---|
| 124 | myString.toInt(&isNumber); //convert to int and see if it succeeds
|
|---|
| 125 |
|
|---|
| 126 | return isNumber;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | bool isStringDouble(QString myString){
|
|---|
| 130 | bool isDouble;
|
|---|
| 131 |
|
|---|
| 132 | myString.toDouble(&isDouble); //convert to double and see if it succeeds
|
|---|
| 133 |
|
|---|
| 134 | return isDouble;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | // Created from scratch
|
|---|
| 138 | bool copyDir(const QString &fromPath, QString toPath, const bool isRecursive){
|
|---|
| 139 | QDir fromDir(fromPath);
|
|---|
| 140 | QDir toDir(toPath);
|
|---|
| 141 |
|
|---|
| 142 | if(!toDir.mkdir(fromDir.dirName())){ // create the folder in the destination
|
|---|
| 143 | return false;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | // Update toPath to include the folder from "fromPath"
|
|---|
| 147 | toPath = toPath + "/" + fromDir.dirName();
|
|---|
| 148 | toDir = QDir(toPath);
|
|---|
| 149 |
|
|---|
| 150 | for(const QFileInfo &currFileInfo : fromDir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)){
|
|---|
| 151 |
|
|---|
| 152 | if(currFileInfo.isFile()){
|
|---|
| 153 |
|
|---|
| 154 | QFile destFile(toPath + "/" + currFileInfo.fileName());
|
|---|
| 155 |
|
|---|
| 156 | if(!QFile::copy(currFileInfo.absoluteFilePath(),toPath + "/" + currFileInfo.fileName())){
|
|---|
| 157 | return false;
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | else if(isRecursive && currFileInfo.isDir() && currFileInfo.absoluteFilePath() != fromDir.absolutePath()){
|
|---|
| 161 |
|
|---|
| 162 | if(!copyDir(currFileInfo.absoluteFilePath(), toPath, isRecursive)){
|
|---|
| 163 | return false;
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | return true;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | //Copied from here: http://stackoverflow.com/questions/2536524/copy-directory-using-qt (ty roop)
|
|---|
| 172 | bool rmDir(const QString &dirPath)
|
|---|
| 173 | {
|
|---|
| 174 | QDir dir(dirPath);
|
|---|
| 175 | if (!dir.exists())
|
|---|
| 176 | return true;
|
|---|
| 177 | foreach(const QFileInfo &info, dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)) {
|
|---|
| 178 | if (info.isDir()) {
|
|---|
| 179 | if (!rmDir(info.filePath()))
|
|---|
| 180 | return false;
|
|---|
| 181 | } else {
|
|---|
| 182 | if (!dir.remove(info.fileName()))
|
|---|
| 183 | return false;
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 | QDir parentDir(QFileInfo(dirPath).path());
|
|---|
| 187 | return parentDir.rmdir(QFileInfo(dirPath).fileName());
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 | QString fullTrim(QString str) {
|
|---|
| 192 |
|
|---|
| 193 | str = str.simplified(); //convert all invisible chars in normal whitespaces
|
|---|
| 194 | str.replace( " ", "" );
|
|---|
| 195 |
|
|---|
| 196 | return str;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | //Searches for the QString "toSearch" in the "myString" variable backward
|
|---|
| 200 | //Returns the index of the first match or -1 if not found
|
|---|
| 201 | int indexOfBackward(QString myString, QString toSearch, int from){
|
|---|
| 202 | int myStringSize=myString.size();
|
|---|
| 203 | int toSearchSize=toSearch.size();
|
|---|
| 204 |
|
|---|
| 205 | if(from==-1){
|
|---|
| 206 | from=myStringSize;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | int i=from;
|
|---|
| 210 |
|
|---|
| 211 | while(i>=0){
|
|---|
| 212 | for(int j=toSearchSize-1; j>=0; j--){
|
|---|
| 213 | i--;
|
|---|
| 214 | if(myString.at(i)!=toSearch.at(j)){
|
|---|
| 215 | break;
|
|---|
| 216 | }
|
|---|
| 217 | if(j==0){
|
|---|
| 218 | return i;
|
|---|
| 219 | }
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | return -1;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | QStringList substring(QString myString,QString separator, Qt::CaseSensitivity cs){
|
|---|
| 227 | QStringList result = QStringList();
|
|---|
| 228 | int currIdx=0, nextIdx=0;
|
|---|
| 229 |
|
|---|
| 230 | while(true){
|
|---|
| 231 | nextIdx=myString.indexOf(separator,currIdx,cs);
|
|---|
| 232 | result << myString.mid(currIdx,nextIdx-currIdx);
|
|---|
| 233 | if(nextIdx==-1) break;
|
|---|
| 234 | currIdx=nextIdx+1;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | return result;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | QString normalizeDecimalSeparator(QString value){
|
|---|
| 241 | return value.replace(',','.');
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | // From here: http://stackoverflow.com/questions/17893328/qt-getting-the-screen-resolution-without-the-extended-monitor ty Chris
|
|---|
| 245 | QRect getScreenResolution(){
|
|---|
| 246 | QDesktopWidget widget;
|
|---|
| 247 | return widget.availableGeometry(widget.primaryScreen()); // or screenGeometry(), depending on your needs
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | }
|
|---|