ctkTransferFunctionBarsItem.cpp 3.8 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 <QGraphicsSceneMouseEvent>
  18. #include <QPainter>
  19. #include <QtGlobal>
  20. #include <QVariant>
  21. /// CTK includes
  22. #include "ctkTransferFunction.h"
  23. #include "ctkTransferFunctionBarsItem.h"
  24. #include "ctkTransferFunctionScene.h"
  25. // std includes
  26. #include <cmath>
  27. //-----------------------------------------------------------------------------
  28. class ctkTransferFunctionBarsItemPrivate: public ctkPrivate<ctkTransferFunctionBarsItem>
  29. {
  30. public:
  31. ctkTransferFunctionBarsItemPrivate();
  32. qreal BarWidth;
  33. bool Log;
  34. };
  35. ctkTransferFunctionBarsItemPrivate::ctkTransferFunctionBarsItemPrivate()
  36. {
  37. this->BarWidth = 0.6180; // golden ratio... why not.
  38. this->Log = true;
  39. }
  40. //-----------------------------------------------------------------------------
  41. ctkTransferFunctionBarsItem::ctkTransferFunctionBarsItem(QGraphicsItem* parentGraphicsItem)
  42. :ctkTransferFunctionItem(parentGraphicsItem)
  43. {
  44. CTK_INIT_PRIVATE(ctkTransferFunctionBarsItem);
  45. }
  46. //-----------------------------------------------------------------------------
  47. ctkTransferFunctionBarsItem::ctkTransferFunctionBarsItem(
  48. ctkTransferFunction* transferFunc, QGraphicsItem* parentItem)
  49. :ctkTransferFunctionItem(transferFunc, parentItem)
  50. {
  51. CTK_INIT_PRIVATE(ctkTransferFunctionBarsItem);
  52. }
  53. //-----------------------------------------------------------------------------
  54. ctkTransferFunctionBarsItem::~ctkTransferFunctionBarsItem()
  55. {
  56. }
  57. //-----------------------------------------------------------------------------
  58. void ctkTransferFunctionBarsItem::setBarWidth(qreal newBarWidth)
  59. {
  60. CTK_D(ctkTransferFunctionBarsItem);
  61. if (d->BarWidth == newBarWidth)
  62. {
  63. return;
  64. }
  65. d->BarWidth = newBarWidth;
  66. this->update();
  67. }
  68. //-----------------------------------------------------------------------------
  69. qreal ctkTransferFunctionBarsItem::barWidth()const
  70. {
  71. CTK_D(const ctkTransferFunctionBarsItem);
  72. return d->BarWidth;
  73. }
  74. //-----------------------------------------------------------------------------
  75. void ctkTransferFunctionBarsItem::paint(
  76. QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
  77. {
  78. CTK_D(ctkTransferFunctionBarsItem);
  79. int count = this->transferFunction() ? this->transferFunction()->count() : 0;
  80. if (count <= 0)
  81. {
  82. return;
  83. }
  84. ctkTransferFunctionScene* tfScene = dynamic_cast<ctkTransferFunctionScene*>(this->scene());
  85. Q_ASSERT(tfScene);
  86. const QList<QPointF>& points = tfScene->points();
  87. QPainterPath bars;
  88. QPen pen(QColor(255, 255, 255, 191), 1);
  89. pen.setCosmetic(true);
  90. painter->setPen(pen);
  91. painter->setBrush(QBrush(QColor(191, 191, 191, 127)));
  92. qreal barWidth = d->BarWidth * (this->rect().width() / (points.size() - 1));
  93. foreach(const QPointF& point, points)
  94. {
  95. qreal barHeight = point.y();
  96. if (d->Log && barHeight != 1.)
  97. {
  98. barHeight = this->rect().height() - log( tfScene->mapYFromScene(barHeight) )/log(this->transferFunction()->maxValue().toReal());// 1. - (-log(barHeight)/100.);
  99. }
  100. bars.addRect(point.x() - barWidth/2, this->rect().height(),
  101. barWidth, barHeight - this->rect().height() );
  102. }
  103. painter->drawPath(bars);
  104. }