File indexing completed on 2024-05-12 03:50:19

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2004-2007 Torsten Rahn <tackat@kde.org>
0004 // SPDX-FileCopyrightText: 2007-2008 Inge Wallin <ingwa@kde.org>
0005 // SPDX-FileCopyrightText: 2008 Patrick Spendrin <ps_ml@gmx.de>
0006 // SPDX-FileCopyrightText: 2011 Friedrich W. H. Kossebau <kossebau@kde.org>
0007 // SPDX-FileCopyrightText: 2011 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0008 // SPDX-FileCopyrightText: 2015 Alejandro Garcia Montoro <alejandro.garciamontoro@gmail.com>
0009 //
0010 
0011 #ifndef MARBLE_LONLATPARSER_P_H
0012 #define MARBLE_LONLATPARSER_P_H
0013 
0014 #include <QStringList>
0015 
0016 class QRegularExpressionMatch;
0017 
0018 namespace Marble
0019 {
0020 
0021 // Helper class for GeoDataCoordinates::fromString(...)
0022 class LonLatParser
0023 {
0024 private:
0025     enum DirPosition { PrefixDir, PostfixDir };
0026 
0027     /**
0028      * Parses the double value from the input string in system locale
0029      * if it contains the system locale decimalpoint char,
0030      * otherwise parses it in the C locale.
0031      */
0032     static double parseDouble(const QString& input);
0033     static QString createDecimalPointExp();
0034     static void getLocaleList(QStringList& localeList, const QString& localeListString,
0035                               const QLatin1String& placeholder, const QString& separator);
0036     static bool isDirection(const QString& input, const QString& direction);
0037     static bool isDirection(const QString& input, const QStringList& directions);
0038     static bool isOneOfDirections(const QString& input,
0039                                   const QString& firstDirection,
0040                                   const QString& secondDirection,
0041                                   bool& isFirstDirection);
0042     static bool isOneOfDirections(const QString& input,
0043                                   const QStringList& firstDirections,
0044                                   const QStringList& secondDirections,
0045                                   bool& isFirstDirection);
0046 
0047     /**
0048      * function template for the function calculating the degree value from
0049      * the captured texts with the degree, the minutes, the seconds and the signedness
0050      * (or less, depending on what the function actually expects)
0051      * @param regexMatch the regexp match to take the texts from
0052      * @param c the index in the list of captured texts of @p regex to start with
0053      * @param isPosHemisphere if the texts of the degree value are relative to the pos hemisphere
0054      * @return the calculated degree value
0055      */
0056     static qreal degreeValueFromDMS(const QRegularExpressionMatch& regexMatch, int c, bool isPosHemisphere);
0057     static qreal degreeValueFromDM(const QRegularExpressionMatch& regexMatch, int c, bool isPosHemisphere);
0058     static qreal degreeValueFromD(const QRegularExpressionMatch& regexMatch, int c, bool isPosHemisphere);
0059 
0060 public:
0061     LonLatParser();
0062 
0063     /**
0064      * @brief parses the complete @p input string and sets the lon and lat properties if successful.
0065      * @param input the string to parse, must not have other content than the coordinates
0066      * @return @c true on successful parsing, @c false otherwise.
0067      */
0068     bool parse(const QString& input);
0069 
0070     /**
0071      * @brief return the lon value from the last successful parsing
0072      */
0073     qreal lon() const { return m_lon; }
0074 
0075     /**
0076      * @brief return the lat value from the last successful parsing
0077      */
0078     qreal lat() const { return m_lat; }
0079 
0080 private:
0081     /**
0082      * @brief tries to parse the input with the given reg expression and get the lon and lat values
0083      * @param input the string to parse, must not have other content than the coordinates
0084      * @param dirPosition position of the dir in the list of captured texts
0085      * @return @c true on successful parsing, @c false otherwise.
0086      */
0087     bool tryMatchFromDms(const QString& input, DirPosition dirPosition);
0088     bool tryMatchFromDm(const QString& input, DirPosition dirPosition);
0089     bool tryMatchFromD(const QString& input, DirPosition dirPosition);
0090 
0091     /**
0092      * @brief initializes also all properties which only need to be lazily initialized
0093      */
0094     void initAll();
0095 
0096     /**
0097      * @brief checks if the both passed directions are correct, also returns more data about them
0098      * @param dir1 first direction string
0099      * @param dir1 second direction string
0100      * @param isDir1LonDir is set to @c true if first direction string is a longitude direction,
0101      *                     @c false otherwise
0102      * @param isLonDirPosHemisphere is set to @c true if longitude direction is in positive hemisphere,
0103      *                              @c false otherwise
0104      * @param isLatDirPosHemisphere is set to @c true if latitude direction is in positive hemisphere,
0105      *                              @c false otherwise
0106      * @return @c true if @p dir1 and @p dir2 are correct, @c false otherwise.
0107      */
0108     bool isCorrectDirections(const QString& dir1, const QString& dir2,
0109                              bool& isDir1LonDir,
0110                              bool& isLonDirPosHemisphere, bool& isLatDirPosHemisphere) const;
0111     bool isLocaleLonDirection(const QString& input,
0112                               bool& isDirPosHemisphere) const;
0113     bool isLocaleLatDirection(const QString& input,
0114                               bool& isDirPosHemisphere) const;
0115     bool isLonDirection(const QString& input,
0116                         bool& isDirPosHemisphere) const;
0117     bool isLatDirection(const QString& input,
0118                         bool& isDirPosHemisphere) const;
0119 
0120 private:
0121     qreal m_lon;
0122     qreal m_lat;
0123 
0124 private: // helper values
0125     const QString m_north;
0126     const QString m_east;
0127     const QString m_south;
0128     const QString m_west;
0129 
0130     const QString m_decimalPointExp;
0131 
0132 private: // helper value, lazily set, in initAll();
0133     QStringList m_northLocale;
0134     QStringList m_eastLocale;
0135     QStringList m_southLocale;
0136     QStringList m_westLocale;
0137 
0138     QStringList m_degreeLocale;
0139     QStringList m_minutesLocale;
0140     QStringList m_secondsLocale;
0141 
0142     QString m_dirCapExp;
0143     QString m_degreeExp;
0144     QString m_minutesExp;
0145     QString m_secondsExp;
0146 };
0147 
0148 }
0149 
0150 #endif