17
17
#include " qgsmaplayerregistry.h"
18
18
#include " qgisapp.h"
19
19
#include " qgsmapcanvas.h"
20
+
20
21
#include < QTableWidget>
21
22
#include < QAction>
22
23
#include < QSettings>
24
+ #include < QMenu>
23
25
24
26
QList< QgsStatisticalSummary::Statistic > QgsStatisticalSummaryDockWidget::mDisplayStats =
25
27
QList< QgsStatisticalSummary::Statistic > () << QgsStatisticalSummary::Count
@@ -76,6 +78,7 @@ static QgsExpressionContext _getExpressionContext( const void* context )
76
78
QgsStatisticalSummaryDockWidget::QgsStatisticalSummaryDockWidget ( QWidget *parent )
77
79
: QgsDockWidget( parent )
78
80
, mLayer( nullptr )
81
+ , mStatisticsMenu( nullptr )
79
82
{
80
83
setupUi ( this );
81
84
@@ -95,28 +98,12 @@ QgsStatisticalSummaryDockWidget::QgsStatisticalSummaryDockWidget( QWidget *paren
95
98
connect ( mButtonRefresh , SIGNAL ( clicked ( bool ) ), this , SLOT ( refreshStatistics () ) );
96
99
connect ( QgsMapLayerRegistry::instance (), SIGNAL ( layersWillBeRemoved ( QStringList ) ), this , SLOT ( layersRemoved ( QStringList ) ) );
97
100
98
- QSettings settings;
99
- Q_FOREACH ( QgsStatisticalSummary::Statistic stat, mDisplayStats )
100
- {
101
- QAction* action = new QAction ( QgsStatisticalSummary::displayName ( stat ), mOptionsToolButton );
102
- action->setCheckable ( true );
103
- bool checked = settings.value ( QString ( " /StatisticalSummaryDock/checked_%1" ).arg ( stat ), true ).toBool ();
104
- action->setChecked ( checked );
105
- action->setData ( stat );
106
- mStatsActions .insert ( stat, action );
107
- connect ( action, SIGNAL ( triggered ( bool ) ), this , SLOT ( statActionTriggered ( bool ) ) );
108
- mOptionsToolButton ->addAction ( action );
109
- }
101
+ mStatisticsMenu = new QMenu ( mOptionsToolButton );
102
+ mOptionsToolButton ->setMenu ( mStatisticsMenu );
110
103
111
- // count of null values statistic:
112
- QAction* nullCountAction = new QAction ( tr ( " Missing (null) values" ), mOptionsToolButton );
113
- nullCountAction->setCheckable ( true );
114
- bool checked = settings.value ( QString ( " /StatisticalSummaryDock/checked_missing_values" ), true ).toBool ();
115
- nullCountAction->setChecked ( checked );
116
- nullCountAction->setData ( MISSING_VALUES );
117
- mStatsActions .insert ( MISSING_VALUES, nullCountAction );
118
- connect ( nullCountAction, SIGNAL ( triggered ( bool ) ), this , SLOT ( statActionTriggered ( bool ) ) );
119
- mOptionsToolButton ->addAction ( nullCountAction );
104
+ mFieldType = DataType::Numeric;
105
+ mPreviousFieldType = DataType::Numeric;
106
+ refreshStatisticsMenu ();
120
107
}
121
108
122
109
QgsStatisticalSummaryDockWidget::~QgsStatisticalSummaryDockWidget ()
@@ -132,39 +119,34 @@ void QgsStatisticalSummaryDockWidget::refreshStatistics()
132
119
return ;
133
120
}
134
121
135
- // non numeric field?
136
- bool isNumeric = true ;
137
- QVariant::Type fieldType = QVariant::Double;
122
+ // determine field type
123
+ mFieldType = DataType::Numeric;
138
124
if ( !mFieldExpressionWidget ->isExpression () )
139
125
{
140
- QString field = mFieldExpressionWidget ->currentField ();
141
- fieldType = mLayer ->fields ().field ( mLayer ->fields ().fieldNameIndex ( field ) ).type ();
142
- if ( fieldType == QVariant::String || fieldType == QVariant::Date || fieldType == QVariant::DateTime )
143
- {
144
- isNumeric = false ;
145
- }
126
+ mFieldType = fieldType ( mFieldExpressionWidget ->currentField () );
146
127
}
147
128
148
- bool selectedOnly = mSelectedOnlyCheckBox ->isChecked ();
149
-
150
- if ( isNumeric )
129
+ if ( mFieldType != mPreviousFieldType )
151
130
{
152
- updateNumericStatistics ( selectedOnly );
131
+ refreshStatisticsMenu ();
132
+ mPreviousFieldType = mFieldType ;
153
133
}
154
- else
134
+
135
+ bool selectedOnly = mSelectedOnlyCheckBox ->isChecked ();
136
+
137
+ switch ( mFieldType )
155
138
{
156
- switch ( fieldType )
157
- {
158
- case QVariant::String:
159
- updateStringStatistics ( selectedOnly );
160
- break ;
161
- case QVariant::Date:
162
- case QVariant::DateTime:
163
- updateDateTimeStatistics ( selectedOnly );
164
- break ;
165
- default :
166
- break ;
167
- }
139
+ case DataType::Numeric:
140
+ updateNumericStatistics ( selectedOnly );
141
+ break ;
142
+ case DataType::String:
143
+ updateStringStatistics ( selectedOnly );
144
+ break ;
145
+ case DataType::DateTime:
146
+ updateDateTimeStatistics ( selectedOnly );
147
+ break ;
148
+ default :
149
+ break ;
168
150
}
169
151
}
170
152
@@ -234,15 +216,26 @@ void QgsStatisticalSummaryDockWidget::updateStringStatistics( bool selectedOnly
234
216
return ;
235
217
}
236
218
219
+ QList< QgsStringStatisticalSummary::Statistic > statsToDisplay;
220
+ QgsStringStatisticalSummary::Statistics statsToCalc = 0 ;
221
+ Q_FOREACH ( QgsStringStatisticalSummary::Statistic stat, mDisplayStringStats )
222
+ {
223
+ if ( mStatsActions .value ( stat )->isChecked () )
224
+ {
225
+ statsToDisplay << stat;
226
+ statsToCalc |= stat;
227
+ }
228
+ }
229
+
237
230
QgsStringStatisticalSummary stats;
238
- stats.setStatistics ( QgsStringStatisticalSummary::All );
231
+ stats.setStatistics ( statsToCalc );
239
232
stats.calculateFromVariants ( values );
240
233
241
- mStatisticsTable ->setRowCount ( mDisplayStringStats .count () );
234
+ mStatisticsTable ->setRowCount ( statsToDisplay .count () );
242
235
mStatisticsTable ->setColumnCount ( 2 );
243
236
244
237
int row = 0 ;
245
- Q_FOREACH ( QgsStringStatisticalSummary::Statistic stat, mDisplayStringStats )
238
+ Q_FOREACH ( QgsStringStatisticalSummary::Statistic stat, statsToDisplay )
246
239
{
247
240
addRow ( row, QgsStringStatisticalSummary::displayName ( stat ),
248
241
stats.statistic ( stat ).toString (),
@@ -280,19 +273,37 @@ void QgsStatisticalSummaryDockWidget::layerChanged( QgsMapLayer *layer )
280
273
281
274
void QgsStatisticalSummaryDockWidget::statActionTriggered ( bool checked )
282
275
{
283
- refreshStatistics ();
284
276
QAction* action = dynamic_cast <QAction*>( sender () );
285
277
int stat = action->data ().toInt ();
286
278
279
+ QString settingsKey;
280
+ switch ( mFieldType )
281
+ {
282
+ case DataType::Numeric:
283
+ settingsKey = " numeric" ;
284
+ break ;
285
+ case DataType::String:
286
+ settingsKey = " string" ;
287
+ break ;
288
+ case DataType::DateTime:
289
+ settingsKey = " datetime" ;
290
+ break ;
291
+ default :
292
+ break ;
293
+ }
294
+
295
+
287
296
QSettings settings;
288
297
if ( stat >= 0 )
289
298
{
290
- settings.setValue ( QString ( " /StatisticalSummaryDock/checked_%1 " ).arg ( stat ), checked );
299
+ settings.setValue ( QString ( " /StatisticalSummaryDock/%1_%2 " ). arg ( settingsKey ).arg ( stat ), checked );
291
300
}
292
301
else if ( stat == MISSING_VALUES )
293
302
{
294
- settings.setValue ( QString ( " /StatisticalSummaryDock/checked_missing_values " ). arg ( stat ), checked );
303
+ settings.setValue ( QString ( " /StatisticalSummaryDock/numeric_missing_values " ), checked );
295
304
}
305
+
306
+ refreshStatistics ();
296
307
}
297
308
298
309
void QgsStatisticalSummaryDockWidget::layersRemoved ( const QStringList& layers )
@@ -322,15 +333,26 @@ void QgsStatisticalSummaryDockWidget::updateDateTimeStatistics( bool selectedOnl
322
333
return ;
323
334
}
324
335
336
+ QList< QgsDateTimeStatisticalSummary::Statistic > statsToDisplay;
337
+ QgsDateTimeStatisticalSummary::Statistics statsToCalc = 0 ;
338
+ Q_FOREACH ( QgsDateTimeStatisticalSummary::Statistic stat, mDisplayDateTimeStats )
339
+ {
340
+ if ( mStatsActions .value ( stat )->isChecked () )
341
+ {
342
+ statsToDisplay << stat;
343
+ statsToCalc |= stat;
344
+ }
345
+ }
346
+
325
347
QgsDateTimeStatisticalSummary stats;
326
- stats.setStatistics ( QgsDateTimeStatisticalSummary::All );
348
+ stats.setStatistics ( statsToCalc );
327
349
stats.calculate ( values );
328
350
329
- mStatisticsTable ->setRowCount ( mDisplayDateTimeStats .count () );
351
+ mStatisticsTable ->setRowCount ( statsToDisplay .count () );
330
352
mStatisticsTable ->setColumnCount ( 2 );
331
353
332
354
int row = 0 ;
333
- Q_FOREACH ( QgsDateTimeStatisticalSummary::Statistic stat, mDisplayDateTimeStats )
355
+ Q_FOREACH ( QgsDateTimeStatisticalSummary::Statistic stat, statsToDisplay )
334
356
{
335
357
QString value = ( stat == QgsDateTimeStatisticalSummary::Range
336
358
? tr ( " %1 seconds" ).arg ( stats.range ().seconds () )
@@ -361,3 +383,93 @@ void QgsStatisticalSummaryDockWidget::addRow( int row, const QString& name, cons
361
383
mStatisticsTable ->setItem ( row, 1 , valueItem );
362
384
}
363
385
386
+ void QgsStatisticalSummaryDockWidget::refreshStatisticsMenu ()
387
+ {
388
+ mStatisticsMenu ->clear ();
389
+ mStatsActions .clear ();
390
+
391
+ QSettings settings;
392
+ switch ( mFieldType )
393
+ {
394
+ case DataType::Numeric:
395
+ {
396
+ Q_FOREACH ( QgsStatisticalSummary::Statistic stat, mDisplayStats )
397
+ {
398
+ QAction *action = new QAction ( QgsStatisticalSummary::displayName ( stat ), mStatisticsMenu );
399
+ action->setCheckable ( true );
400
+ bool checked = settings.value ( QString ( " StatisticalSummaryDock/numeric_%1" ).arg ( stat ), true ).toBool ();
401
+ action->setChecked ( checked );
402
+ action->setData ( stat );
403
+ mStatsActions .insert ( stat, action );
404
+ connect ( action, SIGNAL ( triggered ( bool ) ), this , SLOT ( statActionTriggered ( bool ) ) );
405
+ mStatisticsMenu ->addAction ( action );
406
+ }
407
+
408
+ // count of null values statistic
409
+ QAction *nullCountAction = new QAction ( tr ( " Missing (null) values" ), mStatisticsMenu );
410
+ nullCountAction->setCheckable ( true );
411
+ bool checked = settings.value ( QString ( " StatisticalSummaryDock/numeric_missing_values" ), true ).toBool ();
412
+ nullCountAction->setChecked ( checked );
413
+ nullCountAction->setData ( MISSING_VALUES );
414
+ mStatsActions .insert ( MISSING_VALUES, nullCountAction );
415
+ connect ( nullCountAction, SIGNAL ( triggered ( bool ) ), this , SLOT ( statActionTriggered ( bool ) ) );
416
+ mStatisticsMenu ->addAction ( nullCountAction );
417
+
418
+ break ;
419
+ }
420
+ case DataType::String:
421
+ {
422
+ Q_FOREACH ( QgsStringStatisticalSummary::Statistic stat, mDisplayStringStats )
423
+ {
424
+ QAction *action = new QAction ( QgsStringStatisticalSummary::displayName ( stat ), mStatisticsMenu );
425
+ action->setCheckable ( true );
426
+ bool checked = settings.value ( QString ( " StatisticalSummaryDock/string_%1" ).arg ( stat ), true ).toBool ();
427
+ action->setChecked ( checked );
428
+ action->setData ( stat );
429
+ mStatsActions .insert ( stat, action );
430
+ connect ( action, SIGNAL ( triggered ( bool ) ), this , SLOT ( statActionTriggered ( bool ) ) );
431
+ mStatisticsMenu ->addAction ( action );
432
+ }
433
+ break ;
434
+ }
435
+ case DataType::DateTime:
436
+ {
437
+ Q_FOREACH ( QgsDateTimeStatisticalSummary::Statistic stat, mDisplayDateTimeStats )
438
+ {
439
+ QAction *action = new QAction ( QgsDateTimeStatisticalSummary::displayName ( stat ), mStatisticsMenu );
440
+ action->setCheckable ( true );
441
+ bool checked = settings.value ( QString ( " StatisticalSummaryDock/datetime_%1" ).arg ( stat ), true ).toBool ();
442
+ action->setChecked ( checked );
443
+ action->setData ( stat );
444
+ mStatsActions .insert ( stat, action );
445
+ connect ( action, SIGNAL ( triggered ( bool ) ), this , SLOT ( statActionTriggered ( bool ) ) );
446
+ mStatisticsMenu ->addAction ( action );
447
+ }
448
+ break ;
449
+ }
450
+ default :
451
+ break ;
452
+ }
453
+ }
454
+
455
+ QgsStatisticalSummaryDockWidget::DataType QgsStatisticalSummaryDockWidget::fieldType ( const QString &fieldName )
456
+ {
457
+ QgsField field = mLayer ->fields ().field ( fieldName );
458
+ if ( field.isNumeric () )
459
+ {
460
+ return DataType::Numeric;
461
+ }
462
+
463
+ switch ( field.type () )
464
+ {
465
+ case QVariant::String:
466
+ return DataType::String;
467
+ case QVariant::Date:
468
+ case QVariant::DateTime:
469
+ return DataType::DateTime;
470
+ default :
471
+ break ;
472
+ }
473
+
474
+ return DataType::Numeric;
475
+ }
0 commit comments