ctkSettingsDialog.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 <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. q->setSettings(new QSettings(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->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
  107. d->Settings = settings;
  108. foreach(ctkSettingsPanel* panel, d->Panels.values())
  109. {
  110. panel->setSettings(settings);
  111. }
  112. }
  113. // --------------------------------------------------------------------------
  114. void ctkSettingsDialog
  115. ::addPanel(ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel)
  116. {
  117. Q_D(ctkSettingsDialog);
  118. QTreeWidgetItem* newPanelItem = new QTreeWidgetItem;
  119. newPanelItem->setText(0, panel->windowTitle());
  120. newPanelItem->setIcon(0, panel->windowIcon());
  121. d->Panels[newPanelItem] = panel;
  122. QTreeWidgetItem* parentItem = d->item(parentPanel);
  123. parentItem->addChild(newPanelItem);
  124. d->SettingsStackedWidget->addWidget(panel);
  125. connect(panel, SIGNAL(settingChanged(const QString&, const QVariant&)),
  126. this, SLOT(onSettingChanged(const QString&, const QVariant&)));
  127. panel->setSettings(this->settings());
  128. }
  129. // --------------------------------------------------------------------------
  130. void ctkSettingsDialog
  131. ::addPanel(const QString& label, ctkSettingsPanel* panel,
  132. ctkSettingsPanel* parentPanel)
  133. {
  134. panel->setWindowTitle(label);
  135. this->addPanel(panel, parentPanel);
  136. }
  137. // --------------------------------------------------------------------------
  138. void ctkSettingsDialog
  139. ::addPanel(const QString& label, const QIcon& icon,
  140. ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel)
  141. {
  142. panel->setWindowTitle(label);
  143. panel->setWindowIcon(icon);
  144. this->addPanel(panel, parentPanel);
  145. }
  146. // --------------------------------------------------------------------------
  147. void ctkSettingsDialog::setCurrentPanel(ctkSettingsPanel* panel)
  148. {
  149. Q_D(ctkSettingsDialog);
  150. // eventually calls onCurrentItemChanged() where all the work is done
  151. d->SettingsTreeWidget->setCurrentItem(d->item(panel));
  152. }
  153. // --------------------------------------------------------------------------
  154. void ctkSettingsDialog::setCurrentPanel(const QString& label)
  155. {
  156. Q_D(ctkSettingsDialog);
  157. // eventually calls onCurrentItemChanged() where all the work is done
  158. d->SettingsTreeWidget->setCurrentItem(d->item(label));
  159. }
  160. // --------------------------------------------------------------------------
  161. ctkSettingsPanel* ctkSettingsDialog::currentPanel()const
  162. {
  163. Q_D(const ctkSettingsDialog);
  164. return d->panel(d->SettingsTreeWidget->currentItem());
  165. }
  166. // --------------------------------------------------------------------------
  167. ctkSettingsPanel* ctkSettingsDialog::panel(const QString& label)const
  168. {
  169. Q_D(const ctkSettingsDialog);
  170. foreach(ctkSettingsPanel* settingsPanel, d->Panels.values())
  171. {
  172. if (settingsPanel->windowTitle() == label)
  173. {
  174. return settingsPanel;
  175. }
  176. }
  177. return 0;
  178. }
  179. // --------------------------------------------------------------------------
  180. void ctkSettingsDialog::accept()
  181. {
  182. this->applySettings();
  183. this->Superclass::accept();
  184. }
  185. // --------------------------------------------------------------------------
  186. void ctkSettingsDialog::reject()
  187. {
  188. this->resetSettings();
  189. this->Superclass::accept();
  190. }
  191. // --------------------------------------------------------------------------
  192. void ctkSettingsDialog::applySettings()
  193. {
  194. Q_D(ctkSettingsDialog);
  195. foreach(ctkSettingsPanel* panel, d->Panels.values())
  196. {
  197. panel->applySettings();
  198. }
  199. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
  200. }
  201. // --------------------------------------------------------------------------
  202. void ctkSettingsDialog::resetSettings()
  203. {
  204. Q_D(ctkSettingsDialog);
  205. foreach(ctkSettingsPanel* panel, d->Panels.values())
  206. {
  207. panel->resetSettings();
  208. }
  209. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
  210. }
  211. // --------------------------------------------------------------------------
  212. void ctkSettingsDialog::restoreDefaultSettings()
  213. {
  214. Q_D(ctkSettingsDialog);
  215. // The panels may not contain ALL the settings of the application,
  216. // for the ones we don't default value, the best is to clear all of them...
  217. if (d->Settings)
  218. {
  219. d->Settings->clear();
  220. }
  221. // ... and restore settings for the ones we can
  222. foreach(ctkSettingsPanel* panel, d->Panels.values())
  223. {
  224. panel->restoreDefaultSettings();
  225. }
  226. }
  227. // --------------------------------------------------------------------------
  228. void ctkSettingsDialog
  229. ::onSettingChanged(const QString& key, const QVariant& newVal)
  230. {
  231. Q_D(ctkSettingsDialog);
  232. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(true);
  233. emit settingChanged(key, newVal);
  234. }
  235. // --------------------------------------------------------------------------
  236. void ctkSettingsDialog
  237. ::onCurrentItemChanged(QTreeWidgetItem* currentItem, QTreeWidgetItem* previousItem)
  238. {
  239. Q_D(ctkSettingsDialog);
  240. Q_UNUSED(previousItem);
  241. d->SettingsStackedWidget->setCurrentWidget(d->panel(currentItem));
  242. }
  243. // --------------------------------------------------------------------------
  244. void ctkSettingsDialog::onDialogButtonClicked(QAbstractButton* button)
  245. {
  246. Q_D(ctkSettingsDialog);
  247. switch (d->SettingsButtonBox->standardButton(button))
  248. {
  249. case QDialogButtonBox::Reset:
  250. this->resetSettings();
  251. break;
  252. case QDialogButtonBox::RestoreDefaults:
  253. this->restoreDefaultSettings();
  254. break;
  255. default:
  256. break;
  257. }
  258. }