ctkSettingsDialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. this->SettingsButtonBox->button(QDialogButtonBox::Ok)->setToolTip(
  55. q->tr("Apply settings and close dialog."));
  56. this->SettingsButtonBox->button(QDialogButtonBox::Cancel)->setToolTip(
  57. q->tr("Reject settings changes and close dialog."));
  58. this->SettingsButtonBox->button(QDialogButtonBox::Reset)->setToolTip(
  59. q->tr("Reset settings to their values when the dialog opened"));
  60. this->SettingsButtonBox->button(QDialogButtonBox::RestoreDefaults)->setToolTip(
  61. q->tr("Restore settings to their default values."
  62. "To cancel a \"Restore\", you can \"Reset\" the settings."));
  63. this->setResetButton(false);
  64. q->setSettings(new QSettings(q));
  65. QObject::connect(this->SettingsTreeWidget,
  66. SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
  67. q, SLOT(onCurrentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)));
  68. QObject::connect(this->SettingsButtonBox, SIGNAL(clicked(QAbstractButton*)),
  69. q, SLOT(onDialogButtonClicked(QAbstractButton*)));
  70. }
  71. // --------------------------------------------------------------------------
  72. ctkSettingsPanel* ctkSettingsDialogPrivate::panel(QTreeWidgetItem* item)const
  73. {
  74. return this->Panels.value(item, 0);
  75. }
  76. // --------------------------------------------------------------------------
  77. QTreeWidgetItem* ctkSettingsDialogPrivate::item(ctkSettingsPanel* panel)const
  78. {
  79. return this->Panels.key(panel, this->SettingsTreeWidget->invisibleRootItem());
  80. }
  81. // --------------------------------------------------------------------------
  82. QTreeWidgetItem* ctkSettingsDialogPrivate::item(const QString& label)const
  83. {
  84. QMap<QTreeWidgetItem*, ctkSettingsPanel*>::const_iterator it;
  85. for (it = this->Panels.constBegin(); it != this->Panels.constEnd(); ++it)
  86. {
  87. if (it.value()->windowTitle() == label)
  88. {
  89. return it.key();
  90. }
  91. }
  92. return this->SettingsTreeWidget->invisibleRootItem();
  93. }
  94. // --------------------------------------------------------------------------
  95. ctkSettingsDialog::ctkSettingsDialog(QWidget* _parent)
  96. : Superclass(_parent)
  97. , d_ptr(new ctkSettingsDialogPrivate(*this))
  98. {
  99. Q_D(ctkSettingsDialog);
  100. d->init();
  101. }
  102. // --------------------------------------------------------------------------
  103. ctkSettingsDialog::~ctkSettingsDialog()
  104. {
  105. }
  106. // --------------------------------------------------------------------------
  107. QSettings* ctkSettingsDialog::settings()const
  108. {
  109. Q_D(const ctkSettingsDialog);
  110. return d->Settings;
  111. }
  112. // --------------------------------------------------------------------------
  113. void ctkSettingsDialog::setSettings(QSettings* settings)
  114. {
  115. Q_D(ctkSettingsDialog);
  116. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
  117. d->Settings = settings;
  118. foreach(ctkSettingsPanel* panel, d->Panels.values())
  119. {
  120. panel->setSettings(settings);
  121. }
  122. }
  123. // --------------------------------------------------------------------------
  124. void ctkSettingsDialog
  125. ::addPanel(ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel)
  126. {
  127. Q_D(ctkSettingsDialog);
  128. QTreeWidgetItem* newPanelItem = new QTreeWidgetItem;
  129. newPanelItem->setText(0, panel->windowTitle());
  130. newPanelItem->setIcon(0, panel->windowIcon());
  131. d->Panels[newPanelItem] = panel;
  132. QTreeWidgetItem* parentItem = d->item(parentPanel);
  133. parentItem->addChild(newPanelItem);
  134. d->SettingsStackedWidget->addWidget(panel);
  135. this->adjustTreeWidgetToContents();
  136. connect(panel, SIGNAL(settingChanged(QString,QVariant)),
  137. this, SLOT(onSettingChanged(QString,QVariant)));
  138. panel->setSettings(this->settings());
  139. }
  140. // --------------------------------------------------------------------------
  141. void ctkSettingsDialog
  142. ::addPanel(const QString& label, ctkSettingsPanel* panel,
  143. ctkSettingsPanel* parentPanel)
  144. {
  145. panel->setWindowTitle(label);
  146. this->addPanel(panel, parentPanel);
  147. }
  148. // --------------------------------------------------------------------------
  149. void ctkSettingsDialog
  150. ::addPanel(const QString& label, const QIcon& icon,
  151. ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel)
  152. {
  153. panel->setWindowTitle(label);
  154. panel->setWindowIcon(icon);
  155. this->addPanel(panel, parentPanel);
  156. }
  157. // --------------------------------------------------------------------------
  158. void ctkSettingsDialog::setCurrentPanel(ctkSettingsPanel* panel)
  159. {
  160. Q_D(ctkSettingsDialog);
  161. // eventually calls onCurrentItemChanged() where all the work is done
  162. d->SettingsTreeWidget->setCurrentItem(d->item(panel));
  163. }
  164. // --------------------------------------------------------------------------
  165. void ctkSettingsDialog::setCurrentPanel(const QString& label)
  166. {
  167. Q_D(ctkSettingsDialog);
  168. // eventually calls onCurrentItemChanged() where all the work is done
  169. d->SettingsTreeWidget->setCurrentItem(d->item(label));
  170. }
  171. // --------------------------------------------------------------------------
  172. ctkSettingsPanel* ctkSettingsDialog::currentPanel()const
  173. {
  174. Q_D(const ctkSettingsDialog);
  175. return d->panel(d->SettingsTreeWidget->currentItem());
  176. }
  177. // --------------------------------------------------------------------------
  178. ctkSettingsPanel* ctkSettingsDialog::panel(const QString& label)const
  179. {
  180. Q_D(const ctkSettingsDialog);
  181. foreach(ctkSettingsPanel* settingsPanel, d->Panels.values())
  182. {
  183. if (settingsPanel->windowTitle() == label)
  184. {
  185. return settingsPanel;
  186. }
  187. }
  188. return 0;
  189. }
  190. // --------------------------------------------------------------------------
  191. void ctkSettingsDialog::accept()
  192. {
  193. this->applySettings();
  194. this->Superclass::accept();
  195. }
  196. // --------------------------------------------------------------------------
  197. void ctkSettingsDialog::reject()
  198. {
  199. this->resetSettings();
  200. this->Superclass::accept();
  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. }
  270. // --------------------------------------------------------------------------
  271. void ctkSettingsDialog::adjustTreeWidgetToContents()
  272. {
  273. Q_D(const ctkSettingsDialog);
  274. d->SettingsTreeWidget->resizeColumnToContents(0);
  275. d->SettingsTreeWidget->setFixedWidth(
  276. d->SettingsTreeWidget->QAbstractItemView::sizeHintForColumn(0) +
  277. 2 * d->SettingsTreeWidget->indentation() +
  278. 2 * d->SettingsTreeWidget->frameWidth());
  279. }
  280. // -------------------------------------------------------------------------
  281. bool ctkSettingsDialog::event(QEvent* event)
  282. {
  283. if (event->type() == QEvent::FontChange ||
  284. event->type() == QEvent::StyleChange)
  285. {
  286. this->adjustTreeWidgetToContents();
  287. }
  288. return this->Superclass::event(event);
  289. }
  290. // -------------------------------------------------------------------------
  291. bool ctkSettingsDialog::resetButton()const
  292. {
  293. Q_D(const ctkSettingsDialog);
  294. return d->SettingsButtonBox->button(QDialogButtonBox::Reset)->isVisibleTo(
  295. const_cast<QDialogButtonBox*>(d->SettingsButtonBox));
  296. }
  297. // -------------------------------------------------------------------------
  298. void ctkSettingsDialog::setResetButton(bool show)
  299. {
  300. Q_D(ctkSettingsDialog);
  301. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setVisible(show);
  302. }