ctkTransferFunctionWidget.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <QDebug>
  16. //#include <QGLWidget>
  17. #include <QGraphicsScene>
  18. #include <QResizeEvent>
  19. /// CTK includes
  20. #include "ctkTransferFunction.h"
  21. #include "ctkTransferFunctionWidget.h"
  22. #include "ctkTransferFunctionScene.h"
  23. #include "ctkTransferFunctionGradientItem.h"
  24. #include "ctkTransferFunctionControlPointsItem.h"
  25. //-----------------------------------------------------------------------------
  26. class ctkTransferFunctionWidgetPrivate: public ctkPrivate<ctkTransferFunctionWidget>
  27. {
  28. CTK_DECLARE_PUBLIC(ctkTransferFunctionWidget);
  29. public:
  30. ctkTransferFunctionWidgetPrivate();
  31. void init();
  32. ctkTransferFunction* TransferFunction;
  33. };
  34. //-----------------------------------------------------------------------------
  35. ctkTransferFunctionWidgetPrivate::ctkTransferFunctionWidgetPrivate()
  36. {
  37. this->TransferFunction = 0;
  38. }
  39. //-----------------------------------------------------------------------------
  40. void ctkTransferFunctionWidgetPrivate::init()
  41. {
  42. CTK_P(ctkTransferFunctionWidget);
  43. p->setScene(new ctkTransferFunctionScene(p));
  44. p->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  45. p->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  46. //p->setViewport(new QGLWidget);
  47. }
  48. //-----------------------------------------------------------------------------
  49. ctkTransferFunctionWidget::ctkTransferFunctionWidget(QWidget* parentWidget)
  50. :QGraphicsView(parentWidget)
  51. {
  52. CTK_INIT_PRIVATE(ctkTransferFunctionWidget);
  53. ctk_d()->init();
  54. }
  55. //-----------------------------------------------------------------------------
  56. ctkTransferFunctionWidget::ctkTransferFunctionWidget(
  57. ctkTransferFunction* transferFunction, QWidget* parentWidget)
  58. :QGraphicsView(parentWidget)
  59. {
  60. CTK_INIT_PRIVATE(ctkTransferFunctionWidget);
  61. ctk_d()->init();
  62. this->setTransferFunction(transferFunction);
  63. }
  64. //-----------------------------------------------------------------------------
  65. ctkTransferFunctionWidget::~ctkTransferFunctionWidget()
  66. {
  67. }
  68. //-----------------------------------------------------------------------------
  69. void ctkTransferFunctionWidget::setTransferFunction(ctkTransferFunction* transferFunction)
  70. {
  71. CTK_D(ctkTransferFunctionWidget);
  72. d->TransferFunction = transferFunction;
  73. ctkTransferFunctionScene* tfScene = dynamic_cast<ctkTransferFunctionScene*>(this->scene());
  74. Q_ASSERT(tfScene);
  75. tfScene->clear();
  76. tfScene->setTransferFunction(transferFunction);
  77. ctkTransferFunctionGradientItem* gradient =
  78. new ctkTransferFunctionGradientItem(transferFunction);
  79. //gradient->setRect(tfScene->sceneRect());
  80. this->scene()->addItem(gradient);
  81. ctkTransferFunctionControlPointsItem* controlPoints =
  82. new ctkTransferFunctionControlPointsItem(transferFunction);
  83. //controlPoints->setRect(tfScene->sceneRect());
  84. this->scene()->addItem(controlPoints);
  85. }
  86. //-----------------------------------------------------------------------------
  87. ctkTransferFunction* ctkTransferFunctionWidget::transferFunction()const
  88. {
  89. return ctk_d()->TransferFunction;
  90. }
  91. //-----------------------------------------------------------------------------
  92. void ctkTransferFunctionWidget::resizeEvent(QResizeEvent * event)
  93. {
  94. /*
  95. QRectF sceneRect(QPointF(0,0),event->size());
  96. this->scene()->setSceneRect(sceneRect);
  97. foreach(QGraphicsItem * item, this->scene()->items())
  98. {
  99. ctkTransferFunctionItem* rectItem =
  100. qgraphicsitem_cast<ctkTransferFunctionItem*>(item);
  101. if (rectItem)
  102. {
  103. rectItem->setRect(sceneRect);
  104. }
  105. }
  106. */
  107. QMatrix zoomMatrix;
  108. zoomMatrix.scale(event->size().width(), event->size().height());
  109. bool blocked = this->blockSignals(true);
  110. this->setMatrix(zoomMatrix);
  111. this->blockSignals(blocked);
  112. this->QGraphicsView::resizeEvent(event);
  113. // Control points are resized by the view transform, we want
  114. // fixed size control points, lines...
  115. //this->fitInView(this->scene()->sceneRect());
  116. qDebug() << "resize event caught";
  117. }
  118. /*
  119. //-----------------------------------------------------------------------------
  120. void ctkTransferFunctionWidget::dragEnterEvent ( QDragEnterEvent * event )
  121. {
  122. qDebug() << "drag event caught";
  123. this->QGraphicsView::dragEnterEvent(event);
  124. }
  125. //-----------------------------------------------------------------------------
  126. void ctkTransferFunctionWidget::mousePressEvent ( QMouseEvent * event )
  127. {
  128. qDebug() << "press event caught";
  129. //One control point is added to the scene
  130. // 1 - get position of the mouse
  131. qDebug() << "x position " << event->x();
  132. qDebug() << "y position " << event->y();
  133. this->scene()->items()[1]->;
  134. // 2nd item are the control points
  135. this->QGraphicsView::mousePressEvent(event);
  136. }
  137. //-----------------------------------------------------------------------------
  138. void ctkTransferFunctionWidget::mouseReleaseEvent ( QMouseEvent * event )
  139. {
  140. qDebug() << "release event caught";
  141. this->QGraphicsView::mouseReleaseEvent(event);
  142. }
  143. */