@@ -232,9 +232,10 @@ class CORE_EXPORT QgsSettings : public QObject
232
232
*/
233
233
template <class T >
234
234
T enumValue ( const QString &key, const T &defaultValue,
235
- const Section section = NoSection ) const
235
+ const Section section = NoSection )
236
236
{
237
237
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
238
+ Q_ASSERT ( metaEnum.isValid () );
238
239
if ( !metaEnum.isValid () )
239
240
{
240
241
QgsDebugMsg ( " Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." );
@@ -245,16 +246,29 @@ class CORE_EXPORT QgsSettings : public QObject
245
246
246
247
if ( metaEnum.isValid () )
247
248
{
249
+ // read as string
248
250
QByteArray ba = value ( key, metaEnum.valueToKey ( defaultValue ) ).toString ().toUtf8 ();
249
251
const char *vs = ba.data ();
250
252
v = static_cast <T>( metaEnum.keyToValue ( vs, &ok ) );
251
253
}
252
254
if ( !ok )
253
255
{
256
+ // if failed, try to read as int (old behavior)
257
+ // this code shall be removed later (probably after QGIS 3.4 LTR for 3.6)
258
+ // then the method could be marked as const
254
259
v = static_cast <T>( value ( key, static_cast <int >( defaultValue ), section ).toInt ( &ok ) );
255
- if ( metaEnum.isValid () && ( !ok || !metaEnum. valueToKey ( static_cast < int >( v ) ) ) )
260
+ if ( metaEnum.isValid () )
256
261
{
257
- v = defaultValue;
262
+ if ( !ok || !metaEnum.valueToKey ( static_cast <int >( v ) ) )
263
+ {
264
+ v = defaultValue;
265
+ }
266
+ else
267
+ {
268
+ // found setting as an integer
269
+ // convert the setting to the new form (string)
270
+ setEnumValue ( key, v, section );
271
+ }
258
272
}
259
273
}
260
274
@@ -273,6 +287,7 @@ class CORE_EXPORT QgsSettings : public QObject
273
287
const Section section = NoSection )
274
288
{
275
289
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
290
+ Q_ASSERT ( metaEnum.isValid () );
276
291
if ( metaEnum.isValid () )
277
292
{
278
293
setValue ( key, metaEnum.valueToKey ( value ), section );
@@ -294,9 +309,10 @@ class CORE_EXPORT QgsSettings : public QObject
294
309
*/
295
310
template <class T >
296
311
T flagValue ( const QString &key, const T &defaultValue,
297
- const Section section = NoSection ) const
312
+ const Section section = NoSection )
298
313
{
299
314
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
315
+ Q_ASSERT ( metaEnum.isValid () );
300
316
if ( !metaEnum.isValid () )
301
317
{
302
318
QgsDebugMsg ( " Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." );
@@ -305,19 +321,31 @@ class CORE_EXPORT QgsSettings : public QObject
305
321
T v;
306
322
bool ok = false ;
307
323
308
-
309
324
if ( metaEnum.isValid () )
310
325
{
326
+ // read as string
311
327
QByteArray ba = value ( key, metaEnum.valueToKeys ( defaultValue ) ).toString ().toUtf8 ();
312
328
const char *vs = ba.data ();
313
329
v = static_cast <T>( metaEnum.keysToValue ( vs, &ok ) );
314
330
}
315
331
if ( !ok )
316
332
{
333
+ // if failed, try to read as int (old behavior)
334
+ // this code shall be removed later (probably after QGIS 3.4 LTR for 3.6)
335
+ // then the method could be marked as const
317
336
v = T ( value ( key, static_cast <int >( defaultValue ), section ).toInt ( &ok ) );
318
- if ( metaEnum.isValid () && ( !ok || !metaEnum. valueToKeys ( static_cast < int >( v ) ). size () ) )
337
+ if ( metaEnum.isValid () )
319
338
{
320
- v = defaultValue;
339
+ if ( !ok || !metaEnum.valueToKeys ( static_cast <int >( v ) ).size () )
340
+ {
341
+ v = defaultValue;
342
+ }
343
+ else
344
+ {
345
+ // found setting as an integer
346
+ // convert the setting to the new form (string)
347
+ setFlagValue ( key, v, section );
348
+ }
321
349
}
322
350
}
323
351
@@ -336,6 +364,7 @@ class CORE_EXPORT QgsSettings : public QObject
336
364
const Section section = NoSection )
337
365
{
338
366
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
367
+ Q_ASSERT ( metaEnum.isValid () );
339
368
if ( metaEnum.isValid () )
340
369
{
341
370
setValue ( key, metaEnum.valueToKeys ( value ), section );
0 commit comments