ctkValueProxy.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 __ctkValueProxy_h
  15. #define __ctkValueProxy_h
  16. // Qt includes
  17. #include <QObject>
  18. #include <QScopedPointer>
  19. // CTK includes
  20. #include "ctkCoreExport.h"
  21. class ctkValueProxyPrivate;
  22. /// \ingroup Core
  23. /// \brief Base class for value proxies.
  24. /// Value proxy allows to decouple the displayed value from the values
  25. /// accessed within the program. For example, one may want to display
  26. /// Fahrenheit while still working with Celsius.
  27. ///
  28. /// A ctkValueProxy can be used by connecting signal/slots to the
  29. /// value and proxyValue properties or by using directly the
  30. /// valueFromProxyValue and proxyValueFromValue functions.
  31. ///
  32. /// Subclasses should reimplement the function proxyValueFromValue()
  33. /// and valueFromProxyValue().
  34. /// \sa ctkLinearValueProxy
  35. class CTK_CORE_EXPORT ctkValueProxy : public QObject
  36. {
  37. Q_OBJECT
  38. /// The value holds the current value. If the value proxy is
  39. /// considered as a function, then this function applied to the value is
  40. /// the proxy value.
  41. /// The value is updated if the proxy value is changed.
  42. Q_PROPERTY(double value READ value WRITE setValue NOTIFY valueChanged)
  43. /// The proxy value holds the value transformed. If the value proxy is
  44. /// considered as a function, then the proxy value is the result of
  45. /// this function applied to value.
  46. /// The proxy value is updated if the value is changed.
  47. Q_PROPERTY(double proxyValue READ proxyValue WRITE setProxyValue NOTIFY proxyValueChanged)
  48. public:
  49. typedef QObject Superclass;
  50. explicit ctkValueProxy(QObject* parent = 0);
  51. virtual ~ctkValueProxy();
  52. virtual double proxyValueFromValue(double value) const = 0;
  53. virtual double valueFromProxyValue(double proxyValue) const = 0;
  54. double value() const;
  55. virtual double proxyValue() const;
  56. public Q_SLOTS:
  57. void setValue(double newValue);
  58. void setProxyValue(double newProxyValue);
  59. Q_SIGNALS:
  60. void valueChanged(double);
  61. void proxyValueChanged(double);
  62. void proxyAboutToBeModified();
  63. void proxyModified();
  64. protected:
  65. QScopedPointer<ctkValueProxyPrivate> d_ptr;
  66. /// Utilities function for subclasses.
  67. /// Can be called to update the value/proxyValue from the proxyValue/value.
  68. void updateProxyValue();
  69. void updateValue();
  70. private:
  71. Q_DECLARE_PRIVATE(ctkValueProxy);
  72. Q_DISABLE_COPY(ctkValueProxy);
  73. };
  74. #endif