ctkLinearValueProxy.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include <QDebug>
  16. // CTK includes
  17. #include "ctkLinearValueProxy.h"
  18. // STD includes
  19. #include <limits>
  20. // --------------------------------------------------------------------------
  21. // ctkLinearValueProxyPrivate
  22. class CTK_CORE_EXPORT ctkLinearValueProxyPrivate
  23. {
  24. Q_DECLARE_PUBLIC(ctkLinearValueProxy);
  25. protected:
  26. ctkLinearValueProxy* q_ptr;
  27. public:
  28. ctkLinearValueProxyPrivate(ctkLinearValueProxy& object);
  29. ~ctkLinearValueProxyPrivate();
  30. bool isCoefficientValid() const;
  31. double Coefficient;
  32. double Offset;
  33. };
  34. // --------------------------------------------------------------------------
  35. // ctkLinearValueProxyPrivate methods
  36. // --------------------------------------------------------------------------
  37. ctkLinearValueProxyPrivate::ctkLinearValueProxyPrivate(ctkLinearValueProxy& object)
  38. :q_ptr(&object)
  39. {
  40. this->Coefficient = 1.0;
  41. this->Offset = 0.0;
  42. }
  43. // --------------------------------------------------------------------------
  44. ctkLinearValueProxyPrivate::~ctkLinearValueProxyPrivate()
  45. {
  46. }
  47. // --------------------------------------------------------------------------
  48. bool ctkLinearValueProxyPrivate::isCoefficientValid() const
  49. {
  50. return qAbs(this->Coefficient) > std::numeric_limits<double>::epsilon();
  51. }
  52. // --------------------------------------------------------------------------
  53. // ctkLinearValueProxy methods
  54. // --------------------------------------------------------------------------
  55. ctkLinearValueProxy::ctkLinearValueProxy(QObject* _parent)
  56. : Superclass(_parent)
  57. , d_ptr(new ctkLinearValueProxyPrivate(*this))
  58. {
  59. }
  60. // --------------------------------------------------------------------------
  61. ctkLinearValueProxy::~ctkLinearValueProxy()
  62. {
  63. }
  64. // --------------------------------------------------------------------------
  65. double ctkLinearValueProxy::proxyValueFromValue(double value) const
  66. {
  67. return (this->coefficient() * value) + this->offset();
  68. }
  69. // --------------------------------------------------------------------------
  70. double ctkLinearValueProxy::valueFromProxyValue(double proxyValue) const
  71. {
  72. return (proxyValue - this->offset()) / this->coefficient();
  73. }
  74. // --------------------------------------------------------------------------
  75. CTK_GET_CPP(ctkLinearValueProxy, double, coefficient, Coefficient);
  76. CTK_GET_CPP(ctkLinearValueProxy, double, offset, Offset);
  77. // --------------------------------------------------------------------------
  78. void ctkLinearValueProxy::setCoefficient(double newCoeff)
  79. {
  80. Q_D(ctkLinearValueProxy);
  81. if (d->Coefficient == newCoeff)
  82. {
  83. return;
  84. }
  85. emit proxyAboutToBeModified();
  86. d->Coefficient = newCoeff;
  87. this->updateProxyValue();
  88. emit proxyModified();
  89. }
  90. // --------------------------------------------------------------------------
  91. void ctkLinearValueProxy::setOffset(double newOffset)
  92. {
  93. Q_D(ctkLinearValueProxy);
  94. if (d->Offset == newOffset)
  95. {
  96. return;
  97. }
  98. emit proxyAboutToBeModified();
  99. d->Offset = newOffset;
  100. this->updateProxyValue();
  101. emit proxyModified();
  102. }