QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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.h"
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( const 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 }
45 
46 void QgsDateTimeStatisticalSummary::calculate( const QVariantList& values )
47 {
48  reset();
49 
50  Q_FOREACH ( const QVariant& variant, values )
51  {
52  addValue( variant );
53  }
54  finalize();
55 }
56 
58 {
59  if ( value.type() == QVariant::DateTime )
60  {
61  testDateTime( value.toDateTime() );
62  }
63  else if ( value.type() == QVariant::Date )
64  {
65  QDate date = value.toDate();
66  testDateTime( date.isValid() ? QDateTime( date, QTime( 0, 0, 0 ) )
67  : QDateTime() );
68  }
69  // QTime?
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 QgsDateTimeStatisticalSummary::testDateTime( const QDateTime& dateTime )
79 {
80  mCount++;
81 
82  if ( !dateTime.isValid() )
83  mCountMissing++;
84 
85  if ( mStatistics & CountDistinct )
86  {
87  mValues << dateTime;
88  }
89  if ( mStatistics & Min || mStatistics & Range )
90  {
91  if ( mMin.isValid() && dateTime.isValid() )
92  {
93  mMin = qMin( mMin, dateTime );
94  }
95  else if ( !mMin.isValid() && dateTime.isValid() )
96  {
97  mMin = dateTime;
98  }
99  }
100  if ( mStatistics & Max || mStatistics & Range )
101  {
102  if ( mMax.isValid() && dateTime.isValid() )
103  {
104  mMax = qMax( mMax, dateTime );
105  }
106  else if ( !mMax.isValid() && dateTime.isValid() )
107  {
108  mMax = dateTime;
109  }
110  }
111 }
112 
114 {
115  switch ( stat )
116  {
117  case Count:
118  return mCount;
119  case CountDistinct:
120  return mValues.count();
121  case CountMissing:
122  return mCountMissing;
123  case Min:
124  return mMin;
125  case Max:
126  return mMax;
127  case Range:
128  return QVariant::fromValue( mMax - mMin );
129  case All:
130  return 0;
131  }
132  return 0;
133 }
134 
136 {
137  switch ( statistic )
138  {
139  case Count:
140  return QObject::tr( "Count" );
141  case CountDistinct:
142  return QObject::tr( "Count (distinct)" );
143  case CountMissing:
144  return QObject::tr( "Count (missing)" );
145  case Min:
146  return QObject::tr( "Minimum (earliest)" );
147  case Max:
148  return QObject::tr( "Maximum (latest)" );
149  case Range:
150  return QObject::tr( "Range (interval)" );
151  case All:
152  return QString();
153  }
154  return QString();
155 }
156 
QDateTime toDateTime() const
void addValue(const QVariant &value)
Adds a single datetime to the statistics calculation.
Interval between earliest and latest datetime value.
QgsDateTimeStatisticalSummary(const QgsDateTimeStatisticalSummary::Statistics &stats=All)
Constructor for QgsDateTimeStatisticalSummary.
QString tr(const char *sourceText, const char *disambiguation, int n)
void reset()
Resets the calculated values.
void calculate(const QVariantList &values)
Calculates summary statistics for a list of variants.
bool isValid() const
Statistic
Enumeration of flags that specify statistics to be calculated.
Minimum (earliest) datetime value.
int count() const
void finalize()
Must be called after adding all datetimes with addValue() and before retrieving any calculated dateti...
static QString displayName(Statistic statistic)
Returns the friendly display name for a statistic.
QVariant fromValue(const T &value)
bool isValid() const
QDate toDate() const
void clear()
Type type() const
QVariant statistic(Statistic stat) const
Returns the value of a specified statistic.