ctkTransferFunctionGradientItem.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "ctkTransferFunctionGradientItem.h"
  25. //-----------------------------------------------------------------------------
  26. ctkTransferFunctionGradientItem::ctkTransferFunctionGradientItem(QGraphicsItem* parentGraphicsItem)
  27. :ctkTransferFunctionItem(parentGraphicsItem)
  28. {
  29. }
  30. //-----------------------------------------------------------------------------
  31. ctkTransferFunctionGradientItem::ctkTransferFunctionGradientItem(
  32. ctkTransferFunction* transferFunction, QGraphicsItem* parentItem)
  33. :ctkTransferFunctionItem(transferFunction, parentItem)
  34. {
  35. }
  36. //-----------------------------------------------------------------------------
  37. ctkTransferFunctionGradientItem::~ctkTransferFunctionGradientItem()
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. void ctkTransferFunctionGradientItem::paint(
  42. QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
  43. {
  44. int count = this->transferFunction() ? this->transferFunction()->count() : 0;
  45. if (count <= 0)
  46. {
  47. painter->fillRect(this->rect(),Qt::black);
  48. return;
  49. }
  50. qreal range[2];
  51. this->transferFunction()->range(range);
  52. qreal rangeDiff = this->rect().width() / (range[1] - range[0]);
  53. qreal rangeOffset = range[0];
  54. ctkControlPoint* startCP = this->transferFunction()->controlPoint(0);
  55. ctkControlPoint* endCP = 0;
  56. qreal start = (startCP->x() - rangeOffset) * rangeDiff;
  57. qreal end = 0;
  58. for(int i = 1; i < count; ++i)
  59. {
  60. endCP = this->transferFunction()->controlPoint(i);
  61. // TODO, handle Bezier points for a finer gradient
  62. // TODO, handle nonlinear points
  63. if (this->transferFunction()->isDiscrete())
  64. {
  65. // pos
  66. end = ((startCP->x() + endCP->x()) /2. -rangeOffset) * rangeDiff;
  67. QRectF itemRect = QRectF(start, 0, end - start,
  68. this->rect().height());
  69. if (i==1)
  70. {
  71. itemRect.setLeft(0.);
  72. }
  73. QColor valueColor = this->color(startCP->value());
  74. // paint
  75. painter->fillRect(itemRect, valueColor);
  76. // draw the last item
  77. if (i == count -1)
  78. {
  79. //pos
  80. itemRect = QRectF(end, 0, this->rect().width(),
  81. this->rect().height());
  82. // color
  83. valueColor = this->color(endCP->value());
  84. // paint
  85. painter->fillRect(itemRect, valueColor);
  86. }
  87. }
  88. else if (dynamic_cast<ctkNonLinearControlPoint*>(startCP) != 0)
  89. {
  90. QList<ctkPoint> points = this->nonLinearPoints(startCP, endCP);
  91. for (int j = 1; j < points.count(); ++j)
  92. {
  93. // pos
  94. end = (points[j].X - rangeOffset) * rangeDiff;
  95. QRectF itemRect = QRectF(start, 0, end - start,
  96. this->rect().height());
  97. if (i==1 && j == 1)
  98. {
  99. itemRect.setLeft(0.);
  100. }
  101. if ((i == count -1) && (j == points.count() -1))
  102. {
  103. itemRect.setRight(this->rect().width());
  104. }
  105. // color
  106. QLinearGradient gradient(start, 0, end, 0);
  107. gradient.setColorAt(0, this->color(points[j-1]));
  108. gradient.setColorAt(1, this->color(points[j]));
  109. // paint
  110. painter->fillRect(itemRect, gradient);
  111. start = end;
  112. }
  113. }
  114. else
  115. {
  116. // pos
  117. end = (endCP->x() - rangeOffset) * rangeDiff;
  118. QRectF itemRect = QRectF(start, 0, end - start,
  119. this->rect().height());
  120. if (i==1)
  121. {
  122. itemRect.setLeft(0.);
  123. }
  124. if (i == count -1)
  125. {
  126. itemRect.setRight(this->rect().width());
  127. }
  128. // color
  129. QLinearGradient gradient(start, 0, end, 0);
  130. gradient.setColorAt(0, this->color(startCP->value()));
  131. gradient.setColorAt(1, this->color(endCP->value()));
  132. // paint
  133. painter->fillRect(itemRect, gradient);
  134. }
  135. delete startCP;
  136. startCP = endCP;
  137. start = end;
  138. }
  139. if (startCP)
  140. {
  141. delete startCP;
  142. }
  143. }