ctkSettingsPanel.cpp 12 KB

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