Changeset View
Changeset View
Standalone View
Standalone View
src/lib/marble/geodata/data/GeoDataCoordinates.cpp
Show All 36 Lines | |||||
37 | { | 37 | { | ||
38 | 38 | | |||
39 | // Helper class for GeoDataCoordinates::fromString(...) | 39 | // Helper class for GeoDataCoordinates::fromString(...) | ||
40 | class LonLatParser | 40 | class LonLatParser | ||
41 | { | 41 | { | ||
42 | private: | 42 | private: | ||
43 | enum DirPosition { PrefixDir, PostfixDir }; | 43 | enum DirPosition { PrefixDir, PostfixDir }; | ||
44 | 44 | | |||
45 | /** | ||||
46 | * Parses the double value from the input string in system locale | ||||
47 | * if it contains the system locale decimalpoint char, | ||||
48 | * otherwise parses it in the C locale. | ||||
49 | */ | ||||
50 | static double parseDouble(const QString& input); | ||||
45 | static QString createDecimalPointExp(); | 51 | static QString createDecimalPointExp(); | ||
46 | static QString regExp( const QString& string ); | 52 | static QString regExp( const QString& string ); | ||
47 | static void getLocaleList( QStringList& localeList, const QString& localeListString, | 53 | static void getLocaleList( QStringList& localeList, const QString& localeListString, | ||
48 | const QLatin1String& placeholder, const QString& separator ); | 54 | const QLatin1String& placeholder, const QString& separator ); | ||
49 | static bool isDirection( const QString& input, const QString& direction); | 55 | static bool isDirection( const QString& input, const QString& direction); | ||
50 | static bool isDirection( const QString& input, const QStringList& directions); | 56 | static bool isDirection( const QString& input, const QStringList& directions); | ||
51 | static bool isOneOfDirections( const QString& input, | 57 | static bool isOneOfDirections( const QString& input, | ||
52 | const QString& firstDirection, | 58 | const QString& firstDirection, | ||
▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Line(s) | 252 | { | |||
250 | { | 256 | { | ||
251 | const QString numberCapExp = QString::fromLatin1( | 257 | const QString numberCapExp = QString::fromLatin1( | ||
252 | "([-+]?\\d{1,3}%1?\\d*(?:[eE][+-]?\\d+)?)(?:,|;|\\s)\\s*" | 258 | "([-+]?\\d{1,3}%1?\\d*(?:[eE][+-]?\\d+)?)(?:,|;|\\s)\\s*" | ||
253 | "([-+]?\\d{1,3}%1?\\d*(?:[eE][+-]?\\d+)?)" | 259 | "([-+]?\\d{1,3}%1?\\d*(?:[eE][+-]?\\d+)?)" | ||
254 | ).arg(m_decimalPointExp); | 260 | ).arg(m_decimalPointExp); | ||
255 | 261 | | |||
256 | const QRegExp regex = QRegExp( numberCapExp ); | 262 | const QRegExp regex = QRegExp( numberCapExp ); | ||
257 | if( regex.exactMatch(input) ) { | 263 | if( regex.exactMatch(input) ) { | ||
258 | m_lon = regex.cap(2).toDouble(); | 264 | m_lon = parseDouble(regex.cap(2)); | ||
259 | m_lat = regex.cap(1).toDouble(); | 265 | m_lat = parseDouble(regex.cap(1)); | ||
260 | 266 | | |||
261 | return true; | 267 | return true; | ||
262 | } | 268 | } | ||
263 | } | 269 | } | ||
264 | 270 | | |||
265 | initAll(); | 271 | initAll(); | ||
266 | 272 | | |||
267 | if ( tryMatchFromD( input, PostfixDir ) ) { | 273 | if ( tryMatchFromD( input, PostfixDir ) ) { | ||
▲ Show 20 Lines • Show All 154 Lines • ▼ Show 20 Line(s) | 395 | { | |||
422 | m_lon = degreeValueFromD( regex, isDir1LonDir ? valueStartIndex1 : valueStartIndex2, | 428 | m_lon = degreeValueFromD( regex, isDir1LonDir ? valueStartIndex1 : valueStartIndex2, | ||
423 | isLonDirPosHemisphere ); | 429 | isLonDirPosHemisphere ); | ||
424 | m_lat = degreeValueFromD( regex, isDir1LonDir ? valueStartIndex2 : valueStartIndex1, | 430 | m_lat = degreeValueFromD( regex, isDir1LonDir ? valueStartIndex2 : valueStartIndex1, | ||
425 | isLatDirPosHemisphere ); | 431 | isLatDirPosHemisphere ); | ||
426 | 432 | | |||
427 | return true; | 433 | return true; | ||
428 | } | 434 | } | ||
429 | 435 | | |||
436 | double LonLatParser::parseDouble(const QString& input) | ||||
437 | { | ||||
438 | // Decide by decimalpoint if system locale or C locale should be tried. | ||||
439 | // Otherwise if first trying with a system locale when the string is in C locale, | ||||
440 | // the "." might be misinterpreted as thousands group separator and thus a wrong | ||||
441 | // value yielded | ||||
442 | QLocale locale = QLocale::system(); | ||||
443 | return input.contains(locale.decimalPoint()) ? locale.toDouble(input) : input.toDouble(); | ||||
444 | } | ||||
445 | | ||||
430 | QString LonLatParser::createDecimalPointExp() | 446 | QString LonLatParser::createDecimalPointExp() | ||
431 | { | 447 | { | ||
432 | const QChar decimalPoint = QLocale::system().decimalPoint(); | 448 | const QChar decimalPoint = QLocale::system().decimalPoint(); | ||
433 | 449 | | |||
434 | return (decimalPoint == QLatin1Char('.')) ? QString::fromLatin1("\\.") : | 450 | return (decimalPoint == QLatin1Char('.')) ? QString::fromLatin1("\\.") : | ||
435 | QLatin1String("[.") + decimalPoint + QLatin1Char(']'); | 451 | QLatin1String("[.") + decimalPoint + QLatin1Char(']'); | ||
436 | } | 452 | } | ||
437 | 453 | | |||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Line(s) | |||||
516 | } | 532 | } | ||
517 | 533 | | |||
518 | 534 | | |||
519 | qreal LonLatParser::degreeValueFromDMS( const QRegExp& regex, int c, bool isPosHemisphere ) | 535 | qreal LonLatParser::degreeValueFromDMS( const QRegExp& regex, int c, bool isPosHemisphere ) | ||
520 | { | 536 | { | ||
521 | const bool isNegativeValue = (regex.cap( c++ ) == QLatin1String("-")); | 537 | const bool isNegativeValue = (regex.cap( c++ ) == QLatin1String("-")); | ||
522 | const uint degree = regex.cap( c++ ).toUInt(); | 538 | const uint degree = regex.cap( c++ ).toUInt(); | ||
523 | const uint minutes = regex.cap( c++ ).toUInt(); | 539 | const uint minutes = regex.cap( c++ ).toUInt(); | ||
524 | const qreal seconds = regex.cap( c ).toDouble(); | 540 | const qreal seconds = parseDouble(regex.cap( c )); | ||
525 | 541 | | |||
526 | qreal result = degree + (minutes*MIN2HOUR) + (seconds*SEC2HOUR); | 542 | qreal result = degree + (minutes*MIN2HOUR) + (seconds*SEC2HOUR); | ||
527 | 543 | | |||
528 | if (isNegativeValue) | 544 | if (isNegativeValue) | ||
529 | result *= -1; | 545 | result *= -1; | ||
530 | if (! isPosHemisphere) | 546 | if (! isPosHemisphere) | ||
531 | result *= -1; | 547 | result *= -1; | ||
532 | 548 | | |||
533 | return result; | 549 | return result; | ||
534 | } | 550 | } | ||
535 | 551 | | |||
536 | qreal LonLatParser::degreeValueFromDM( const QRegExp& regex, int c, bool isPosHemisphere ) | 552 | qreal LonLatParser::degreeValueFromDM( const QRegExp& regex, int c, bool isPosHemisphere ) | ||
537 | { | 553 | { | ||
538 | const bool isNegativeValue = (regex.cap( c++ ) == QLatin1String("-")); | 554 | const bool isNegativeValue = (regex.cap( c++ ) == QLatin1String("-")); | ||
539 | const uint degree = regex.cap( c++ ).toUInt(); | 555 | const uint degree = regex.cap( c++ ).toUInt(); | ||
540 | const qreal minutes = regex.cap( c ).toDouble(); | 556 | const qreal minutes = parseDouble(regex.cap( c )); | ||
541 | 557 | | |||
542 | qreal result = degree + (minutes*MIN2HOUR); | 558 | qreal result = degree + (minutes*MIN2HOUR); | ||
543 | 559 | | |||
544 | if (isNegativeValue) | 560 | if (isNegativeValue) | ||
545 | result *= -1; | 561 | result *= -1; | ||
546 | if (! isPosHemisphere) | 562 | if (! isPosHemisphere) | ||
547 | result *= -1; | 563 | result *= -1; | ||
548 | 564 | | |||
549 | return result; | 565 | return result; | ||
550 | } | 566 | } | ||
551 | 567 | | |||
552 | qreal LonLatParser::degreeValueFromD( const QRegExp& regex, int c, bool isPosHemisphere ) | 568 | qreal LonLatParser::degreeValueFromD( const QRegExp& regex, int c, bool isPosHemisphere ) | ||
553 | { | 569 | { | ||
554 | qreal result = regex.cap( c ).toDouble(); | 570 | qreal result = parseDouble(regex.cap( c )); | ||
555 | 571 | | |||
556 | if (! isPosHemisphere) | 572 | if (! isPosHemisphere) | ||
557 | result *= -1; | 573 | result *= -1; | ||
558 | 574 | | |||
559 | return result; | 575 | return result; | ||
560 | } | 576 | } | ||
561 | 577 | | |||
562 | bool LonLatParser::isCorrectDirections(const QString& dir1, const QString& dir2, | 578 | bool LonLatParser::isCorrectDirections(const QString& dir1, const QString& dir2, | ||
▲ Show 20 Lines • Show All 1144 Lines • Show Last 20 Lines |