Skip to content
Permalink
Browse files
Merge pull request #4501 from alexbruy/statistics-by-type
Statistics panel: show only relevant statistics depending on the field type
  • Loading branch information
alexbruy committed May 5, 2017
2 parents 931bf32 + 391cb73 commit 3e05bd85002eaee6d5825b4ec7c53e9d3e3cfbb1
Showing with 182 additions and 56 deletions.
  1. +166 −56 src/app/qgsstatisticalsummarydockwidget.cpp
  2. +16 −0 src/app/qgsstatisticalsummarydockwidget.h
@@ -22,6 +22,7 @@

#include <QTableWidget>
#include <QAction>
#include <QMenu>

QList< QgsStatisticalSummary::Statistic > QgsStatisticalSummaryDockWidget::sDisplayStats =
QList< QgsStatisticalSummary::Statistic > () << QgsStatisticalSummary::Count
@@ -92,28 +93,12 @@ QgsStatisticalSummaryDockWidget::QgsStatisticalSummaryDockWidget( QWidget *paren
connect( mButtonRefresh, &QAbstractButton::clicked, this, &QgsStatisticalSummaryDockWidget::refreshStatistics );
connect( QgsProject::instance(), static_cast<void ( QgsProject::* )( const QStringList & )>( &QgsProject::layersWillBeRemoved ), this, &QgsStatisticalSummaryDockWidget::layersRemoved );

QgsSettings settings;
Q_FOREACH ( QgsStatisticalSummary::Statistic stat, sDisplayStats )
{
QAction *action = new QAction( QgsStatisticalSummary::displayName( stat ), mOptionsToolButton );
action->setCheckable( true );
bool checked = settings.value( QStringLiteral( "StatisticalSummaryDock/checked_%1" ).arg( stat ), true ).toBool();
action->setChecked( checked );
action->setData( stat );
mStatsActions.insert( stat, action );
connect( action, &QAction::triggered, this, &QgsStatisticalSummaryDockWidget::statActionTriggered );
mOptionsToolButton->addAction( action );
}
mStatisticsMenu = new QMenu( mOptionsToolButton );
mOptionsToolButton->setMenu( mStatisticsMenu );

//count of null values statistic:
QAction *nullCountAction = new QAction( tr( "Missing (null) values" ), mOptionsToolButton );
nullCountAction->setCheckable( true );
bool checked = settings.value( QStringLiteral( "StatisticalSummaryDock/checked_missing_values" ), true ).toBool();
nullCountAction->setChecked( checked );
nullCountAction->setData( MISSING_VALUES );
mStatsActions.insert( MISSING_VALUES, nullCountAction );
connect( nullCountAction, &QAction::triggered, this, &QgsStatisticalSummaryDockWidget::statActionTriggered );
mOptionsToolButton->addAction( nullCountAction );
mFieldType = DataType::Numeric;
mPreviousFieldType = DataType::Numeric;
refreshStatisticsMenu();
}

QgsStatisticalSummaryDockWidget::~QgsStatisticalSummaryDockWidget()
@@ -129,39 +114,34 @@ void QgsStatisticalSummaryDockWidget::refreshStatistics()
return;
}

// non numeric field?
bool isNumeric = true;
QVariant::Type fieldType = QVariant::Double;
// determine field type
mFieldType = DataType::Numeric;
if ( !mFieldExpressionWidget->isExpression() )
{
QString field = mFieldExpressionWidget->currentField();
fieldType = mLayer->fields().field( mLayer->fields().lookupField( field ) ).type();
if ( fieldType == QVariant::String || fieldType == QVariant::Date || fieldType == QVariant::DateTime )
{
isNumeric = false;
}
mFieldType = fieldType( mFieldExpressionWidget->currentField() );
}

bool selectedOnly = mSelectedOnlyCheckBox->isChecked();

if ( isNumeric )
if ( mFieldType != mPreviousFieldType )
{
updateNumericStatistics( selectedOnly );
refreshStatisticsMenu();
mPreviousFieldType = mFieldType;
}
else

bool selectedOnly = mSelectedOnlyCheckBox->isChecked();

switch ( mFieldType )
{
switch ( fieldType )
{
case QVariant::String:
updateStringStatistics( selectedOnly );
break;
case QVariant::Date:
case QVariant::DateTime:
updateDateTimeStatistics( selectedOnly );
break;
default:
break;
}
case DataType::Numeric:
updateNumericStatistics( selectedOnly );
break;
case DataType::String:
updateStringStatistics( selectedOnly );
break;
case DataType::DateTime:
updateDateTimeStatistics( selectedOnly );
break;
default:
break;
}
}

