ctkTransferFunctionBarsItem.cpp 7.8 KB

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