ctkSettingsPanel.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QSettings>
  17. #include <QSignalMapper>
  18. // CTK includes
  19. #include "ctkSettingsPanel.h"
  20. #include "ctkLogger.h"
  21. static ctkLogger logger("org.commontk.libs.widgets.ctkSettingsPanel");
  22. typedef QPair<QObject*, QString> PropertyType;
  23. //-----------------------------------------------------------------------------
  24. class ctkSettingsPanelPrivate
  25. {
  26. Q_DECLARE_PUBLIC(ctkSettingsPanel);
  27. protected:
  28. ctkSettingsPanel* const q_ptr;
  29. public:
  30. ctkSettingsPanelPrivate(ctkSettingsPanel& object);
  31. void init();
  32. PropertyType property(const QString& key);
  33. QVariant propertyValue(const PropertyType& property)const;
  34. bool setPropertyValue(const PropertyType& property, const QVariant& val);
  35. QSettings* Settings;
  36. QMap<QString, PropertyType > Properties;
  37. QSignalMapper* SignalMapper;
  38. bool SaveToSettingsWhenRegister;
  39. };
  40. // --------------------------------------------------------------------------
  41. ctkSettingsPanelPrivate::ctkSettingsPanelPrivate(ctkSettingsPanel& object)
  42. :q_ptr(&object)
  43. {
  44. this->Settings = 0;
  45. this->SignalMapper = 0;
  46. this->SaveToSettingsWhenRegister = true;
  47. }
  48. // --------------------------------------------------------------------------
  49. void ctkSettingsPanelPrivate::init()
  50. {
  51. Q_Q(ctkSettingsPanel);
  52. this->SignalMapper = new QSignalMapper(q);
  53. QObject::connect(this->SignalMapper, SIGNAL(mapped(const QString&)),
  54. q, SLOT(updateSetting(const QString&)));
  55. }
  56. // --------------------------------------------------------------------------
  57. PropertyType ctkSettingsPanelPrivate::property(const QString& key)
  58. {
  59. PropertyType defaultProp(0, QString());
  60. return this->Properties.value(key, defaultProp);
  61. }
  62. // --------------------------------------------------------------------------
  63. QVariant ctkSettingsPanelPrivate::propertyValue(const PropertyType& prop)const
  64. {
  65. if (prop.first == 0 ||
  66. prop.second.isEmpty())
  67. {
  68. return QVariant();
  69. }
  70. return prop.first->property(prop.second.toLatin1());
  71. }
  72. // --------------------------------------------------------------------------
  73. bool ctkSettingsPanelPrivate::setPropertyValue(const PropertyType& prop, const QVariant& val)
  74. {
  75. if (prop.first == 0 ||
  76. prop.second.isEmpty())
  77. {
  78. return false;
  79. }
  80. // the following return true if the property has been added using Q_PROPERTY
  81. // false otherwise (and the property is then a dynamic property)
  82. return prop.first->setProperty(prop.second.toLatin1(), val);
  83. }
  84. // --------------------------------------------------------------------------
  85. ctkSettingsPanel::ctkSettingsPanel(QWidget* _parent)
  86. : Superclass(_parent)
  87. , d_ptr(new ctkSettingsPanelPrivate(*this))
  88. {
  89. Q_D(ctkSettingsPanel);
  90. d->init();
  91. }
  92. // --------------------------------------------------------------------------
  93. ctkSettingsPanel::~ctkSettingsPanel()
  94. {
  95. }
  96. // --------------------------------------------------------------------------
  97. QSettings* ctkSettingsPanel::settings()const
  98. {
  99. Q_D(const ctkSettingsPanel);
  100. return d->Settings;
  101. }
  102. // --------------------------------------------------------------------------
  103. void ctkSettingsPanel::setSettings(QSettings* settings)
  104. {
  105. Q_D(ctkSettingsPanel);
  106. d->Settings = settings;
  107. this->updateProperties();
  108. }
  109. // --------------------------------------------------------------------------
  110. void ctkSettingsPanel::updateProperties()
  111. {
  112. Q_D(ctkSettingsPanel);
  113. if (!d->Settings)
  114. {
  115. return;
  116. }
  117. foreach(const QString& key, d->Properties.keys())
  118. {
  119. QVariant value = d->Settings->value(key);
  120. if (value.isValid())
  121. {
  122. bool res = d->setPropertyValue(d->property(key), value);
  123. Q_ASSERT(res);
  124. Q_UNUSED(res);
  125. }
  126. else
  127. {
  128. this->updateSetting(key);
  129. }
  130. }
  131. }
  132. // --------------------------------------------------------------------------
  133. void ctkSettingsPanel::registerProperty(const QString& key,
  134. QObject* object,
  135. const QString& property,
  136. const char* signal)
  137. {
  138. Q_D(ctkSettingsPanel);
  139. d->Properties[key] = PropertyType(object, property);
  140. d->SignalMapper->setMapping(object, key);
  141. connect(object, signal, d->SignalMapper, SLOT(map()));
  142. if (d->SaveToSettingsWhenRegister)
  143. {
  144. this->updateSetting(key);
  145. }
  146. }
  147. // --------------------------------------------------------------------------
  148. void ctkSettingsPanel::updateSetting(const QString& key)
  149. {
  150. Q_D(ctkSettingsPanel);
  151. if (!d->Settings)
  152. {
  153. return;
  154. }
  155. QVariant oldVal = d->Settings->value(key);
  156. QVariant newVal = d->propertyValue(d->property(key));
  157. d->Settings->setValue(key, newVal);
  158. if (d->Settings->status() != QSettings::NoError)
  159. {
  160. logger.warn( QString("Error %1 while writing setting %1")
  161. .arg(d->Settings->status())
  162. .arg(key));
  163. }
  164. if (oldVal != newVal)
  165. {
  166. emit settingChanged(key, newVal);
  167. }
  168. }