ctkTransferFunctionItems.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 "ctkTransferFunctionItems.h"
  25. //-----------------------------------------------------------------------------
  26. class ctkTransferFunctionItemPrivate :
  27. public ctkPrivate<ctkTransferFunctionItem>
  28. {
  29. public:
  30. ctkTransferFunctionItemPrivate();
  31. QRectF Rect;
  32. ctkTransferFunction* TransferFunction;
  33. };
  34. //-----------------------------------------------------------------------------
  35. ctkTransferFunctionItemPrivate::ctkTransferFunctionItemPrivate()
  36. {
  37. this->TransferFunction = 0;
  38. }
  39. //-----------------------------------------------------------------------------
  40. ctkTransferFunctionItem::ctkTransferFunctionItem(QGraphicsItem* parentGraphicsItem)
  41. :QGraphicsObject(parentGraphicsItem)
  42. {
  43. CTK_INIT_PRIVATE(ctkTransferFunctionItem);
  44. }
  45. //-----------------------------------------------------------------------------
  46. ctkTransferFunctionItem::ctkTransferFunctionItem(
  47. ctkTransferFunction* transferFunction, QGraphicsItem* parentItem)
  48. :QGraphicsObject(parentItem)
  49. {
  50. CTK_INIT_PRIVATE(ctkTransferFunctionItem);
  51. this->setTransferFunction(transferFunction);
  52. }
  53. //-----------------------------------------------------------------------------
  54. ctkTransferFunctionItem::~ctkTransferFunctionItem()
  55. {
  56. }
  57. //-----------------------------------------------------------------------------
  58. void ctkTransferFunctionItem::setTransferFunction(ctkTransferFunction* transferFunction)
  59. {
  60. CTK_D(ctkTransferFunctionItem);
  61. if (d->TransferFunction == transferFunction)
  62. {
  63. return;
  64. }
  65. d->TransferFunction = transferFunction;
  66. connect(d->TransferFunction, SIGNAL(changed()),
  67. this, SLOT(onTransferFunctionChanged()));
  68. this->update();
  69. }
  70. //-----------------------------------------------------------------------------
  71. ctkTransferFunction* ctkTransferFunctionItem::transferFunction() const
  72. {
  73. CTK_D(const ctkTransferFunctionItem);
  74. return d->TransferFunction;
  75. }
  76. //-----------------------------------------------------------------------------
  77. void ctkTransferFunctionItem::onTransferFunctionChanged()
  78. {
  79. this->update();
  80. }
  81. //-----------------------------------------------------------------------------
  82. void ctkTransferFunctionItem::setRect(const QRectF& newRect)
  83. {
  84. CTK_D(ctkTransferFunctionItem);
  85. if (d->Rect == newRect)
  86. {
  87. return;
  88. }
  89. d->Rect = newRect;
  90. this->update();
  91. }
  92. //-----------------------------------------------------------------------------
  93. QRectF ctkTransferFunctionItem::rect() const
  94. {
  95. CTK_D(const ctkTransferFunctionItem);
  96. return d->Rect;
  97. }
  98. //-----------------------------------------------------------------------------
  99. QRectF ctkTransferFunctionItem::boundingRect()const
  100. {
  101. CTK_D(const ctkTransferFunctionItem);
  102. return d->Rect;
  103. }
  104. //-----------------------------------------------------------------------------
  105. QList<ctkPoint> ctkTransferFunctionItem::bezierParams(
  106. ctkControlPoint* start, ctkControlPoint* end) const
  107. {
  108. Q_ASSERT(start);
  109. Q_ASSERT(end);
  110. QList<ctkPoint> points;
  111. ctkBezierControlPoint* bezierCP = dynamic_cast<ctkBezierControlPoint*>(start);
  112. if (!bezierCP)
  113. {// just duplicate start and end into p1 and p2
  114. points << start->P;
  115. points << start->P;
  116. points << end->P;
  117. points << end->P;
  118. return points;
  119. }
  120. points << start->P;
  121. points << bezierCP->P1;
  122. points << bezierCP->P2;
  123. points << end->P;
  124. return points;
  125. }
  126. //-----------------------------------------------------------------------------
  127. QList<ctkPoint> ctkTransferFunctionItem::nonLinearPoints(
  128. ctkControlPoint* start, ctkControlPoint* end) const
  129. {
  130. Q_ASSERT(start);
  131. ctkNonLinearControlPoint* nonLinearCP =
  132. dynamic_cast<ctkNonLinearControlPoint*>(start);
  133. if (!nonLinearCP)
  134. {
  135. QList<ctkPoint> points;
  136. points << start->P;
  137. points << end->P;
  138. return points;
  139. }
  140. return nonLinearCP->SubPoints;
  141. }
  142. //-----------------------------------------------------------------------------
  143. qreal ctkTransferFunctionItem::y(const QVariant& v) const
  144. {
  145. Q_ASSERT(v.canConvert<qreal>() || v.canConvert<QColor>());
  146. if (v.canConvert<QColor>())
  147. {
  148. return v.value<QColor>().alphaF();
  149. }
  150. return v.toReal();
  151. }
  152. //-----------------------------------------------------------------------------
  153. QColor ctkTransferFunctionItem::color(const QVariant& v) const
  154. {
  155. Q_ASSERT(v.canConvert<QColor>());
  156. if (v.canConvert<QColor>())
  157. {
  158. return v.value<QColor>();
  159. }
  160. return QColor();
  161. }
  162. //-----------------------------------------------------------------------------
  163. ctkTransferFunctionGradientItem::ctkTransferFunctionGradientItem(QGraphicsItem* parentGraphicsItem)
  164. :ctkTransferFunctionItem(parentGraphicsItem)
  165. {
  166. }
  167. //-----------------------------------------------------------------------------
  168. ctkTransferFunctionGradientItem::ctkTransferFunctionGradientItem(
  169. ctkTransferFunction* transferFunction, QGraphicsItem* parentItem)
  170. :ctkTransferFunctionItem(transferFunction, parentItem)
  171. {
  172. }
  173. //-----------------------------------------------------------------------------
  174. ctkTransferFunctionGradientItem::~ctkTransferFunctionGradientItem()
  175. {
  176. }
  177. //-----------------------------------------------------------------------------
  178. void ctkTransferFunctionGradientItem::paint(
  179. QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
  180. {
  181. int count = this->transferFunction() ? this->transferFunction()->count() : 0;
  182. if (count <= 0)
  183. {
  184. painter->fillRect(this->rect(),Qt::black);
  185. return;
  186. }
  187. qreal range[2];
  188. this->transferFunction()->range(range);
  189. qreal rangeDiff = this->rect().width() / (range[1] - range[0]);
  190. ctkControlPoint* startCP = this->transferFunction()->controlPoint(0);
  191. ctkControlPoint* endCP = 0;
  192. qreal start = startCP->x() * rangeDiff;
  193. qreal end = 0;
  194. for(int i = 1; i < count; ++i)
  195. {
  196. endCP = this->transferFunction()->controlPoint(i);
  197. // TODO, handle Bezier points for a finer gradient
  198. // TODO, handle nonlinear points
  199. if (dynamic_cast<ctkNonLinearControlPoint*>(startCP) != 0)
  200. {
  201. QList<ctkPoint> points = this->nonLinearPoints(startCP, endCP);
  202. for (int j = 1; j < points.count(); ++j)
  203. {
  204. end = points[j].X * rangeDiff;
  205. QLinearGradient gradient(start, 0, end, 0);
  206. gradient.setColorAt(0, this->color(points[j-1]));
  207. gradient.setColorAt(1, this->color(points[j]));
  208. QRectF itemRect = QRectF(start, 0, end - start,
  209. this->rect().height());
  210. if (i==1 && j == 1)
  211. {
  212. itemRect.setLeft(0.);
  213. }
  214. if ((i == count -1) && (j == points.count() -1))
  215. {
  216. itemRect.setRight(this->rect().width());
  217. }
  218. painter->fillRect(itemRect, gradient);
  219. start = end;
  220. }
  221. }
  222. else
  223. {
  224. end = endCP->x() * rangeDiff;
  225. QLinearGradient gradient(start, 0, end, 0);
  226. gradient.setColorAt(0, this->color(startCP->value()));
  227. gradient.setColorAt(1, this->color(endCP->value()));
  228. QRectF itemRect = QRectF(start, 0, end - start,
  229. this->rect().height());
  230. if (i==1)
  231. {
  232. itemRect.setLeft(0.);
  233. }
  234. if (i == count -1)
  235. {
  236. itemRect.setRight(this->rect().width());
  237. }
  238. painter->fillRect(itemRect, gradient);
  239. }
  240. delete startCP;
  241. startCP = endCP;
  242. start = end;
  243. }
  244. if (startCP)
  245. {
  246. delete startCP;
  247. }
  248. }
  249. //-----------------------------------------------------------------------------
  250. class ctkTransferFunctionControlPointsItemPrivate:
  251. public ctkPrivate<ctkTransferFunctionControlPointsItem>
  252. {
  253. public:
  254. ctkTransferFunctionControlPointsItemPrivate();
  255. void init();
  256. QList<QPointF> ControlPoints;
  257. QSizeF PointSize;
  258. int SelectedPoint;
  259. };
  260. //-----------------------------------------------------------------------------
  261. ctkTransferFunctionControlPointsItemPrivate::ctkTransferFunctionControlPointsItemPrivate()
  262. {
  263. this->PointSize = QSizeF(10.,10.);
  264. this->SelectedPoint = -1;
  265. }
  266. //-----------------------------------------------------------------------------
  267. void ctkTransferFunctionControlPointsItemPrivate::init()
  268. {
  269. CTK_P(ctkTransferFunctionControlPointsItem);
  270. p->setAcceptedMouseButtons(Qt::LeftButton);
  271. }
  272. //-----------------------------------------------------------------------------
  273. ctkTransferFunctionControlPointsItem::ctkTransferFunctionControlPointsItem(QGraphicsItem* parentGraphicsItem)
  274. :ctkTransferFunctionItem(parentGraphicsItem)
  275. {
  276. CTK_INIT_PRIVATE(ctkTransferFunctionControlPointsItem);
  277. ctk_d()->init();
  278. }
  279. //-----------------------------------------------------------------------------
  280. ctkTransferFunctionControlPointsItem::ctkTransferFunctionControlPointsItem(
  281. ctkTransferFunction* transferFunction, QGraphicsItem* parentItem)
  282. :ctkTransferFunctionItem(transferFunction, parentItem)
  283. {
  284. CTK_INIT_PRIVATE(ctkTransferFunctionControlPointsItem);
  285. ctk_d()->init();
  286. }
  287. //-----------------------------------------------------------------------------
  288. ctkTransferFunctionControlPointsItem::~ctkTransferFunctionControlPointsItem()
  289. {
  290. }
  291. //-----------------------------------------------------------------------------
  292. void ctkTransferFunctionControlPointsItem::paint(
  293. QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
  294. {
  295. CTK_D(ctkTransferFunctionControlPointsItem);
  296. int count = this->transferFunction() ? this->transferFunction()->count() : 0;
  297. if (count <= 0)
  298. {
  299. return;
  300. }
  301. qreal rangeX[2];
  302. this->transferFunction()->range(rangeX);
  303. qreal rangeXDiff = this->rect().width() / (rangeX[1] - rangeX[0]);
  304. QVariant rangeY[2];
  305. rangeY[0] = this->transferFunction()->minValue();
  306. rangeY[1] = this->transferFunction()->maxValue();
  307. qreal rangeYDiff = this->rect().height();
  308. if (rangeY[0].canConvert<qreal>())
  309. {
  310. rangeYDiff /= rangeY[1].toReal() - rangeY[0].toReal();
  311. }
  312. else if (rangeY[0].canConvert<qreal>())
  313. {
  314. rangeYDiff /= rangeY[1].value<QColor>().alphaF() - rangeY[0].value<QColor>().alphaF();
  315. }
  316. else
  317. {
  318. Q_ASSERT(rangeY[0].canConvert<qreal>() ||
  319. rangeY[0].canConvert<QColor>());
  320. }
  321. ctkControlPoint* startCP = this->transferFunction()->controlPoint(0);
  322. ctkControlPoint* endCP = 0;
  323. qreal start = 0;
  324. qreal end = 0;
  325. QPainterPath path;
  326. QPointF startPos(startCP->x(), this->y(startCP->value()));
  327. startPos.rx() *= rangeXDiff;
  328. startPos.setY(this->rect().height()
  329. - startPos.y() * rangeYDiff);
  330. d->ControlPoints.clear();
  331. d->ControlPoints << startPos;
  332. path.moveTo(startPos);
  333. for(int i = 1; i < count; ++i)
  334. {
  335. endCP = this->transferFunction()->controlPoint(i);
  336. if (dynamic_cast<ctkNonLinearControlPoint*>(startCP))
  337. {
  338. QList<ctkPoint> points = this->nonLinearPoints(startCP, endCP);
  339. int j;
  340. for (j = 1; j < points.count(); ++j)
  341. {
  342. path.lineTo(
  343. QPointF(points[j].X * rangeXDiff, this->rect().height() -
  344. this->y(points[j].Value) * rangeYDiff));
  345. }
  346. j = points.count() -1;
  347. d->ControlPoints << QPointF(points[j].X * rangeXDiff, this->rect().height() -
  348. this->y(points[j].Value) * rangeYDiff);
  349. }
  350. else //dynamic_cast<ctkBezierControlPoint*>(startCP))
  351. {
  352. QList<ctkPoint> points = this->bezierParams(startCP, endCP);
  353. QList<ctkPoint>::iterator it = points.begin();
  354. QList<QPointF> bezierPoints;
  355. foreach(const ctkPoint& p, points)
  356. {
  357. bezierPoints <<
  358. QPointF(p.X * rangeXDiff,
  359. this->rect().height() - this->y(p.Value) * rangeYDiff);
  360. }
  361. path.cubicTo(bezierPoints[1], bezierPoints[2], bezierPoints[3]);
  362. d->ControlPoints << bezierPoints[3];
  363. }
  364. //qDebug() << i << points[0] << points[1] << points[2] << points[3];
  365. delete startCP;
  366. startCP = endCP;
  367. }
  368. if (startCP)
  369. {
  370. delete startCP;
  371. }
  372. painter->setRenderHint(QPainter::Antialiasing);
  373. painter->setPen(QPen(QColor(255, 255, 255, 191), 1));
  374. painter->drawPath(path);
  375. QPainterPath points;
  376. foreach(const QPointF& point, d->ControlPoints)
  377. {
  378. points.addEllipse(point, d->PointSize.width(), d->PointSize.height());
  379. }
  380. painter->setBrush(QBrush(QColor(191, 191, 191, 127)));
  381. painter->drawPath(points);
  382. }
  383. //-----------------------------------------------------------------------------
  384. void ctkTransferFunctionControlPointsItem::mousePressEvent(QGraphicsSceneMouseEvent* e)
  385. {
  386. CTK_D(ctkTransferFunctionControlPointsItem);
  387. QRectF pointArea(QPointF(0,0), d->PointSize*2.);
  388. d->SelectedPoint = -1;
  389. for(int i = 0; i < d->ControlPoints.count(); ++i)
  390. {
  391. pointArea.moveCenter(d->ControlPoints[i]);
  392. if (pointArea.contains(e->pos()))
  393. {
  394. d->SelectedPoint = i;
  395. break;
  396. }
  397. }
  398. if (d->SelectedPoint < 0)
  399. {
  400. e->ignore();
  401. }
  402. }
  403. //-----------------------------------------------------------------------------
  404. void ctkTransferFunctionControlPointsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
  405. {
  406. CTK_D(ctkTransferFunctionControlPointsItem);
  407. if (d->SelectedPoint < 0)
  408. {
  409. e->ignore();
  410. return;
  411. }
  412. qreal range[2];
  413. this->transferFunction()->range(range);
  414. qreal newPos = range[0] + e->pos().x() / (this->rect().width() / (range[1] - range[0]));
  415. newPos = qBound(range[0], newPos, range[1]);
  416. this->transferFunction()->setControlPointPos(d->SelectedPoint, newPos);
  417. //this->transferFunction()->setControlPointValue(d->SelectedPoint, e->y());
  418. }
  419. //-----------------------------------------------------------------------------
  420. void ctkTransferFunctionControlPointsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* e)
  421. {
  422. CTK_D(ctkTransferFunctionControlPointsItem);
  423. if (d->SelectedPoint < 0)
  424. {
  425. e->ignore();
  426. return;
  427. }
  428. d->SelectedPoint = -1;
  429. }