| 1 | /** | 
 
 
 
 
 | 2 | * Copyright (C) 2017 - 2018 Fábio Bento (fabiobento512) | 
 
 
 
 
 | 3 | * | 
 
 
 
 
 | 4 | * This library is distributed under the MIT License. See notice at the end | 
 
 
 
 
 | 5 | * of this file. | 
 
 
 
 
 | 6 | * | 
 
 
 
 
 | 7 | */ | 
 
 
 
 
 | 8 |  | 
 
 
 
 
 | 9 | #ifndef UTIL_H | 
 
 
 
 
 | 10 | #define UTIL_H | 
 
 
 
 
 | 11 |  | 
 
 
 
 
 | 12 | #include <QtGlobal> // for debug macros | 
 
 
 
 
 | 13 | #ifdef QT_DEBUG | 
 
 
 
 
 | 14 | #include <QDebug> | 
 
 
 
 
 | 15 | #endif | 
 
 
 
 
 | 16 | #include <QRegularExpression> | 
 
 
 
 
 | 17 | #include <QString> | 
 
 
 
 
 | 18 | #include <QUrl> | 
 
 
 
 
 | 19 | #include <QCoreApplication> | 
 
 
 
 
 | 20 | #include <QSettings> | 
 
 
 
 
 | 21 | #include <QXmlStreamReader> | 
 
 
 
 
 | 22 | #include <memory> | 
 
 
 
 
 | 23 | #include <QDirIterator> | 
 
 
 
 
 | 24 | #include <QCryptographicHash> | 
 
 
 
 
 | 25 | #include <string.h> | 
 
 
 
 
 | 26 |  | 
 
 
 
 
 | 27 | #ifdef QT_GUI_LIB | 
 
 
 
 
 | 28 | #include <QMessageBox> | 
 
 
 
 
 | 29 | #include <QFileDialog> | 
 
 
 
 
 | 30 | #include <QListView> | 
 
 
 
 
 | 31 | #include <QTreeView> | 
 
 
 
 
 | 32 | #include <QDesktopServices> | 
 
 
 
 
 | 33 | #include <QDesktopWidget> | 
 
 
 
 
 | 34 | #include <QStatusBar> | 
 
 
 
 
 | 35 | #include <QTableWidget> | 
 
 
 
 
 | 36 | #include <QCheckBox> | 
 
 
 
 
 | 37 | #include <QHBoxLayout> | 
 
 
 
 
 | 38 | #endif | 
 
 
 
 
 | 39 |  | 
 
 
 
 
 | 40 | /** | 
 
 
 
 
 | 41 | Utilities functions (global) | 
 
 
 
 
 | 42 | **/ | 
 
 
 
 
 | 43 | namespace Util{ | 
 
 
 
 
 | 44 |  | 
 
 
 
 
 | 45 | namespace FileSystem { | 
 
 
 
 
 | 46 |  | 
 
 
 
 
 | 47 | QString normalizePath(QString path); | 
 
 
 
 
 | 48 |  | 
 
 
 
 
 | 49 | QString cutName(QString path); | 
 
 
 
 
 | 50 |  | 
 
 
 
 
 | 51 | QString cutNameWithoutBackSlash(QString path); | 
 
 
 
 
 | 52 |  | 
 
 
 
 
 | 53 | QString normalizeAndQuote(QString path); | 
 
 
 
 
 | 54 |  | 
 
 
 
 
 | 55 | bool copyDir(const QString &fromPath, QString toPath, const bool isRecursive = false); | 
 
 
 
 
 | 56 |  | 
 
 
 
 
 | 57 | bool rmDir(const QString &dirPath); | 
 
 
 
 
 | 58 |  | 
 
 
 
 
 | 59 | QStringList getFolderFilesByWildcard(const QString &entryFolder, const QString &wildcard, bool isRecursive = false); | 
 
 
 
 
 | 60 |  | 
 
 
 
 
 | 61 | QStringList filterFilesByWildcard(const QStringList &filePaths, const QString &wildcard); | 
 
 
 
 
 | 62 |  | 
 
 
 
 
 | 63 | QString fileHash(const QString &fileName, QCryptographicHash::Algorithm hashAlgorithm); | 
 
 
 
 
 | 64 |  | 
 
 
 
 
 | 65 | QString getAppPath(); | 
 
 
 
 
 | 66 |  | 
 
 
 
 
 | 67 | bool backupFile(const QString &file, QString newFilename=""); | 
 
 
 
 
 | 68 |  | 
 
 
 
 
 | 69 | } | 
 
 
 
 
 | 70 |  | 
 
 
 
 
 | 71 | namespace String { | 
 
 
 
 
 | 72 |  | 
 
 
 
 
 | 73 | // Not using a function because we need to have this as inline function (in both debug and release mods) | 
 
 
 
 
 | 74 | // QString(string).toUtf8().constData() -> since this creates a temporary it needs to be inline | 
 
 
 
 
 | 75 | #ifndef QSTR_TO_CSTR | 
 
 
 
 
 | 76 | #  define QSTR_TO_CSTR qUtf8Printable | 
 
 
 
 
 | 77 | #endif | 
 
 
 
 
 | 78 |  | 
 
 
 
 
 | 79 | QString insertApostrophes(const QString &currString); | 
 
 
 
 
 | 80 |  | 
 
 
 
 
 | 81 | QString insertQuotes(const QString &currString); | 
 
 
 
 
 | 82 |  | 
 
 
 
 
 | 83 | QString fullTrim(QString str); | 
 
 
 
 
 | 84 |  | 
 
 
 
 
 | 85 | QStringList substring(QString myString, QString separator, Qt::CaseSensitivity cs = Qt::CaseSensitive); | 
 
 
 
 
 | 86 |  | 
 
 
 
 
 | 87 | QString normalizeDecimalSeparator(QString value); | 
 
 
 
 
 | 88 |  | 
 
 
 
 
 | 89 | // no problem here with "temporary" cstr | 
 
 
 
 
 | 90 | // https://stackoverflow.com/questions/1971183/when-does-c-allocate-deallocate-string-literals | 
 
 
 
 
 | 91 | const char* boolToCstr(bool currentBoolean); | 
 
 
 
 
 | 92 |  | 
 
 
 
 
 | 93 | } | 
 
 
 
 
 | 94 |  | 
 
 
 
 
 | 95 | #ifdef QT_GUI_LIB | 
 
 
 
 
 | 96 | namespace Dialogs { | 
 
 
 
 
 | 97 |  | 
 
 
 
 
 | 98 | void showInfo(const QString &message, const bool richText = false); | 
 
 
 
 
 | 99 |  | 
 
 
 
 
 | 100 | void showWarning(const QString &message, const bool richText = false); | 
 
 
 
 
 | 101 |  | 
 
 
 
 
 | 102 | void showError(const QString &message, const bool richText = false); | 
 
 
 
 
 | 103 |  | 
 
 
 
 
 | 104 | bool showQuestion(QWidget * parent, QString message, QMessageBox::StandardButton standardButton=QMessageBox::NoButton); | 
 
 
 
 
 | 105 |  | 
 
 
 
 
 | 106 | QMessageBox::StandardButton showQuestionWithCancel(QWidget * parent, QString message, QMessageBox::StandardButton standardButton=QMessageBox::NoButton); | 
 
 
 
 
 | 107 |  | 
 
 
 
 
 | 108 | QStringList multipleDirSelection(const QString &title); | 
 
 
 
 
 | 109 |  | 
 
 
 
 
 | 110 | } | 
 
 
 
 
 | 111 | #endif | 
 
 
 
 
 | 112 |  | 
 
 
 
 
 | 113 | namespace Validation { | 
 
 
 
 
 | 114 |  | 
 
 
 
 
 | 115 | bool checkEmptySpaces(QStringList toCheck); | 
 
 
 
 
 | 116 | bool checkIfIntegers(QStringList toCheck); | 
 
 
 
 
 | 117 | bool checkIfDoubles(QStringList toCheck); | 
 
 
 
 
 | 118 | bool isStringInteger(QString myString); | 
 
 
 
 
 | 119 | bool isStringDouble(QString myString); | 
 
 
 
 
 | 120 |  | 
 
 
 
 
 | 121 | } | 
 
 
 
 
 | 122 |  | 
 
 
 
 
 | 123 | #ifdef QT_GUI_LIB | 
 
 
 
 
 | 124 | namespace TableWidget { | 
 
 
 
 
 | 125 |  | 
 
 
 
 
 | 126 | void addRow(QTableWidget *myTable, QStringList &columns); | 
 
 
 
 
 | 127 | QModelIndexList getSelectedRows(QTableWidget *myTable); | 
 
 
 
 
 | 128 | QModelIndexList getCurrentRows(QTableWidget *myTable); | 
 
 
 
 
 | 129 | int getNumberSelectedRows(QTableWidget *myTable); | 
 
 
 
 
 | 130 | void clearContents(QTableWidget *myTable, const QString ¬hingToClearMessage, const QString &questionToClear); | 
 
 
 
 
 | 131 | void clearContentsNoPrompt(QTableWidget *myTable); | 
 
 
 
 
 | 132 | void addCheckBox(QTableWidget *myTable, int row, int column, QCheckBox *checkbox = nullptr); | 
 
 
 
 
 | 133 | QCheckBox* getCheckBoxFromCell(QTableWidget *myTable, int row, int column); | 
 
 
 
 
 | 134 | void swapRows(QTableWidget *myTable, const int indexSourceRow, const int indexDestinationRow, bool selectSwappedRow); | 
 
 
 
 
 | 135 | void deleteSelectedRows(QTableWidget *myTable); | 
 
 
 
 
 | 136 |  | 
 
 
 
 
 | 137 | } | 
 
 
 
 
 | 138 | #endif | 
 
 
 
 
 | 139 |  | 
 
 
 
 
 | 140 | namespace System { | 
 
 
 
 
 | 141 | #ifdef QT_GUI_LIB | 
 
 
 
 
 | 142 | QRect getScreenResolution(); | 
 
 
 
 
 | 143 | #endif | 
 
 
 
 
 | 144 | } | 
 
 
 
 
 | 145 |  | 
 
 
 
 
 | 146 | #ifdef QT_GUI_LIB | 
 
 
 
 
 | 147 | namespace StatusBar { | 
 
 
 
 
 | 148 |  | 
 
 
 
 
 | 149 | void showInfo(QStatusBar * const statusBar, const QString &message); | 
 
 
 
 
 | 150 | void showError(QStatusBar * const statusBar, const QString &message); | 
 
 
 
 
 | 151 | void showSuccess(QStatusBar * const  statusBar,const QString &message); | 
 
 
 
 
 | 152 |  | 
 
 
 
 
 | 153 | } | 
 
 
 
 
 | 154 | #endif | 
 
 
 
 
 | 155 |  | 
 
 
 
 
 | 156 |  | 
 
 
 
 
 | 157 | } | 
 
 
 
 
 | 158 | #endif // UTIL_H | 
 
 
 
 
 | 159 |  | 
 
 
 
 
 | 160 | /** | 
 
 
 
 
 | 161 | * Copyright (c) 2017 - 2018 Fábio Bento (fabiobento512) | 
 
 
 
 
 | 162 | * | 
 
 
 
 
 | 163 | * Permission is hereby granted, free of charge, to any person | 
 
 
 
 
 | 164 | * obtaining a copy of this software and associated documentation | 
 
 
 
 
 | 165 | * files (the "Software"), to deal in the Software without | 
 
 
 
 
 | 166 | * restriction, including without limitation the rights to use, | 
 
 
 
 
 | 167 | * copy, modify, merge, publish, distribute, sublicense, and/or sell | 
 
 
 
 
 | 168 | * copies of the Software, and to permit persons to whom the | 
 
 
 
 
 | 169 | * Software is furnished to do so, subject to the following | 
 
 
 
 
 | 170 | * conditions: | 
 
 
 
 
 | 171 | * | 
 
 
 
 
 | 172 | * The above copyright notice and this permission notice shall be | 
 
 
 
 
 | 173 | * included in all copies or substantial portions of the Software. | 
 
 
 
 
 | 174 | * | 
 
 
 
 
 | 175 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 
 
 
 
 
 | 176 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | 
 
 
 
 
 | 177 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
 
 
 
 
 | 178 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | 
 
 
 
 
 | 179 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | 
 
 
 
 
 | 180 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | 
 
 
 
 
 | 181 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | 
 
 
 
 
 | 182 | * OTHER DEALINGS IN THE SOFTWARE. | 
 
 
 
 
 | 183 | */ |