ctkSettingsPanel.cpp 11 KB

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