ctkTransferFunctionItem.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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.commontk.org/LICENSE
  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 <QColor>
  16. #include <QDebug>
  17. #include <QLinearGradient>
  18. #include <QGraphicsSceneMouseEvent>
  19. #include <QPainter>
  20. #include <QtGlobal>
  21. #include <QVariant>
  22. /// CTK includes
  23. #include "ctkTransferFunction.h"
  24. #include "ctkTransferFunctionItem.h"
  25. //-----------------------------------------------------------------------------
  26. class ctkTransferFunctionItemPrivate :
  27. public ctkPrivate<ctkTransferFunctionItem>
  28. {
  29. public:
  30. ctkTransferFunctionItemPrivate();
  31. QRectF Rect;
  32. ctkTransferFunction* TransferFunction;
  33. };
  34. //-----------------------------------------------------------------------------
  35. ctkTransferFunctionItemPrivate::ctkTransferFunctionItemPrivate()
  36. {
  37. this->TransferFunction = 0;
  38. }
  39. //-----------------------------------------------------------------------------
  40. ctkTransferFunctionItem::ctkTransferFunctionItem(QGraphicsItem* parentGraphicsItem)
  41. :QGraphicsObject(parentGraphicsItem)
  42. {
  43. CTK_INIT_PRIVATE(ctkTransferFunctionItem);
  44. }
  45. //-----------------------------------------------------------------------------
  46. ctkTransferFunctionItem::ctkTransferFunctionItem(
  47. ctkTransferFunction* transferFunction, QGraphicsItem* parentItem)
  48. :QGraphicsObject(parentItem)
  49. {
  50. CTK_INIT_PRIVATE(ctkTransferFunctionItem);
  51. this->setTransferFunction(transferFunction);
  52. }
  53. //-----------------------------------------------------------------------------
  54. ctkTransferFunctionItem::~ctkTransferFunctionItem()
  55. {
  56. }
  57. //-----------------------------------------------------------------------------
  58. void ctkTransferFunctionItem::setTransferFunction(ctkTransferFunction* transferFunction)
  59. {
  60. CTK_D(ctkTransferFunctionItem);
  61. if (d->TransferFunction == transferFunction)
  62. {
  63. return;
  64. }
  65. d->TransferFunction = transferFunction;
  66. connect(d->TransferFunction, SIGNAL(changed()),
  67. this, SLOT(onTransferFunctionChanged()));
  68. this->update();
  69. }
  70. //-----------------------------------------------------------------------------
  71. ctkTransferFunction* ctkTransferFunctionItem::transferFunction() const
  72. {
  73. CTK_D(const ctkTransferFunctionItem);
  74. return d->TransferFunction;
  75. }
  76. //-----------------------------------------------------------------------------
  77. void ctkTransferFunctionItem::onTransferFunctionChanged()
  78. {
  79. this->update();
  80. }
  81. //-----------------------------------------------------------------------------
  82. void ctkTransferFunctionItem::setRect(const QRectF& newRect)
  83. {
  84. CTK_D(ctkTransferFunctionItem);
  85. if (d->Rect == newRect)
  86. {
  87. return;
  88. }
  89. d->Rect = newRect;
  90. this->update();
  91. }
  92. //-----------------------------------------------------------------------------
  93. QRectF ctkTransferFunctionItem::rect() const
  94. {
  95. CTK_D(const ctkTransferFunctionItem);
  96. return d->Rect;
  97. }
  98. //-----------------------------------------------------------------------------
  99. QRectF ctkTransferFunctionItem::boundingRect()const
  100. {
  101. CTK_D(const ctkTransferFunctionItem);
  102. return d->Rect;
  103. }
  104. //-----------------------------------------------------------------------------
  105. QList<ctkPoint> ctkTransferFunctionItem::bezierParams(
  106. ctkControlPoint* start, ctkControlPoint* end) const
  107. {
  108. Q_ASSERT(start);
  109. Q_ASSERT(end);
  110. QList<ctkPoint> points;
  111. ctkBezierControlPoint* bezierCP = dynamic_cast<ctkBezierControlPoint*>(start);
  112. if (!bezierCP)
  113. {// just duplicate start and end into p1 and p2
  114. points << start->P;
  115. points << start->P;
  116. points << end->P;
  117. points << end->P;
  118. return points;
  119. }
  120. points << start->P;
  121. points << bezierCP->P1;
  122. points << bezierCP->P2;
  123. points << end->P;
  124. return points;
  125. }
  126. //-----------------------------------------------------------------------------
  127. QList<ctkPoint> ctkTransferFunctionItem::nonLinearPoints(
  128. ctkControlPoint* start, ctkControlPoint* end) const
  129. {
  130. Q_ASSERT(start);
  131. ctkNonLinearControlPoint* nonLinearCP =
  132. dynamic_cast<ctkNonLinearControlPoint*>(start);
  133. if (!nonLinearCP)
  134. {
  135. QList<ctkPoint> points;
  136. points << start->P;
  137. points << end->P;
  138. return points;
  139. }
  140. return nonLinearCP->SubPoints;
  141. }
  142. //-----------------------------------------------------------------------------
  143. qreal ctkTransferFunctionItem::y(const QVariant& v) const
  144. {
  145. Q_ASSERT(v.canConvert<qreal>() || v.canConvert<QColor>());
  146. if (v.canConvert<QColor>())
  147. {
  148. return v.value<QColor>().alphaF();
  149. }
  150. return v.toReal();
  151. }
  152. //-----------------------------------------------------------------------------
  153. QColor ctkTransferFunctionItem::color(const QVariant& v) const
  154. {
  155. //Q_ASSERT(v.canConvert<QColor>());
  156. if (v.canConvert<QColor>())
  157. {
  158. return v.value<QColor>();
  159. }
  160. else
  161. {
  162. //black background
  163. QColor defaultColor(0., 0., 0.);
  164. return defaultColor;
  165. }
  166. return QColor();
  167. }