ctkSettingsPanel.cpp 13 KB

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