Commit 91a54f483b001ab107214fe425594269b0610f8e

Authored by Josh Klontz
1 parent e753e4ed

Introduced QtUtils::euclideanLength(QPointF) and refactored source into a 'using namespace QtUtils'

openbr/core/qtutils.cpp
... ... @@ -33,7 +33,10 @@
33 33  
34 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 41 dir = QDir(dir.canonicalPath());
39 42  
... ... @@ -51,7 +54,7 @@ QStringList QtUtils::getFiles(QDir dir, bool recursive)
51 54 return files;
52 55 }
53 56  
54   -QStringList QtUtils::getFiles(const QString &regexp)
  57 +QStringList getFiles(const QString &regexp)
55 58 {
56 59 QFileInfo fileInfo(regexp);
57 60 QDir dir(fileInfo.dir());
... ... @@ -65,14 +68,14 @@ QStringList QtUtils::getFiles(const QString &regexp)
65 68 return files;
66 69 }
67 70  
68   -QStringList QtUtils::readLines(const QString &file)
  71 +QStringList readLines(const QString &file)
69 72 {
70 73 QStringList lines;
71 74 readFile(file, lines);
72 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 80 QByteArray data;
78 81 readFile(file, data);
... ... @@ -81,7 +84,7 @@ void QtUtils::readFile(const QString &file, QStringList &lines)
81 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 89 QFile f(file);
87 90 if (!f.open(QFile::ReadOnly)) {
... ... @@ -93,17 +96,17 @@ void QtUtils::readFile(const QString &file, QByteArray &data, bool uncompress)
93 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 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 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 111 if (file.isEmpty()) return;
109 112 const QString baseName = QFileInfo(file).baseName();
... ... @@ -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 130 touchDir(QFileInfo(dst));
128 131 if (!QFile::copy(src, 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 139 if (dir.exists(".")) return;
137 140 if (!dir.mkpath("."))
138 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 146 touchDir(QFileInfo(file));
144 147 }
145 148  
146   -void QtUtils::touchDir(const QFileInfo &fileInfo)
  149 +void touchDir(const QFileInfo &fileInfo)
147 150 {
148 151 touchDir(fileInfo.dir());
149 152 }
150 153  
151   -void QtUtils::emptyDir(QDir &dir)
  154 +void emptyDir(QDir &dir)
152 155 {
153 156 foreach (const QString &folder, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks)) {
154 157 QDir subdir(dir);
... ... @@ -166,13 +169,13 @@ void QtUtils::emptyDir(QDir &dir)
166 169 dir.remove(symlink);
167 170 }
168 171  
169   -void QtUtils::deleteDir(QDir &dir)
  172 +void deleteDir(QDir &dir)
170 173 {
171 174 emptyDir(dir);
172 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 180 if (QFileInfo(file).exists()) return file;
178 181 if (QFileInfo(alt).exists()) return alt;
... ... @@ -180,28 +183,28 @@ QString QtUtils::find(const QString &file, const QString &alt)
180 183 return "";
181 184 }
182 185  
183   -bool QtUtils::toBool(const QString &string)
  186 +bool toBool(const QString &string)
184 187 {
185 188 bool ok;
186 189 bool result = (bool)string.toInt(&ok); if (!ok) qFatal("Expected integer value, got %s.", qPrintable(string));
187 190 return result;
188 191 }
189 192  
190   -int QtUtils::toInt(const QString &string)
  193 +int toInt(const QString &string)
191 194 {
192 195 bool ok;
193 196 int result = string.toInt(&ok); if (!ok) qFatal("Expected integer value, got %s.", qPrintable(string));
194 197 return result;
195 198 }
196 199  
197   -float QtUtils::toFloat(const QString &string)
  200 +float toFloat(const QString &string)
198 201 {
199 202 bool ok;
200 203 float result = string.toFloat(&ok); if (!ok) qFatal("Expected floating point value, got %s.", qPrintable(string));
201 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 209 QList<float> floats;
207 210 bool ok;
... ... @@ -212,7 +215,7 @@ QList&lt;float&gt; QtUtils::toFloats(const QStringList &amp;strings)
212 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 220 QStringList result; result.reserve(values.size());
218 221 foreach (float value, values)
... ... @@ -220,7 +223,7 @@ QStringList QtUtils::toStringList(const QList&lt;float&gt; &amp;values)
220 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 228 QStringList result;
226 229 foreach (const std::string &string, string_list)
... ... @@ -228,7 +231,7 @@ QStringList QtUtils::toStringList(const std::vector&lt;std::string&gt; &amp;string_list)
228 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 236 QStringList result;
234 237 for (int i=0; i<num_strings; i++)
... ... @@ -236,13 +239,13 @@ QStringList QtUtils::toStringList(int num_strings, const char *strings[])
236 239 return result;
237 240 }
238 241  
239   -QString QtUtils::shortTextHash(QString string)
  242 +QString shortTextHash(QString string)
240 243 {
241 244 string.remove(QRegExp("[{}<>&]"));
242 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 250 if (args.isEmpty()) return QStringList();
248 251  
... ... @@ -295,7 +298,7 @@ QStringList QtUtils::parse(QString args, char split, bool *ok)
295 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 303 if (max == -1) max = std::numeric_limits<int>::max();
301 304 if (max == 0) max = min;
... ... @@ -303,7 +306,7 @@ void QtUtils::checkArgsSize(const QString &amp;name, const QStringList &amp;args, int mi
303 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 311 if (string.startsWith('(') && string.endsWith(')')) {
309 312 bool okParse;
... ... @@ -324,7 +327,7 @@ QPointF QtUtils::toPoint(const QString &amp;string, bool *ok)
324 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 332 if (string.startsWith('(') && string.endsWith(')')) {
330 333 bool okParse;
... ... @@ -347,7 +350,7 @@ QRectF QtUtils::toRect(const QString &amp;string, bool *ok)
347 350 return QRectF();
348 351 }
349 352  
350   -QStringList QtUtils::naturalSort(const QStringList &strings)
  353 +QStringList naturalSort(const QStringList &strings)
351 354 {
352 355 QList<std::string> stdStrings; stdStrings.reserve(strings.size());
353 356 foreach (const QString &string, strings)
... ... @@ -362,7 +365,7 @@ QStringList QtUtils::naturalSort(const QStringList &amp;strings)
362 365 return result;
363 366 }
364 367  
365   -bool QtUtils::runRScript(const QString &file)
  368 +bool runRScript(const QString &file)
366 369 {
367 370 QProcess RScript;
368 371 RScript.start("Rscript", QStringList() << file);
... ... @@ -374,7 +377,7 @@ bool QtUtils::runRScript(const QString &amp;file)
374 377 return result;
375 378 }
376 379  
377   -bool QtUtils::runDot(const QString &file)
  380 +bool runDot(const QString &file)
378 381 {
379 382 QProcess dot;
380 383 dot.start("dot -Tpdf -O " + file);
... ... @@ -382,7 +385,7 @@ bool QtUtils::runDot(const QString &amp;file)
382 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 390 #ifndef BR_EMBEDDED
388 391 (void) file;
... ... @@ -393,7 +396,7 @@ void QtUtils::showFile(const QString &amp;file)
393 396 #endif // BR_EMBEDDED
394 397 }
395 398  
396   -QString QtUtils::toString(const QVariant &variant)
  399 +QString toString(const QVariant &variant)
397 400 {
398 401 if (variant.canConvert(QVariant::String)) return variant.toString();
399 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 &amp;variant)
404 407 QString::number(qvariant_cast<QRectF>(variant).height()));
405 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 73  
74 74 /**** Variant Utilities ****/
75 75 QString toString(const QVariant &variant);
  76 +
  77 + /**** Point Utilities ****/
  78 + float euclideanLength(const QPointF &point);
76 79 }
77 80  
78 81 #endif // __QTUTILS_H
... ...