~ubuntu-branches/ubuntu/raring/openwalnut/raring

« back to all changes in this revision

Viewing changes to src/qt4gui/controlPanel/transferFunction/WTransferFunctionColorPoint.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Eichelbaum
  • Date: 2012-12-12 11:26:32 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20121212112632-xhiuwkxuz5h0idkh
Tags: 1.3.1+hg5849-1
* Minor changes compared to 1.3.0 but included several bug fixes.
* See http://www.openwalnut.org/versions/4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "QtCore/QMimeData"
 
28
#include "QtGui/QColorDialog"
 
29
#include "QtGui/QGraphicsSceneMouseEvent"
 
30
#include "QtGui/QPainter"
 
31
#include "QtGui/QStyleOption"
 
32
 
 
33
#include "WTransferFunctionWidget.h"
 
34
 
 
35
#include "WTransferFunctionColorPoint.h"
 
36
 
 
37
#include "WTransferFunctionColorPoint.moc"
 
38
 
 
39
WTransferFunctionColorPoint::WTransferFunctionColorPoint( WTransferFunctionWidget *parent )
 
40
    : BaseClass(),
 
41
    radius( 8.0 ), left( 0x0 ), right( 0x0 ),
 
42
    _parent( parent ),
 
43
    color( QColor( 0, 0, 1, 255 ) )
 
44
{
 
45
    this->setFlag( ItemIsMovable );
 
46
    // position is always on y axis
 
47
    this->setPos( this->pos().x(), 0 );
 
48
 
 
49
    //this->setFlag( QGraphicsItem::ItemIsSelectable ); //< do we want selections?
 
50
    this->setFlag( QGraphicsItem::ItemIsMovable );
 
51
    this->setFlag( QGraphicsItem::ItemSendsGeometryChanges );
 
52
    this->setAcceptDrops( true );
 
53
 
 
54
    //this->setCursor( Qt::SplitHCursor );
 
55
 
 
56
    setZValue( 3 );
 
57
}
 
58
 
 
59
WTransferFunctionColorPoint::~WTransferFunctionColorPoint()
 
60
{
 
61
}
 
62
 
 
63
void WTransferFunctionColorPoint::setLeft( WTransferFunctionColorPoint *point )
 
64
{
 
65
    left = point;
 
66
}
 
67
 
 
68
WTransferFunctionColorPoint *WTransferFunctionColorPoint::getLeft() const
 
69
{
 
70
    return left;
 
71
}
 
72
 
 
73
void WTransferFunctionColorPoint::setRight( WTransferFunctionColorPoint *point )
 
74
{
 
75
    right = point;
 
76
}
 
77
 
 
78
WTransferFunctionColorPoint* WTransferFunctionColorPoint::getRight() const
 
79
{
 
80
    return right;
 
81
}
 
82
 
 
83
QRectF WTransferFunctionColorPoint::boundingRect() const
 
84
{
 
85
    const double padding( 2 );
 
86
    const double diameter( radius * 2 );
 
87
    return QRectF( -radius - padding, -radius - padding, diameter + padding, radius + padding );
 
88
}
 
89
 
 
90
void WTransferFunctionColorPoint::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget* )
 
91
{
 
92
    QRadialGradient gradient( 0.0, 0.0, radius );
 
93
 
 
94
    if( option->state & QStyle::State_Selected )
 
95
    {
 
96
        gradient.setColorAt( 0, Qt::yellow );
 
97
        gradient.setColorAt( 1, Qt::red );
 
98
        painter->setPen( QPen( Qt::black, 2 ) );
 
99
    }
 
100
    else if( option->state & QStyle::State_Sunken )
 
101
    {
 
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 ) );
 
105
    }
 
106
    else
 
107
    {
 
108
        gradient.setColorAt( 0, color ); //Qt::yellow );
 
109
        gradient.setColorAt( 1, color ); //Qt::darkYellow );
 
110
        painter->setPen( QPen( Qt::black, 0 ) );
 
111
    }
 
