ctkSettingsPanel.h 5.9 KB

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