ctkTransferFunctionControlPointsItem.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "ctkTransferFunctionControlPointsItem.h"
  25. //-----------------------------------------------------------------------------
  26. class ctkTransferFunctionControlPointsItemPrivate:
  27. public ctkPrivate<ctkTransferFunctionControlPointsItem>
  28. {
  29. public:
  30. ctkTransferFunctionControlPointsItemPrivate();
  31. void init();
  32. QList<QPointF> ControlPoints;
  33. QSizeF PointSize;
  34. int SelectedPoint;
  35. };
  36. //-----------------------------------------------------------------------------
  37. ctkTransferFunctionControlPointsItemPrivate::ctkTransferFunctionControlPointsItemPrivate()
  38. {
  39. this->PointSize = QSizeF(10.,10.);
  40. this->SelectedPoint = -1;
  41. }
  42. //-----------------------------------------------------------------------------
  43. void ctkTransferFunctionControlPointsItemPrivate::init()
  44. {
  45. CTK_P(ctkTransferFunctionControlPointsItem);
  46. p->setAcceptedMouseButtons(Qt::LeftButton);
  47. }
  48. //-----------------------------------------------------------------------------
  49. ctkTransferFunctionControlPointsItem::ctkTransferFunctionControlPointsItem(QGraphicsItem* parentGraphicsItem)
  50. :ctkTransferFunctionItem(parentGraphicsItem)
  51. {
  52. CTK_INIT_PRIVATE(ctkTransferFunctionControlPointsItem);
  53. ctk_d()->init();
  54. }
  55. //-----------------------------------------------------------------------------
  56. ctkTransferFunctionControlPointsItem::ctkTransferFunctionControlPointsItem(
  57. ctkTransferFunction* transferFunction, QGraphicsItem* parentItem)
  58. :ctkTransferFunctionItem(transferFunction, parentItem)
  59. {
  60. CTK_INIT_PRIVATE(ctkTransferFunctionControlPointsItem);
  61. ctk_d()->init();
  62. }
  63. //-----------------------------------------------------------------------------
  64. ctkTransferFunctionControlPointsItem::~ctkTransferFunctionControlPointsItem()
  65. {
  66. }
  67. //-----------------------------------------------------------------------------
  68. void ctkTransferFunctionControlPointsItem::paint(
  69. QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
  70. {
  71. CTK_D(ctkTransferFunctionControlPointsItem);
  72. int count = this->transferFunction() ? this->transferFunction()->count() : 0;
  73. if (count <= 0)
  74. {
  75. return;
  76. }
  77. qreal rangeXDiff = this->rangeXDiff();
  78. qreal rangeXOffSet = this->rangeXOffSet();
  79. qreal rangeYDiff = this->rangeYDiff();
  80. qreal rangeYOffSet = this->rangeYDiff();
  81. ctkControlPoint* startCP = this->transferFunction()->controlPoint(0);
  82. ctkControlPoint* endCP = 0;
  83. qreal start = 0;
  84. qreal end = 0;
  85. QPainterPath path;
  86. // this->x(controlpoint)
  87. QPointF startPos(startCP->x() - rangeXOffSet, this->y(startCP->value()) - rangeYOffSet);
  88. startPos.rx() *= rangeXDiff;
  89. startPos.setY(this->rect().height()
  90. - startPos.y() * rangeYDiff);
  91. d->ControlPoints.clear();
  92. d->ControlPoints << startPos;
  93. path.moveTo(startPos);
  94. for(int i = 1; i < count; ++i)
  95. {
  96. endCP = this->transferFunction()->controlPoint(i);
  97. if (dynamic_cast<ctkNonLinearControlPoint*>(startCP))
  98. {
  99. QList<ctkPoint> points = this->nonLinearPoints(startCP, endCP);
  100. int j;
  101. for (j = 1; j < points.count(); ++j)
  102. {
  103. path.lineTo(
  104. QPointF(
  105. this->transferFunction2ScreenCoordinates(points[j].X,
  106. this->y(points[j].Value))));
  107. }
  108. j = points.count() - 1;
  109. d->ControlPoints << QPointF( this->transferFunction2ScreenCoordinates(
  110. points[j].X,
  111. this->y(points[j].Value)));
  112. }
  113. else //dynamic_cast<ctkBezierControlPoint*>(startCP))
  114. {
  115. QList<ctkPoint> points = this->bezierParams(startCP, endCP);
  116. QList<ctkPoint>::iterator it = points.begin();
  117. QList<QPointF> bezierPoints;
  118. foreach(const ctkPoint& p, points)
  119. {
  120. bezierPoints <<
  121. QPointF((p.X - rangeXOffSet)* rangeXDiff ,
  122. this->rect().height() - (this->y(p.Value) - rangeYOffSet)* rangeYDiff);
  123. }
  124. path.cubicTo(bezierPoints[1], bezierPoints[2], bezierPoints[3]);
  125. d->ControlPoints << bezierPoints[3];
  126. }
  127. //qDebug() << i << points[0] << points[1] << points[2] << points[3];
  128. delete startCP;
  129. startCP = endCP;
  130. }
  131. if (startCP)
  132. {
  133. delete startCP;
  134. }
  135. painter->setRenderHint(QPainter::Antialiasing);
  136. painter->setPen(QPen(QColor(255, 255, 255, 191), 1));
  137. painter->drawPath(path);
  138. QPainterPath points;
  139. foreach(const QPointF& point, d->ControlPoints)
  140. {
  141. points.addEllipse(point, d->PointSize.width(), d->PointSize.height());
  142. }
  143. painter->setBrush(QBrush(QColor(191, 191, 191, 127)));
  144. painter->drawPath(points);
  145. }
  146. //-----------------------------------------------------------------------------
  147. void ctkTransferFunctionControlPointsItem::mousePressEvent(QGraphicsSceneMouseEvent* e)
  148. {
  149. qDebug() << "mouse press caught";
  150. CTK_D(ctkTransferFunctionControlPointsItem);
  151. QRectF pointArea(QPointF(0,0), d->PointSize*2.);
  152. d->SelectedPoint = -1;
  153. for(int i = 0; i < d->ControlPoints.count(); ++i)
  154. {
  155. pointArea.moveCenter(d->ControlPoints[i]);
  156. if (pointArea.contains(e->pos()))
  157. {
  158. d->SelectedPoint = i;
  159. break;
  160. }
  161. }
  162. if (d->SelectedPoint < 0)
  163. {
  164. //QPointF currentPoint( e->pos() );
  165. // e->pos()->x();
  166. //this->transferFunction()->insertControlPoint( e->pos().x() );
  167. // convert coordinates
  168. QPointF functionCoordinates = screen2TransferFunctionCoordinates( e->pos().x(), e->pos().y());
  169. // add point to transfer function
  170. // returns index
  171. int index = this->transferFunction()->insertControlPoint( functionCoordinates.x());
  172. // update value of the point
  173. this->transferFunction()->setControlPointValue( index, functionCoordinates.y());
  174. }
  175. }
  176. //-----------------------------------------------------------------------------
  177. void ctkTransferFunctionControlPointsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
  178. {
  179. qDebug() << "mouse caught";
  180. CTK_D(ctkTransferFunctionControlPointsItem);
  181. if (d->SelectedPoint < 0)
  182. {
  183. e->ignore();
  184. return;
  185. }
  186. qreal range[2];
  187. this->transferFunction()->range(range);
  188. qreal newPos = range[0] + e->pos().x() / (this->rect().width() / (range[1] - range[0]));
  189. newPos = qBound(range[0], newPos, range[1]);
  190. this->transferFunction()->setControlPointPos(d->SelectedPoint, newPos);
  191. //this->transferFunction()->setControlPointValue(d->SelectedPoint, e->y());
  192. }
  193. //-----------------------------------------------------------------------------
  194. void ctkTransferFunctionControlPointsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* e)
  195. {
  196. qDebug() << "mouse release caught";
  197. CTK_D(ctkTransferFunctionControlPointsItem);
  198. if (d->SelectedPoint < 0)
  199. {
  200. e->ignore();
  201. return;
  202. }
  203. d->SelectedPoint = -1;
  204. }