ctkTransferFunctionGradientItem.cpp 3.6 KB

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