Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qsafesettings.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Carla plugin host
  3. * Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "qsafesettings.hpp"
  18. //---------------------------------------------------------------------------------------------------------------------
  19. bool QSafeSettings::valueBool(const QString& key, const bool defaultValue) const
  20. {
  21. QVariant var(value(key, defaultValue));
  22. #if QT_VERSION >= 0x60000
  23. if (!var.isNull() && var.convert(QMetaType(QMetaType::Bool)) && var.isValid())
  24. #else
  25. if (!var.isNull() && var.convert(QVariant::Bool) && var.isValid())
  26. #endif
  27. return var.toBool();
  28. return defaultValue;
  29. }
  30. Qt::CheckState QSafeSettings::valueCheckState(const QString& key, const Qt::CheckState defaultValue) const
  31. {
  32. QVariant var(value(key, defaultValue));
  33. #if QT_VERSION >= 0x60000
  34. if (!var.isNull() && var.convert(QMetaType(QMetaType::UInt)) && var.isValid())
  35. #else
  36. if (!var.isNull() && var.convert(QVariant::UInt) && var.isValid())
  37. #endif
  38. {
  39. const uint value = var.toUInt();
  40. switch (value)
  41. {
  42. case Qt::Unchecked:
  43. case Qt::PartiallyChecked:
  44. case Qt::Checked:
  45. return static_cast<Qt::CheckState>(value);
  46. }
  47. }
  48. return defaultValue;
  49. }
  50. int QSafeSettings::valueIntPositive(const QString& key, const int defaultValue) const
  51. {
  52. QVariant var(value(key, defaultValue));
  53. #if QT_VERSION >= 0x60000
  54. if (!var.isNull() && var.convert(QMetaType(QMetaType::Int)) && var.isValid())
  55. #else
  56. if (!var.isNull() && var.convert(QVariant::Int) && var.isValid())
  57. #endif
  58. {
  59. const int value = var.toInt();
  60. return value >= 0 ? value : defaultValue;
  61. }
  62. return defaultValue;
  63. }
  64. uint QSafeSettings::valueUInt(const QString& key, const uint defaultValue) const
  65. {
  66. QVariant var(value(key, defaultValue));
  67. #if QT_VERSION >= 0x60000
  68. if (!var.isNull() && var.convert(QMetaType(QMetaType::UInt)) && var.isValid())
  69. #else
  70. if (!var.isNull() && var.convert(QVariant::UInt) && var.isValid())
  71. #endif
  72. return var.toUInt();
  73. return defaultValue;
  74. }
  75. double QSafeSettings::valueDouble(const QString& key, const double defaultValue) const
  76. {
  77. QVariant var(value(key, defaultValue));
  78. #if QT_VERSION >= 0x60000
  79. if (!var.isNull() && var.convert(QMetaType(QMetaType::Double)) && var.isValid())
  80. #else
  81. if (!var.isNull() && var.convert(QVariant::Double) && var.isValid())
  82. #endif
  83. return var.toDouble();
  84. return defaultValue;
  85. }
  86. QString QSafeSettings::valueString(const QString& key, const QString& defaultValue) const
  87. {
  88. QVariant var(value(key, defaultValue));
  89. #if QT_VERSION >= 0x60000
  90. if (!var.isNull() && var.convert(QMetaType(QMetaType::QString)) && var.isValid())
  91. #else
  92. if (!var.isNull() && var.convert(QVariant::String) && var.isValid())
  93. #endif
  94. return var.toString();
  95. return defaultValue;
  96. }
  97. QByteArray QSafeSettings::valueByteArray(const QString& key, const QByteArray defaultValue) const
  98. {
  99. QVariant var(value(key, defaultValue));
  100. #if QT_VERSION >= 0x60000
  101. if (!var.isNull() && var.convert(QMetaType(QMetaType::QByteArray)) && var.isValid())
  102. #else
  103. if (!var.isNull() && var.convert(QVariant::ByteArray) && var.isValid())
  104. #endif
  105. return var.toByteArray();
  106. return defaultValue;
  107. }
  108. QStringList QSafeSettings::valueStringList(const QString& key, const QStringList defaultValue) const
  109. {
  110. QVariant var(value(key, defaultValue));
  111. #if QT_VERSION >= 0x60000
  112. if (!var.isNull() && var.convert(QMetaType(QMetaType::QStringList)) && var.isValid())
  113. #else
  114. if (!var.isNull() && var.convert(QVariant::StringList) && var.isValid())
  115. #endif
  116. return var.toStringList();
  117. return defaultValue;
  118. }
  119. //---------------------------------------------------------------------------------------------------------------------