ctkSettingsPanel.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. struct PropertyType
  23. {
  24. PropertyType();
  25. QObject* Object;
  26. QString Property;
  27. QVariant PreviousValue;
  28. QVariant DefaultValue;
  29. QVariant value()const;
  30. bool setValue(const QVariant& value);
  31. };
  32. // --------------------------------------------------------------------------
  33. PropertyType::PropertyType()
  34. : Object(0)
  35. {
  36. }
  37. // --------------------------------------------------------------------------
  38. QVariant PropertyType::value()const
  39. {
  40. if (this->Object == 0 ||
  41. this->Property.isEmpty())
  42. {
  43. return QVariant();
  44. }
  45. return this->Object->property(this->Property.toLatin1());
  46. }
  47. // --------------------------------------------------------------------------
  48. bool PropertyType::setValue(const QVariant& val)
  49. {
  50. if (this->Object == 0 || this->Property.isEmpty())
  51. {
  52. Q_ASSERT(this->Object && !this->Property.isEmpty());
  53. return false;
  54. }
  55. // the following returns true if the property has been added using Q_PROPERTY
  56. // false otherwise (and the property is then a dynamic property)
  57. return this->Object->setProperty(this->Property.toLatin1(), val);
  58. }
  59. //-----------------------------------------------------------------------------
  60. class ctkSettingsPanelPrivate
  61. {
  62. Q_DECLARE_PUBLIC(ctkSettingsPanel);
  63. protected:
  64. ctkSettingsPanel* const q_ptr;
  65. public:
  66. ctkSettingsPanelPrivate(ctkSettingsPanel& object);
  67. void init();
  68. QSettings* Settings;
  69. QMap<QString, PropertyType> Properties;
  70. QSignalMapper* SignalMapper;
  71. bool SaveToSettingsWhenRegister;
  72. };
  73. // --------------------------------------------------------------------------
  74. ctkSettingsPanelPrivate::ctkSettingsPanelPrivate(ctkSettingsPanel& object)
  75. :q_ptr(&object)
  76. {
  77. this->Settings = 0;
  78. this->SignalMapper = 0;
  79. this->SaveToSettingsWhenRegister = true;
  80. }
  81. // --------------------------------------------------------------------------
  82. void ctkSettingsPanelPrivate::init()
  83. {
  84. Q_Q(ctkSettingsPanel);
  85. this->SignalMapper = new QSignalMapper(q);
  86. QObject::connect(this->SignalMapper, SIGNAL(mapped(const QString&)),
  87. q, SLOT(updateSetting(const QString&)));
  88. }
  89. // --------------------------------------------------------------------------
  90. ctkSettingsPanel::ctkSettingsPanel(QWidget* _parent)
  91. : Superclass(_parent)
  92. , d_ptr(new ctkSettingsPanelPrivate(*this))
  93. {
  94. Q_D(ctkSettingsPanel);
  95. d->init();
  96. }
  97. // --------------------------------------------------------------------------
  98. ctkSettingsPanel::~ctkSettingsPanel()
  99. {
  100. this->applySettings();
  101. }
  102. // --------------------------------------------------------------------------
  103. QSettings* ctkSettingsPanel::settings()const
  104. {
  105. Q_D(const ctkSettingsPanel);
  106. return d->Settings;
  107. }
  108. // --------------------------------------------------------------------------
  109. void ctkSettingsPanel::setSettings(QSettings* settings)
  110. {
  111. Q_D(ctkSettingsPanel);
  112. if (d->Settings == settings)
  113. {
  114. return;
  115. }
  116. d->Settings = settings;
  117. this->updateProperties();
  118. }
  119. // --------------------------------------------------------------------------
  120. void ctkSettingsPanel::updateProperties()
  121. {
  122. Q_D(ctkSettingsPanel);
  123. if (!d->Settings)
  124. {
  125. return;
  126. }
  127. foreach(const QString& key, d->Properties.keys())
  128. {
  129. if (d->Settings->contains(key))
  130. {
  131. QVariant value = d->Settings->value(key);
  132. PropertyType& prop = d->Properties[key];
  133. // Update object registered using registerProperty()
  134. bool res = prop.setValue(value);
  135. Q_ASSERT(res);
  136. Q_UNUSED(res);
  137. prop.PreviousValue = value;
  138. }
  139. else
  140. {
  141. this->updateSetting(key);
  142. }
  143. }
  144. }
  145. // --------------------------------------------------------------------------
  146. void ctkSettingsPanel::updateSetting(const QString& key)
  147. {
  148. Q_D(ctkSettingsPanel);
  149. if (!d->Settings)
  150. {
  151. return;
  152. }
  153. this->setSetting(key, d->Properties[key].value());
  154. }
  155. // --------------------------------------------------------------------------
  156. void ctkSettingsPanel::setSetting(const QString& key, const QVariant& newVal)
  157. {
  158. Q_D(ctkSettingsPanel);
  159. QVariant oldVal = d->Settings->value(key);
  160. d->Settings->setValue(key, newVal);
  161. d->Properties[key].setValue(newVal);
  162. if (d->Settings->status() != QSettings::NoError)
  163. {
  164. logger.warn( QString("Error %1 while writing setting %1")
  165. .arg(static_cast<int>(d->Settings->status()))
  166. .arg(key));
  167. }
  168. if (oldVal != newVal)
  169. {
  170. emit settingChanged(key, newVal);
  171. }
  172. }
  173. // --------------------------------------------------------------------------
  174. void ctkSettingsPanel::registerProperty(const QString& key,
  175. QObject* object,
  176. const QString& property,
  177. const char* signal)
  178. {
  179. Q_D(ctkSettingsPanel);
  180. PropertyType prop;
  181. prop.Object = object;
  182. prop.Property = property;
  183. prop.DefaultValue = prop.PreviousValue = prop.value();
  184. if (d->Settings && d->Settings->contains(key))
  185. {
  186. QVariant val = d->Settings->value(key);
  187. prop.setValue(val);
  188. prop.PreviousValue = val;
  189. }
  190. d->Properties[key] = prop;
  191. d->SignalMapper->setMapping(object, key);
  192. this->connect(object, signal, d->SignalMapper, SLOT(map()));
  193. if (d->SaveToSettingsWhenRegister)
  194. {
  195. this->updateSetting(key);
  196. }
  197. }
  198. // --------------------------------------------------------------------------
  199. void ctkSettingsPanel::applySettings()
  200. {
  201. Q_D(ctkSettingsPanel);
  202. foreach(const QString& key, d->Properties.keys())
  203. {
  204. PropertyType& prop = d->Properties[key];
  205. prop.PreviousValue = prop.value();
  206. }
  207. }
  208. // --------------------------------------------------------------------------
  209. void ctkSettingsPanel::resetSettings()
  210. {
  211. Q_D(ctkSettingsPanel);
  212. foreach(const QString& key, d->Properties.keys())
  213. {
  214. this->setSetting(key, d->Properties[key].PreviousValue);
  215. }
  216. }
  217. // --------------------------------------------------------------------------
  218. void ctkSettingsPanel::restoreDefaultSettings()
  219. {
  220. Q_D(ctkSettingsPanel);
  221. foreach(const QString& key, d->Properties.keys())
  222. {
  223. this->setSetting(key, d->Properties[key].DefaultValue);
  224. }
  225. }