ctkTransferFunctionItem.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00022 #include <QColor>
00023 #include <QDebug>
00024 #include <QLinearGradient>
00025 #include <QGraphicsSceneMouseEvent>
00026 #include <QPainter>
00027 #include <QtGlobal>
00028 #include <QVariant>
00029 
00031 #include "ctkTransferFunction.h"
00032 #include "ctkTransferFunctionItem.h"
00033 
00034 //-----------------------------------------------------------------------------
00035 class ctkTransferFunctionItemPrivate : 
00036   public ctkPrivate<ctkTransferFunctionItem>
00037 {
00038 public:
00039   ctkTransferFunctionItemPrivate();
00040   void init();
00041   QRectF               Rect;
00042   ctkTransferFunction* TransferFunction;
00043 };
00044 
00045 //-----------------------------------------------------------------------------
00046 ctkTransferFunctionItemPrivate::ctkTransferFunctionItemPrivate()
00047 {
00048   this->TransferFunction = 0;
00049   this->Rect = QRectF(0.,0.,1.,1.);
00050 }
00051 
00052 //-----------------------------------------------------------------------------
00053 void ctkTransferFunctionItemPrivate::init()
00054 {
00055   CTK_P(ctkTransferFunctionItem);
00056 }
00057 
00058 //-----------------------------------------------------------------------------
00059 ctkTransferFunctionItem::ctkTransferFunctionItem(QGraphicsItem* parentGraphicsItem)
00060   :QGraphicsObject(parentGraphicsItem)
00061 {
00062   CTK_INIT_PRIVATE(ctkTransferFunctionItem);
00063   ctk_d()->init();
00064 }
00065 
00066 //-----------------------------------------------------------------------------
00067 ctkTransferFunctionItem::ctkTransferFunctionItem(
00068   ctkTransferFunction* transferFunction, QGraphicsItem* parentItem)
00069   :QGraphicsObject(parentItem)
00070 {
00071   CTK_INIT_PRIVATE(ctkTransferFunctionItem);
00072   ctk_d()->init();
00073   this->setTransferFunction(transferFunction);
00074 }
00075 
00076 //-----------------------------------------------------------------------------
00077 ctkTransferFunctionItem::~ctkTransferFunctionItem()
00078 {
00079   
00080 }
00081 
00082 //-----------------------------------------------------------------------------
00083 void ctkTransferFunctionItem::setTransferFunction(ctkTransferFunction* transferFunction)
00084 {
00085   CTK_D(ctkTransferFunctionItem);
00086   d->TransferFunction = transferFunction;
00087 }
00088 
00089 //-----------------------------------------------------------------------------
00090 ctkTransferFunction* ctkTransferFunctionItem::transferFunction() const
00091 {
00092   CTK_D(const ctkTransferFunctionItem);
00093   return d->TransferFunction;
00094 }
00095 
00096 //-----------------------------------------------------------------------------
00097 void ctkTransferFunctionItem::setRect(const QRectF& newRect)
00098 {
00099   CTK_D(ctkTransferFunctionItem);
00100   if (d->Rect == newRect)
00101     {
00102     return;
00103     }
00104   d->Rect = newRect;
00105   this->update();
00106 }
00107 
00108 //-----------------------------------------------------------------------------
00109 QRectF ctkTransferFunctionItem::rect() const
00110 {
00111   CTK_D(const ctkTransferFunctionItem);
00112   return d->Rect;
00113 }
00114 
00115 //-----------------------------------------------------------------------------
00116 QRectF ctkTransferFunctionItem::boundingRect()const
00117 {
00118   CTK_D(const ctkTransferFunctionItem);
00119   return d->Rect;
00120 }
00121 
00122 //-----------------------------------------------------------------------------
00123 QList<ctkPoint> ctkTransferFunctionItem::bezierParams(
00124   ctkControlPoint* start, ctkControlPoint* end) const
00125 {
00126   Q_ASSERT(start);
00127   Q_ASSERT(end);
00128   QList<ctkPoint> points; 
00129   
00130   ctkBezierControlPoint* bezierCP = dynamic_cast<ctkBezierControlPoint*>(start);
00131   if (!bezierCP)
00132     {// just duplicate start and end into p1 and p2
00133     points << start->P;
00134     points << start->P;
00135     points << end->P;
00136     points << end->P;
00137     return points;
00138     }
00139   
00140   points << start->P;
00141   points << bezierCP->P1;
00142   points << bezierCP->P2;
00143   points << end->P;
00144   return points;
00145 }
00146 
00147 //-----------------------------------------------------------------------------
00148 QList<ctkPoint> ctkTransferFunctionItem::nonLinearPoints(
00149   ctkControlPoint* start, ctkControlPoint* end) const
00150 {
00151   Q_ASSERT(start);
00152     
00153   ctkNonLinearControlPoint* nonLinearCP = 
00154     dynamic_cast<ctkNonLinearControlPoint*>(start);
00155   if (!nonLinearCP)
00156     {
00157     QList<ctkPoint> points; 
00158     points << start->P;
00159     points << end->P;
00160     return points;
00161     }
00162   return nonLinearCP->SubPoints;
00163 }
00164 
00165 //-----------------------------------------------------------------------------
00166 qreal ctkTransferFunctionItem::y(const QVariant& v) const
00167 { 
00168   Q_ASSERT(v.canConvert<qreal>() || v.canConvert<QColor>());
00169   if (v.canConvert<QColor>())
00170     {
00171     return v.value<QColor>().alphaF();
00172     }
00173   return v.toReal();
00174 }
00175 
00176 //-----------------------------------------------------------------------------
00177 QColor ctkTransferFunctionItem::color(const QVariant& v) const
00178 { 
00179   //Q_ASSERT(v.canConvert<QColor>());
00180   if (v.canConvert<QColor>())
00181     {
00182     return v.value<QColor>();
00183     }
00184   else
00185     {
00186     //black background
00187     QColor defaultColor(0., 0., 0.);
00188     return defaultColor;
00189     }
00190   return QColor();
00191 }
00192 //-----------------------------------------------------------------------------
00193 qreal ctkTransferFunctionItem::rangeXDiff()
00194 {
00195   // pointer to private class
00196   CTK_D(ctkTransferFunctionItem);
00197 
00198   qreal rangeX[2];
00199   d->TransferFunction->range(rangeX);
00200   return this->rect().width() / (rangeX[1] - rangeX[0]);
00201 }
00202 
00203 //-----------------------------------------------------------------------------
00204 qreal ctkTransferFunctionItem::rangeXOffSet()
00205 {
00206   // pointer to private class
00207   CTK_D(ctkTransferFunctionItem);
00208 
00209   qreal rangeX[2];
00210   d->TransferFunction->range(rangeX);
00211   return rangeX[0];
00212 }
00213 
00214 //-----------------------------------------------------------------------------
00215 qreal ctkTransferFunctionItem::rangeYDiff()
00216 {
00217   // pointer to private class
00218   CTK_D(ctkTransferFunctionItem);
00219 
00220   QVariant rangeY[2];
00221   rangeY[0] = d->TransferFunction->minValue();
00222   rangeY[1] = d->TransferFunction->maxValue();
00223   qreal rangeYDiff = this->rect().height();
00224   if (rangeY[0].canConvert<qreal>())
00225     {
00226     if (rangeY[1].toReal() == rangeY[0].toReal())
00227       {
00228       rangeYDiff /= rangeY[0].toReal();
00229       return rangeYDiff;
00230       }
00231     else
00232       {
00233       rangeYDiff /= rangeY[1].toReal() - rangeY[0].toReal();
00234       return rangeYDiff;
00235       }
00236     }
00237     else if (rangeY[0].canConvert<QColor>())
00238       {
00239       if ( rangeY[1].value<QColor>().alphaF() == rangeY[0].value<QColor>().alphaF())
00240         {
00241         rangeYDiff /= rangeY[0].value<QColor>().alphaF();
00242         return rangeYDiff;
00243         }
00244       else
00245         {
00246         rangeYDiff /= rangeY[1].value<QColor>().alphaF() - rangeY[0].value<QColor>().alphaF();
00247         return rangeYDiff;
00248         }
00249       }
00250     else
00251       {
00252       Q_ASSERT(rangeY[0].canConvert<qreal>() ||
00253       rangeY[0].canConvert<QColor>());
00254       }
00255   return rangeYDiff;
00256 }
00257 
00258 //-----------------------------------------------------------------------------
00259 qreal ctkTransferFunctionItem::rangeYOffSet()
00260 {
00261   // pointer to private class
00262   CTK_D(ctkTransferFunctionItem);
00263 
00264   QVariant rangeY[2];
00265   rangeY[0] = d->TransferFunction->minValue();
00266   rangeY[1] = d->TransferFunction->maxValue();
00267   qreal rangeYOffSet;
00268   if (rangeY[0].canConvert<qreal>())
00269     {
00270     if (rangeY[1].toReal() == rangeY[0].toReal())
00271       {
00272       return 0.;
00273       }
00274     else
00275       {
00276       return rangeY[0].toReal();
00277       }
00278     }
00279   else if (rangeY[0].canConvert<QColor>())
00280     {
00281     if ( rangeY[1].value<QColor>().alphaF() == rangeY[0].value<QColor>().alphaF())
00282       {
00283       return 0.;
00284       }
00285     else
00286       {
00287       return rangeY[0].value<QColor>().alphaF();
00288       }
00289     }
00290   else
00291     {
00292     Q_ASSERT(rangeY[0].canConvert<qreal>() ||
00293         rangeY[0].canConvert<QColor>());
00294     }
00295   return 0;
00296 }
00297 
00298 //-----------------------------------------------------------------------------
00299 QPointF ctkTransferFunctionItem::transferFunction2ScreenCoordinates( qreal x, qreal y)
00300 {
00301   QPointF screenCoordinates((x - this->rangeXOffSet() ) * this->rangeXDiff(),
00302       this->rect().height() - (y - this->rangeYOffSet() ) * this->rangeYDiff() );
00303   return screenCoordinates;
00304 }
00305 
00306 //-----------------------------------------------------------------------------
00307 QPointF ctkTransferFunctionItem::screen2TransferFunctionCoordinates( qreal x, qreal y)
00308 {
00309   QPointF transferFunctionCoordinates((x / this->rangeXDiff() ) + this->rangeXOffSet(),
00310       ( - y + this->rect().height() )/this->rangeYDiff() + this->rangeYOffSet());
00311 
00312   return transferFunctionCoordinates;
00313 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1