Bläddra i källkod

ENH: in ctkTransferFunctionControlPointsItem, several behaviors implemented for control points dragging

Nicolas Rannou 15 år sedan
förälder
incheckning
af3ef35da8

+ 104 - 6
Libs/Widgets/ctkTransferFunctionControlPointsItem.cpp

@@ -147,6 +147,8 @@ void ctkTransferFunctionControlPointsItem::mousePressEvent(QGraphicsSceneMouseEv
   // add point to transfer function
   // returns index
   int index = this->transferFunction()->insertControlPoint( tfPos.x());
+
+  //NOT WORKING IN COMPOSITE
   // update value of the point
   if (!QSharedPointer<ctkControlPoint>(this->transferFunction()->controlPoint(index))->value().canConvert<QColor>())
     {
@@ -157,7 +159,6 @@ void ctkTransferFunctionControlPointsItem::mousePressEvent(QGraphicsSceneMouseEv
 //-----------------------------------------------------------------------------
 void ctkTransferFunctionControlPointsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
 {
-  qDebug() << "mouse caught";
   CTK_D(ctkTransferFunctionControlPointsItem);
   if (d->SelectedPoint < 0)
     {
@@ -172,22 +173,119 @@ void ctkTransferFunctionControlPointsItem::mouseMoveEvent(QGraphicsSceneMouseEve
   // Deal with borders
   if(d->SelectedPoint == 0 || d->SelectedPoint == this->transferFunction()->count() )
     {
+    // BEHAVIOR TO BE IMPLEMENTED
+    // int borderBehavior = tfScene->borderBehavior();
+    // LockBorder
+    // Create new point on border at same height
+    // Create new point on border on top
+    // Create new point on border on bottom
     qDebug() << "border" ;
     return;
     }
-  else if( this->transferFunction()->controlPoint(d->SelectedPoint - 1)->x() > newPos.x() ||
-      this->transferFunction()->controlPoint(d->SelectedPoint + 1)->x() < newPos.x())
+  else
+    {
+    // TO BE IMPLEMENTED
+    //int movePointsBehavior = tfScene->movePointsBehavior();
+    // initialize to BLOCK_MOVE for now
+    int movePointsBehavior = STOP_MOVE;
+
+    switch( movePointsBehavior ){
+      case STOP_MOVE:
+        stopPoints(newPos);
+        break;
+      case SWITCH_MOVE:
+        switchPoints(newPos);
+        break;
+      case DRAW_MOVE:
+        drawPoints(newPos);
+        break;
+      case FUSION_MOVE:
+        fusionPoints(newPos);
+        break;
+      default:
+        break;
+      }
+    }
+}
+
+//-----------------------------------------------------------------------------
+void ctkTransferFunctionControlPointsItem::stopPoints( QPointF iPointF )
+{
+  CTK_D(ctkTransferFunctionControlPointsItem);
+
+  if( this->transferFunction()->controlPoint(d->SelectedPoint - 1)->x() > iPointF.x() ||
+      this->transferFunction()->controlPoint(d->SelectedPoint + 1)->x() < iPointF.x())
     {
     return;
     }
-  this->transferFunction()->setControlPointPos(d->SelectedPoint, newPos.x());
-  if (!QSharedPointer<ctkControlPoint>(this->transferFunction()->controlPoint(d->SelectedPoint))->value().canConvert<QColor>())
+
+  updatePointPosition(iPointF);
+}
+
+//-----------------------------------------------------------------------------
+void ctkTransferFunctionControlPointsItem::switchPoints( QPointF iPointF )
+{
+  CTK_D(ctkTransferFunctionControlPointsItem);
+
+  // Increment or decrement selected point?
+  // Don't need to check borders since it is done just before calling this method...
+  if( this->transferFunction()->controlPoint(d->SelectedPoint - 1)->x() > iPointF.x() )
+    {
+    // NOT WORKING IF COMPOSITE TRANSFER FUNCTION
+    double value = this->transferFunction()->value(d->SelectedPoint-1 ).toDouble();
+    d->SelectedPoint -= 1;
+    this->transferFunction()->setControlPointValue(d->SelectedPoint+1, value);
+    }
+  else if ( this->transferFunction()->controlPoint(d->SelectedPoint + 1)->x() < iPointF.x() )
+    {
+    double value = this->transferFunction()->value(d->SelectedPoint + 1 ).toDouble();
+    d->SelectedPoint += 1;
+    this->transferFunction()->setControlPointValue(d->SelectedPoint-1, value);
+    }
+
+  updatePointPosition(iPointF);
+}
+
+//-----------------------------------------------------------------------------
+void ctkTransferFunctionControlPointsItem::drawPoints( QPointF iPointF )
+{
+  CTK_D(ctkTransferFunctionControlPointsItem);
+
+  // Increment or decrement selected point
+  if( this->transferFunction()->controlPoint(d->SelectedPoint - 1)->x() > iPointF.x() )
     {
-    this->transferFunction()->setControlPointValue(d->SelectedPoint, newPos.y());
+    //change the selected point
+    d->SelectedPoint = d->SelectedPoint -1;
     }
+  else if ( this->transferFunction()->controlPoint(d->SelectedPoint + 1)->x() < iPointF.x() )
+    {
+    d->SelectedPoint = d->SelectedPoint +1;
+    }
+
+  updatePointPosition(iPointF);
 }
 
 //-----------------------------------------------------------------------------
+void ctkTransferFunctionControlPointsItem::fusionPoints( QPointF iPointF )
+{
+  // TO BE IMPLEMENTED
+  // if 2 points are to close: delete one..?
+}
+
+//-----------------------------------------------------------------------------
+void ctkTransferFunctionControlPointsItem::updatePointPosition( QPointF iPoint )
+{
+  CTK_D(ctkTransferFunctionControlPointsItem);
+
+  this->transferFunction()->setControlPointPos(d->SelectedPoint, iPoint.x());
+
+  // TEST NOT WORKING IN COMPOSITE TRANSFER FUNCTION
+  if (!QSharedPointer<ctkControlPoint>(this->transferFunction()->controlPoint(d->SelectedPoint))->value().canConvert<QColor>())
+  {
+  this->transferFunction()->setControlPointValue(d->SelectedPoint, iPoint.y());
+  }
+}
+//-----------------------------------------------------------------------------
 void ctkTransferFunctionControlPointsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* e)
 {
   CTK_D(ctkTransferFunctionControlPointsItem);

+ 31 - 0
Libs/Widgets/ctkTransferFunctionControlPointsItem.h

@@ -36,6 +36,14 @@ class CTK_WIDGETS_EXPORT ctkTransferFunctionControlPointsItem: public ctkTransfe
 {
   Q_OBJECT
 public:
+
+  enum MOVE_TYPE{
+    STOP_MOVE = 0,
+    SWITCH_MOVE,
+    DRAW_MOVE,
+    FUSION_MOVE
+  };
+
   explicit ctkTransferFunctionControlPointsItem(QGraphicsItem* parent = 0);
   ctkTransferFunctionControlPointsItem(ctkTransferFunction* transferFunction, 
                                        QGraphicsItem* parent = 0);
@@ -49,6 +57,29 @@ protected:
   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* e);
 private:
   CTK_DECLARE_PRIVATE(ctkTransferFunctionControlPointsItem);
+
+  /*
+   * \brief Update the position of a point when a move mouse event occurs
+   */
+  void updatePointPosition( QPointF iPoint );
+  /*
+   * \brief Stop "moving point" if its position reaches another point
+   */
+  void stopPoints( QPointF iPointF );
+  /*
+   * \brief Switch "moving point" and "reached point" when "moving point" reaches a point.
+   * Position of "reached point" - IS NOT - lost.
+   */
+  void switchPoints( QPointF iPointF );
+  /*
+   * \brief Switch "moving point" and "reached point" when "moving point" reaches a point.
+   * Position of "reached point" - IS - lost.
+   */
+  void drawPoints( QPointF iPointF );
+  /*
+   * \brief The moving (or reached point?) is deleted
+   */
+  void fusionPoints( QPointF iPointF );
 };
 
 #endif