ctkSettingsDialog.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. QList<ctkSettingsPanel*> panels()const;
  36. ctkSettingsPanel* panel(QTreeWidgetItem* item)const;
  37. QTreeWidgetItem* item(ctkSettingsPanel* panel)const;
  38. QTreeWidgetItem* item(const QString& label)const;
  39. void beginGroup(ctkSettingsPanel* panel);
  40. void endGroup(ctkSettingsPanel* panel);
  41. void updatePanelTitle(ctkSettingsPanel* panel);
  42. void updateRestartRequiredLabel();
  43. QSettings* Settings;
  44. protected:
  45. QMap<QTreeWidgetItem*, ctkSettingsPanel*> Panels;
  46. };
  47. // --------------------------------------------------------------------------
  48. ctkSettingsDialogPrivate::ctkSettingsDialogPrivate(ctkSettingsDialog& object)
  49. :q_ptr(&object)
  50. {
  51. this->Settings = 0;
  52. }
  53. // --------------------------------------------------------------------------
  54. void ctkSettingsDialogPrivate::init()
  55. {
  56. Q_Q(ctkSettingsDialog);
  57. this->setupUi(q);
  58. this->SettingsButtonBox->button(QDialogButtonBox::Ok)->setToolTip(
  59. q->tr("Apply settings and close dialog."));
  60. this->SettingsButtonBox->button(QDialogButtonBox::Cancel)->setToolTip(
  61. q->tr("Reject settings changes and close dialog."));
  62. this->SettingsButtonBox->button(QDialogButtonBox::Reset)->setToolTip(
  63. q->tr("Reset settings to their values when the dialog opened"));
  64. this->SettingsButtonBox->button(QDialogButtonBox::RestoreDefaults)->setToolTip(
  65. q->tr("Restore settings to their default values."
  66. "To cancel a \"Restore\", you can \"Reset\" the settings."));
  67. q->setResetButton(false);
  68. q->setSettings(new QSettings(q));
  69. QObject::connect(this->SettingsTreeWidget,
  70. SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
  71. q, SLOT(onCurrentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)));
  72. QObject::connect(this->SettingsTreeWidget, SIGNAL(expanded(QModelIndex)),
  73. q, SLOT(adjustTreeWidgetToContents()));
  74. QObject::connect(this->SettingsTreeWidget, SIGNAL(collapsed(QModelIndex)),
  75. q, SLOT(adjustTreeWidgetToContents()));
  76. QObject::connect(this->SettingsButtonBox, SIGNAL(clicked(QAbstractButton*)),
  77. q, SLOT(onDialogButtonClicked(QAbstractButton*)));
  78. this->updateRestartRequiredLabel();
  79. q->adjustTreeWidgetToContents();
  80. }
  81. // --------------------------------------------------------------------------
  82. QList<ctkSettingsPanel*> ctkSettingsDialogPrivate::panels()const
  83. {
  84. return this->Panels.values();
  85. }
  86. // --------------------------------------------------------------------------
  87. ctkSettingsPanel* ctkSettingsDialogPrivate::panel(QTreeWidgetItem* item)const
  88. {
  89. return this->Panels.value(item, 0);
  90. }
  91. // --------------------------------------------------------------------------
  92. QTreeWidgetItem* ctkSettingsDialogPrivate::item(ctkSettingsPanel* panel)const
  93. {
  94. return this->Panels.key(panel, this->SettingsTreeWidget->invisibleRootItem());
  95. }
  96. // --------------------------------------------------------------------------
  97. QTreeWidgetItem* ctkSettingsDialogPrivate::item(const QString& label)const
  98. {
  99. QMap<QTreeWidgetItem*, ctkSettingsPanel*>::const_iterator it;
  100. for (it = this->Panels.constBegin(); it != this->Panels.constEnd(); ++it)
  101. {
  102. if (it.value()->windowTitle() == label)
  103. {
  104. return it.key();
  105. }
  106. }
  107. return this->SettingsTreeWidget->invisibleRootItem();
  108. }
  109. // --------------------------------------------------------------------------
  110. void ctkSettingsDialogPrivate::updatePanelTitle(ctkSettingsPanel* panel)
  111. {
  112. QTreeWidgetItem* panelItem = this->item(panel);
  113. QString title = panelItem->text(0);
  114. title.replace(QRegExp("\\*$"),"");
  115. if (!panel->changedSettings().isEmpty())
  116. {
  117. title.append('*');
  118. }
  119. panelItem->setText(0,title);
  120. }
  121. // --------------------------------------------------------------------------
  122. void ctkSettingsDialogPrivate::updateRestartRequiredLabel()
  123. {
  124. Q_Q(ctkSettingsDialog);
  125. QStringList restartRequiredSettings;
  126. foreach(const ctkSettingsPanel* panel, this->panels())
  127. {
  128. foreach(const QString& settingKey, panel->changedSettings())
  129. {
  130. if (panel->settingOptions(settingKey) & ctkSettingsPanel::OptionRequireRestart)
  131. {
  132. restartRequiredSettings << (panel->settingLabel(settingKey).isEmpty() ?
  133. settingKey : panel->settingLabel(settingKey));
  134. }
  135. }
  136. }
  137. bool restartRequired = !restartRequiredSettings.isEmpty();
  138. if (restartRequired)
  139. {
  140. QString header = q->tr(
  141. "<b style=\"color:red\">Restart required!</b><br>\n<small>"
  142. "The application must be restarted to take into account "
  143. "the new values of the following properties:\n");
  144. QString footer = q->tr("</small>");
  145. restartRequiredSettings.push_front(QString());
  146. this->RestartRequiredLabel->setText( header + restartRequiredSettings.join("<br>&nbsp;&nbsp;") + footer);
  147. }
  148. this->RestartRequiredLabel->setVisible(restartRequired);
  149. }
  150. // --------------------------------------------------------------------------
  151. ctkSettingsDialog::ctkSettingsDialog(QWidget* _parent)
  152. : Superclass(_parent)
  153. , d_ptr(new ctkSettingsDialogPrivate(*this))
  154. {
  155. Q_D(ctkSettingsDialog);
  156. d->init();
  157. }
  158. // --------------------------------------------------------------------------
  159. ctkSettingsDialog::~ctkSettingsDialog()
  160. {
  161. }
  162. // --------------------------------------------------------------------------
  163. QSettings* ctkSettingsDialog::settings()const
  164. {
  165. Q_D(const ctkSettingsDialog);
  166. return d->Settings;
  167. }
  168. // --------------------------------------------------------------------------
  169. void ctkSettingsDialog::setSettings(QSettings* settings)
  170. {
  171. Q_D(ctkSettingsDialog);
  172. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
  173. d->Settings = settings;
  174. foreach(ctkSettingsPanel* panel, d->Panels.values())
  175. {
  176. panel->setSettings(settings);
  177. }
  178. }
  179. // --------------------------------------------------------------------------
  180. void ctkSettingsDialog
  181. ::addPanel(ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel)
  182. {
  183. Q_D(ctkSettingsDialog);
  184. QTreeWidgetItem* newPanelItem = new QTreeWidgetItem;
  185. newPanelItem->setText(0, panel->windowTitle());
  186. newPanelItem->setIcon(0, panel->windowIcon());
  187. d->Panels[newPanelItem] = panel;
  188. QTreeWidgetItem* parentItem = d->item(parentPanel);
  189. parentItem->addChild(newPanelItem);
  190. d->SettingsStackedWidget->addWidget(panel);
  191. this->adjustTreeWidgetToContents();
  192. connect(panel, SIGNAL(settingChanged(QString,QVariant)),
  193. this, SLOT(onSettingChanged(QString,QVariant)));
  194. panel->setSettings(this->settings());
  195. }
  196. // --------------------------------------------------------------------------
  197. void ctkSettingsDialog
  198. ::addPanel(const QString& label, ctkSettingsPanel* panel,
  199. ctkSettingsPanel* parentPanel)
  200. {
  201. panel->setWindowTitle(label);
  202. this->addPanel(panel, parentPanel);
  203. }
  204. // --------------------------------------------------------------------------
  205. void ctkSettingsDialog
  206. ::addPanel(const QString& label, const QIcon& icon,
  207. ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel)
  208. {
  209. panel->setWindowTitle(label);
  210. panel->setWindowIcon(icon);
  211. this->addPanel(panel, parentPanel);
  212. }
  213. // --------------------------------------------------------------------------
  214. void ctkSettingsDialog::setCurrentPanel(ctkSettingsPanel* panel)
  215. {
  216. Q_D(ctkSettingsDialog);
  217. // eventually calls onCurrentItemChanged() where all the work is done
  218. d->SettingsTreeWidget->setCurrentItem(d->item(panel));
  219. }
  220. // --------------------------------------------------------------------------
  221. void ctkSettingsDialog::setCurrentPanel(const QString& label)
  222. {
  223. Q_D(ctkSettingsDialog);
  224. // eventually calls onCurrentItemChanged() where all the work is done
  225. d->SettingsTreeWidget->setCurrentItem(d->item(label));
  226. }
  227. // --------------------------------------------------------------------------
  228. ctkSettingsPanel* ctkSettingsDialog::currentPanel()const
  229. {
  230. Q_D(const ctkSettingsDialog);
  231. return d->panel(d->SettingsTreeWidget->currentItem());
  232. }
  233. // --------------------------------------------------------------------------
  234. ctkSettingsPanel* ctkSettingsDialog::panel(const QString& label)const
  235. {
  236. Q_D(const ctkSettingsDialog);
  237. foreach(ctkSettingsPanel* settingsPanel, d->Panels.values())
  238. {
  239. if (settingsPanel->windowTitle() == label)
  240. {
  241. return settingsPanel;
  242. }
  243. }
  244. return 0;
  245. }
  246. // --------------------------------------------------------------------------
  247. void ctkSettingsDialog::accept()
  248. {
  249. bool emitRestartRequested = false;
  250. if (this->isRestartRequired())
  251. {
  252. QMessageBox::StandardButton answer = QMessageBox::question(this,"Restart required",
  253. "For settings to be taken into account, the application\n"
  254. "must be restarted. Restart the application now ?\n",
  255. QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel, QMessageBox::No);
  256. if (answer == QMessageBox::Cancel)
  257. {
  258. return;
  259. }
  260. else
  261. {
  262. emitRestartRequested = (answer == QMessageBox::Yes);
  263. }
  264. }
  265. this->applySettings();
  266. this->Superclass::accept();
  267. if (emitRestartRequested)
  268. {
  269. emit restartRequested();
  270. }
  271. }
  272. // --------------------------------------------------------------------------
  273. void ctkSettingsDialog::reject()
  274. {
  275. this->resetSettings();
  276. this->Superclass::accept();
  277. }
  278. // --------------------------------------------------------------------------
  279. void ctkSettingsDialog::applySettings()
  280. {
  281. Q_D(ctkSettingsDialog);
  282. foreach(ctkSettingsPanel* panel, d->Panels.values())
  283. {
  284. panel->applySettings();
  285. }
  286. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
  287. }
  288. // --------------------------------------------------------------------------
  289. void ctkSettingsDialog::reloadSettings()
  290. {
  291. Q_D(ctkSettingsDialog);
  292. foreach(ctkSettingsPanel* panel, d->Panels.values())
  293. {
  294. panel->reloadSettings();
  295. }
  296. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
  297. }
  298. // --------------------------------------------------------------------------
  299. void ctkSettingsDialog::resetSettings()
  300. {
  301. Q_D(ctkSettingsDialog);
  302. foreach(ctkSettingsPanel* panel, d->Panels.values())
  303. {
  304. panel->resetSettings();
  305. }
  306. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
  307. }
  308. // --------------------------------------------------------------------------
  309. void ctkSettingsDialog::restoreDefaultSettings()
  310. {
  311. Q_D(ctkSettingsDialog);
  312. // The panels may not contain ALL the settings of the application,
  313. // for the ones we don't default value, the best is to clear all of them...
  314. if (d->Settings)
  315. {
  316. d->Settings->clear();
  317. }
  318. // ... and restore settings for the ones we can
  319. foreach(ctkSettingsPanel* panel, d->Panels.values())
  320. {
  321. panel->restoreDefaultSettings();
  322. }
  323. }
  324. // --------------------------------------------------------------------------
  325. void ctkSettingsDialog
  326. ::onSettingChanged(const QString& key, const QVariant& newVal)
  327. {
  328. Q_D(ctkSettingsDialog);
  329. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(true);
  330. d->updatePanelTitle(qobject_cast<ctkSettingsPanel*>(this->sender()));
  331. d->updateRestartRequiredLabel();
  332. emit settingChanged(key, newVal);
  333. }
  334. // --------------------------------------------------------------------------
  335. void ctkSettingsDialog
  336. ::onCurrentItemChanged(QTreeWidgetItem* currentItem, QTreeWidgetItem* previousItem)
  337. {
  338. Q_D(ctkSettingsDialog);
  339. Q_UNUSED(previousItem);
  340. d->SettingsStackedWidget->setCurrentWidget(d->panel(currentItem));
  341. }
  342. // --------------------------------------------------------------------------
  343. void ctkSettingsDialog::onDialogButtonClicked(QAbstractButton* button)
  344. {
  345. Q_D(ctkSettingsDialog);
  346. switch (d->SettingsButtonBox->standardButton(button))
  347. {
  348. case QDialogButtonBox::Reset:
  349. this->resetSettings();
  350. break;
  351. case QDialogButtonBox::RestoreDefaults:
  352. if (QMessageBox::warning(this,"Restore all settings",
  353. "Are you sure you want to reset\n"
  354. "all settings to their default values?\n",
  355. QMessageBox::RestoreDefaults, QMessageBox::Cancel)
  356. == QMessageBox::RestoreDefaults)
  357. {
  358. this->restoreDefaultSettings();
  359. }
  360. break;
  361. default:
  362. break;
  363. }
  364. }
  365. // --------------------------------------------------------------------------
  366. void ctkSettingsDialog::adjustTreeWidgetToContents()
  367. {
  368. Q_D(const ctkSettingsDialog);
  369. d->SettingsTreeWidget->resizeColumnToContents(0);
  370. d->SettingsTreeWidget->setFixedWidth(
  371. qobject_cast<QAbstractItemView*>(d->SettingsTreeWidget)->sizeHintForColumn(0) +
  372. d->SettingsTreeWidget->fontMetrics().width('*') +
  373. d->SettingsTreeWidget->indentation() / 2+
  374. 2 * d->SettingsTreeWidget->frameWidth());
  375. }
  376. // -------------------------------------------------------------------------
  377. bool ctkSettingsDialog::event(QEvent* event)
  378. {
  379. if (event->type() == QEvent::FontChange ||
  380. event->type() == QEvent::StyleChange)
  381. {
  382. this->adjustTreeWidgetToContents();
  383. }
  384. return this->Superclass::event(event);
  385. }
  386. // -------------------------------------------------------------------------
  387. bool ctkSettingsDialog::resetButton()const
  388. {
  389. Q_D(const ctkSettingsDialog);
  390. return d->SettingsButtonBox->button(QDialogButtonBox::Reset)->isVisibleTo(
  391. const_cast<QDialogButtonBox*>(d->SettingsButtonBox));
  392. }
  393. // -------------------------------------------------------------------------
  394. void ctkSettingsDialog::setResetButton(bool show)
  395. {
  396. Q_D(ctkSettingsDialog);
  397. d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setVisible(show);
  398. }
  399. // --------------------------------------------------------------------------
  400. bool ctkSettingsDialog::isRestartRequired()const
  401. {
  402. Q_D(const ctkSettingsDialog);
  403. return d->RestartRequiredLabel->isVisibleTo(
  404. const_cast<ctkSettingsDialog*>(this));
  405. }