ctkTransferFunctionControlPointsItem.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 "ctkTransferFunctionControlPointsItem.h"
00032 #include "ctkTransferFunctionScene.h"
00033 #include "ctkTransferFunctionWidget.h"
00034 
00035 //-----------------------------------------------------------------------------
00036 class ctkTransferFunctionControlPointsItemPrivate: 
00037   public ctkPrivate<ctkTransferFunctionControlPointsItem>
00038 {
00039 public:
00040   ctkTransferFunctionControlPointsItemPrivate();
00041   void init();
00042   QList<QPointF> ControlPoints;
00043   QSize          PointSize;
00044   int            SelectedPoint;
00045 };
00046 
00047 //-----------------------------------------------------------------------------
00048 ctkTransferFunctionControlPointsItemPrivate::ctkTransferFunctionControlPointsItemPrivate()
00049 {
00050   this->PointSize = QSize(12,12);
00051   this->SelectedPoint = -1;
00052 }
00053 
00054 //-----------------------------------------------------------------------------
00055 void ctkTransferFunctionControlPointsItemPrivate::init()
00056 {
00057   CTK_P(ctkTransferFunctionControlPointsItem);
00058   p->setAcceptedMouseButtons(Qt::LeftButton);
00059 }
00060 
00061 //-----------------------------------------------------------------------------
00062 ctkTransferFunctionControlPointsItem::ctkTransferFunctionControlPointsItem(QGraphicsItem* parentGraphicsItem)
00063   :ctkTransferFunctionItem(parentGraphicsItem)
00064 {
00065   CTK_INIT_PRIVATE(ctkTransferFunctionControlPointsItem);
00066   ctk_d()->init();
00067 }
00068 
00069 //-----------------------------------------------------------------------------
00070 ctkTransferFunctionControlPointsItem::ctkTransferFunctionControlPointsItem(
00071   ctkTransferFunction* transferFunction, QGraphicsItem* parentItem)
00072   :ctkTransferFunctionItem(transferFunction, parentItem)
00073 {
00074   CTK_INIT_PRIVATE(ctkTransferFunctionControlPointsItem);
00075   ctk_d()->init();
00076 }
00077 
00078 //-----------------------------------------------------------------------------
00079 ctkTransferFunctionControlPointsItem::~ctkTransferFunctionControlPointsItem()
00080 {  
00081 }
00082 
00083 //-----------------------------------------------------------------------------
00084 void ctkTransferFunctionControlPointsItem::paint(
00085   QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
00086 {
00087   CTK_D(ctkTransferFunctionControlPointsItem);
00088   int count = this->transferFunction() ? this->transferFunction()->count() : 0;
00089   if (count <= 0)
00090     {
00091     return;
00092     }
00093 
00094   ctkTransferFunctionScene* tfScene = dynamic_cast<ctkTransferFunctionScene*>(this->scene());
00095   Q_ASSERT(tfScene);
00096   
00097   const QPainterPath& curve = tfScene->curve();
00098   QPen pen(QColor(255, 255, 255, 191), 1);
00099   pen.setCosmetic(true);
00100   painter->setPen(pen);
00101   painter->drawPath(curve);
00102 
00103   d->ControlPoints = tfScene->points();
00104   painter->setBrush(QBrush(QColor(191, 191, 191, 127)));
00105   painter->save();
00106   QTransform transform = painter->transform();
00107   painter->setTransform(QTransform());
00108   foreach(const QPointF& point, d->ControlPoints)
00109     {
00110     QPointF pos = transform.map(point);
00111     painter->drawEllipse(pos.x() - d->PointSize.width() / 2, 
00112                          pos.y() - d->PointSize.height() / 2, 
00113                          d->PointSize.width(), d->PointSize.width());
00114     //points.addEllipse(point, d->PointSize.width(), d->PointSize.height());
00115     }
00116   painter->restore();
00117 }
00118 
00119 //-----------------------------------------------------------------------------
00120 void ctkTransferFunctionControlPointsItem::mousePressEvent(QGraphicsSceneMouseEvent* e)
00121 {
00122   CTK_D(ctkTransferFunctionControlPointsItem);
00123   QWidget* w = e->widget();
00124   ctkTransferFunctionWidget* view = qobject_cast<ctkTransferFunctionWidget*>(e->widget()->parentWidget());
00125   Q_ASSERT(view);
00126   // e->pos() is ok, pointArea should be in the world coordiate
00127   QRect pointViewArea(QPoint(-d->PointSize.width() / 2, -d->PointSize.height() / 2), d->PointSize);
00128   QPolygonF pointItemArea = this->mapFromScene(view->mapToScene(pointViewArea));
00129   d->SelectedPoint = -1;
00130   for(int i = 0; i < d->ControlPoints.count(); ++i)
00131     {
00132     if (pointItemArea.translated(d->ControlPoints[i]).containsPoint(e->pos(), Qt::OddEvenFill))
00133       {
00134       d->SelectedPoint = i;
00135       break;
00136       }
00137     }
00138   if (d->SelectedPoint >= 0)
00139     {
00140     return;
00141     }
00142   ctkTransferFunctionScene* tfScene = dynamic_cast<ctkTransferFunctionScene*>(this->scene());
00143   Q_ASSERT(tfScene);
00144   
00145   // convert coordinates
00146   QPointF tfPos = tfScene->mapPointFromScene(e->pos());
00147   // add point to transfer function
00148   // returns index
00149   int index = this->transferFunction()->insertControlPoint( tfPos.x());
00150   // update value of the point
00151   this->transferFunction()->setControlPointValue( index, tfPos.y());
00152 }
00153 
00154 //-----------------------------------------------------------------------------
00155 void ctkTransferFunctionControlPointsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
00156 {
00157   qDebug() << "mouse caught";
00158   CTK_D(ctkTransferFunctionControlPointsItem);
00159   if (d->SelectedPoint < 0)
00160     {
00161     e->ignore();
00162     return;
00163     }
00164 
00165   ctkTransferFunctionScene* tfScene = dynamic_cast<ctkTransferFunctionScene*>(this->scene());
00166   Q_ASSERT(tfScene);
00167   QPointF newPos = tfScene->mapPointFromScene(e->pos());
00168 
00169   // Deal with borders
00170   if(d->SelectedPoint == 0 || d->SelectedPoint == this->transferFunction()->count() )
00171     {
00172     qDebug() << "border" ;
00173     }
00174   else if( this->transferFunction()->controlPoint(d->SelectedPoint - 1)->x() < newPos.x() &&
00175       this->transferFunction()->controlPoint(d->SelectedPoint + 1)->x() > newPos.x())
00176     {
00177     // update pos of the point
00178     this->transferFunction()->setControlPointPos( d->SelectedPoint, newPos.x());
00179     // update value of the point
00180     this->transferFunction()->setControlPointValue( d->SelectedPoint, newPos.y());
00181     }
00182 }
00183 
00184 //-----------------------------------------------------------------------------
00185 void ctkTransferFunctionControlPointsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* e)
00186 {
00187   CTK_D(ctkTransferFunctionControlPointsItem);
00188   if (d->SelectedPoint < 0)
00189     {
00190     e->ignore();
00191     return;
00192     }
00193   d->SelectedPoint = -1;
00194 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1