ctkTransferFunctionControlPointsItem.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "ctkTransferFunctionControlPointsItem.h"
  24. #include "ctkTransferFunctionScene.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(12,12);
  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. ctkTransferFunctionScene* tfScene = dynamic_cast<ctkTransferFunctionScene*>(this->scene());
  78. Q_ASSERT(tfScene);
  79. const QPainterPath& curve = tfScene->curve();
  80. QPen pen(QColor(255, 255, 255, 191), 1);
  81. pen.setCosmetic(true);
  82. painter->setPen(pen);
  83. painter->drawPath(curve);
  84. d->ControlPoints = tfScene->points();
  85. painter->setBrush(QBrush(QColor(191, 191, 191, 127)));
  86. painter->save();
  87. QTransform transform = painter->transform();
  88. painter->setTransform(QTransform());
  89. foreach(const QPointF& point, d->ControlPoints)
  90. {
  91. QPointF pos = transform.map(point);
  92. painter->drawEllipse(pos.x() - d->PointSize.width() / 2,
  93. pos.y() - d->PointSize.height() / 2,
  94. d->PointSize.width(), d->PointSize.width());
  95. //points.addEllipse(point, d->PointSize.width(), d->PointSize.height());
  96. }
  97. painter->restore();
  98. }
  99. //-----------------------------------------------------------------------------
  100. void ctkTransferFunctionControlPointsItem::mousePressEvent(QGraphicsSceneMouseEvent* e)
  101. {
  102. qDebug() << "mouse press caught";
  103. CTK_D(ctkTransferFunctionControlPointsItem);
  104. QRectF pointArea(QPointF(0,0), d->PointSize*2.);
  105. d->SelectedPoint = -1;
  106. for(int i = 0; i < d->ControlPoints.count(); ++i)
  107. {
  108. pointArea.moveCenter(d->ControlPoints[i]);
  109. if (pointArea.contains(e->pos()))
  110. {
  111. d->SelectedPoint = i;
  112. break;
  113. }
  114. }
  115. if (d->SelectedPoint < 0)
  116. {
  117. //QPointF currentPoint( e->pos() );
  118. // e->pos()->x();
  119. //this->transferFunction()->insertControlPoint( e->pos().x() );
  120. // convert coordinates
  121. QPointF functionCoordinates = screen2TransferFunctionCoordinates( e->pos().x(), e->pos().y());
  122. // add point to transfer function
  123. // returns index
  124. int index = this->transferFunction()->insertControlPoint( functionCoordinates.x());
  125. // update value of the point
  126. this->transferFunction()->setControlPointValue( index, functionCoordinates.y());
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. void ctkTransferFunctionControlPointsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
  131. {
  132. qDebug() << "mouse caught";
  133. CTK_D(ctkTransferFunctionControlPointsItem);
  134. if (d->SelectedPoint < 0)
  135. {
  136. e->ignore();
  137. return;
  138. }
  139. qreal range[2];
  140. this->transferFunction()->range(range);
  141. qreal newPos = range[0] + e->pos().x() / (this->rect().width() / (range[1] - range[0]));
  142. newPos = qBound(range[0], newPos, range[1]);
  143. this->transferFunction()->setControlPointPos(d->SelectedPoint, newPos);
  144. //this->transferFunction()->setControlPointValue(d->SelectedPoint, e->y());
  145. }
  146. //-----------------------------------------------------------------------------
  147. void ctkTransferFunctionControlPointsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* e)
  148. {
  149. qDebug() << "mouse release caught";
  150. CTK_D(ctkTransferFunctionControlPointsItem);
  151. if (d->SelectedPoint < 0)
  152. {
  153. e->ignore();
  154. return;
  155. }
  156. d->SelectedPoint = -1;
  157. }