ctkSettingsDialog.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.commontk.org/LICENSE
  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 <QMap>
  17. #include <QPushButton>
  18. #include <QSettings>
  19. // CTK includes
  20. #include "ctkSettingsPanel.h"
  21. #include "ctkSettingsDialog.h"
  22. #include "ui_ctkSettingsDialog.h"
  23. #include "ctkLogger.h"
  24. static ctkLogger logger("org.commontk.libs.widgets.ctkSettingsDialog");
  25. //-----------------------------------------------------------------------------
  26. class ctkSettingsDialogPrivate: public Ui_ctkSettingsDialog
  27. {
  28. Q_DECLARE_PUBLIC(ctkSettingsDialog);
  29. protected:
  30. ctkSettingsDialog* const q_ptr;
  31. public:
  32. ctkSettingsDialogPrivate(ctkSettingsDialog& object);
  33. void init();
  34. ctkSettingsPanel* panel(QTreeWidgetItem* item)const;
  35. QTreeWidgetItem* item(ctkSettingsPanel* panel)const;
  36. QTreeWidgetItem* item(const QString& label)const;
  37. void beginGroup(ctkSettingsPanel* panel);
  38. void endGroup(ctkSettingsPanel* panel);
  39. QSettings* Settings;
  40. protected:
  41. QMap<QTreeWidgetItem*, ctkSettingsPanel*> Panels;
  42. };
  43. // --------------------------------------------------------------------------
  44. ctkSettingsDialogPrivate::ctkSettingsDialogPrivate(ctkSettingsDialog& object)
  45. :q_ptr(&object)
  46. {
  47. this->Settings = 0;
  48. }
  49. // --------------------------------------------------------------------------
  50. void ctkSettingsDialogPrivate::init()
  51. {
  52. Q_Q(ctkSettingsDialog);
  53. this->Settings = new QSettings(q);
  54. this->setupUi(q);
  55. QObject::connect(this->SettingsTreeWidget,
  56. SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
  57. q, SLOT(onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
  58. QObject::connect(this->SettingsButtonBox, SIGNAL(clicked(QAbstractButton*)),
  59. q, SLOT(onDialogButtonClicked(QAbstractButton*)));
  60. }
  61. // --------------------------------------------------------------------------
  62. ctkSettingsPanel* ctkSettingsDialogPrivate::panel(QTreeWidgetItem* item)const
  63. {
  64. return this->Panels.value(item, 0);
  65. }
  66. // --------------------------------------------------------------------------
  67. QTreeWidgetItem* ctkSettingsDialogPrivate::item(ctkSettingsPanel* panel)const
  68. {
  69. return this->Panels.key(panel, this->SettingsTreeWidget->invisibleRootItem());
  70. }
  71. // --------------------------------------------------------------------------
  72. QTreeWidgetItem* ctkSettingsDialogPrivate::item(const QString& label)const
  73. {
  74. QMap<QTreeWidgetItem*, ctkSettingsPanel*>::const_iterator it;
  75. for (it = this->Panels.constBegin(); it != this->Panels.constEnd(); ++it)
  76. {
  77. if (it.value()->windowTitle() == label)
  78. {
  79. return it.key();
  80. }
  81. }
  82. return this->SettingsTreeWidget->invisibleRootItem();
  83. }
  84. // --------------------------------------------------------------------------
  85. ctkSettingsDialog::ctkSettingsDialog(QWidget* _parent)
  86. : Superclass(_parent)
  87. , d_ptr(new ctkSettingsDialogPrivate(*this))
  88. {
  89. Q_D(ctkSettingsDialog);
  90. d->init();
  91. }
  92. // --------------------------------------------------------------------------
  93. ctkSettingsDialog::~ctkSettingsDialog()
  94. {
  95. }
  96. // --------------------------------------------------------------------------
  97. QSettings* ctkSettingsDialog::settings()const
  98. {
  99. Q_D(const ctkSettingsDialog);
  100. return d->Settings;
  101. }
  102. // --------------------------------------------------------------------------
  103. void ctkSettingsDialog::setSettings(QSettings* settings)
  104. {
  105. Q_D(ctkSettingsDialog);
  106. d->Settings = settings;
  107. foreach(ctkSettingsPanel* panel, d->Panels.values())
  108. {
  109. panel->setSettings(settings);
  110. }
  111. }
  112. // --------------------------------------------------------------------------
  113. void ctkSettingsDialog
  114. ::addPanel(ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel)
  115. {
  116. Q_D(ctkSettingsDialog);
  117. QTreeWidgetItem* newPanelItem = new QTreeWidgetItem;
  118. newPanelItem->setText(0, panel->windowTitle());
  119. newPanelItem->setIcon(0, panel->windowIcon());
  120. d->Panels[newPanelItem] = panel;
  121. QTreeWidgetItem* parentItem = d->item(parentPanel);
  122. parentItem->addChild(newPanelItem);
  123. d->SettingsStackedWidget->addWidget(panel);
  124. connect(panel, SIGNAL(settingChanged(const QString&, const QVariant&)),
  125. this, SLOT(onSettingChanged(const QString&, const QVariant&)));
  126. panel->setSettings(this->settings());
  127. }
  128. // --------------------------------------------------------------------------
  129. void ctkSettingsDialog
  130. ::addPanel(const QString& label, ctkSettingsPanel* panel,
  131. ctkSettingsPanel* parentPanel)
  132. {
  133. panel->setWindowTitle(label);
  134. this->addPanel(panel, parentPanel);
  135. }
  136. // --------------------------------------------------------------------------
  137. void ctkSettingsDialog
  138. ::addPanel(const QString& label, const QIcon& icon,
  139. ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel)
  140. {
  141. panel->setWindowTitle(label);
  142. panel->setWindowIcon(icon);
  143. this->addPanel(panel, parentPanel);
  144. }
  145. // --------------------------------------------------------------------------
  146. void ctkSettingsDialog::setCurrentPanel(ctkSettingsPanel* panel)
  147. {
  148. Q_D(ctkSettingsDialog);
  149. // eventually calls onCurrentItemChanged() where all the work is done
  150. d->SettingsTreeWidget->setCurrentItem(d->item(panel));
  151. }
  152. // --------------------------------------------------------------------------
  153. void ctkSettingsDialog::setCurrentPanel(const QString& label)
  154. {
  155. Q_D(ctkSettingsDialog);
  156. // eventually calls onCurrentItemChanged() where all the work is done
  157. d->SettingsTreeWidget->setCurrentItem(d->item(label));
  158. }
  159. // --------------------------------------------------------------------------
  160. ctkSettingsPanel* ctkSettingsDialog::currentPanel()const
  161. {
  162. Q_D(const ctkSettingsDialog);
  163. return d->panel(d->SettingsTreeWidget->currentItem());
  164. }
  165. // --------------------------------------------------------------------------
  166. ctkSettingsPanel* ctkSettingsDialog::panel(const QString& label)const
  167. {
  168. Q_D(const ctkSettingsDialog);
  169. foreach(ctkSettingsPanel* settingsPanel, d->Panels.values())
  170. {
  171. if (settingsPanel->windowTitle() == label)
  172. {
  173. return settingsPanel;
  174. }
  175. }
  176. return 0;
  177. }
  178. // --------------------------------------------------------------------------
  179. void ctkSettingsDialog::accept()
  180. {
  181. this->applySettings();
  182. this->Superclass::accept();
  183. }
  184. // --------------------------------------------------------------------------
  185. void ctkSettingsDialog::reject()
  186. {
  187. this->resetSettings();
  188. this->Superclass::accept();
  189. }
  190. // --------------------------------------------------------------------------
  191. void ctkSettingsDialog::applySettings()
  192. {
  193. Q_D(ctkSettingsDialog);
  194. foreach(ctkSettingsPanel* panel, d->Panels.values())
  195. {
  196. panel->applySettings();
  197. }
  198. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
  199. }
  200. // --------------------------------------------------------------------------
  201. void ctkSettingsDialog::resetSettings()
  202. {
  203. Q_D(ctkSettingsDialog);
  204. foreach(ctkSettingsPanel* panel, d->Panels.values())
  205. {
  206. panel->resetSettings();
  207. }
  208. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
  209. }
  210. // --------------------------------------------------------------------------
  211. void ctkSettingsDialog::restoreDefaultSettings()
  212. {
  213. Q_D(ctkSettingsDialog);
  214. // The panels may not contain ALL the settings of the application,
  215. // for the ones we don't default value, the best is to clear all of them...
  216. if (d->Settings)
  217. {
  218. d->Settings->clear();
  219. }
  220. // ... and restore settings for the ones we can
  221. foreach(ctkSettingsPanel* panel, d->Panels.values())
  222. {
  223. panel->restoreDefaultSettings();
  224. }
  225. }
  226. // --------------------------------------------------------------------------
  227. void ctkSettingsDialog
  228. ::onSettingChanged(const QString& key, const QVariant& newVal)
  229. {
  230. Q_D(ctkSettingsDialog);
  231. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(true);
  232. emit settingChanged(key, newVal);
  233. }
  234. // --------------------------------------------------------------------------
  235. void ctkSettingsDialog
  236. ::onCurrentItemChanged(QTreeWidgetItem* currentItem, QTreeWidgetItem* previousItem)
  237. {
  238. Q_D(ctkSettingsDialog);
  239. Q_UNUSED(previousItem);
  240. d->SettingsStackedWidget->setCurrentWidget(d->panel(currentItem));
  241. }
  242. // --------------------------------------------------------------------------
  243. void ctkSettingsDialog::onDialogButtonClicked(QAbstractButton* button)
  244. {
  245. Q_D(ctkSettingsDialog);
  246. switch (d->SettingsButtonBox->standardButton(button))
  247. {
  248. case QDialogButtonBox::Reset:
  249. this->resetSettings();
  250. break;
  251. case QDialogButtonBox::RestoreDefaults:
  252. this->restoreDefaultSettings();
  253. break;
  254. default:
  255. break;
  256. }
  257. }