OpenWalnut  1.5.0dev
WTransferFunctionPoint.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <iostream>
26 
27 #include "QPainter"
28 #include "QStyleOption"
29 #include "QGraphicsSceneMouseEvent"
30 
31 #include "WTransferFunctionWidget.h"
32 
33 #include "WTransferFunctionPoint.h"
34 
36  : BaseClass(), radius( 6.0 ), left( 0x0 ), right( 0x0 ), line( 0x0 ), _parent( parent )
37 {
38  this->setFlag( ItemIsMovable );
39  this->setFlag( ItemSendsGeometryChanges );
40  //this->setFlag( ItemIsSelectable ); //< we should be able to use this framework, but we do not make use of it right now
41 
42  setZValue( 4 );
43 }
44 
46 {
47 }
48 
50 {
51  this->line = line;
52 }
53 
55 {
56  return line;
57 }
58 
59 
61 {
62  left = point;
63 }
64 
66 {
67  return left;
68 }
69 
71 {
72  right = point;
73 }
74 
76 {
77  return right;
78 }
79 
81 {
82  const double padding( 2 );
83  const double diameter( radius * 2 );
84  return QRectF( -radius - padding, -radius - padding, diameter + padding, diameter + padding );
85 }
86 
87 void WTransferFunctionPoint::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget* )
88 {
89  QRadialGradient gradient( 0.0, 0.0, radius );
90 
91  if( option->state & QStyle::State_Sunken )
92  {
93  gradient.setColorAt( 0, QColor( Qt::yellow ).lighter( 120 ) );
94  gradient.setColorAt( 1, QColor( Qt::darkYellow ).lighter( 120 ) );
95  painter->setPen( QPen( Qt::red, 0 ) );
96  }
97  else
98  {
99  gradient.setColorAt( 0, Qt::yellow );
100  gradient.setColorAt( 1, Qt::darkYellow );
101  painter->setPen( QPen( Qt::black, 0 ) );
102  }
103 
104  painter->setBrush( gradient );
105 
106  const double diameter( radius*2 );
107  painter->drawEllipse( -radius, -radius, diameter, diameter );
108 }
109 
110 QVariant WTransferFunctionPoint::itemChange( GraphicsItemChange change, const QVariant &value )
111 {
112  if( !scene() )
113  {
114  return value; //< we are still initializing things, no changes at this point
115  }
116 
117  if( _parent )
118  {
119  _parent->dataChanged();
120  }
121 
122  if( !left && !right )
123  {
124  return BaseClass::itemChange( change, value );
125  }
126  if( ( change == ItemPositionHasChanged || change == ItemPositionChange ) && scene() )
127  {
128  QPointF newPos = value.toPointF();
129 
130  QRectF boundingBox( this->scene()->sceneRect() );
131 
132  if( !left )
133  {
134  newPos = ( QPointF( 0, newPos.y() ) );
135  }
136 
137  if( !right )
138  {
139  newPos = ( QPointF( boundingBox.x() + boundingBox.width(), newPos.y() ) );
140  }
141 
142  this->clampToRectangle( &newPos, boundingBox );
143  this->clampToLeftAndRight( &newPos );
144 
145  // if ( _parent )
146  // _parent->dataChanged();
147 
148  this->setToolTip( QString( "isovalue=" ) + QString::number( newPos.x() ) + " alpha=" + QString::number( newPos.y() ) );
149  return newPos;
150  }
151  return BaseClass::itemChange( change, value );
152 }
153 
154 void WTransferFunctionPoint::mousePressEvent( QGraphicsSceneMouseEvent *event )
155 {
156  //this->update();
157  BaseClass::mousePressEvent( event );
158  if( _parent )
159  _parent->setCurrent( this );
160 }
161 
162 void WTransferFunctionPoint::clampToLeftAndRight( QPointF* const pos ) const
163 {
164  Q_ASSERT( pos );
165  if( left )
166  {
167  if( pos->x() <= left->pos().x() )
168  {
169  ( *pos ) = QPointF( left->pos().x() + 1, pos->y() );
170  }
171  }
172  if( right )
173  {
174  if( pos->x() >= right->pos().x() )
175  {
176  ( *pos ) = QPointF( right->pos().x() - 1, pos->y() );
177  }
178  }
179 }
180 
181 void WTransferFunctionPoint::clampToRectangle( QPointF* const pos, const QRectF& rectangle ) const
182 {
183  Q_ASSERT( pos );
184  qreal x( pos->x() );
185  qreal y( pos->y() );
186 
187  const qreal xMin( rectangle.x() );
188  const qreal xMax( xMin + rectangle.width() );
189  const qreal yMin( rectangle.y() );
190  const qreal yMax( yMin + rectangle.height() );
191 
192  x = qMax( x, xMin );
193  x = qMin( x, xMax );
194 
195  y = qMax( y, yMin );
196  y = qMin( y, yMax );
197 
198  ( *pos ) = QPointF( x, y );
199 }
200 
201 void WTransferFunctionPoint::setPos( QPointF point )
202 {
203  BaseClass::setPos( point );
204 }
205 
Line object for the connection of alpha control points.
A control point for the alpha function.
QRectF boundingRect() const
Get the bounding rectangle.
WTransferFunctionPoint * getLeft() const
Get point to the left.
WTransferFunctionPoint * left
pointer to point to the left
virtual ~WTransferFunctionPoint()
Default destructor.
void mousePressEvent(QGraphicsSceneMouseEvent *event)
Handle mouse press events for selections and highlighting.
WTransferFunctionLine * getLine() const
The current line if there is one.
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
Handle item changes and change requests.
WTransferFunctionLine * line
pointer to line to the right
WTransferFunctionPoint * getRight() const
Get point to the right.
WTransferFunctionPoint * right
pointer to point to the right
virtual void setPos(QPointF point)
Overloaded form base class for debugging.
void clampToLeftAndRight(QPointF *const pos) const
Helper to itemChange.
void setLeft(WTransferFunctionPoint *left)
Set point to the left.
void setRight(WTransferFunctionPoint *right)
Set point to the right.
void setLine(WTransferFunctionLine *line)
Set the line pointing to the right.
double radius
the radius of the object
WTransferFunctionPoint(WTransferFunctionWidget *parent=NULL)
Default constructor.
WTransferFunctionWidget * _parent
reference to the parent widget
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
Paint the object.
void clampToRectangle(QPointF *const pos, const QRectF &rectangle) const
Helper to itemChange.
A widget that holds a scene to display and modify the transfer function.
void setCurrent(WTransferFunctionPoint *current)
Set the current active point => this should be changed to QGraphicsScene object selection.
void dataChanged()
Notification that the data changed, i.e., a control point has been moved or a color changed.