Commit 91a54f483b001ab107214fe425594269b0610f8e
1 parent
e753e4ed
Introduced QtUtils::euclideanLength(QPointF) and refactored source into a 'using namespace QtUtils'
Showing
2 changed files
with
46 additions
and
32 deletions
openbr/core/qtutils.cpp
| @@ -33,7 +33,10 @@ | @@ -33,7 +33,10 @@ | ||
| 33 | 33 | ||
| 34 | using namespace br; | 34 | using namespace br; |
| 35 | 35 | ||
| 36 | -QStringList QtUtils::getFiles(QDir dir, bool recursive) | 36 | +namespace QtUtils |
| 37 | +{ | ||
| 38 | + | ||
| 39 | +QStringList getFiles(QDir dir, bool recursive) | ||
| 37 | { | 40 | { |
| 38 | dir = QDir(dir.canonicalPath()); | 41 | dir = QDir(dir.canonicalPath()); |
| 39 | 42 | ||
| @@ -51,7 +54,7 @@ QStringList QtUtils::getFiles(QDir dir, bool recursive) | @@ -51,7 +54,7 @@ QStringList QtUtils::getFiles(QDir dir, bool recursive) | ||
| 51 | return files; | 54 | return files; |
| 52 | } | 55 | } |
| 53 | 56 | ||
| 54 | -QStringList QtUtils::getFiles(const QString ®exp) | 57 | +QStringList getFiles(const QString ®exp) |
| 55 | { | 58 | { |
| 56 | QFileInfo fileInfo(regexp); | 59 | QFileInfo fileInfo(regexp); |
| 57 | QDir dir(fileInfo.dir()); | 60 | QDir dir(fileInfo.dir()); |
| @@ -65,14 +68,14 @@ QStringList QtUtils::getFiles(const QString &regexp) | @@ -65,14 +68,14 @@ QStringList QtUtils::getFiles(const QString &regexp) | ||
| 65 | return files; | 68 | return files; |
| 66 | } | 69 | } |
| 67 | 70 | ||
| 68 | -QStringList QtUtils::readLines(const QString &file) | 71 | +QStringList readLines(const QString &file) |
| 69 | { | 72 | { |
| 70 | QStringList lines; | 73 | QStringList lines; |
| 71 | readFile(file, lines); | 74 | readFile(file, lines); |
| 72 | return lines; | 75 | return lines; |
| 73 | } | 76 | } |
| 74 | 77 | ||
| 75 | -void QtUtils::readFile(const QString &file, QStringList &lines) | 78 | +void readFile(const QString &file, QStringList &lines) |
| 76 | { | 79 | { |
| 77 | QByteArray data; | 80 | QByteArray data; |
| 78 | readFile(file, data); | 81 | readFile(file, data); |
| @@ -81,7 +84,7 @@ void QtUtils::readFile(const QString &file, QStringList &lines) | @@ -81,7 +84,7 @@ void QtUtils::readFile(const QString &file, QStringList &lines) | ||
| 81 | lines[i] = lines[i].simplified(); | 84 | lines[i] = lines[i].simplified(); |
| 82 | } | 85 | } |
| 83 | 86 | ||
| 84 | -void QtUtils::readFile(const QString &file, QByteArray &data, bool uncompress) | 87 | +void readFile(const QString &file, QByteArray &data, bool uncompress) |
| 85 | { | 88 | { |
| 86 | QFile f(file); | 89 | QFile f(file); |
| 87 | if (!f.open(QFile::ReadOnly)) { | 90 | if (!f.open(QFile::ReadOnly)) { |
| @@ -93,17 +96,17 @@ void QtUtils::readFile(const QString &file, QByteArray &data, bool uncompress) | @@ -93,17 +96,17 @@ void QtUtils::readFile(const QString &file, QByteArray &data, bool uncompress) | ||
| 93 | f.close(); | 96 | f.close(); |
| 94 | } | 97 | } |
| 95 | 98 | ||
| 96 | -void QtUtils::writeFile(const QString &file, const QStringList &lines) | 99 | +void writeFile(const QString &file, const QStringList &lines) |
| 97 | { | 100 | { |
| 98 | writeFile(file, lines.join("\n")); | 101 | writeFile(file, lines.join("\n")); |
| 99 | } | 102 | } |
| 100 | 103 | ||
| 101 | -void QtUtils::writeFile(const QString &file, const QString &data) | 104 | +void writeFile(const QString &file, const QString &data) |
| 102 | { | 105 | { |
| 103 | writeFile(file, data.toLocal8Bit()); | 106 | writeFile(file, data.toLocal8Bit()); |
| 104 | } | 107 | } |
| 105 | 108 | ||
| 106 | -void QtUtils::writeFile(const QString &file, const QByteArray &data, int compression) | 109 | +void writeFile(const QString &file, const QByteArray &data, int compression) |
| 107 | { | 110 | { |
| 108 | if (file.isEmpty()) return; | 111 | if (file.isEmpty()) return; |
| 109 | const QString baseName = QFileInfo(file).baseName(); | 112 | const QString baseName = QFileInfo(file).baseName(); |
| @@ -122,7 +125,7 @@ void QtUtils::writeFile(const QString &file, const QByteArray &data, int compres | @@ -122,7 +125,7 @@ void QtUtils::writeFile(const QString &file, const QByteArray &data, int compres | ||
| 122 | } | 125 | } |
| 123 | } | 126 | } |
| 124 | 127 | ||
| 125 | -void QtUtils::copyFile(const QString &src, const QString &dst) | 128 | +void copyFile(const QString &src, const QString &dst) |
| 126 | { | 129 | { |
| 127 | touchDir(QFileInfo(dst)); | 130 | touchDir(QFileInfo(dst)); |
| 128 | if (!QFile::copy(src, dst)) { | 131 | if (!QFile::copy(src, dst)) { |
| @@ -131,24 +134,24 @@ void QtUtils::copyFile(const QString &src, const QString &dst) | @@ -131,24 +134,24 @@ void QtUtils::copyFile(const QString &src, const QString &dst) | ||
| 131 | } | 134 | } |
| 132 | } | 135 | } |
| 133 | 136 | ||
| 134 | -void QtUtils::touchDir(const QDir &dir) | 137 | +void touchDir(const QDir &dir) |
| 135 | { | 138 | { |
| 136 | if (dir.exists(".")) return; | 139 | if (dir.exists(".")) return; |
| 137 | if (!dir.mkpath(".")) | 140 | if (!dir.mkpath(".")) |
| 138 | qFatal("Unable to create path to dir %s", qPrintable(dir.absolutePath())); | 141 | qFatal("Unable to create path to dir %s", qPrintable(dir.absolutePath())); |
| 139 | } | 142 | } |
| 140 | 143 | ||
| 141 | -void QtUtils::touchDir(const QFile &file) | 144 | +void touchDir(const QFile &file) |
| 142 | { | 145 | { |
| 143 | touchDir(QFileInfo(file)); | 146 | touchDir(QFileInfo(file)); |
| 144 | } | 147 | } |
| 145 | 148 | ||
| 146 | -void QtUtils::touchDir(const QFileInfo &fileInfo) | 149 | +void touchDir(const QFileInfo &fileInfo) |
| 147 | { | 150 | { |
| 148 | touchDir(fileInfo.dir()); | 151 | touchDir(fileInfo.dir()); |
| 149 | } | 152 | } |
| 150 | 153 | ||
| 151 | -void QtUtils::emptyDir(QDir &dir) | 154 | +void emptyDir(QDir &dir) |
| 152 | { | 155 | { |
| 153 | foreach (const QString &folder, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks)) { | 156 | foreach (const QString &folder, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks)) { |
| 154 | QDir subdir(dir); | 157 | QDir subdir(dir); |
| @@ -166,13 +169,13 @@ void QtUtils::emptyDir(QDir &dir) | @@ -166,13 +169,13 @@ void QtUtils::emptyDir(QDir &dir) | ||
| 166 | dir.remove(symlink); | 169 | dir.remove(symlink); |
| 167 | } | 170 | } |
| 168 | 171 | ||
| 169 | -void QtUtils::deleteDir(QDir &dir) | 172 | +void deleteDir(QDir &dir) |
| 170 | { | 173 | { |
| 171 | emptyDir(dir); | 174 | emptyDir(dir); |
| 172 | dir.rmdir("."); | 175 | dir.rmdir("."); |
| 173 | } | 176 | } |
| 174 | 177 | ||
| 175 | -QString QtUtils::find(const QString &file, const QString &alt) | 178 | +QString find(const QString &file, const QString &alt) |
| 176 | { | 179 | { |
| 177 | if (QFileInfo(file).exists()) return file; | 180 | if (QFileInfo(file).exists()) return file; |
| 178 | if (QFileInfo(alt).exists()) return alt; | 181 | if (QFileInfo(alt).exists()) return alt; |
| @@ -180,28 +183,28 @@ QString QtUtils::find(const QString &file, const QString &alt) | @@ -180,28 +183,28 @@ QString QtUtils::find(const QString &file, const QString &alt) | ||
| 180 | return ""; | 183 | return ""; |
| 181 | } | 184 | } |
| 182 | 185 | ||
| 183 | -bool QtUtils::toBool(const QString &string) | 186 | +bool toBool(const QString &string) |
| 184 | { | 187 | { |
| 185 | bool ok; | 188 | bool ok; |
| 186 | bool result = (bool)string.toInt(&ok); if (!ok) qFatal("Expected integer value, got %s.", qPrintable(string)); | 189 | bool result = (bool)string.toInt(&ok); if (!ok) qFatal("Expected integer value, got %s.", qPrintable(string)); |
| 187 | return result; | 190 | return result; |
| 188 | } | 191 | } |
| 189 | 192 | ||
| 190 | -int QtUtils::toInt(const QString &string) | 193 | +int toInt(const QString &string) |
| 191 | { | 194 | { |
| 192 | bool ok; | 195 | bool ok; |
| 193 | int result = string.toInt(&ok); if (!ok) qFatal("Expected integer value, got %s.", qPrintable(string)); | 196 | int result = string.toInt(&ok); if (!ok) qFatal("Expected integer value, got %s.", qPrintable(string)); |
| 194 | return result; | 197 | return result; |
| 195 | } | 198 | } |
| 196 | 199 | ||
| 197 | -float QtUtils::toFloat(const QString &string) | 200 | +float toFloat(const QString &string) |
| 198 | { | 201 | { |
| 199 | bool ok; | 202 | bool ok; |
| 200 | float result = string.toFloat(&ok); if (!ok) qFatal("Expected floating point value, got %s.", qPrintable(string)); | 203 | float result = string.toFloat(&ok); if (!ok) qFatal("Expected floating point value, got %s.", qPrintable(string)); |
| 201 | return result; | 204 | return result; |
| 202 | } | 205 | } |
| 203 | 206 | ||
| 204 | -QList<float> QtUtils::toFloats(const QStringList &strings) | 207 | +QList<float> toFloats(const QStringList &strings) |
| 205 | { | 208 | { |
| 206 | QList<float> floats; | 209 | QList<float> floats; |
| 207 | bool ok; | 210 | bool ok; |
| @@ -212,7 +215,7 @@ QList<float> QtUtils::toFloats(const QStringList &strings) | @@ -212,7 +215,7 @@ QList<float> QtUtils::toFloats(const QStringList &strings) | ||
| 212 | return floats; | 215 | return floats; |
| 213 | } | 216 | } |
| 214 | 217 | ||
| 215 | -QStringList QtUtils::toStringList(const QList<float> &values) | 218 | +QStringList toStringList(const QList<float> &values) |
| 216 | { | 219 | { |
| 217 | QStringList result; result.reserve(values.size()); | 220 | QStringList result; result.reserve(values.size()); |
| 218 | foreach (float value, values) | 221 | foreach (float value, values) |
| @@ -220,7 +223,7 @@ QStringList QtUtils::toStringList(const QList<float> &values) | @@ -220,7 +223,7 @@ QStringList QtUtils::toStringList(const QList<float> &values) | ||
| 220 | return result; | 223 | return result; |
| 221 | } | 224 | } |
| 222 | 225 | ||
| 223 | -QStringList QtUtils::toStringList(const std::vector<std::string> &string_list) | 226 | +QStringList toStringList(const std::vector<std::string> &string_list) |
| 224 | { | 227 | { |
| 225 | QStringList result; | 228 | QStringList result; |
| 226 | foreach (const std::string &string, string_list) | 229 | foreach (const std::string &string, string_list) |
| @@ -228,7 +231,7 @@ QStringList QtUtils::toStringList(const std::vector<std::string> &string_list) | @@ -228,7 +231,7 @@ QStringList QtUtils::toStringList(const std::vector<std::string> &string_list) | ||
| 228 | return result; | 231 | return result; |
| 229 | } | 232 | } |
| 230 | 233 | ||
| 231 | -QStringList QtUtils::toStringList(int num_strings, const char *strings[]) | 234 | +QStringList toStringList(int num_strings, const char *strings[]) |
| 232 | { | 235 | { |
| 233 | QStringList result; | 236 | QStringList result; |
| 234 | for (int i=0; i<num_strings; i++) | 237 | for (int i=0; i<num_strings; i++) |
| @@ -236,13 +239,13 @@ QStringList QtUtils::toStringList(int num_strings, const char *strings[]) | @@ -236,13 +239,13 @@ QStringList QtUtils::toStringList(int num_strings, const char *strings[]) | ||
| 236 | return result; | 239 | return result; |
| 237 | } | 240 | } |
| 238 | 241 | ||
| 239 | -QString QtUtils::shortTextHash(QString string) | 242 | +QString shortTextHash(QString string) |
| 240 | { | 243 | { |
| 241 | string.remove(QRegExp("[{}<>&]")); | 244 | string.remove(QRegExp("[{}<>&]")); |
| 242 | return QString(QCryptographicHash::hash(qPrintable(string), QCryptographicHash::Md5).toBase64()).remove(QRegExp("[^a-zA-Z1-9]")).left(6); | 245 | return QString(QCryptographicHash::hash(qPrintable(string), QCryptographicHash::Md5).toBase64()).remove(QRegExp("[^a-zA-Z1-9]")).left(6); |
| 243 | } | 246 | } |
| 244 | 247 | ||
| 245 | -QStringList QtUtils::parse(QString args, char split, bool *ok) | 248 | +QStringList parse(QString args, char split, bool *ok) |
| 246 | { | 249 | { |
| 247 | if (args.isEmpty()) return QStringList(); | 250 | if (args.isEmpty()) return QStringList(); |
| 248 | 251 | ||
| @@ -295,7 +298,7 @@ QStringList QtUtils::parse(QString args, char split, bool *ok) | @@ -295,7 +298,7 @@ QStringList QtUtils::parse(QString args, char split, bool *ok) | ||
| 295 | return words; | 298 | return words; |
| 296 | } | 299 | } |
| 297 | 300 | ||
| 298 | -void QtUtils::checkArgsSize(const QString &name, const QStringList &args, int min, int max) | 301 | +void checkArgsSize(const QString &name, const QStringList &args, int min, int max) |
| 299 | { | 302 | { |
| 300 | if (max == -1) max = std::numeric_limits<int>::max(); | 303 | if (max == -1) max = std::numeric_limits<int>::max(); |
| 301 | if (max == 0) max = min; | 304 | if (max == 0) max = min; |
| @@ -303,7 +306,7 @@ void QtUtils::checkArgsSize(const QString &name, const QStringList &args, int mi | @@ -303,7 +306,7 @@ void QtUtils::checkArgsSize(const QString &name, const QStringList &args, int mi | ||
| 303 | if (args.size() > max) qFatal("%s expects no more than %d arguments, got %d", qPrintable(name), max, args.size()); | 306 | if (args.size() > max) qFatal("%s expects no more than %d arguments, got %d", qPrintable(name), max, args.size()); |
| 304 | } | 307 | } |
| 305 | 308 | ||
| 306 | -QPointF QtUtils::toPoint(const QString &string, bool *ok) | 309 | +QPointF toPoint(const QString &string, bool *ok) |
| 307 | { | 310 | { |
| 308 | if (string.startsWith('(') && string.endsWith(')')) { | 311 | if (string.startsWith('(') && string.endsWith(')')) { |
| 309 | bool okParse; | 312 | bool okParse; |
| @@ -324,7 +327,7 @@ QPointF QtUtils::toPoint(const QString &string, bool *ok) | @@ -324,7 +327,7 @@ QPointF QtUtils::toPoint(const QString &string, bool *ok) | ||
| 324 | return QPointF(); | 327 | return QPointF(); |
| 325 | } | 328 | } |
| 326 | 329 | ||
| 327 | -QRectF QtUtils::toRect(const QString &string, bool *ok) | 330 | +QRectF toRect(const QString &string, bool *ok) |
| 328 | { | 331 | { |
| 329 | if (string.startsWith('(') && string.endsWith(')')) { | 332 | if (string.startsWith('(') && string.endsWith(')')) { |
| 330 | bool okParse; | 333 | bool okParse; |
| @@ -347,7 +350,7 @@ QRectF QtUtils::toRect(const QString &string, bool *ok) | @@ -347,7 +350,7 @@ QRectF QtUtils::toRect(const QString &string, bool *ok) | ||
| 347 | return QRectF(); | 350 | return QRectF(); |
| 348 | } | 351 | } |
| 349 | 352 | ||
| 350 | -QStringList QtUtils::naturalSort(const QStringList &strings) | 353 | +QStringList naturalSort(const QStringList &strings) |
| 351 | { | 354 | { |
| 352 | QList<std::string> stdStrings; stdStrings.reserve(strings.size()); | 355 | QList<std::string> stdStrings; stdStrings.reserve(strings.size()); |
| 353 | foreach (const QString &string, strings) | 356 | foreach (const QString &string, strings) |
| @@ -362,7 +365,7 @@ QStringList QtUtils::naturalSort(const QStringList &strings) | @@ -362,7 +365,7 @@ QStringList QtUtils::naturalSort(const QStringList &strings) | ||
| 362 | return result; | 365 | return result; |
| 363 | } | 366 | } |
| 364 | 367 | ||
| 365 | -bool QtUtils::runRScript(const QString &file) | 368 | +bool runRScript(const QString &file) |
| 366 | { | 369 | { |
| 367 | QProcess RScript; | 370 | QProcess RScript; |
| 368 | RScript.start("Rscript", QStringList() << file); | 371 | RScript.start("Rscript", QStringList() << file); |
| @@ -374,7 +377,7 @@ bool QtUtils::runRScript(const QString &file) | @@ -374,7 +377,7 @@ bool QtUtils::runRScript(const QString &file) | ||
| 374 | return result; | 377 | return result; |
| 375 | } | 378 | } |
| 376 | 379 | ||
| 377 | -bool QtUtils::runDot(const QString &file) | 380 | +bool runDot(const QString &file) |
| 378 | { | 381 | { |
| 379 | QProcess dot; | 382 | QProcess dot; |
| 380 | dot.start("dot -Tpdf -O " + file); | 383 | dot.start("dot -Tpdf -O " + file); |
| @@ -382,7 +385,7 @@ bool QtUtils::runDot(const QString &file) | @@ -382,7 +385,7 @@ bool QtUtils::runDot(const QString &file) | ||
| 382 | return ((dot.exitCode() == 0) && (dot.error() == QProcess::UnknownError)); | 385 | return ((dot.exitCode() == 0) && (dot.error() == QProcess::UnknownError)); |
| 383 | } | 386 | } |
| 384 | 387 | ||
| 385 | -void QtUtils::showFile(const QString &file) | 388 | +void showFile(const QString &file) |
| 386 | { | 389 | { |
| 387 | #ifndef BR_EMBEDDED | 390 | #ifndef BR_EMBEDDED |
| 388 | (void) file; | 391 | (void) file; |
| @@ -393,7 +396,7 @@ void QtUtils::showFile(const QString &file) | @@ -393,7 +396,7 @@ void QtUtils::showFile(const QString &file) | ||
| 393 | #endif // BR_EMBEDDED | 396 | #endif // BR_EMBEDDED |
| 394 | } | 397 | } |
| 395 | 398 | ||
| 396 | -QString QtUtils::toString(const QVariant &variant) | 399 | +QString toString(const QVariant &variant) |
| 397 | { | 400 | { |
| 398 | if (variant.canConvert(QVariant::String)) return variant.toString(); | 401 | if (variant.canConvert(QVariant::String)) return variant.toString(); |
| 399 | else if(variant.canConvert(QVariant::PointF)) return QString("(%1,%2)").arg(QString::number(qvariant_cast<QPointF>(variant).x()), | 402 | else if(variant.canConvert(QVariant::PointF)) return QString("(%1,%2)").arg(QString::number(qvariant_cast<QPointF>(variant).x()), |
| @@ -404,3 +407,11 @@ QString QtUtils::toString(const QVariant &variant) | @@ -404,3 +407,11 @@ QString QtUtils::toString(const QVariant &variant) | ||
| 404 | QString::number(qvariant_cast<QRectF>(variant).height())); | 407 | QString::number(qvariant_cast<QRectF>(variant).height())); |
| 405 | return QString(); | 408 | return QString(); |
| 406 | } | 409 | } |
| 410 | + | ||
| 411 | +float euclideanLength(const QPointF &point) | ||
| 412 | +{ | ||
| 413 | + return sqrt(pow(point.x(), 2) + pow(point.y(), 2)); | ||
| 414 | +} | ||
| 415 | + | ||
| 416 | +} // namespace QtUtils | ||
| 417 | + |
openbr/core/qtutils.h
| @@ -73,6 +73,9 @@ namespace QtUtils | @@ -73,6 +73,9 @@ namespace QtUtils | ||
| 73 | 73 | ||
| 74 | /**** Variant Utilities ****/ | 74 | /**** Variant Utilities ****/ |
| 75 | QString toString(const QVariant &variant); | 75 | QString toString(const QVariant &variant); |
| 76 | + | ||
| 77 | + /**** Point Utilities ****/ | ||
| 78 | + float euclideanLength(const QPointF &point); | ||
| 76 | } | 79 | } |
| 77 | 80 | ||
| 78 | #endif // __QTUTILS_H | 81 | #endif // __QTUTILS_H |