ctkTransferFunctionGradientItem.cpp 3.8 KB

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