ctkSettingsPanel.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. QString Label;
  34. ctkSettingsPanel::SettingOptions Options;
  35. QVariant value()const;
  36. bool setValue(const QVariant& value);
  37. QMetaProperty metaProperty();
  38. };
  39. // --------------------------------------------------------------------------
  40. PropertyType::PropertyType()
  41. : Object(0)
  42. , Options(ctkSettingsPanel::OptionNone)
  43. {
  44. }
  45. // --------------------------------------------------------------------------
  46. QVariant PropertyType::value()const
  47. {
  48. if (this->Object == 0 ||
  49. this->Property.isEmpty())
  50. {
  51. return QVariant();
  52. }
  53. return this->Object->property(this->Property.toLatin1());
  54. }
  55. // --------------------------------------------------------------------------
  56. bool PropertyType::setValue(const QVariant& val)
  57. {
  58. if (this->Object == 0 || this->Property.isEmpty())
  59. {
  60. Q_ASSERT(this->Object && !this->Property.isEmpty());
  61. return false;
  62. }
  63. QVariant value(val);
  64. // HACK - See http://bugreports.qt.nokia.com/browse/QTBUG-19823
  65. if (qstrcmp(this->metaProperty().typeName(), "QStringList") == 0 && !value.isValid())
  66. {
  67. value = QVariant(QStringList());
  68. }
  69. bool success = this->Object->setProperty(this->Property.toLatin1(), value);
  70. Q_ASSERT(success);
  71. return success;
  72. }
  73. // --------------------------------------------------------------------------
  74. QMetaProperty PropertyType::metaProperty()
  75. {
  76. Q_ASSERT(this->Object);
  77. for(int i=0; i < this->Object->metaObject()->propertyCount(); ++i)
  78. {
  79. this->Object->metaObject()->property(i);
  80. if (this->Object->metaObject()->property(i).name() == this->Property)
  81. {
  82. return this->Object->metaObject()->property(i);
  83. }
  84. }
  85. return QMetaProperty();
  86. }
  87. } // end of anonymous namespace
  88. //-----------------------------------------------------------------------------
  89. class ctkSettingsPanelPrivate
  90. {
  91. Q_DECLARE_PUBLIC(ctkSettingsPanel);
  92. protected:
  93. ctkSettingsPanel* const q_ptr;
  94. public:
  95. ctkSettingsPanelPrivate(ctkSettingsPanel& object);
  96. void init();
  97. QSettings* Settings;
  98. QMap<QString, PropertyType> Properties;
  99. bool SaveToSettingsWhenRegister;
  100. };
  101. // --------------------------------------------------------------------------
  102. ctkSettingsPanelPrivate::ctkSettingsPanelPrivate(ctkSettingsPanel& object)
  103. :q_ptr(&object)
  104. {
  105. qRegisterMetaType<ctkSettingsPanel::SettingOption>("ctkSettingsPanel::SettingOption");
  106. qRegisterMetaType<ctkSettingsPanel::SettingOptions>("ctkSettingsPanel::SettingOptions");
  107. this->Settings = 0;
  108. this->SaveToSettingsWhenRegister = true;
  109. }
  110. // --------------------------------------------------------------------------
  111. void ctkSettingsPanelPrivate::init()
  112. {
  113. }
  114. // --------------------------------------------------------------------------
  115. ctkSettingsPanel::ctkSettingsPanel(QWidget* _parent)
  116. : Superclass(_parent)
  117. , d_ptr(new ctkSettingsPanelPrivate(*this))
  118. {
  119. Q_D(ctkSettingsPanel);
  120. d->init();
  121. }
  122. // --------------------------------------------------------------------------
  123. ctkSettingsPanel::~ctkSettingsPanel()
  124. {
  125. this->applySettings();
  126. }
  127. // --------------------------------------------------------------------------
  128. QSettings* ctkSettingsPanel::settings()const
  129. {
  130. Q_D(const ctkSettingsPanel);
  131. return d->Settings;
  132. }
  133. // --------------------------------------------------------------------------
  134. void ctkSettingsPanel::setSettings(QSettings* settings)
  135. {
  136. Q_D(ctkSettingsPanel);
  137. if (d->Settings == settings)
  138. {
  139. return;
  140. }
  141. d->Settings = settings;
  142. this->updateProperties();
  143. }
  144. // --------------------------------------------------------------------------
  145. void ctkSettingsPanel::updateProperties()
  146. {
  147. Q_D(ctkSettingsPanel);
  148. if (!d->Settings)
  149. {
  150. return;
  151. }
  152. foreach(const QString& key, d->Properties.keys())
  153. {
  154. if (d->Settings->contains(key))
  155. {
  156. QVariant value = d->Settings->value(key);
  157. PropertyType& prop = d->Properties[key];
  158. // Update object registered using registerProperty()
  159. prop.setValue(value);
  160. prop.PreviousValue = value;
  161. }
  162. else
  163. {
  164. this->updateSetting(key);
  165. }
  166. }
  167. }
  168. // --------------------------------------------------------------------------
  169. void ctkSettingsPanel::updateSetting(const QString& key)
  170. {
  171. Q_D(ctkSettingsPanel);
  172. if (!d->Settings)
  173. {
  174. return;
  175. }
  176. this->setSetting(key, d->Properties[key].value());
  177. }
  178. // --------------------------------------------------------------------------
  179. void ctkSettingsPanel::setSetting(const QString& key, const QVariant& newVal)
  180. {
  181. Q_D(ctkSettingsPanel);
  182. if (!d->Settings)
  183. {
  184. return;
  185. }
  186. QVariant oldVal = d->Settings->value(key);
  187. d->Settings->setValue(key, newVal);
  188. d->Properties[key].setValue(newVal);
  189. if (d->Settings->status() != QSettings::NoError)
  190. {
  191. logger.warn( QString("Error #%1 while writing setting \"%2\"")
  192. .arg(static_cast<int>(d->Settings->status()))
  193. .arg(key));
  194. }
  195. if (oldVal != newVal)
  196. {
  197. emit settingChanged(key, newVal);
  198. }
  199. }
  200. // --------------------------------------------------------------------------
  201. void ctkSettingsPanel::registerProperty(const QString& key,
  202. QObject* object,
  203. const QString& property,
  204. const char* signal,
  205. const QString& label,
  206. ctkSettingsPanel::SettingOptions options)
  207. {
  208. Q_D(ctkSettingsPanel);
  209. PropertyType prop;
  210. prop.Object = object;
  211. prop.Property = property;
  212. prop.DefaultValue = prop.PreviousValue = prop.value();
  213. prop.Label = label;
  214. prop.Options = options;
  215. if (d->Settings && d->Settings->contains(key))
  216. {
  217. QVariant val = d->Settings->value(key);
  218. prop.setValue(val);
  219. prop.PreviousValue = val;
  220. }
  221. d->Properties[key] = prop;
  222. // Create a signal mapper per property to be able to support
  223. // multiple signals from the same sender.
  224. QSignalMapper* signalMapper = new QSignalMapper(this);
  225. QObject::connect(signalMapper, SIGNAL(mapped(QString)),
  226. this, SLOT(updateSetting(QString)));
  227. signalMapper->setMapping(object, key);
  228. this->connect(object, signal, signalMapper, SLOT(map()));
  229. if (d->SaveToSettingsWhenRegister)
  230. {
  231. this->updateSetting(key);
  232. }
  233. }
  234. // --------------------------------------------------------------------------
  235. QVariant ctkSettingsPanel::defaultPropertyValue(const QString& key) const
  236. {
  237. Q_D(const ctkSettingsPanel);
  238. if (!d->Properties.contains(key))
  239. {
  240. return QVariant();
  241. }
  242. return d->Properties.value(key).DefaultValue;
  243. }
  244. // --------------------------------------------------------------------------
  245. QVariant ctkSettingsPanel::previousPropertyValue(const QString& key) const
  246. {
  247. Q_D(const ctkSettingsPanel);
  248. if (!d->Properties.contains(key))
  249. {
  250. return QVariant();
  251. }
  252. return d->Properties.value(key).PreviousValue;
  253. }
  254. // --------------------------------------------------------------------------
  255. QVariant ctkSettingsPanel::propertyValue(const QString& key) const
  256. {
  257. Q_D(const ctkSettingsPanel);
  258. if (!d->Properties.contains(key))
  259. {
  260. return QVariant();
  261. }
  262. return d->Properties.value(key).value();
  263. }
  264. // --------------------------------------------------------------------------
  265. QStringList ctkSettingsPanel::changedSettings()const
  266. {
  267. Q_D(const ctkSettingsPanel);
  268. QStringList settingsKeys;
  269. foreach(const QString& key, d->Properties.keys())
  270. {
  271. const PropertyType& prop = d->Properties[key];
  272. if (prop.PreviousValue != prop.value())
  273. {
  274. settingsKeys << key;
  275. }
  276. }
  277. return settingsKeys;
  278. }
  279. // --------------------------------------------------------------------------
  280. QString ctkSettingsPanel::settingLabel(const QString& settingKey)const
  281. {
  282. Q_D(const ctkSettingsPanel);
  283. return d->Properties[settingKey].Label;
  284. }
  285. // --------------------------------------------------------------------------
  286. ctkSettingsPanel::SettingOptions ctkSettingsPanel
  287. ::settingOptions(const QString& settingKey)const
  288. {
  289. Q_D(const ctkSettingsPanel);
  290. return d->Properties[settingKey].Options;
  291. }
  292. // --------------------------------------------------------------------------
  293. void ctkSettingsPanel::applySettings()
  294. {
  295. Q_D(ctkSettingsPanel);
  296. foreach(const QString& key, d->Properties.keys())
  297. {
  298. PropertyType& prop = d->Properties[key];
  299. prop.PreviousValue = prop.value();
  300. }
  301. }
  302. // --------------------------------------------------------------------------
  303. void ctkSettingsPanel::resetSettings()
  304. {
  305. Q_D(ctkSettingsPanel);
  306. foreach(const QString& key, d->Properties.keys())
  307. {
  308. this->setSetting(key, d->Properties[key].PreviousValue);
  309. }
  310. }
  311. // --------------------------------------------------------------------------
  312. void ctkSettingsPanel::restoreDefaultSettings()
  313. {
  314. Q_D(ctkSettingsPanel);
  315. foreach(const QString& key, d->Properties.keys())
  316. {
  317. this->setSetting(key, d->Properties[key].DefaultValue);
  318. }
  319. }