| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 | /*=========================================================================  Library:   CTK   Copyright (c) 2010  Kitware Inc.  Licensed under the Apache License, Version 2.0 (the "License");  you may not use this file except in compliance with the License.  You may obtain a copy of the License at      http://www.commontk.org/LICENSE  Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and  limitations under the License. =========================================================================*//// Qt includes#include <QColor>#include <QDebug>#include <QLinearGradient>#include <QGraphicsSceneMouseEvent>#include <QPainter>#include <QtGlobal>#include <QVariant>/// CTK includes#include "ctkTransferFunction.h"#include "ctkTransferFunctionItem.h"//-----------------------------------------------------------------------------class ctkTransferFunctionItemPrivate :   public ctkPrivate<ctkTransferFunctionItem>{public:  ctkTransferFunctionItemPrivate();  QRectF               Rect;  ctkTransferFunction* TransferFunction;};//-----------------------------------------------------------------------------ctkTransferFunctionItemPrivate::ctkTransferFunctionItemPrivate(){  this->TransferFunction = 0;}//-----------------------------------------------------------------------------ctkTransferFunctionItem::ctkTransferFunctionItem(QGraphicsItem* parentGraphicsItem)  :QGraphicsObject(parentGraphicsItem){  CTK_INIT_PRIVATE(ctkTransferFunctionItem);}//-----------------------------------------------------------------------------ctkTransferFunctionItem::ctkTransferFunctionItem(  ctkTransferFunction* transferFunction, QGraphicsItem* parentItem)  :QGraphicsObject(parentItem){  CTK_INIT_PRIVATE(ctkTransferFunctionItem);  this->setTransferFunction(transferFunction);}//-----------------------------------------------------------------------------ctkTransferFunctionItem::~ctkTransferFunctionItem(){  }//-----------------------------------------------------------------------------void ctkTransferFunctionItem::setTransferFunction(ctkTransferFunction* transferFunction){  CTK_D(ctkTransferFunctionItem);  if (d->TransferFunction == transferFunction)    {    return;    }  d->TransferFunction = transferFunction;  connect(d->TransferFunction, SIGNAL(changed()),          this, SLOT(onTransferFunctionChanged()));  this->update();}//-----------------------------------------------------------------------------ctkTransferFunction* ctkTransferFunctionItem::transferFunction() const{  CTK_D(const ctkTransferFunctionItem);  return d->TransferFunction;}//-----------------------------------------------------------------------------void ctkTransferFunctionItem::onTransferFunctionChanged(){  this->update();}//-----------------------------------------------------------------------------void ctkTransferFunctionItem::setRect(const QRectF& newRect){  CTK_D(ctkTransferFunctionItem);  if (d->Rect == newRect)    {    return;    }  d->Rect = newRect;  this->update();}//-----------------------------------------------------------------------------QRectF ctkTransferFunctionItem::rect() const{  CTK_D(const ctkTransferFunctionItem);  return d->Rect;}//-----------------------------------------------------------------------------QRectF ctkTransferFunctionItem::boundingRect()const{  CTK_D(const ctkTransferFunctionItem);  return d->Rect;}//-----------------------------------------------------------------------------QList<ctkPoint> ctkTransferFunctionItem::bezierParams(  ctkControlPoint* start, ctkControlPoint* end) const{  Q_ASSERT(start);  Q_ASSERT(end);  QList<ctkPoint> points;     ctkBezierControlPoint* bezierCP = dynamic_cast<ctkBezierControlPoint*>(start);  if (!bezierCP)    {// just duplicate start and end into p1 and p2    points << start->P;    points << start->P;    points << end->P;    points << end->P;    return points;    }    points << start->P;  points << bezierCP->P1;  points << bezierCP->P2;  points << end->P;  return points;}//-----------------------------------------------------------------------------QList<ctkPoint> ctkTransferFunctionItem::nonLinearPoints(  ctkControlPoint* start, ctkControlPoint* end) const{  Q_ASSERT(start);      ctkNonLinearControlPoint* nonLinearCP =     dynamic_cast<ctkNonLinearControlPoint*>(start);  if (!nonLinearCP)    {    QList<ctkPoint> points;     points << start->P;    points << end->P;    return points;    }  return nonLinearCP->SubPoints;}//-----------------------------------------------------------------------------qreal ctkTransferFunctionItem::y(const QVariant& v) const{   Q_ASSERT(v.canConvert<qreal>() || v.canConvert<QColor>());  if (v.canConvert<QColor>())    {    return v.value<QColor>().alphaF();    }  return v.toReal();}//-----------------------------------------------------------------------------QColor ctkTransferFunctionItem::color(const QVariant& v) const{   //Q_ASSERT(v.canConvert<QColor>());  if (v.canConvert<QColor>())    {    return v.value<QColor>();    }  else    {    //black background    QColor defaultColor(0., 0., 0.);    return defaultColor;    }  return QColor();}
 |