ctkSettingsWidget.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <QSettings>
  18. // CTK includes
  19. #include "ctkSettingsPanel.h"
  20. #include "ctkSettingsWidget.h"
  21. #include "ui_ctkSettingsWidget.h"
  22. #include "ctkLogger.h"
  23. static ctkLogger logger("org.commontk.libs.widgets.ctkSettingsWidget");
  24. //-----------------------------------------------------------------------------
  25. class ctkSettingsWidgetPrivate: public Ui_ctkSettingsWidget
  26. {
  27. Q_DECLARE_PUBLIC(ctkSettingsWidget);
  28. protected:
  29. ctkSettingsWidget* const q_ptr;
  30. public:
  31. ctkSettingsWidgetPrivate(ctkSettingsWidget& object);
  32. void init();
  33. ctkSettingsPanel* panel(QTreeWidgetItem* item)const;
  34. QTreeWidgetItem* item(ctkSettingsPanel* panel)const;
  35. QTreeWidgetItem* item(const QString& label)const;
  36. void beginGroup(ctkSettingsPanel* panel);
  37. void endGroup(ctkSettingsPanel* panel);
  38. QSettings* Settings;
  39. protected:
  40. QMap<QTreeWidgetItem*, ctkSettingsPanel*> Panels;
  41. };
  42. // --------------------------------------------------------------------------
  43. ctkSettingsWidgetPrivate::ctkSettingsWidgetPrivate(ctkSettingsWidget& object)
  44. :q_ptr(&object)
  45. {
  46. this->Settings = 0;
  47. }
  48. // --------------------------------------------------------------------------
  49. void ctkSettingsWidgetPrivate::init()
  50. {
  51. Q_Q(ctkSettingsWidget);
  52. this->setupUi(q);
  53. QObject::connect(this->SettingsTreeWidget,
  54. SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
  55. q, SLOT(onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
  56. }
  57. // --------------------------------------------------------------------------
  58. ctkSettingsPanel* ctkSettingsWidgetPrivate::panel(QTreeWidgetItem* item)const
  59. {
  60. return this->Panels.value(item, 0);
  61. }
  62. // --------------------------------------------------------------------------
  63. QTreeWidgetItem* ctkSettingsWidgetPrivate::item(ctkSettingsPanel* panel)const
  64. {
  65. return this->Panels.key(panel, this->SettingsTreeWidget->invisibleRootItem());
  66. }
  67. // --------------------------------------------------------------------------
  68. QTreeWidgetItem* ctkSettingsWidgetPrivate::item(const QString& label)const
  69. {
  70. QMap<QTreeWidgetItem*, ctkSettingsPanel*>::const_iterator it;
  71. for (it = this->Panels.constBegin(); it != this->Panels.constEnd(); ++it)
  72. {
  73. if (it.value()->windowTitle() == label)
  74. {
  75. return it.key();
  76. }
  77. }
  78. return this->SettingsTreeWidget->invisibleRootItem();
  79. }
  80. // --------------------------------------------------------------------------
  81. ctkSettingsWidget::ctkSettingsWidget(QWidget* _parent)
  82. : Superclass(_parent)
  83. , d_ptr(new ctkSettingsWidgetPrivate(*this))
  84. {
  85. Q_D(ctkSettingsWidget);
  86. d->init();
  87. }
  88. // --------------------------------------------------------------------------
  89. ctkSettingsWidget::~ctkSettingsWidget()
  90. {
  91. }
  92. // --------------------------------------------------------------------------
  93. QSettings* ctkSettingsWidget::settings()const
  94. {
  95. Q_D(const ctkSettingsWidget);
  96. return d->Settings;
  97. }
  98. // --------------------------------------------------------------------------
  99. void ctkSettingsWidget::setSettings(QSettings* settings)
  100. {
  101. Q_D(ctkSettingsWidget);
  102. d->Settings = settings;
  103. foreach(ctkSettingsPanel* panel, d->Panels.values())
  104. {
  105. panel->setSettings(settings);
  106. }
  107. }
  108. // --------------------------------------------------------------------------
  109. void ctkSettingsWidget
  110. ::addPanel(ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel)
  111. {
  112. Q_D(ctkSettingsWidget);
  113. QTreeWidgetItem* newPanelItem = new QTreeWidgetItem;
  114. newPanelItem->setText(0, panel->windowTitle());
  115. newPanelItem->setIcon(0, panel->windowIcon());
  116. d->Panels[newPanelItem] = panel;
  117. QTreeWidgetItem* parentItem = d->item(parentPanel);
  118. parentItem->addChild(newPanelItem);
  119. d->SettingsStackedWidget->addWidget(panel);
  120. connect(panel, SIGNAL(settingChanged(const QString&, const QVariant&)),
  121. this, SIGNAL(settingChanged(const QString&, const QVariant&)));
  122. panel->setSettings(this->settings());
  123. }
  124. // --------------------------------------------------------------------------
  125. void ctkSettingsWidget
  126. ::addPanel(const QString& label, ctkSettingsPanel* panel,
  127. ctkSettingsPanel* parentPanel)
  128. {
  129. panel->setWindowTitle(label);
  130. this->addPanel(panel, parentPanel);
  131. }
  132. // --------------------------------------------------------------------------
  133. void ctkSettingsWidget
  134. ::addPanel(const QString& label, const QIcon& icon,
  135. ctkSettingsPanel* panel, ctkSettingsPanel* parentPanel)
  136. {
  137. panel->setWindowTitle(label);
  138. panel->setWindowIcon(icon);
  139. this->addPanel(panel, parentPanel);
  140. }
  141. // --------------------------------------------------------------------------
  142. void ctkSettingsWidget::setCurrentPanel(ctkSettingsPanel* panel)
  143. {
  144. Q_D(ctkSettingsWidget);
  145. // eventually calls onCurrentItemChanged() where all the work is done
  146. d->SettingsTreeWidget->setCurrentItem(d->item(panel));
  147. }
  148. // --------------------------------------------------------------------------
  149. void ctkSettingsWidget::setCurrentPanel(const QString& label)
  150. {
  151. Q_D(ctkSettingsWidget);
  152. // eventually calls onCurrentItemChanged() where all the work is done
  153. d->SettingsTreeWidget->setCurrentItem(d->item(label));
  154. }
  155. // --------------------------------------------------------------------------
  156. ctkSettingsPanel* ctkSettingsWidget::currentPanel()const
  157. {
  158. Q_D(const ctkSettingsWidget);
  159. return d->panel(d->SettingsTreeWidget->currentItem());
  160. }
  161. // --------------------------------------------------------------------------
  162. ctkSettingsPanel* ctkSettingsWidget::panel(const QString& label)const
  163. {
  164. Q_D(const ctkSettingsWidget);
  165. foreach(ctkSettingsPanel* settingsPanel, d->Panels.values())
  166. {
  167. if (settingsPanel->windowTitle() == label)
  168. {
  169. return settingsPanel;
  170. }
  171. }
  172. return 0;
  173. }
  174. // --------------------------------------------------------------------------
  175. void ctkSettingsWidget
  176. ::onCurrentItemChanged(QTreeWidgetItem* currentItem, QTreeWidgetItem* previousItem)
  177. {
  178. Q_D(ctkSettingsWidget);
  179. Q_UNUSED(previousItem);
  180. d->SettingsStackedWidget->setCurrentWidget(
  181. d->panel(currentItem));
  182. }