ctkTransferFunctionControlPointsItem.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #include <QKeyEvent>
  23. /// CTK includes
  24. #include "ctkTransferFunctionControlPointsItem.h"
  25. #include "ctkTransferFunctionRepresentation.h"
  26. #include "ctkTransferFunctionScene.h"
  27. #include "ctkTransferFunctionView.h"
  28. //-----------------------------------------------------------------------------
  29. class ctkTransferFunctionControlPointsItemPrivate:
  30. public ctkPrivate<ctkTransferFunctionControlPointsItem>
  31. {
  32. public:
  33. ctkTransferFunctionControlPointsItemPrivate();
  34. void init();
  35. QList<QPointF> ControlPoints;
  36. QSize PointSize;
  37. int SelectedPoint;
  38. };
  39. //-----------------------------------------------------------------------------
  40. ctkTransferFunctionControlPointsItemPrivate::ctkTransferFunctionControlPointsItemPrivate()
  41. {
  42. this->PointSize = QSize(12,12);
  43. this->SelectedPoint = -1;
  44. }
  45. //-----------------------------------------------------------------------------
  46. void ctkTransferFunctionControlPointsItemPrivate::init()
  47. {
  48. CTK_P(ctkTransferFunctionControlPointsItem);
  49. p->setAcceptedMouseButtons(Qt::LeftButton);
  50. }
  51. //-----------------------------------------------------------------------------
  52. ctkTransferFunctionControlPointsItem::ctkTransferFunctionControlPointsItem(QGraphicsItem* parentGraphicsItem)
  53. :ctkTransferFunctionItem(parentGraphicsItem)
  54. {
  55. CTK_INIT_PRIVATE(ctkTransferFunctionControlPointsItem);
  56. ctk_d()->init();
  57. }
  58. //-----------------------------------------------------------------------------
  59. ctkTransferFunctionControlPointsItem::ctkTransferFunctionControlPointsItem(
  60. ctkTransferFunction* transferFunction, QGraphicsItem* parentItem)
  61. :ctkTransferFunctionItem(transferFunction, parentItem)
  62. {
  63. CTK_INIT_PRIVATE(ctkTransferFunctionControlPointsItem);
  64. ctk_d()->init();
  65. }
  66. //-----------------------------------------------------------------------------
  67. ctkTransferFunctionControlPointsItem::~ctkTransferFunctionControlPointsItem()
  68. {
  69. }
  70. //-----------------------------------------------------------------------------
  71. void ctkTransferFunctionControlPointsItem::paint(
  72. QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
  73. {
  74. Q_UNUSED(option);
  75. Q_UNUSED(widget);
  76. CTK_D(ctkTransferFunctionControlPointsItem);
  77. int count = this->transferFunction() ? this->transferFunction()->count() : 0;
  78. if (count <= 0)
  79. {
  80. return;
  81. }
  82. //ctkTransferFunctionScene* tfScene = dynamic_cast<ctkTransferFunctionScene*>(this->scene());
  83. //Q_ASSERT(tfScene);
  84. ctkTransferFunctionRepresentation* tfRep = this->transferFunction()->representation();
  85. const QPainterPath& curve = tfRep->curve();
  86. QPen pen(QColor(255, 255, 255, 191), 1);
  87. pen.setCosmetic(true);
  88. painter->setPen(pen);
  89. painter->drawPath(curve);
  90. d->ControlPoints = tfRep->points();
  91. painter->setBrush(QBrush(QColor(191, 191, 191, 127)));
  92. painter->save();
  93. QTransform transform = painter->transform();
  94. painter->setTransform(QTransform());
  95. foreach(const QPointF& point, d->ControlPoints)
  96. {
  97. QPointF pos = transform.map(point);
  98. painter->drawEllipse(pos.x() - d->PointSize.width() / 2,
  99. pos.y() - d->PointSize.height() / 2,
  100. d->PointSize.width(), d->PointSize.width());
  101. //points.addEllipse(point, d->PointSize.width(), d->PointSize.height());
  102. }
  103. painter->restore();
  104. }
  105. //-----------------------------------------------------------------------------
  106. void ctkTransferFunctionControlPointsItem::mousePressEvent(QGraphicsSceneMouseEvent* e)
  107. {
  108. CTK_D(ctkTransferFunctionControlPointsItem);
  109. ctkTransferFunctionView* view = qobject_cast<ctkTransferFunctionView*>(e->widget()->parentWidget());
  110. Q_ASSERT(view);
  111. // e->pos() is ok, pointArea should be in the world coordiate
  112. QRect pointViewArea(QPoint(-d->PointSize.width() / 2, -d->PointSize.height() / 2), d->PointSize);
  113. QPolygonF pointItemArea = this->mapFromScene(view->mapToScene(pointViewArea));
  114. d->SelectedPoint = -1;
  115. for(int i = 0; i < d->ControlPoints.count(); ++i)
  116. {
  117. if (pointItemArea.translated(d->ControlPoints[i]).containsPoint(e->pos(), Qt::OddEvenFill))
  118. {
  119. d->SelectedPoint = i;
  120. break;
  121. }
  122. }
  123. if (d->SelectedPoint >= 0)
  124. {
  125. return;
  126. }
  127. //ctkTransferFunctionScene* tfScene = dynamic_cast<ctkTransferFunctionScene*>(this->scene());
  128. //Q_ASSERT(tfScene);
  129. ctkTransferFunctionRepresentation* tfRep = this->transferFunction()->representation();
  130. // convert coordinates
  131. QPointF tfPos = tfRep->mapPointFromScene(e->pos());
  132. // add point to transfer function
  133. // returns index
  134. int index = this->transferFunction()->insertControlPoint( tfPos.x());
  135. //NOT WORKING IN COMPOSITE
  136. // update value of the point
  137. if (!QSharedPointer<ctkControlPoint>(this->transferFunction()->controlPoint(index))->value().canConvert<QColor>())
  138. {
  139. this->transferFunction()->setControlPointValue( index, tfPos.y());
  140. }
  141. }
  142. //-----------------------------------------------------------------------------
  143. void ctkTransferFunctionControlPointsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
  144. {
  145. CTK_D(ctkTransferFunctionControlPointsItem);
  146. if (d->SelectedPoint < 0)
  147. {
  148. e->ignore();
  149. return;
  150. }
  151. //ctkTransferFunctionScene* tfScene = dynamic_cast<ctkTransferFunctionScene*>(this->scene());
  152. //Q_ASSERT(tfScene);
  153. ctkTransferFunctionRepresentation* tfRep = this->transferFunction()->representation();
  154. QPointF newPos = tfRep->mapPointFromScene(e->pos());
  155. // Deal with borders
  156. if(d->SelectedPoint == 0 || d->SelectedPoint == this->transferFunction()->count() )
  157. {
  158. // BEHAVIOR TO BE IMPLEMENTED
  159. // int borderBehavior = tfScene->borderBehavior();
  160. // LockBorder
  161. // Create new point on border at same height
  162. // Create new point on border on top
  163. // Create new point on border on bottom
  164. qDebug() << "border" ;
  165. return;
  166. }
  167. else
  168. {
  169. // TO BE IMPLEMENTED
  170. //int movePointsBehavior = tfScene->movePointsBehavior();
  171. // initialize to BLOCK_MOVE for now
  172. int movePointsBehavior = STOP_MOVE;
  173. switch( movePointsBehavior ){
  174. case STOP_MOVE:
  175. stopPoints(newPos);
  176. break;
  177. case SWITCH_MOVE:
  178. switchPoints(newPos);
  179. break;
  180. case DRAW_MOVE:
  181. drawPoints(newPos);
  182. break;
  183. case FUSION_MOVE:
  184. fusionPoints(newPos);
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. }
  191. //-----------------------------------------------------------------------------
  192. void ctkTransferFunctionControlPointsItem::stopPoints( QPointF iPointF )
  193. {
  194. CTK_D(ctkTransferFunctionControlPointsItem);
  195. if( this->transferFunction()->controlPoint(d->SelectedPoint - 1)->x() > iPointF.x() ||
  196. this->transferFunction()->controlPoint(d->SelectedPoint + 1)->x() < iPointF.x())
  197. {
  198. return;
  199. }
  200. updatePointPosition(iPointF);
  201. }
  202. //-----------------------------------------------------------------------------
  203. void ctkTransferFunctionControlPointsItem::switchPoints( QPointF iPointF )
  204. {
  205. CTK_D(ctkTransferFunctionControlPointsItem);
  206. // Increment or decrement selected point?
  207. // Don't need to check borders since it is done just before calling this method...
  208. if( this->transferFunction()->controlPoint(d->SelectedPoint - 1)->x() > iPointF.x() )
  209. {
  210. // NOT WORKING IF COMPOSITE TRANSFER FUNCTION
  211. double value = this->transferFunction()->value(d->SelectedPoint-1 ).toDouble();
  212. d->SelectedPoint -= 1;
  213. this->transferFunction()->setControlPointValue(d->SelectedPoint+1, value);
  214. }
  215. else if ( this->transferFunction()->controlPoint(d->SelectedPoint + 1)->x() < iPointF.x() )
  216. {
  217. double value = this->transferFunction()->value(d->SelectedPoint + 1 ).toDouble();
  218. d->SelectedPoint += 1;
  219. this->transferFunction()->setControlPointValue(d->SelectedPoint-1, value);
  220. }
  221. updatePointPosition(iPointF);
  222. }
  223. //-----------------------------------------------------------------------------
  224. void ctkTransferFunctionControlPointsItem::drawPoints( QPointF iPointF )
  225. {
  226. CTK_D(ctkTransferFunctionControlPointsItem);
  227. // Increment or decrement selected point
  228. if( this->transferFunction()->controlPoint(d->SelectedPoint - 1)->x() > iPointF.x() )
  229. {
  230. //change the selected point
  231. d->SelectedPoint = d->SelectedPoint -1;
  232. }
  233. else if ( this->transferFunction()->controlPoint(d->SelectedPoint + 1)->x() < iPointF.x() )
  234. {
  235. d->SelectedPoint = d->SelectedPoint +1;
  236. }
  237. updatePointPosition(iPointF);
  238. }
  239. //-----------------------------------------------------------------------------
  240. void ctkTransferFunctionControlPointsItem::fusionPoints( QPointF iPointF )
  241. {
  242. Q_UNUSED(iPointF);
  243. // TO BE IMPLEMENTED
  244. // if 2 points are to close: delete one..?
  245. }
  246. //-----------------------------------------------------------------------------
  247. void ctkTransferFunctionControlPointsItem::updatePointPosition( QPointF iPoint )
  248. {
  249. CTK_D(ctkTransferFunctionControlPointsItem);
  250. this->transferFunction()->setControlPointPos(d->SelectedPoint, iPoint.x());
  251. // TEST NOT WORKING IN COMPOSITE TRANSFER FUNCTION
  252. if ( ! QSharedPointer<ctkControlPoint>(this->transferFunction()->controlPoint(d->SelectedPoint))->value().canConvert<QColor>() )
  253. {
  254. this->transferFunction()->setControlPointValue(d->SelectedPoint, iPoint.y());
  255. }
  256. }
  257. //-----------------------------------------------------------------------------
  258. void ctkTransferFunctionControlPointsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* e)
  259. {
  260. CTK_D(ctkTransferFunctionControlPointsItem);
  261. if (d->SelectedPoint < 0)
  262. {
  263. e->ignore();
  264. return;
  265. }
  266. d->SelectedPoint = -1;
  267. }
  268. //-----------------------------------------------------------------------------
  269. void ctkTransferFunctionControlPointsItem::keyPressEvent ( QKeyEvent * keyEvent )
  270. {
  271. CTK_D(ctkTransferFunctionControlPointsItem);
  272. // if a point is selected, "d" and "D" = delete the point
  273. if (d->SelectedPoint >= 0)
  274. {
  275. if ( ! QString::compare(keyEvent->text(), "d") || ! QString::compare(keyEvent->text(), "D") )
  276. {
  277. // Get value of selected point
  278. qreal xValue = this->transferFunction()->controlPoint( d->SelectedPoint )->x();
  279. // remove point using value ( can't use point ID because it could engender
  280. // problems when multiple points selection implemented)
  281. this->transferFunction()->removeControlPoint( xValue );
  282. }
  283. }
  284. }