Selaa lähdekoodia

ENH: ctkTransferFunctionGradientItem now supports discrete ctkTransferFunctions
BUG: ctkTransferFunctionGradientItem now supports ctkTransferFunctions not starting from 0

Julien Finet 15 vuotta sitten
vanhempi
commit
e33add93e3
1 muutettua tiedostoa jossa 42 lisäystä ja 10 poistoa
  1. 42 10
      Libs/Widgets/ctkTransferFunctionGradientItem.cpp

+ 42 - 10
Libs/Widgets/ctkTransferFunctionGradientItem.cpp

@@ -62,25 +62,49 @@ void ctkTransferFunctionGradientItem::paint(
   qreal range[2];
   this->transferFunction()->range(range);
   qreal rangeDiff = this->rect().width() / (range[1] - range[0]);
+  qreal rangeOffset = range[0];
   ctkControlPoint* startCP = this->transferFunction()->controlPoint(0);
   ctkControlPoint* endCP = 0;
   
-  qreal start = startCP->x() * rangeDiff;
+  qreal start = (startCP->x() - rangeOffset) * rangeDiff;
   qreal end = 0;
   for(int i = 1; i < count; ++i)
     {
     endCP = this->transferFunction()->controlPoint(i);
     // TODO, handle Bezier points for a finer gradient
     // TODO, handle nonlinear points
-    if (dynamic_cast<ctkNonLinearControlPoint*>(startCP) != 0)
+    if (this->transferFunction()->isDiscrete())
+      {
+      // pos
+      end = ((startCP->x() + endCP->x()) /2. -rangeOffset) * rangeDiff;
+      QRectF itemRect = QRectF(start, 0, end - start, 
+                               this->rect().height());
+      if (i==1)
+        {
+        itemRect.setLeft(0.);
+        }
+      QColor valueColor = this->color(startCP->value());
+      // paint
+      painter->fillRect(itemRect, valueColor);
+      // draw the last item
+      if (i == count -1)
+        {
+        //pos
+        itemRect = QRectF(end, 0, this->rect().width(), 
+                          this->rect().height());
+        // color
+        valueColor = this->color(endCP->value());
+        // paint
+        painter->fillRect(itemRect, valueColor);
+        }
+      }
+    else if (dynamic_cast<ctkNonLinearControlPoint*>(startCP) != 0)
       {
       QList<ctkPoint> points = this->nonLinearPoints(startCP, endCP);
       for (int j = 1; j < points.count(); ++j)
         {
-        end = points[j].X * rangeDiff;
-        QLinearGradient gradient(start, 0, end, 0);
-        gradient.setColorAt(0, this->color(points[j-1]));
-        gradient.setColorAt(1, this->color(points[j]));
+        // pos
+        end = (points[j].X - rangeOffset) * rangeDiff;
         QRectF itemRect = QRectF(start, 0, end - start, 
                                  this->rect().height());
         if (i==1 && j == 1)
@@ -91,16 +115,19 @@ void ctkTransferFunctionGradientItem::paint(
           {
           itemRect.setRight(this->rect().width());
           }
+        // color
+        QLinearGradient gradient(start, 0, end, 0);
+        gradient.setColorAt(0, this->color(points[j-1]));
+        gradient.setColorAt(1, this->color(points[j]));
+        // paint
         painter->fillRect(itemRect, gradient);
         start = end;
         }
       }
     else
       {
-      end = endCP->x() * rangeDiff;
-      QLinearGradient gradient(start, 0, end, 0);
-      gradient.setColorAt(0, this->color(startCP->value()));
-      gradient.setColorAt(1, this->color(endCP->value()));
+      // pos
+      end = (endCP->x() - rangeOffset) * rangeDiff;
       QRectF itemRect = QRectF(start, 0, end - start, 
                                this->rect().height());
       if (i==1)
@@ -111,6 +138,11 @@ void ctkTransferFunctionGradientItem::paint(
         {
         itemRect.setRight(this->rect().width());
         }
+      // color
+      QLinearGradient gradient(start, 0, end, 0);
+      gradient.setColorAt(0, this->color(startCP->value()));
+      gradient.setColorAt(1, this->color(endCP->value()));
+      // paint
       painter->fillRect(itemRect, gradient);
       }
     delete startCP;