ctkSettingsDialog.cpp 11 KB

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