ctkTransferFunctionGradientItem.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. ctkControlPoint* startCP = this->transferFunction()->controlPoint(0);
  54. ctkControlPoint* endCP = 0;
  55. qreal start = startCP->x() * rangeDiff;
  56. qreal end = 0;
  57. for(int i = 1; i < count; ++i)
  58. {
  59. endCP = this->transferFunction()->controlPoint(i);
  60. // TODO, handle Bezier points for a finer gradient
  61. // TODO, handle nonlinear points
  62. if (dynamic_cast<ctkNonLinearControlPoint*>(startCP) != 0)
  63. {
  64. QList<ctkPoint> points = this->nonLinearPoints(startCP, endCP);
  65. for (int j = 1; j < points.count(); ++j)
  66. {
  67. end = points[j].X * rangeDiff;
  68. QLinearGradient gradient(start, 0, end, 0);
  69. gradient.setColorAt(0, this->color(points[j-1]));
  70. gradient.setColorAt(1, this->color(points[j]));
  71. QRectF itemRect = QRectF(start, 0, end - start,
  72. this->rect().height());
  73. if (i==1 && j == 1)
  74. {
  75. itemRect.setLeft(0.);
  76. }
  77. if ((i == count -1) && (j == points.count() -1))
  78. {
  79. itemRect.setRight(this->rect().width());
  80. }
  81. painter->fillRect(itemRect, gradient);
  82. start = end;
  83. }
  84. }
  85. else
  86. {
  87. end = endCP->x() * rangeDiff;
  88. QLinearGradient gradient(start, 0, end, 0);
  89. gradient.setColorAt(0, this->color(startCP->value()));
  90. gradient.setColorAt(1, this->color(endCP->value()));
  91. QRectF itemRect = QRectF(start, 0, end - start,
  92. this->rect().height());
  93. if (i==1)
  94. {
  95. itemRect.setLeft(0.);
  96. }
  97. if (i == count -1)
  98. {
  99. itemRect.setRight(this->rect().width());
  100. }
  101. painter->fillRect(itemRect, gradient);
  102. }
  103. delete startCP;
  104. startCP = endCP;
  105. start = end;
  106. }
  107. if (startCP)
  108. {
  109. delete startCP;
  110. }
  111. }