1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
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.
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.
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/>.
23
//---------------------------------------------------------------------------
27
#include "QtCore/QMimeData"
28
#include "QtGui/QColorDialog"
29
#include "QtGui/QGraphicsSceneMouseEvent"
30
#include "QtGui/QPainter"
31
#include "QtGui/QStyleOption"
33
#include "WTransferFunctionWidget.h"
35
#include "WTransferFunctionColorPoint.h"
37
#include "WTransferFunctionColorPoint.moc"
39
WTransferFunctionColorPoint::WTransferFunctionColorPoint( WTransferFunctionWidget *parent )
41
radius( 8.0 ), left( 0x0 ), right( 0x0 ),
43
color( QColor( 0, 0, 1, 255 ) )
45
this->setFlag( ItemIsMovable );
46
// position is always on y axis
47
this->setPos( this->pos().x(), 0 );
49
//this->setFlag( QGraphicsItem::ItemIsSelectable ); //< do we want selections?
50
this->setFlag( QGraphicsItem::ItemIsMovable );
51
this->setFlag( QGraphicsItem::ItemSendsGeometryChanges );
52
this->setAcceptDrops( true );
54
//this->setCursor( Qt::SplitHCursor );
59
WTransferFunctionColorPoint::~WTransferFunctionColorPoint()
63
void WTransferFunctionColorPoint::setLeft( WTransferFunctionColorPoint *point )
68
WTransferFunctionColorPoint *WTransferFunctionColorPoint::getLeft() const
73
void WTransferFunctionColorPoint::setRight( WTransferFunctionColorPoint *point )
78
WTransferFunctionColorPoint* WTransferFunctionColorPoint::getRight() const
83
QRectF WTransferFunctionColorPoint::boundingRect() const
85
const double padding( 2 );
86
const double diameter( radius * 2 );
87
return QRectF( -radius - padding, -radius - padding, diameter + padding, radius + padding );
90
void WTransferFunctionColorPoint::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget* )
92
QRadialGradient gradient( 0.0, 0.0, radius );
94
if( option->state & QStyle::State_Selected )
96
gradient.setColorAt( 0, Qt::yellow );
97
gradient.setColorAt( 1, Qt::red );
98
painter->setPen( QPen( Qt::black, 2 ) );
100
else if( option->state & QStyle::State_Sunken )
102
gradient.setColorAt( 0, color ); //QColor( Qt::yellow ).light( 120 ) );
103
gradient.setColorAt( 1, color ); //QColor( Qt::darkYellow ).light( 120 ) );
104
painter->setPen( QPen( Qt::black, 2 ) );
108
gradient.setColorAt( 0, color ); //Qt::yellow );
109
gradient.setColorAt( 1, color ); //Qt::darkYellow );
110
painter->setPen( QPen( Qt::black, 0 ) );
113
QColor color = painter->pen().color();
114
color.setAlpha( 0.3 );
115
painter->setPen( QPen( color, painter->pen().width() ) );
116
painter->setBrush( gradient );
120
triangle << QPoint( -radius, -radius );
121
triangle << QPoint( 0, 0 );
122
triangle << QPoint( radius, -radius );
124
painter->drawPolygon( triangle );
128
if( option->state & QStyle::State_Sunken )
130
painter->setPen( QPen( Qt::red, 1, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin ) );
134
painter->setPen( QPen( Qt::black, 1, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin ) );
136
// FIXME: fixed size in code
137
painter->drawLine( QPoint( 0, 0 ), QPoint( 0, scene()->sceneRect().height() ) );
141
QVariant WTransferFunctionColorPoint::itemChange( GraphicsItemChange change, const QVariant &value )
143
// if ( change == ItemPositionChange )
145
// QPointF newPos = value.toPointF();
147
// QRectF boundingBox( this->scene()->sceneRect() );
148
// if ( boundingBox.left() > newPos.x() || boundingBox.right() < newPos.x() )
150
// newPos.setX( qMin( boundingBox.right(), qMax( newPos.x(), boundingBox.left() ) ) );
151
// if ( newPos.y() != 0 )
155
// if ( newPos.y() != 0 )
163
// // This is the wrong place for such an update :-(
166
// _parent->forceRedraw();
169
if( change == ItemPositionChange )
171
QPointF newPos = value.toPointF();
172
this->setToolTip( QString( "isovalue=" ) + QString::number( newPos.x() ) + " color=("
173
+ QString::number( color.red() ) +','
174
+ QString::number( color.green() ) +','
175
+ QString::number( color.blue() ) + ')' );
177
_parent->dataChanged();
180
return BaseClass::itemChange( change, value );
183
void WTransferFunctionColorPoint::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
185
BaseClass::mouseDoubleClickEvent( event );
190
void WTransferFunctionColorPoint::colorSelected( const QColor &newcolor )
193
color.setAlpha( 255 ); //< force opacity
197
_parent->dataChanged();
201
void WTransferFunctionColorPoint::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
206
BaseClass::mouseMoveEvent( event );
210
QRectF boundingBox( this->scene()->sceneRect() );
214
this->setPos( 0, this->pos().y() );
219
this->setPos( boundingBox.x() + boundingBox.width(), this->pos().y() );
223
this->clampToRectangle( boundingBox );
224
this->clampToLeftAndRight();
226
// color markers are always at zero
227
this->setPos( this->pos().x(), 0 );
231
void WTransferFunctionColorPoint::mousePressEvent( QGraphicsSceneMouseEvent *event )
234
BaseClass::mousePressEvent( event );
235
_parent->setCurrentColor( this );
238
void WTransferFunctionColorPoint::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
241
BaseClass::mouseReleaseEvent( event );
244
void WTransferFunctionColorPoint::clampToLeftAndRight()
248
if( this->pos().x() <= left->pos().x() )
250
this->setPos( left->pos().x() + 1, this->pos().y() );
255
if( this->pos().x() >= right->pos().x() )
257
this->setPos( right->pos().x() - 1, this->pos().y() );
262
void WTransferFunctionColorPoint::clampToRectangle( const QRectF& rectangle )
264
qreal x( this->pos().x() );
265
qreal y( this->pos().y() );
267
const qreal xMin( rectangle.x() );
268
const qreal xMax( xMin + rectangle.width() );
269
const qreal yMin( rectangle.y() );
270
const qreal yMax( yMin + rectangle.height() );
278
this->setPos( x, y );
281
QColor WTransferFunctionColorPoint::getColor() const
286
void WTransferFunctionColorPoint::dragEnterEvent( QGraphicsSceneDragDropEvent* event )
288
if( event->mimeData()->hasColor() )
290
event->setAccepted( true );
294
void WTransferFunctionColorPoint::dropEvent( QGraphicsSceneDragDropEvent* event )
296
if( event->mimeData()->hasColor() )
298
color = qvariant_cast<QColor>( event->mimeData()->colorData() );
299
color.setAlpha( 255 ); //< force opacity
302
_parent->dataChanged();
306
void WTransferFunctionColorPoint::contextMenuEvent( QGraphicsSceneContextMenuEvent* event )
308
// I get right clicks as context menu events on OSX which may be intended,
309
// but I get both at the same time, so I create a new object and get its
310
// context menu as well, which is confusing. Therefore, this is disabled
312
// Additionally, I do not know how to remove the item from its own menu
313
#ifdef ALLOW_CONTEXT_MENU
316
QAction *removeAction = menu.addAction( "Remove" );
317
QAction *changeColor = menu.addAction( "Change Color" );
318
QAction *selectedAction = menu.exec( event->screenPos() );
320
if( selectedAction == removeAction )
323
if( selectedAction == changeColor )
328
BaseClass::contextMenuEvent( event );
331
void WTransferFunctionColorPoint::showColorPicker()
333
QColorDialog *dialog = new QColorDialog( );
334
dialog->setCurrentColor( color );
335
dialog->setOption( QColorDialog::NoButtons );
336
connect( dialog, SIGNAL( currentColorChanged( const QColor& ) ), this, SLOT( colorSelected( const QColor& ) ) );
337
//dialog->open( this, SLOT( colorSelected( const QColor& ) ) );