ctkTransferFunctionGradientItem.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <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. #include "ctkTransferFunctionRepresentation.h"
  26. #include "ctkTransferFunctionScene.h"
  27. //-----------------------------------------------------------------------------
  28. class ctkTransferFunctionGradientItemPrivate
  29. {
  30. public:
  31. ctkTransferFunctionGradientItemPrivate();
  32. bool Mask;
  33. };
  34. //-----------------------------------------------------------------------------
  35. ctkTransferFunctionGradientItemPrivate::ctkTransferFunctionGradientItemPrivate()
  36. {
  37. this->Mask = true;
  38. }
  39. //-----------------------------------------------------------------------------
  40. ctkTransferFunctionGradientItem::ctkTransferFunctionGradientItem(QGraphicsItem* parentGraphicsItem)
  41. :ctkTransferFunctionItem(parentGraphicsItem)
  42. , d_ptr(new ctkTransferFunctionGradientItemPrivate)
  43. {
  44. }
  45. //-----------------------------------------------------------------------------
  46. ctkTransferFunctionGradientItem::ctkTransferFunctionGradientItem(
  47. ctkTransferFunction* transferFunction, QGraphicsItem* parentItem)
  48. :ctkTransferFunctionItem(transferFunction, parentItem)
  49. , d_ptr(new ctkTransferFunctionGradientItemPrivate)
  50. {
  51. }
  52. //-----------------------------------------------------------------------------
  53. ctkTransferFunctionGradientItem::~ctkTransferFunctionGradientItem()
  54. {
  55. }
  56. //-----------------------------------------------------------------------------
  57. void ctkTransferFunctionGradientItem::paint(
  58. QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
  59. {
  60. Q_UNUSED(option);
  61. Q_UNUSED(widget);
  62. if (this->transferFunction()->count() <= 0)
  63. {
  64. return;
  65. }
  66. //ctkTransferFunctionScene* tfScene = dynamic_cast<ctkTransferFunctionScene*>(this->scene());
  67. //Q_ASSERT(tfScene);
  68. ctkTransferFunctionRepresentation* tfRep = this->transferFunction()->representation();
  69. const QGradient& gradient = tfRep->gradient();
  70. if ( this->mask() )
  71. {
  72. QPainterPath closedPath = tfRep->curve();
  73. QRectF position = this->rect();
  74. // link to last point
  75. closedPath.lineTo(position.x() + position.width(), position.y() + position.height());
  76. // link to first point
  77. closedPath.lineTo(position.x(), position.y() + position.height());
  78. //Don,t need to close because automatic
  79. QPen pen(QColor(255, 255, 255, 191), 1);
  80. pen.setCosmetic(true);
  81. painter->setPen(pen);
  82. painter->setBrush(gradient);
  83. painter->drawPath(closedPath);
  84. }
  85. else
  86. {
  87. painter->fillRect(this->rect(), gradient);
  88. }
  89. }
  90. //-----------------------------------------------------------------------------
  91. bool ctkTransferFunctionGradientItem::mask() const
  92. {
  93. Q_D( const ctkTransferFunctionGradientItem );
  94. return d->Mask;
  95. }
  96. //-----------------------------------------------------------------------------
  97. void ctkTransferFunctionGradientItem::setMask( bool mask )
  98. {
  99. Q_D( ctkTransferFunctionGradientItem );
  100. d->Mask = mask;
  101. }