QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsdatetimestatisticalsummary.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatetimestatisticalsummary.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 <QDateTime>
19 #include <QStringList>
20 #include <QObject>
21 #include <QVariant>
22 #include <QVariantList>
23 #include <limits>
24 
25 /***************************************************************************
26  * This class is considered CRITICAL and any change MUST be accompanied with
27  * full unit tests in test_qgsdatetimestatisticalsummary.py.
28  * See details in QEP #17
29  ****************************************************************************/
30 
31 QgsDateTimeStatisticalSummary::QgsDateTimeStatisticalSummary( QgsDateTimeStatisticalSummary::Statistics stats )
32  : mStatistics( stats )
33 {
34  reset();
35 }
36 
38 {
39  mCount = 0;
40  mValues.clear();
41  mCountMissing = 0;
42  mMin = QDateTime();
43  mMax = QDateTime();
44  mIsTimes = false;
45 }
46 
47 void QgsDateTimeStatisticalSummary::calculate( const QVariantList &values )
48 {
49  reset();
50 
51  Q_FOREACH ( const QVariant &variant, values )
52  {
53  addValue( variant );
54  }
55  finalize();
56 }
57 
58 void QgsDateTimeStatisticalSummary::addValue( const QVariant &value )
59 {
60  if ( value.type() == QVariant::DateTime )
61  {
62  testDateTime( value.toDateTime() );
63  }
64  else if ( value.type() == QVariant::Date )
65  {
66  QDate date = value.toDate();
67  testDateTime( date.isValid() ? QDateTime( date, QTime( 0, 0, 0 ) )
68  : QDateTime() );
69  }
70  else if ( value.type() == QVariant::Time )
71  {
72  mIsTimes = true;
73  QTime time = value.toTime();
74  testDateTime( time.isValid() ? QDateTime( QDate::fromJulianDay( 0 ), time )
75  : QDateTime() );
76  }
77  else //not a date
78  {
79  mCountMissing++;
80  mCount++;
81  }
82  // QTime?
83 }
84 
86 {
87  //nothing to do for now - this method has been added for forward compatibility
88  //if statistics are implemented which require a post-calculation step
89 }
90 
91 void QgsDateTimeStatisticalSummary::testDateTime( const QDateTime &dateTime )
92 {
93  mCount++;
94 
95  if ( !dateTime.isValid() )
96  mCountMissing++;
97 
98  if ( mStatistics & CountDistinct )
99  {
100  mValues << dateTime;
101  }
102  if ( mStatistics & Min || mStatistics & Range )
103  {
104  if ( mMin.isValid() && dateTime.isValid() )
105  {
106  mMin = std::min( mMin, dateTime );
107  }
108  else if ( !mMin.isValid() && dateTime.isValid() )
109  {
110  mMin = dateTime;
111  }
112  }
113  if ( mStatistics & Max || mStatistics & Range )
114  {
115  if ( mMax.isValid() && dateTime.isValid() )
116  {
117  mMax = std::max( mMax, dateTime );
118  }
119  else if ( !mMax.isValid() && dateTime.isValid() )
120  {
121  mMax = dateTime;
122  }
123  }
124 }
125 
127 {
128  switch ( stat )
129  {
130  case Count:
131  return mCount;
132  case CountDistinct:
133  return mValues.count();
134  case CountMissing:
135  return mCountMissing;
136  case Min:
137  return mIsTimes ? QVariant( mMin.time() ) : QVariant( mMin );
138  case Max:
139  return mIsTimes ? QVariant( mMax.time() ) : QVariant( mMax );
140  case Range:
141  return mIsTimes ? QVariant::fromValue( mMax.time() - mMin.time() ) : QVariant::fromValue( mMax - mMin );
142  case All:
143  return 0;
144  }
145  return 0;
146 }
147 
149 {
150  switch ( statistic )
151  {
152  case Count:
153  return QObject::tr( "Count" );
154  case CountDistinct:
155  return QObject::tr( "Count (distinct)" );
156  case CountMissing:
157  return QObject::tr( "Count (missing)" );
158  case Min:
159  return QObject::tr( "Minimum (earliest)" );
160  case Max:
161  return QObject::tr( "Maximum (latest)" );
162  case Range:
163  return QObject::tr( "Range (interval)" );
164  case All:
165  return QString();
166  }
167  return QString();
168 }
169 
static QString displayName(QgsDateTimeStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.
void addValue(const QVariant &value)
Adds a single datetime to the statistics calculation.
Interval between earliest and latest datetime value.
void reset()
Resets the calculated values.
void calculate(const QVariantList &values)
Calculates summary statistics for a list of variants.
Statistic
Enumeration of flags that specify statistics to be calculated.
Minimum (earliest) datetime value.
void finalize()
Must be called after adding all datetimes with addValue() and before retrieving any calculated dateti...
QgsDateTimeStatisticalSummary(QgsDateTimeStatisticalSummary::Statistics stats=All)
Constructor for QgsDateTimeStatisticalSummary.
QVariant statistic(QgsDateTimeStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.