Просмотр исходного кода

ENH: Add support for log mode and bar color in ctkTransferFunctionBarsItem

Julien Finet лет назад: 15
Родитель
Сommit
ea4116d9bb
2 измененных файлов с 58 добавлено и 13 удалено
  1. 45 10
      Libs/Widgets/ctkTransferFunctionBarsItem.cpp
  2. 13 3
      Libs/Widgets/ctkTransferFunctionBarsItem.h

+ 45 - 10
Libs/Widgets/ctkTransferFunctionBarsItem.cpp

@@ -1,7 +1,7 @@
 /*=========================================================================
 /*=========================================================================
 
 
   Library:   CTK
   Library:   CTK
- 
+
   Copyright (c) 2010  Kitware Inc.
   Copyright (c) 2010  Kitware Inc.
 
 
   Licensed under the Apache License, Version 2.0 (the "License");
   Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,7 +15,7 @@
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   See the License for the specific language governing permissions and
   limitations under the License.
   limitations under the License.
- 
+
 =========================================================================*/
 =========================================================================*/
 
 
 /// Qt includes
 /// Qt includes
@@ -39,14 +39,17 @@ class ctkTransferFunctionBarsItemPrivate: public ctkPrivate<ctkTransferFunctionB
 {
 {
 public:
 public:
   ctkTransferFunctionBarsItemPrivate();
   ctkTransferFunctionBarsItemPrivate();
-  qreal BarWidth;
-  bool  Log;
+  qreal  BarWidth;
+  QColor BarColor;
+  bool   LogMode;
 };
 };
 
 
+//-----------------------------------------------------------------------------
 ctkTransferFunctionBarsItemPrivate::ctkTransferFunctionBarsItemPrivate()
 ctkTransferFunctionBarsItemPrivate::ctkTransferFunctionBarsItemPrivate()
 {
 {
   this->BarWidth = 0.6180; // golden ratio... why not.
   this->BarWidth = 0.6180; // golden ratio... why not.
-  this->Log = true;
+  this->BarColor = QColor(191, 191, 191, 127);
+  this->LogMode = ctkTransferFunctionBarsItem::AutoLog;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -66,13 +69,14 @@ ctkTransferFunctionBarsItem::ctkTransferFunctionBarsItem(
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 ctkTransferFunctionBarsItem::~ctkTransferFunctionBarsItem()
 ctkTransferFunctionBarsItem::~ctkTransferFunctionBarsItem()
-{  
+{
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void ctkTransferFunctionBarsItem::setBarWidth(qreal newBarWidth)
 void ctkTransferFunctionBarsItem::setBarWidth(qreal newBarWidth)
 {
 {
   CTK_D(ctkTransferFunctionBarsItem);
   CTK_D(ctkTransferFunctionBarsItem);
+  newBarWidth = qBound(0., newBarWidth, 1.);
   if (d->BarWidth == newBarWidth)
   if (d->BarWidth == newBarWidth)
     {
     {
     return;
     return;
@@ -89,6 +93,20 @@ qreal ctkTransferFunctionBarsItem::barWidth()const
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
+void ctkTransferFunctionBarsItem::setBarColor(const QColor& color)
+{
+  CTK_D(ctkTransferFunctionBarsItem);
+  d->BarColor = color;
+}
+
+//-----------------------------------------------------------------------------
+QColor ctkTransferFunctionBarsItem::barColor()const
+{
+  CTK_D(const ctkTransferFunctionBarsItem);
+  return d->BarColor;
+}
+
+//-----------------------------------------------------------------------------
 void ctkTransferFunctionBarsItem::paint(
 void ctkTransferFunctionBarsItem::paint(
   QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
   QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
 {
 {
@@ -106,16 +124,33 @@ void ctkTransferFunctionBarsItem::paint(
   const QList<QPointF>& points = tfScene->points();
   const QList<QPointF>& points = tfScene->points();
 
 
   QPainterPath bars;
   QPainterPath bars;
-  QPen pen(QColor(255, 255, 255, 191), 1);
+  QPen pen( QColor(255, 255, 255, 191), 1);
   pen.setCosmetic(true);
   pen.setCosmetic(true);
+  if (qFuzzyCompare(d->BarWidth, 1.))
+    {
+    pen = QPen(QBrush(), 0, Qt::NoPen);
+    }
   painter->setPen(pen);
   painter->setPen(pen);
-  painter->setBrush(QBrush(QColor(191, 191, 191, 127)));
+  painter->setBrush(QBrush(d->BarColor));
 
 
-  qreal barWidth = d->BarWidth * (this->rect().width() / (points.size() - 1)); 
+  qreal barWidth = d->BarWidth * (this->rect().width() / (points.size() - 1));
+  bool useLog = false;
+  switch (d->LogMode)
+    {
+    case ctkTransferFunctionBarsItem::AutoLog:
+      useLog = this->transferFunction()->maxValue().toReal() - this->transferFunction()->minValue().toReal() > 1000.;
+      break;
+    case ctkTransferFunctionBarsItem::UseLog:
+      useLog = true;
+      break;
+    default:
+    case ctkTransferFunctionBarsItem::NoLog:
+      useLog = false;
+    }
   foreach(const QPointF& point, points)
   foreach(const QPointF& point, points)
     {
     {
     qreal barHeight = point.y();
     qreal barHeight = point.y();
-    if (d->Log && barHeight != 1.)
+    if (useLog && barHeight != 1.)
       {
       {
       barHeight = this->rect().height() - log( tfScene->mapYFromScene(barHeight) )/log(this->transferFunction()->maxValue().toReal());// 1. - (-log(barHeight)/100.);
       barHeight = this->rect().height() - log( tfScene->mapYFromScene(barHeight) )/log(this->transferFunction()->maxValue().toReal());// 1. - (-log(barHeight)/100.);
       }
       }

+ 13 - 3
Libs/Widgets/ctkTransferFunctionBarsItem.h

@@ -37,15 +37,25 @@ class CTK_WIDGETS_EXPORT ctkTransferFunctionBarsItem: public ctkTransferFunction
 {
 {
   Q_OBJECT
   Q_OBJECT
   Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth)
   Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth)
+  Q_PROPERTY(QColor barColor READ barColor WRITE setBarColor)
 public:
 public:
   ctkTransferFunctionBarsItem(QGraphicsItem* parent = 0);
   ctkTransferFunctionBarsItem(QGraphicsItem* parent = 0);
-  ctkTransferFunctionBarsItem(ctkTransferFunction* transferFunc, 
+  ctkTransferFunctionBarsItem(ctkTransferFunction* transferFunc,
                               QGraphicsItem* parent = 0);
                               QGraphicsItem* parent = 0);
   virtual ~ctkTransferFunctionBarsItem();
   virtual ~ctkTransferFunctionBarsItem();
-  
-  void setBarWidth(qreal newBarWidth);  
+
+  void setBarWidth(qreal newBarWidth);
   qreal barWidth()const;
   qreal barWidth()const;
 
 
+  void setBarColor(const QColor& newBarColor);
+  QColor barColor()const;
+  
+  enum LogMode
+  {
+    NoLog = 0,
+    UseLog = 1,
+    AutoLog =2
+  };
   virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
   virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
 private:
 private:
   CTK_DECLARE_PRIVATE(ctkTransferFunctionBarsItem);
   CTK_DECLARE_PRIVATE(ctkTransferFunctionBarsItem);