@@ -231,15 +211,26 @@ void QgsStatisticalSummaryDockWidget::updateStringStatistics( bool selectedOnly
return;
}

QList< QgsStringStatisticalSummary::Statistic > statsToDisplay;
QgsStringStatisticalSummary::Statistics statsToCalc = 0;
Q_FOREACH ( QgsStringStatisticalSummary::Statistic stat, sDisplayStringStats )
{
if ( mStatsActions.value( stat )->isChecked() )
{
statsToDisplay << stat;
statsToCalc |= stat;
}
}

QgsStringStatisticalSummary stats;
stats.setStatistics( QgsStringStatisticalSummary::All );
stats.setStatistics( statsToCalc );
stats.calculateFromVariants( values );

mStatisticsTable->setRowCount( sDisplayStringStats.count() );
mStatisticsTable->setRowCount( statsToDisplay.count() );
mStatisticsTable->setColumnCount( 2 );

int row = 0;
Q_FOREACH ( QgsStringStatisticalSummary::Statistic stat, sDisplayStringStats )
Q_FOREACH ( QgsStringStatisticalSummary::Statistic stat, statsToDisplay )
{
addRow( row, QgsStringStatisticalSummary::displayName( stat ),
stats.statistic( stat ).toString(),
@@ -277,19 +268,36 @@ void QgsStatisticalSummaryDockWidget::layerChanged( QgsMapLayer *layer )

void QgsStatisticalSummaryDockWidget::statActionTriggered( bool checked )
{
refreshStatistics();
QAction *action = dynamic_cast<QAction *>( sender() );
int stat = action->data().toInt();

QString settingsKey;
switch ( mFieldType )
{
case DataType::Numeric:
settingsKey = QStringLiteral( "numeric" );
break;
case DataType::String:
settingsKey = QStringLiteral( "string" );
break;
case DataType::DateTime:
settingsKey = QStringLiteral( "datetime" );
break;
default:
break;
}

QgsSettings settings;
if ( stat >= 0 )
{
settings.setValue( QStringLiteral( "StatisticalSummaryDock/checked_%1" ).arg( stat ), checked );
settings.setValue( QStringLiteral( "StatisticalSummaryDock/%1_%2" ).arg( settingsKey ).arg( stat ), checked );
}
else if ( stat == MISSING_VALUES )
{
settings.setValue( QStringLiteral( "StatisticalSummaryDock/checked_missing_values" ).arg( stat ), checked );
settings.setValue( QStringLiteral( "StatisticalSummaryDock/numeric_missing_values" ), checked );
}

refreshStatistics();
}

void QgsStatisticalSummaryDockWidget::layersRemoved( const QStringList &layers )
@@ -319,15 +327,27 @@ void QgsStatisticalSummaryDockWidget::updateDateTimeStatistics( bool selectedOnl
return;
}

QList< QgsDateTimeStatisticalSummary::Statistic > statsToDisplay;
QgsDateTimeStatisticalSummary::Statistics statsToCalc = 0;
Q_FOREACH ( QgsDateTimeStatisticalSummary::Statistic stat, sDisplayDateTimeStats )
{
if ( mStatsActions.value( stat )->isChecked() )
{
statsToDisplay << stat;
statsToCalc |= stat;
}
}


QgsDateTimeStatisticalSummary stats;
stats.setStatistics( QgsDateTimeStatisticalSummary::All );
stats.setStatistics( statsToCalc );
stats.calculate( values );

mStatisticsTable->setRowCount( sDisplayDateTimeStats.count() );
mStatisticsTable->setRowCount( statsToDisplay.count() );
mStatisticsTable->setColumnCount( 2 );

int row = 0;
Q_FOREACH ( QgsDateTimeStatisticalSummary::Statistic stat, sDisplayDateTimeStats )
Q_FOREACH ( QgsDateTimeStatisticalSummary::Statistic stat, statsToDisplay )
{
QString value = ( stat == QgsDateTimeStatisticalSummary::Range
? tr( "%1 seconds" ).arg( stats.range().seconds() )
@@ -358,3 +378,93 @@ void QgsStatisticalSummaryDockWidget::addRow( int row, const QString &name, cons
mStatisticsTable->setItem( row, 1, valueItem );
}

void QgsStatisticalSummaryDockWidget::refreshStatisticsMenu()
{
mStatisticsMenu->clear();
mStatsActions.clear();

QgsSettings settings;
switch ( mFieldType )
{
case DataType::Numeric:
{
Q_FOREACH ( QgsStatisticalSummary::Statistic stat, sDisplayStats )
{
QAction *action = new QAction( QgsStatisticalSummary::displayName( stat ), mStatisticsMenu );
action->setCheckable( true );
bool checked = settings.value( QStringLiteral( "StatisticalSummaryDock/numeric_%1" ).arg( stat ), true ).toBool();
action->setChecked( checked );
action->setData( stat );
mStatsActions.insert( stat, action );
connect( action, &QAction::toggled, this, &QgsStatisticalSummaryDockWidget::statActionTriggered );
mStatisticsMenu->addAction( action );
}

//count of null values statistic
QAction *nullCountAction = new QAction( tr( "Missing (null) values" ), mStatisticsMenu );
nullCountAction->setCheckable( true );
bool checked = settings.value( QStringLiteral( "StatisticalSummaryDock/numeric_missing_values" ), true ).toBool();
nullCountAction->setChecked( checked );
nullCountAction->setData( MISSING_VALUES );
mStatsActions.insert( MISSING_VALUES, nullCountAction );
connect( nullCountAction, &QAction::toggled, this, &QgsStatisticalSummaryDockWidget::statActionTriggered );
mStatisticsMenu->addAction( nullCountAction );

break;
}
case DataType::String:
{
Q_FOREACH ( QgsStringStatisticalSummary::Statistic stat, sDisplayStringStats )
{
QAction *action = new QAction( QgsStringStatisticalSummary::displayName( stat ), mStatisticsMenu );
action->setCheckable( true );
bool checked = settings.value( QStringLiteral( "StatisticalSummaryDock/string_%1" ).arg( stat ), true ).toBool();
action->setChecked( checked );
action->setData( stat );
mStatsActions.insert( stat, action );
connect( action, &QAction::toggled, this, &QgsStatisticalSummaryDockWidget::statActionTriggered );
mStatisticsMenu->addAction( action );
}
break;
}
case DataType::DateTime:
{
Q_FOREACH ( QgsDateTimeStatisticalSummary::Statistic stat, sDisplayDateTimeStats )
{
QAction *action = new QAction( QgsDateTimeStatisticalSummary::displayName( stat ), mStatisticsMenu );
action->setCheckable( true );
bool checked = settings.value( QStringLiteral( "StatisticalSummaryDock/datetime_%1" ).arg( stat ), true ).toBool();
action->setChecked( checked );
action->setData( stat );
mStatsActions.insert( stat, action );
connect( action, &QAction::toggled, this, &QgsStatisticalSummaryDockWidget::statActionTriggered );
mStatisticsMenu->addAction( action );
}
break;
}
default:
break;
}
}

QgsStatisticalSummaryDockWidget::DataType QgsStatisticalSummaryDockWidget::fieldType( const QString &fieldName )
{
QgsField field = mLayer->fields().field( mLayer->fields().lookupField( fieldName ) );
if ( field.isNumeric() )
{
return DataType::Numeric;
}

switch ( field.type() )
{
case QVariant::String:
return DataType::String;
case QVariant::Date:
case QVariant::DateTime:
return DataType::DateTime;
default:
break;
}

return DataType::Numeric;
}
@@ -24,6 +24,7 @@
#include "qgsdockwidget.h"
#include "qgis_app.h"

class QMenu;
class QgsBrowserModel;
class QModelIndex;
class QgsDockBrowserTreeView;
@@ -61,6 +62,14 @@ class APP_EXPORT QgsStatisticalSummaryDockWidget : public QgsDockWidget, private

private:

//! Enumeration of supported statistics types
enum DataType
{
Numeric, //!< Numeric fields: int, double, etc
String, //!< String fields
DateTime //!< Date and DateTime fields
};

QgsVectorLayer *mLayer = nullptr;

QMap< int, QAction * > mStatsActions;
@@ -74,6 +83,13 @@ class APP_EXPORT QgsStatisticalSummaryDockWidget : public QgsDockWidget, private
void addRow( int row, const QString &name, const QString &value, bool showValue );

QgsExpressionContext createExpressionContext() const override;

void refreshStatisticsMenu();
DataType fieldType( const QString &fieldName );

QMenu *mStatisticsMenu = nullptr;
DataType mFieldType;
DataType mPreviousFieldType;
};

#endif // QGSSTATISTICALSUMMARYDOCKWIDGET_H

0 comments on commit 3e05bd8

Please sign in to comment.