QGIS API Documentation  3.6.0-Noosa (5873452)
qgsstringutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstringutils.h
3  ----------------
4  begin : June 2015
5  copyright : (C) 2015 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgis_core.h"
17 #include <QString>
18 #include <QRegExp>
19 #include <QList>
20 #include <QDomDocument>
21 #include <QFont> // for enum values
22 #include "qgis.h"
23 
24 #ifndef QGSSTRINGUTILS_H
25 #define QGSSTRINGUTILS_H
26 
27 
35 class CORE_EXPORT QgsStringReplacement
36 {
37 
38  public:
39 
47  QgsStringReplacement( const QString &match,
48  const QString &replacement,
49  bool caseSensitive = false,
50  bool wholeWordOnly = false );
51 
53  QString match() const { return mMatch; }
54 
56  QString replacement() const { return mReplacement; }
57 
59  bool caseSensitive() const { return mCaseSensitive; }
60 
62  bool wholeWordOnly() const { return mWholeWordOnly; }
63 
69  QString process( const QString &input ) const;
70 
71  bool operator==( const QgsStringReplacement &other )
72  {
73  return mMatch == other.mMatch
74  && mReplacement == other.mReplacement
75  && mCaseSensitive == other.mCaseSensitive
76  && mWholeWordOnly == other.mWholeWordOnly;
77  }
78 
83  QgsStringMap properties() const;
84 
89  static QgsStringReplacement fromProperties( const QgsStringMap &properties );
90 
91  private:
92 
93  QString mMatch;
94 
95  QString mReplacement;
96 
97  bool mCaseSensitive;
98 
99  bool mWholeWordOnly;
100 
101  QRegExp mRx;
102 };
103 
104 
113 {
114 
115  public:
116 
121  QgsStringReplacementCollection( const QList< QgsStringReplacement > &replacements = QList< QgsStringReplacement >() )
122  : mReplacements( replacements )
123  {}
124 
129  QList< QgsStringReplacement > replacements() const { return mReplacements; }
130 
137  void setReplacements( const QList< QgsStringReplacement > &replacements )
138  {
139  mReplacements = replacements;
140  }
141 
149  QString process( const QString &input ) const;
150 
157  void writeXml( QDomElement &elem, QDomDocument &doc ) const;
158 
164  void readXml( const QDomElement &elem );
165 
166  private:
167 
168  QList< QgsStringReplacement > mReplacements;
169 
170 
171 };
172 
180 class CORE_EXPORT QgsStringUtils
181 {
182  public:
183 
186  {
187  MixedCase = QFont::MixedCase,
188  AllUppercase = QFont::AllUppercase,
189  AllLowercase = QFont::AllLowercase,
190  ForceFirstLetterToCapital = QFont::Capitalize,
191  TitleCase = QFont::Capitalize + 1000,
192  UpperCamelCase = QFont::Capitalize + 1001,
193  };
194 
202  static QString capitalize( const QString &string, Capitalization capitalization );
203 
212  static QString ampersandEncode( const QString &string );
213 
223  static int levenshteinDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
224 
234  static QString longestCommonSubstring( const QString &string1, const QString &string2, bool caseSensitive = false );
235 
245  static int hammingDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
246 
253  static QString soundex( const QString &string );
254 
263  static QString insertLinks( const QString &string, bool *foundLinks = nullptr );
264 
277  static QString wordWrap( const QString &string, int length, bool useMaxLineLength = true, const QString &customDelimiter = QString() );
278 };
279 
280 #endif //QGSSTRINGUTILS_H
bool operator==(const QgsStringReplacement &other)
A representation of a single string replacement.
bool caseSensitive() const
Returns true if match is case sensitive.
QgsStringReplacementCollection(const QList< QgsStringReplacement > &replacements=QList< QgsStringReplacement >())
Constructor for QgsStringReplacementCollection.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:587
QList< QgsStringReplacement > replacements() const
Returns the list of string replacements in this collection.
QString match() const
Returns the string matched by this object.
QString replacement() const
Returns the string to replace matches with.
Capitalization
Capitalization options.
bool wholeWordOnly() const
Returns true if match only applies to whole words, or false if partial word matches are permitted...
A collection of string replacements (specified using QgsStringReplacement objects).
Utility functions for working with strings.
void setReplacements(const QList< QgsStringReplacement > &replacements)
Sets the list of string replacements in this collection.