ctkTransferFunctionBarsItem.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 <QApplication>
  16. #include <QColor>
  17. #include <QDebug>
  18. #include <QGraphicsSceneMouseEvent>
  19. #include <QPainter>
  20. #include <QPalette>
  21. #include <QtGlobal>
  22. #include <QVariant>
  23. /// CTK includes
  24. #include "ctkTransferFunction.h"
  25. #include "ctkTransferFunctionBarsItem.h"
  26. #include "ctkTransferFunctionRepresentation.h"
  27. #include "ctkTransferFunctionScene.h"
  28. // std includes
  29. #include <cmath>
  30. //-----------------------------------------------------------------------------
  31. class ctkTransferFunctionBarsItemPrivate: public ctkPrivate<ctkTransferFunctionBarsItem>
  32. {
  33. public:
  34. ctkTransferFunctionBarsItemPrivate();
  35. QPainterPath createBarsPath(ctkTransferFunction* tf, const QList<QPointF>& points, qreal barWidth, bool useLog, const QRectF& rect);
  36. QPainterPath createAreaPath(ctkTransferFunction* tf, const QList<QPointF>& points, qreal barWidth, bool useLog, const QRectF& rect);
  37. qreal barWidth()const;
  38. bool useLog()const;
  39. qreal BarWidthRatio;
  40. QColor BarColor;
  41. ctkTransferFunctionBarsItem::LogMode LogMode;
  42. };
  43. //-----------------------------------------------------------------------------
  44. ctkTransferFunctionBarsItemPrivate::ctkTransferFunctionBarsItemPrivate()
  45. {
  46. this->BarWidthRatio = 0.6180; // golden ratio... why not.
  47. this->BarColor = QColor(191, 191, 191, 127);
  48. this->BarColor = QApplication::palette().color(QPalette::Normal, QPalette::Highlight);
  49. this->BarColor.setAlphaF(0.2);
  50. this->LogMode = ctkTransferFunctionBarsItem::AutoLog;
  51. }
  52. //-----------------------------------------------------------------------------
  53. ctkTransferFunctionBarsItem::ctkTransferFunctionBarsItem(QGraphicsItem* parentGraphicsItem)
  54. :ctkTransferFunctionItem(parentGraphicsItem)
  55. {
  56. CTK_INIT_PRIVATE(ctkTransferFunctionBarsItem);
  57. }
  58. //-----------------------------------------------------------------------------
  59. ctkTransferFunctionBarsItem::ctkTransferFunctionBarsItem(
  60. ctkTransferFunction* transferFunc, QGraphicsItem* parentItem)
  61. :ctkTransferFunctionItem(transferFunc, parentItem)
  62. {
  63. CTK_INIT_PRIVATE(ctkTransferFunctionBarsItem);
  64. }
  65. //-----------------------------------------------------------------------------
  66. ctkTransferFunctionBarsItem::~ctkTransferFunctionBarsItem()
  67. {
  68. }
  69. //-----------------------------------------------------------------------------
  70. void ctkTransferFunctionBarsItem::setBarWidth(qreal newBarWidthRatio)
  71. {
  72. CTK_D(ctkTransferFunctionBarsItem);
  73. newBarWidthRatio = qBound(0., newBarWidthRatio, 1.);
  74. if (d->BarWidthRatio == newBarWidthRatio)
  75. {
  76. return;
  77. }
  78. d->BarWidthRatio = newBarWidthRatio;
  79. this->update();
  80. }
  81. //-----------------------------------------------------------------------------
  82. qreal ctkTransferFunctionBarsItem::barWidth()const
  83. {
  84. CTK_D(const ctkTransferFunctionBarsItem);
  85. return d->BarWidthRatio;
  86. }
  87. //-----------------------------------------------------------------------------
  88. void ctkTransferFunctionBarsItem::setBarColor(const QColor& color)
  89. {
  90. CTK_D(ctkTransferFunctionBarsItem);
  91. d->BarColor = color;
  92. }
  93. //-----------------------------------------------------------------------------
  94. QColor ctkTransferFunctionBarsItem::barColor()const
  95. {
  96. CTK_D(const ctkTransferFunctionBarsItem);
  97. return d->BarColor;
  98. }
  99. //-----------------------------------------------------------------------------
  100. void ctkTransferFunctionBarsItem::paint(
  101. QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
  102. {
  103. CTK_D(ctkTransferFunctionBarsItem);
  104. Q_UNUSED(option);
  105. Q_UNUSED(widget);
  106. // setup colors
  107. QColor penColor = d->BarColor;
  108. penColor.setAlphaF(1.);
  109. QPen pen;
  110. if (penColor == QApplication::palette().color(QPalette::Normal, QPalette::Highlight))
  111. {
  112. pen = QPen(penColor, 1);
  113. }
  114. else
  115. {
  116. pen = QPen(QColor(255, 255, 255, 191), 1);
  117. }
  118. pen.setCosmetic(true);
  119. // setup drawing
  120. ctkTransferFunction* tf = this->transferFunction();
  121. if (tf == 0 || tf->count() < 1)
  122. {
  123. return;
  124. }
  125. Q_ASSERT(tf->representation());
  126. const QList<QPointF>& points = tf->representation()->points();
  127. qreal barWidth = d->barWidth();
  128. bool useLog = d->useLog();
  129. QPainterPath bars;
  130. if (qFuzzyCompare(d->BarWidthRatio, 1.))
  131. {
  132. bars = d->createAreaPath(tf, points, barWidth, useLog, this->rect());
  133. pen.setWidth(2);
  134. }
  135. else
  136. {
  137. bars = d->createBarsPath(tf, points, barWidth, useLog, this->rect());
  138. }
  139. painter->setPen(pen);
  140. painter->setBrush(QBrush(d->BarColor));
  141. painter->drawPath(bars);
  142. }
  143. //-----------------------------------------------------------------------------
  144. qreal ctkTransferFunctionBarsItemPrivate::barWidth()const
  145. {
  146. CTK_P(const ctkTransferFunctionBarsItem);
  147. ctkTransferFunction* tf = p->transferFunction();
  148. Q_ASSERT(tf);
  149. return this->BarWidthRatio * (p->rect().width() / (tf->representation()->points().size() - 1));
  150. }
  151. //-----------------------------------------------------------------------------
  152. bool ctkTransferFunctionBarsItemPrivate::useLog()const
  153. {
  154. CTK_P(const ctkTransferFunctionBarsItem);
  155. ctkTransferFunction* tf = p->transferFunction();
  156. Q_ASSERT(tf);
  157. bool useLog = false;
  158. switch (this->LogMode)
  159. {
  160. case ctkTransferFunctionBarsItem::AutoLog:
  161. useLog = tf->maxValue().toReal() - tf->minValue().toReal() > 1000.;
  162. break;
  163. case ctkTransferFunctionBarsItem::UseLog:
  164. useLog = true;
  165. break;
  166. default:
  167. case ctkTransferFunctionBarsItem::NoLog:
  168. useLog = false;
  169. }
  170. return useLog;
  171. }
  172. //-----------------------------------------------------------------------------
  173. QPainterPath ctkTransferFunctionBarsItemPrivate::createBarsPath(ctkTransferFunction* tf, const QList<QPointF>& points, qreal barWidth, bool useLog, const QRectF& rect)
  174. {
  175. ctkTransferFunctionRepresentation* tfRep = tf->representation();
  176. Q_ASSERT(tfRep);
  177. QPainterPath bars;
  178. foreach(const QPointF& point, points)
  179. {
  180. qreal barHeight = point.y();
  181. if (useLog && barHeight != 1.)
  182. {
  183. barHeight = rect.height() - log( tfRep->mapYFromScene(barHeight) )/log(tf->maxValue().toReal());
  184. }
  185. bars.addRect(point.x() - barWidth/2, rect.height(),
  186. barWidth, barHeight - rect.height() );
  187. }
  188. return bars;
  189. }
  190. //-----------------------------------------------------------------------------
  191. QPainterPath ctkTransferFunctionBarsItemPrivate::createAreaPath(ctkTransferFunction* tf, const QList<QPointF>& points, qreal barWidth, bool useLog, const QRectF& rect)
  192. {
  193. ctkTransferFunctionRepresentation* tfRep = tf->representation();
  194. Q_ASSERT(tfRep);
  195. QPainterPath bars;
  196. // 0.001 is here to ensure the outer border is not displayed on the screen
  197. bars.moveTo(-barWidth/2, rect.height() + 0.1);
  198. foreach(const QPointF& point, points)
  199. {
  200. qreal barHeight = point.y();
  201. if (useLog && barHeight != 1.)
  202. {
  203. barHeight = rect.height() - log( tfRep->mapYFromScene(barHeight) )/log(tf->maxValue().toReal());
  204. }
  205. bars.lineTo(point.x() - barWidth/2, barHeight);
  206. bars.lineTo(point.x() + barWidth/2, barHeight);
  207. }
  208. // close the path ?
  209. bars.lineTo(rect.width() + barWidth/2, rect.height() + 0.1);
  210. bars.lineTo(-barWidth/2, rect.height() + 0.1);
  211. return bars;
  212. }