ctkSettingsPanel.cpp 11 KB

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