ctkSettingsDialog.cpp 9.0 KB

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