ctkSettingsPanel.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.apache.org/licenses/LICENSE-2.0.txt
  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 <QMetaProperty>
  17. #include <QSettings>
  18. #include <QSignalMapper>
  19. // CTK includes
  20. #include "ctkSettingsPanel.h"
  21. #include "ctkLogger.h"
  22. static ctkLogger logger("org.commontk.libs.widgets.ctkSettingsPanel");
  23. namespace
  24. {
  25. // --------------------------------------------------------------------------
  26. struct PropertyType
  27. {
  28. PropertyType();
  29. QObject* Object;
  30. QString Property;
  31. QVariant PreviousValue;
  32. QVariant DefaultValue;
  33. QVariant value()const;
  34. bool setValue(const QVariant& value);
  35. QMetaProperty metaProperty();
  36. };
  37. // --------------------------------------------------------------------------
  38. PropertyType::PropertyType()
  39. : Object(0)
  40. {
  41. }
  42. // --------------------------------------------------------------------------
  43. QVariant PropertyType::value()const
  44. {
  45. if (this->Object == 0 ||
  46. this->Property.isEmpty())
  47. {
  48. return QVariant();
  49. }
  50. return this->Object->property(this->Property.toLatin1());
  51. }
  52. // --------------------------------------------------------------------------
  53. bool PropertyType::setValue(const QVariant& val)
  54. {
  55. if (this->Object == 0 || this->Property.isEmpty())
  56. {
  57. Q_ASSERT(this->Object && !this->Property.isEmpty());
  58. return false;
  59. }
  60. QVariant value(val);
  61. // HACK - See http://bugreports.qt.nokia.com/browse/QTBUG-19823
  62. if (qstrcmp(this->metaProperty().typeName(), "QStringList") == 0 && !value.isValid())
  63. {
  64. value = QVariant(QStringList());
  65. }
  66. bool success = this->Object->setProperty(this->Property.toLatin1(), value);
  67. Q_ASSERT(success);
  68. return success;
  69. }
  70. // --------------------------------------------------------------------------
  71. QMetaProperty PropertyType::metaProperty()
  72. {
  73. Q_ASSERT(this->Object);
  74. for(int i=0; i < this->Object->metaObject()->propertyCount(); ++i)
  75. {
  76. this->Object->metaObject()->property(i);
  77. if (this->Object->metaObject()->property(i).name() == this->Property)
  78. {
  79. return this->Object->metaObject()->property(i);
  80. }
  81. }
  82. return QMetaProperty();
  83. }
  84. } // end of anonymous namespace
  85. //-----------------------------------------------------------------------------
  86. class ctkSettingsPanelPrivate
  87. {
  88. Q_DECLARE_PUBLIC(ctkSettingsPanel);
  89. protected:
  90. ctkSettingsPanel* const q_ptr;
  91. public:
  92. ctkSettingsPanelPrivate(ctkSettingsPanel& object);
  93. void init();
  94. QSettings* Settings;
  95. QMap<QString, PropertyType> Properties;
  96. QSignalMapper* SignalMapper;
  97. bool SaveToSettingsWhenRegister;
  98. };
  99. // --------------------------------------------------------------------------
  100. ctkSettingsPanelPrivate::ctkSettingsPanelPrivate(ctkSettingsPanel& object)
  101. :q_ptr(&object)
  102. {
  103. this->Settings = 0;
  104. this->SignalMapper = 0;
  105. this->SaveToSettingsWhenRegister = true;
  106. }
  107. // --------------------------------------------------------------------------
  108. void ctkSettingsPanelPrivate::init()
  109. {
  110. Q_Q(ctkSettingsPanel);
  111. this->SignalMapper = new QSignalMapper(q);
  112. QObject::connect(this->SignalMapper, SIGNAL(mapped(QString)),
  113. q, SLOT(updateSetting(QString)));
  114. }
  115. // --------------------------------------------------------------------------
  116. ctkSettingsPanel::ctkSettingsPanel(QWidget* _parent)
  117. : Superclass(_parent)
  118. , d_ptr(new ctkSettingsPanelPrivate(*this))
  119. {
  120. Q_D(ctkSettingsPanel);
  121. d->init();
  122. }
  123. // --------------------------------------------------------------------------
  124. ctkSettingsPanel::~ctkSettingsPanel()
  125. {
  126. this->applySettings();
  127. }
  128. // --------------------------------------------------------------------------
  129. QSettings* ctkSettingsPanel::settings()const
  130. {
  131. Q_D(const ctkSettingsPanel);
  132. return d->Settings;
  133. }
  134. // --------------------------------------------------------------------------
  135. void ctkSettingsPanel::setSettings(QSettings* settings)
  136. {
  137. Q_D(ctkSettingsPanel);
  138. if (d->Settings == settings)
  139. {
  140. return;
  141. }
  142. d->Settings = settings;
  143. this->updateProperties();
  144. }
  145. // --------------------------------------------------------------------------
  146. void ctkSettingsPanel::updateProperties()
  147. {
  148. Q_D(ctkSettingsPanel);
  149. if (!d->Settings)
  150. {
  151. return;
  152. }
  153. foreach(const QString& key, d->Properties.keys())
  154. {
  155. if (d->Settings->contains(key))
  156. {
  157. QVariant value = d->Settings->value(key);
  158. PropertyType& prop = d->Properties[key];
  159. // Update object registered using registerProperty()
  160. prop.setValue(value);
  161. prop.PreviousValue = value;
  162. }
  163. else
  164. {
  165. this->updateSetting(key);
  166. }
  167. }
  168. }
  169. // --------------------------------------------------------------------------
  170. void ctkSettingsPanel::updateSetting(const QString& key)
  171. {
  172. Q_D(ctkSettingsPanel);
  173. if (!d->Settings)
  174. {
  175. return;
  176. }
  177. this->setSetting(key, d->Properties[key].value());
  178. }
  179. // --------------------------------------------------------------------------
  180. void ctkSettingsPanel::setSetting(const QString& key, const QVariant& newVal)
  181. {
  182. Q_D(ctkSettingsPanel);
  183. QVariant oldVal = d->Settings->value(key);
  184. d->Settings->setValue(key, newVal);
  185. d->Properties[key].setValue(newVal);
  186. if (d->Settings->status() != QSettings::NoError)
  187. {
  188. logger.warn( QString("Error %1 while writing setting %1")
  189. .arg(static_cast<int>(d->Settings->status()))
  190. .arg(key));
  191. }
  192. if (oldVal != newVal)
  193. {
  194. emit settingChanged(key, newVal);
  195. }
  196. }
  197. // --------------------------------------------------------------------------
  198. void ctkSettingsPanel::registerProperty(const QString& key,
  199. QObject* object,
  200. const QString& property,
  201. const char* signal)
  202. {
  203. Q_D(ctkSettingsPanel);
  204. PropertyType prop;
  205. prop.Object = object;
  206. prop.Property = property;
  207. prop.DefaultValue = prop.PreviousValue = prop.value();
  208. if (d->Settings && d->Settings->contains(key))
  209. {
  210. QVariant val = d->Settings->value(key);
  211. prop.setValue(val);
  212. prop.PreviousValue = val;
  213. }
  214. d->Properties[key] = prop;
  215. d->SignalMapper->setMapping(object, key);
  216. this->connect(object, signal, d->SignalMapper, SLOT(map()));
  217. if (d->SaveToSettingsWhenRegister)
  218. {
  219. this->updateSetting(key);
  220. }
  221. }
  222. // --------------------------------------------------------------------------
  223. QVariant ctkSettingsPanel::defaultPropertyValue(const QString& key) const
  224. {
  225. Q_D(const ctkSettingsPanel);
  226. if (!d->Properties.contains(key))
  227. {
  228. return QVariant();
  229. }
  230. return d->Properties.value(key).DefaultValue;
  231. }
  232. // --------------------------------------------------------------------------
  233. QVariant ctkSettingsPanel::previousPropertyValue(const QString& key) const
  234. {
  235. Q_D(const ctkSettingsPanel);
  236. if (!d->Properties.contains(key))
  237. {
  238. return QVariant();
  239. }
  240. return d->Properties.value(key).PreviousValue;
  241. }
  242. // --------------------------------------------------------------------------
  243. QVariant ctkSettingsPanel::propertyValue(const QString& key) const
  244. {
  245. Q_D(const ctkSettingsPanel);
  246. if (!d->Properties.contains(key))
  247. {
  248. return QVariant();
  249. }
  250. return d->Properties.value(key).value();
  251. }
  252. // --------------------------------------------------------------------------
  253. void ctkSettingsPanel::applySettings()
  254. {
  255. Q_D(ctkSettingsPanel);
  256. foreach(const QString& key, d->Properties.keys())
  257. {
  258. PropertyType& prop = d->Properties[key];
  259. prop.PreviousValue = prop.value();
  260. }
  261. }
  262. // --------------------------------------------------------------------------
  263. void ctkSettingsPanel::resetSettings()
  264. {
  265. Q_D(ctkSettingsPanel);
  266. foreach(const QString& key, d->Properties.keys())
  267. {
  268. this->setSetting(key, d->Properties[key].PreviousValue);
  269. }
  270. }
  271. // --------------------------------------------------------------------------
  272. void ctkSettingsPanel::restoreDefaultSettings()
  273. {
  274. Q_D(ctkSettingsPanel);
  275. foreach(const QString& key, d->Properties.keys())
  276. {
  277. this->setSetting(key, d->Properties[key].DefaultValue);
  278. }
  279. }