|
| 1 | +/*************************************************************************** |
| 2 | + qgsstatisticalsummarydockwidget.cpp |
| 3 | + ----------------------------------- |
| 4 | + begin : May 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 | +#include "qgsstatisticalsummarydockwidget.h" |
| 16 | +#include "qgsstatisticalsummary.h" |
| 17 | +#include <QTableWidget> |
| 18 | +#include <QAction> |
| 19 | +#include <QSettings> |
| 20 | + |
| 21 | +QList< QgsStatisticalSummary::Statistic > QgsStatisticalSummaryDockWidget::mDisplayStats = |
| 22 | + QList< QgsStatisticalSummary::Statistic > () << QgsStatisticalSummary::Count |
| 23 | + << QgsStatisticalSummary::Sum |
| 24 | + << QgsStatisticalSummary::Mean |
| 25 | + << QgsStatisticalSummary::Median |
| 26 | + << QgsStatisticalSummary::StDev |
| 27 | + << QgsStatisticalSummary::StDevSample |
| 28 | + << QgsStatisticalSummary::Min |
| 29 | + << QgsStatisticalSummary::Max |
| 30 | + << QgsStatisticalSummary::Range |
| 31 | + << QgsStatisticalSummary::Minority |
| 32 | + << QgsStatisticalSummary::Majority |
| 33 | + << QgsStatisticalSummary::Variety |
| 34 | + << QgsStatisticalSummary::FirstQuartile |
| 35 | + << QgsStatisticalSummary::ThirdQuartile |
| 36 | + << QgsStatisticalSummary::InterQuartileRange; |
| 37 | + |
| 38 | +#define MISSING_VALUES -1 |
| 39 | + |
| 40 | +QgsStatisticalSummaryDockWidget::QgsStatisticalSummaryDockWidget( QWidget *parent ) |
| 41 | + : QDockWidget( parent ) |
| 42 | + , mLayer( 0 ) |
| 43 | +{ |
| 44 | + setupUi( this ); |
| 45 | + |
| 46 | + mLayerComboBox->setFilters( QgsMapLayerProxyModel::VectorLayer ); |
| 47 | + mFieldExpressionWidget->setFilters( QgsFieldProxyModel::Numeric ); |
| 48 | + connect( mLayerComboBox, SIGNAL( layerChanged( QgsMapLayer* ) ), this, SLOT( layerChanged( QgsMapLayer* ) ) ); |
| 49 | + connect( mFieldExpressionWidget, SIGNAL( fieldChanged( QString ) ), this, SLOT( refreshStatistics() ) ); |
| 50 | + connect( mSelectedOnlyCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( refreshStatistics() ) ); |
| 51 | + connect( mButtonRefresh, SIGNAL( clicked( bool ) ), this, SLOT( refreshStatistics() ) ); |
| 52 | + |
| 53 | + if ( mLayerComboBox->currentLayer() ) |
| 54 | + { |
| 55 | + mFieldExpressionWidget->setLayer( mLayerComboBox->currentLayer() ); |
| 56 | + } |
| 57 | + |
| 58 | + QSettings settings; |
| 59 | + foreach ( QgsStatisticalSummary::Statistic stat, mDisplayStats ) |
| 60 | + { |
| 61 | + QAction* action = new QAction( QgsStatisticalSummary::displayName( stat ), mOptionsToolButton ); |
| 62 | + action->setCheckable( true ); |
| 63 | + bool checked = settings.value( QString( "/StatisticalSummaryDock/checked_%1" ).arg( stat ), true ).toBool(); |
| 64 | + action->setChecked( checked ); |
| 65 | + action->setData( stat ); |
| 66 | + mStatsActions.insert( stat, action ); |
| 67 | + connect( action, SIGNAL( triggered( bool ) ), this, SLOT( statActionTriggered( bool ) ) ); |
| 68 | + mOptionsToolButton->addAction( action ); |
| 69 | + } |
| 70 | + |
| 71 | + //count of null values statistic: |
| 72 | + QAction* nullCountAction = new QAction( tr( "Missing (null) values" ), mOptionsToolButton ); |
| 73 | + nullCountAction->setCheckable( true ); |
| 74 | + bool checked = settings.value( QString( "/StatisticalSummaryDock/checked_missing_values" ), true ).toBool(); |
| 75 | + nullCountAction->setChecked( checked ); |
| 76 | + nullCountAction->setData( MISSING_VALUES ); |
| 77 | + mStatsActions.insert( MISSING_VALUES, nullCountAction ); |
| 78 | + connect( nullCountAction, SIGNAL( triggered( bool ) ), this, SLOT( statActionTriggered( bool ) ) ); |
| 79 | + mOptionsToolButton->addAction( nullCountAction ); |
| 80 | +} |
| 81 | + |
| 82 | +QgsStatisticalSummaryDockWidget::~QgsStatisticalSummaryDockWidget() |
| 83 | +{ |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +void QgsStatisticalSummaryDockWidget::refreshStatistics() |
| 88 | +{ |
| 89 | + if ( !mLayer || !mFieldExpressionWidget->isValidExpression() ) |
| 90 | + { |
| 91 | + mStatisticsTable->setRowCount( 0 ); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + QString sourceFieldExp = mFieldExpressionWidget->currentField(); |
| 96 | + |
| 97 | + bool ok; |
| 98 | + bool selectedOnly = mSelectedOnlyCheckBox->isChecked(); |
| 99 | + int missingValues = 0; |
| 100 | + QList< double > values = mLayer->getDoubleValues( sourceFieldExp, ok, selectedOnly, &missingValues ); |
| 101 | + |
| 102 | + if ( ! ok ) |
| 103 | + { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + QList< QgsStatisticalSummary::Statistic > statsToDisplay; |
| 108 | + foreach ( QgsStatisticalSummary::Statistic stat, mDisplayStats ) |
| 109 | + { |
| 110 | + if ( mStatsActions.value( stat )->isChecked() ) |
| 111 | + statsToDisplay << stat; |
| 112 | + } |
| 113 | + |
| 114 | + int extraRows = 0; |
| 115 | + if ( mStatsActions.value( MISSING_VALUES )->isChecked() ) |
| 116 | + extraRows++; |
| 117 | + |
| 118 | + QgsStatisticalSummary stats; |
| 119 | + stats.setStatistics( QgsStatisticalSummary::All ); |
| 120 | + stats.calculate( values ); |
| 121 | + |
| 122 | + mStatisticsTable->setRowCount( statsToDisplay.count() + extraRows ); |
| 123 | + mStatisticsTable->setColumnCount( 2 ); |
| 124 | + |
| 125 | + int row = 0; |
| 126 | + foreach ( QgsStatisticalSummary::Statistic stat, statsToDisplay ) |
| 127 | + { |
| 128 | + QTableWidgetItem* nameItem = new QTableWidgetItem( QgsStatisticalSummary::displayName( stat ) ); |
| 129 | + nameItem->setToolTip( nameItem->text() ); |
| 130 | + nameItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled ); |
| 131 | + mStatisticsTable->setItem( row, 0, nameItem ); |
| 132 | + |
| 133 | + QTableWidgetItem* valueItem = new QTableWidgetItem(); |
| 134 | + if ( stats.count() != 0 ) |
| 135 | + { |
| 136 | + valueItem->setText( QString::number( stats.statistic( stat ) ) ); |
| 137 | + } |
| 138 | + valueItem->setToolTip( valueItem->text() ); |
| 139 | + valueItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled ); |
| 140 | + mStatisticsTable->setItem( row, 1, valueItem ); |
| 141 | + |
| 142 | + row++; |
| 143 | + } |
| 144 | + |
| 145 | + if ( mStatsActions.value( MISSING_VALUES )->isChecked() ) |
| 146 | + { |
| 147 | + QTableWidgetItem* nameItem = new QTableWidgetItem( tr( "Missing (null) values" ) ); |
| 148 | + nameItem->setToolTip( nameItem->text() ); |
| 149 | + nameItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled ); |
| 150 | + mStatisticsTable->setItem( row, 0, nameItem ); |
| 151 | + |
| 152 | + QTableWidgetItem* valueItem = new QTableWidgetItem(); |
| 153 | + if ( stats.count() != 0 || missingValues != 0 ) |
| 154 | + { |
| 155 | + valueItem->setText( QString::number( missingValues ) ); |
| 156 | + } |
| 157 | + valueItem->setToolTip( valueItem->text() ); |
| 158 | + valueItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled ); |
| 159 | + mStatisticsTable->setItem( row, 1, valueItem ); |
| 160 | + row++; |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +void QgsStatisticalSummaryDockWidget::layerChanged( QgsMapLayer *layer ) |
| 165 | +{ |
| 166 | + QgsVectorLayer* newLayer = dynamic_cast< QgsVectorLayer* >( layer ); |
| 167 | + if ( mLayer && mLayer != newLayer ) |
| 168 | + { |
| 169 | + disconnect( mLayer, SIGNAL( selectionChanged() ), this, SLOT( refreshStatistics() ) ); |
| 170 | + } |
| 171 | + |
| 172 | + mLayer = newLayer; |
| 173 | + |
| 174 | + if ( mLayer ) |
| 175 | + { |
| 176 | + connect( mLayer, SIGNAL( selectionChanged() ), this, SLOT( refreshStatistics() ) ); |
| 177 | + } |
| 178 | + |
| 179 | + mFieldExpressionWidget->setLayer( mLayer ); |
| 180 | + |
| 181 | + if ( mFieldExpressionWidget->currentField().isEmpty() ) |
| 182 | + { |
| 183 | + mStatisticsTable->setRowCount( 0 ); |
| 184 | + } |
| 185 | + else |
| 186 | + { |
| 187 | + refreshStatistics(); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +void QgsStatisticalSummaryDockWidget::statActionTriggered( bool checked ) |
| 192 | +{ |
| 193 | + refreshStatistics(); |
| 194 | + QAction* action = dynamic_cast<QAction*>( sender() ); |
| 195 | + int stat = action->data().toInt(); |
| 196 | + |
| 197 | + QSettings settings; |
| 198 | + if ( stat >= 0 ) |
| 199 | + { |
| 200 | + settings.setValue( QString( "/StatisticalSummaryDock/checked_%1" ).arg( stat ), checked ); |
| 201 | + } |
| 202 | + else if ( stat == MISSING_VALUES ) |
| 203 | + { |
| 204 | + settings.setValue( QString( "/StatisticalSummaryDock/checked_missing_values" ).arg( stat ), checked ); |
| 205 | + } |
| 206 | +} |
0 commit comments