112
 
 
113
    QColor color = painter->pen().color();
 
114
    color.setAlpha( 0.3 );
 
115
    painter->setPen( QPen( color, painter->pen().width() ) );
 
116
    painter->setBrush( gradient );
 
117
 
 
118
    QPolygon triangle;
 
119
    triangle.clear();
 
120
    triangle << QPoint( -radius, -radius );
 
121
    triangle << QPoint( 0, 0 );
 
122
    triangle << QPoint( radius, -radius );
 
123
 
 
124
    painter->drawPolygon( triangle );
 
125
 
 
126
    if( left && right )
 
127
    {
 
128
        if( option->state & QStyle::State_Sunken )
 
129
        {
 
130
            painter->setPen(  QPen( Qt::red, 1, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin ) );
 
131
        }
 
132
        else
 
133
        {
 
134
            painter->setPen(  QPen( Qt::black, 1, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin ) );
 
135
        }
 
136
        // FIXME: fixed size in code
 
137
        painter->drawLine( QPoint( 0, 0 ), QPoint( 0, scene()->sceneRect().height() ) );
 
138
    }
 
139
}
 
140
 
 
141
QVariant WTransferFunctionColorPoint::itemChange( GraphicsItemChange change, const QVariant &value )
 
142
{
 
143
    // if ( change == ItemPositionChange )
 
144
    // {
 
145
    //  QPointF newPos = value.toPointF();
 
146
    //
 
147
    //  QRectF boundingBox( this->scene()->sceneRect() );
 
148
    //  if ( boundingBox.left() > newPos.x() || boundingBox.right() < newPos.x() )
 
149
    //  {
 
150
    //    newPos.setX( qMin( boundingBox.right(), qMax( newPos.x(), boundingBox.left() ) ) );
 
151
    //    if ( newPos.y() != 0 )
 
152
    //      newPos.setY( 0 );
 
153
    //    return newPos;
 
154
    //  }
 
155
    //  if ( newPos.y() != 0 )
 
156
    //  {
 
157
    //    newPos.setY( 0 );
 
158
    //
 
159
    //    return newPos;
 
160
    //  }
 
161
    // }
 
162
 
 
163
    // // This is the wrong place for such an update :-(
 
164
    // if ( _parent )
 
165
    // {
 
166
    //   _parent->forceRedraw();
 
167
    // }
 
168
 
 
169
    if( change == ItemPositionChange )
 
170
    {
 
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() ) + ')' );
 
176
        if( _parent )
 
177
            _parent->dataChanged();
 
178
    }
 
179
 
 
180
    return BaseClass::itemChange( change, value );
 
181
}
 
182
 
 
183
void WTransferFunctionColorPoint::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
 
184
{
 
185
    BaseClass::mouseDoubleClickEvent( event );
 
186
 
 
187
    showColorPicker();
 
188
}
 
189
 
 
190
void WTransferFunctionColorPoint::colorSelected( const QColor &newcolor )
 
191
{
 
192
    color = newcolor;
 
193
    color.setAlpha( 255 ); //< force opacity
 
194
    //this->update();
 
195
    if( _parent )
 
196
    {
 
197
        _parent->dataChanged();
 
198
    }
 
199
}
 
200
 
 
201
void WTransferFunctionColorPoint::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
 
202
{
 
203
    if( left && right )
 
204
    {
 
205
        //this->update();
 
206
        BaseClass::mouseMoveEvent( event );
 
207
 
 
208
 
 
209
#if 1
 
210
        QRectF boundingBox( this->scene()->sceneRect() );
 
211
 
 
212
        if( !left )
 
213
        {
 
214
            this->setPos( 0, this->pos().y() );
 
215
        }
 
216
 
 
217
        if( !right )
 
218
        {
 
219
            this->setPos( boundingBox.x() + boundingBox.width(), this->pos().y() );
 
220
        }
 
221
#endif
 
222
 
 
223
        this->clampToRectangle( boundingBox );
 
224
        this->clampToLeftAndRight();
 
225
 
 
226
        // color markers are always at zero
 
227
        this->setPos( this->pos().x(), 0 );
 
228
    }
 
229
}
 
