ctkSettingsDialog.cpp 9.5 KB

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