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