ctkValueProxy.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // CTK includes
  16. #include "ctkValueProxy.h"
  17. // --------------------------------------------------------------------------
  18. // ctkValueProxyPrivate
  19. class CTK_CORE_EXPORT ctkValueProxyPrivate
  20. {
  21. Q_DECLARE_PUBLIC(ctkValueProxy);
  22. public:
  23. ctkValueProxy* q_ptr;
  24. ctkValueProxyPrivate(ctkValueProxy& object);
  25. ~ctkValueProxyPrivate();
  26. double Value;
  27. double ProxyValue;
  28. };
  29. // --------------------------------------------------------------------------
  30. // ctkValueProxyPrivate methods
  31. // --------------------------------------------------------------------------
  32. ctkValueProxyPrivate::ctkValueProxyPrivate(ctkValueProxy& object)
  33. : q_ptr(&object)
  34. {
  35. this->Value = 0.0;
  36. this->ProxyValue = 0.0;
  37. }
  38. // --------------------------------------------------------------------------
  39. ctkValueProxyPrivate::~ctkValueProxyPrivate()
  40. {
  41. }
  42. // --------------------------------------------------------------------------
  43. // ctkValueProxy methods
  44. // --------------------------------------------------------------------------
  45. ctkValueProxy::ctkValueProxy(QObject* _parent) : Superclass(_parent)
  46. , d_ptr(new ctkValueProxyPrivate(*this))
  47. {
  48. }
  49. // --------------------------------------------------------------------------
  50. ctkValueProxy::~ctkValueProxy()
  51. {
  52. }
  53. // --------------------------------------------------------------------------
  54. double ctkValueProxy::value() const
  55. {
  56. Q_D(const ctkValueProxy);
  57. return d->Value;
  58. }
  59. // --------------------------------------------------------------------------
  60. void ctkValueProxy::setValue(double newValue)
  61. {
  62. Q_D(ctkValueProxy);
  63. if (d->Value == newValue)
  64. {
  65. return;
  66. }
  67. d->Value = newValue;
  68. emit this->valueChanged(d->Value);
  69. this->updateProxyValue();
  70. }
  71. // --------------------------------------------------------------------------
  72. double ctkValueProxy::proxyValue() const
  73. {
  74. Q_D(const ctkValueProxy);
  75. return d->ProxyValue;
  76. }
  77. // --------------------------------------------------------------------------
  78. void ctkValueProxy::setProxyValue(double newProxyValue)
  79. {
  80. Q_D(ctkValueProxy);
  81. if (d->ProxyValue == newProxyValue)
  82. {
  83. return;
  84. }
  85. d->ProxyValue = newProxyValue;
  86. emit this->proxyValueChanged(d->ProxyValue);
  87. this->updateValue();
  88. }
  89. // --------------------------------------------------------------------------
  90. void ctkValueProxy::updateProxyValue()
  91. {
  92. Q_D(ctkValueProxy);
  93. double newProxyValue = this->proxyValueFromValue(d->Value);
  94. if (newProxyValue == d->ProxyValue)
  95. {
  96. return;
  97. }
  98. d->ProxyValue = newProxyValue;
  99. emit this->proxyValueChanged(d->ProxyValue);
  100. }
  101. // --------------------------------------------------------------------------
  102. void ctkValueProxy::updateValue()
  103. {
  104. Q_D(ctkValueProxy);
  105. double newValue = this->valueFromProxyValue(d->ProxyValue);
  106. if (newValue == d->Value)
  107. {
  108. return;
  109. }
  110. d->Value = newValue;
  111. emit this->valueChanged(d->Value);
  112. }