ctkSettingsPanel.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #ifndef __ctkSettingsPanel_h
  15. #define __ctkSettingsPanel_h
  16. // Qt includes
  17. #include <QMetaType>
  18. #include <QWidget>
  19. // CTK includes
  20. #include "ctkWidgetsExport.h"
  21. class QSettings;
  22. class ctkSettingsPanelPrivate;
  23. /// \ingroup Widgets
  24. class CTK_WIDGETS_EXPORT ctkSettingsPanel : public QWidget
  25. {
  26. Q_OBJECT
  27. Q_ENUMS(SettingOption)
  28. Q_FLAGS(SettingOptions)
  29. public:
  30. /// Superclass typedef
  31. typedef QWidget Superclass;
  32. /// Constructor
  33. explicit ctkSettingsPanel(QWidget* parent = 0);
  34. /// Destructor
  35. virtual ~ctkSettingsPanel();
  36. QSettings* settings()const;
  37. void setSettings(QSettings* settings);
  38. enum SettingOption{
  39. OptionNone = 0x0000,
  40. OptionRequireRestart = 0x0001,
  41. OptionAll_Mask = ~0
  42. };
  43. Q_DECLARE_FLAGS(SettingOptions, SettingOption)
  44. /// Add an entry into the settings uniquely defined by the \a key name and the
  45. /// current value of the property.
  46. /// The property is then synchronized with the settings by observing the signal
  47. /// notification. Anytime the property is modified (the signal \a signal is
  48. /// fired), its value associated to \a key is updated in the settings.
  49. /// \a signal is typically the value under NOTIFY in Q_PROPERTY.
  50. /// The current value of the property is later used when
  51. /// restoreDefaultSettings() is called.
  52. /// If you want to register the logical complement of a boolean property
  53. /// you can use ctkBooleanMapper:
  54. /// <code>
  55. /// panel->registerProperty("unchecked",
  56. /// new ctkBooleanMapper(checkBox, "checked", SIGNAL(toggled(bool))),
  57. /// "complement", SIGNAL(complementChanged(bool)));
  58. /// </code>
  59. /// By default, property are associated with the general settings set using setSettings(QSettings*)
  60. /// or ctkSettingsDialog::setSettings(QSettings*). Note that it also possible to associate
  61. /// a specific \a settings for any given \a settingKey.
  62. /// \sa Q_PROPERTY(), \sa ctkBooleanMapper
  63. void registerProperty(const QString& settingKey,
  64. QObject* object,
  65. const QString& objectProperty,
  66. const char* propertySignal,
  67. const QString& settingLabel = QString(),
  68. SettingOptions options = OptionNone,
  69. QSettings * settings = 0);
  70. /// Set the setting to the property defined by the key.
  71. /// The old value can be restored using resetSettings()
  72. void setSetting(const QString& key, const QVariant& newVal);
  73. /// Return the list of settings keys that have been modified and are
  74. /// not yet applied.
  75. QStringList changedSettings()const;
  76. /// Return the label associated to a setting
  77. QString settingLabel(const QString& settingKey)const;
  78. /// Return the options associated to a setting
  79. SettingOptions settingOptions(const QString& settingKey)const;
  80. public Q_SLOTS:
  81. /// Forget the old property values so next time resetSettings is called it
  82. /// will set the properties with the same values when applySettings() is
  83. /// called.
  84. virtual void applySettings();
  85. /// Restore all the properties with their values when applySettings() was
  86. /// called last (or their original values if applySettings was never called).
  87. virtual void resetSettings();
  88. /// Restore all the properties with their original values; the current values
  89. /// of the properties when they were registered using registerProperty().
  90. virtual void restoreDefaultSettings();
  91. Q_SIGNALS:
  92. /// Fired anytime a property is modified.
  93. void settingChanged(const QString& key, const QVariant& value);
  94. protected:
  95. /// Return the default value of a property identified by its settings \a key
  96. /// \sa registerProperty();
  97. QVariant defaultPropertyValue(const QString& key) const;
  98. /// Return the previous value of a property identified by its settings \a key
  99. /// \sa registerProperty();
  100. QVariant previousPropertyValue(const QString& key) const;
  101. /// Return the value of a property identified by its settings \a key
  102. /// \sa registerProperty();
  103. QVariant propertyValue(const QString& key) const;
  104. protected Q_SLOTS:
  105. void updateSetting(const QString& key);
  106. protected:
  107. QScopedPointer<ctkSettingsPanelPrivate> d_ptr;
  108. virtual void updateProperties();
  109. private:
  110. Q_DECLARE_PRIVATE(ctkSettingsPanel);
  111. Q_DISABLE_COPY(ctkSettingsPanel);
  112. };
  113. Q_DECLARE_METATYPE(ctkSettingsPanel::SettingOption)
  114. Q_DECLARE_METATYPE(ctkSettingsPanel::SettingOptions)
  115. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkSettingsPanel::SettingOptions)
  116. #endif