QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsstringstatisticalsummary.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstringstatisticalsummary.cpp
3  -------------------------------
4  Date : May 2016
5  Copyright : (C) 2016 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 
17 #include <QString>
18 #include <QStringList>
19 #include <QObject>
20 #include <QVariant>
21 #include <QVariantList>
22 #include "limits.h"
23 
24 /***************************************************************************
25  * This class is considered CRITICAL and any change MUST be accompanied with
26  * full unit tests in test_qgsstringstatisticalsummary.py.
27  * See details in QEP #17
28  ****************************************************************************/
29 
30 QgsStringStatisticalSummary::QgsStringStatisticalSummary( const QgsStringStatisticalSummary::Statistics& stats )
31  : mStatistics( stats )
32 {
33  reset();
34 }
35 
37 {
38  mCount = 0;
39  mValues.clear();
40  mCountMissing = 0;
41  mMin.clear();
42  mMax.clear();
43  mMinLength = INT_MAX;
44  mMaxLength = 0;
45 }
46 
48 {
49  reset();
50 
51  Q_FOREACH ( const QString& string, values )
52  {
53  testString( string );
54  }
55  finalize();
56 }
57 
59 {
60  testString( string );
61 }
62 
64 {
65  if ( value.type() == QVariant::String )
66  {
67  testString( value.toString() );
68  }
69  finalize();
70 }
71 
73 {
74  //nothing to do for now - this method has been added for forward compatibility
75  //if statistics are implemented which require a post-calculation step
76 }
77 
78 void QgsStringStatisticalSummary::calculateFromVariants( const QVariantList& values )
79 {
80  reset();
81 
82  Q_FOREACH ( const QVariant& variant, values )
83  {
84  if ( variant.type() == QVariant::String )
85  {
86  testString( variant.toString() );
87  }
88  }
89 }
90 
91 void QgsStringStatisticalSummary::testString( const QString& string )
92 {
93  mCount++;
94 
95  if ( string.isEmpty() )
96  mCountMissing++;
97 
98  if ( mStatistics & CountDistinct )
99  {
100  mValues << string;
101  }
102  if ( mStatistics & Min )
103  {
104  if ( !mMin.isEmpty() && !string.isEmpty() )
105  {
106  mMin = qMin( mMin, string );
107  }
108  else if ( mMin.isEmpty() && !string.isEmpty() )
109  {
110  mMin = string;
111  }
112  }
113  if ( mStatistics & Max )
114  {
115  if ( !mMax.isEmpty() && !string.isEmpty() )
116  {
117  mMax = qMax( mMax, string );
118  }
119  else if ( mMax.isEmpty() && !string.isEmpty() )
120  {
121  mMax = string;
122  }
123  }
124  mMinLength = qMin( mMinLength, string.length() );
125  mMaxLength = qMax( mMaxLength, string.length() );
126 }
127 
129 {
130  switch ( stat )
131  {
132  case Count:
133  return mCount;
134  case CountDistinct:
135  return mValues.count();
136  case CountMissing:
137  return mCountMissing;
138  case Min:
139  return mMin;
140  case Max:
141  return mMax;
142  case MinimumLength:
143  return mMinLength;
144  case MaximumLength:
145  return mMaxLength;
146  case All:
147  return 0;
148  }
149  return 0;
150 }
151 
153 {
154  switch ( statistic )
155  {
156  case Count:
157  return QObject::tr( "Count" );
158  case CountDistinct:
159  return QObject::tr( "Count (distinct)" );
160  case CountMissing:
161  return QObject::tr( "Count (missing)" );
162  case Min:
163  return QObject::tr( "Minimum" );
164  case Max:
165  return QObject::tr( "Maximum" );
166  case MinimumLength:
167  return QObject::tr( "Minimum length" );
168  case MaximumLength:
169  return QObject::tr( "Maximum length" );
170  case All:
171  return QString();
172  }
173  return QString();
174 }
175 
QVariant statistic(Statistic stat) const
Returns the value of a specified statistic.
Statistic
Enumeration of flags that specify statistics to be calculated.
QString tr(const char *sourceText, const char *disambiguation, int n)
void clear()
QgsStringStatisticalSummary(const QgsStringStatisticalSummary::Statistics &stats=All)
Constructor for QgsStringStatistics.
static QString displayName(Statistic statistic)
Returns the friendly display name for a statistic.
bool isEmpty() const
void addValue(const QVariant &value)
Adds a single variant to the statistics calculation.
int count() const
void addString(const QString &string)
Adds a single string to the statistics calculation.
void calculateFromVariants(const QVariantList &values)
Calculates summary statistics for an entire list of variants at once.
void finalize()
Must be called after adding all strings with addString() and before retrieving any calculated string ...
void calculate(const QStringList &values)
Calculates summary statistics for an entire list of strings at once.
void clear()
Type type() const
void reset()
Resets the calculated values.
QString toString() const