230
 
 
231
void WTransferFunctionColorPoint::mousePressEvent( QGraphicsSceneMouseEvent *event )
 
232
{
 
233
    //this->update();
 
234
    BaseClass::mousePressEvent( event );
 
235
    _parent->setCurrentColor( this );
 
236
}
 
237
 
 
238
void WTransferFunctionColorPoint::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
 
239
{
 
240
    //this->update();
 
241
    BaseClass::mouseReleaseEvent( event );
 
242
}
 
243
 
 
244
void WTransferFunctionColorPoint::clampToLeftAndRight()
 
245
{
 
246
    if( left )
 
247
    {
 
248
        if( this->pos().x() <= left->pos().x() )
 
249
        {
 
250
            this->setPos( left->pos().x() + 1, this->pos().y() );
 
251
        }
 
252
    }
 
253
    if( right )
 
254
    {
 
255
        if( this->pos().x() >= right->pos().x() )
 
256
        {
 
257
            this->setPos( right->pos().x() - 1, this->pos().y() );
 
258
        }
 
259
    }
 
260
}
 
261
 
 
262
void WTransferFunctionColorPoint::clampToRectangle( const QRectF& rectangle )
 
263
{
 
264
    qreal x( this->pos().x() );
 
265
    qreal y( this->pos().y() );
 
266
 
 
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() );
 
271
 
 
272
    x = qMax( x, xMin );
 
273
    x = qMin( x, xMax );
 
274
 
 
275
    y = qMax( y, yMin );
 
276
    y = qMin( y, yMax );
 
277
 
 
278
    this->setPos( x, y );
 
279
}
 
280
 
 
281
QColor WTransferFunctionColorPoint::getColor() const
 
282
{
 
283
    return color;
 
284
}
 
285
 
 
286
void WTransferFunctionColorPoint::dragEnterEvent( QGraphicsSceneDragDropEvent* event )
 
287
{
 
288
    if( event->mimeData()->hasColor() )
 
289
    {
 
290
        event->setAccepted( true );
 
291
    }
 
292
}
 
293
 
 
294
void WTransferFunctionColorPoint::dropEvent( QGraphicsSceneDragDropEvent* event )
 
295
{
 
296
    if( event->mimeData()->hasColor() )
 
297
    {
 
298
        color = qvariant_cast<QColor>( event->mimeData()->colorData() );
 
299
        color.setAlpha( 255 ); //< force opacity
 
300
        //this->update();
 
301
        if( _parent )
 
302
            _parent->dataChanged();
 
303
    }
 
304
}
 
305
 
 
306
void WTransferFunctionColorPoint::contextMenuEvent( QGraphicsSceneContextMenuEvent* event )
 
307
{
 
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
 
311
    // right now.
 
312
    // Additionally, I do not know how to remove the item from its own menu
 
313
#ifdef ALLOW_CONTEXT_MENU
 
314
    event->accept();
 
315
    QMenu menu;
 
316
    QAction *removeAction = menu.addAction( "Remove" );
 
317
    QAction *changeColor  = menu.addAction( "Change Color" );
 
318
    QAction *selectedAction = menu.exec( event->screenPos() );
 
319
 
 
320
    if( selectedAction == removeAction )
 
321
    {
 
322
    }
 
323
    if( selectedAction == changeColor )
 
324
    {
 
325
        showColorPicker();
 
326
    }
 
327
#endif
 
328
    BaseClass::contextMenuEvent( event );
 
329
}
 
330
 
 
331
void WTransferFunctionColorPoint::showColorPicker()
 
332
{
 
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& ) ) );
 
338
    dialog->open();
 
339
}
 
340