File: shadewidget.cpp

package info (click to toggle)
fraqtive 0.4.3-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 988 kB
  • ctags: 1,249
  • sloc: cpp: 8,387; sh: 103; makefile: 45
file content (227 lines) | stat: -rw-r--r-- 7,008 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**************************************************************************
* This file is part of the Fraqtive program
* Copyright (C) 2004-2008 Michał Męciński
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
**************************************************************************/

#include "shadewidget.h"

#include <QPainter>
#include <QPaintEvent>
#include <QToolTip>

ShadeWidget::ShadeWidget( QWidget* parent ) : QWidget( parent ),
    m_dragging( false )
{
}

ShadeWidget::~ShadeWidget()
{
}

void ShadeWidget::setShadeColor( const QColor& color )
{
    m_color = color;
    m_stops.clear();
    update();
}

void ShadeWidget::setGradientStops( const QGradientStops& stops )
{
    m_color = QColor();
    m_stops = stops;
    update();
}

void ShadeWidget::setPoints( const QPolygonF& points )
{
    if ( m_points != points ) {
        m_points = points;
        emit pointsChanged();
        update();
    }
}

QSize ShadeWidget::minimumSizeHint() const
{
    return QSize( 30, 30 );
}

void ShadeWidget::paintEvent( QPaintEvent* e )
{
    QPainter painter( this );

    painter.setRenderHint( QPainter::Antialiasing );

    painter.setPen( QColor( 80, 80, 80 ) );
    painter.setBrush( QColor( 160, 160, 160 ) );

    QPainterPath border;
    border.moveTo( 5.5, 0.0 );
    border.arcTo( e->rect().width() - 11.5, 0.0, 11.0, 11.0, 90.0, -90.0 );
    border.arcTo( e->rect().width() - 11.5, e->rect().height() - 11.5, 11.0, 11.0, 0.0, -90.0 );
    border.arcTo( 0.0, e->rect().height() - 11.5, 11.0, 11.0, -90.0, -90.0 );
    border.arcTo( 0.0, 0.0, 11.0, 11.0, -180.0, -90.0 );

    painter.drawPath( border );

    QRectF rect = e->rect();
    rect.adjust( 5.5, 5.5, -5.5, -5.5 );

    painter.setPen( QColor( 128, 128, 128 ) );

    for ( int i = 1; i <= 9; i++ ) {
        double x = mapToWidget( QPointF( i / 10.0, 0.0 ) ).x();
        painter.drawLine( QPointF( x, 0.0 ), QPointF( x, e->rect().bottom() ) );
    }

    if ( m_color.isValid() ) {
        QLinearGradient gradient( rect.bottomLeft(), rect.topLeft() );
        gradient.setColorAt( 0.0, Qt::black );
        gradient.setColorAt( 1.0, m_color );
        painter.setBrush( gradient );
    } else if ( !m_stops.isEmpty() ) {
        QLinearGradient gradient( rect.topLeft(), rect.topRight() );
        gradient.setStops( m_stops );
        painter.setBrush( gradient );
    }

    painter.setPen( QColor( 80, 80, 80 ) );

    painter.drawRect( rect );

    if ( !m_points.isEmpty() ) {
        painter.setPen( QPen( QColor( 255, 255, 255, 128 ), 2.0 ) );
        painter.setBrush( Qt::NoBrush );

        QPainterPath path;
        path.moveTo( mapToWidget( m_points.at( 0 ) ) );

        for ( int i = 1; i < m_points.size(); i++ ) {
            QPointF p1 = mapToWidget( m_points.at( i - 1 ) );
            QPointF p2 = mapToWidget( m_points.at( i ) );
            path.cubicTo( ( p1.x() + p2.x() ) / 2, p1.y(), ( p1.x() + p2.x() ) / 2, p2.y(), p2.x(), p2.y() );
        }

        painter.drawPath( path );

        QPen pointPen( QColor( 255, 255, 255, 192 ) );
        pointPen.setWidth( 1 );

        painter.setPen( QPen( QColor( 255, 255, 255, 192 ), 1.0 ) );
        painter.setBrush( QColor( 255, 255, 255, 144 ) );

        for ( int i = 0; i < m_points.size(); i++ ) {
            QPointF point = mapToWidget( m_points.at( i ) );
            painter.drawEllipse( QRectF( point.x() - 4.5, point.y() - 4.5, 9.0, 9.0 ) );
        }
    }
}

void ShadeWidget::mousePressEvent( QMouseEvent* e )
{
    if ( e->button() != Qt::LeftButton || m_points.isEmpty() )
        return;

    int index = -1;
    double minDistance = 0.0;

    for ( int i = 0; i < m_points.count(); i++ ) {
        QPointF center = mapToWidget( m_points.at( i ) );
        double distance = QLineF( e->pos(), center ).length();
        if ( index < 0 || distance < minDistance ) {
            index = i;
            minDistance = distance;
        }
    }

    QPointF point = mapFromWidget( e->pos() );
    point.setX( qBound( (qreal)0, point.x(), (qreal)1 ) );
    point.setY( qBound( (qreal)0, point.y(), (qreal)1 ) ); 

    if ( index >= 0 && minDistance <= 10.0 ) {
        m_dragging = true;
        m_index = index;
        if ( index == 0 )
            point.setX( 0.0 );
        else if ( index == m_points.count() - 1 )
            point.setX( 1.0 );
        m_points[ index ] = point;
    } else if ( QRectF( 0.0, 0.0, 1.0, 1.0 ).contains( point ) ) {
        index = 1;
        while ( index < m_points.count() - 1 && m_points.at( index ).x() < point.x() )
            index++;
        m_points.insert( index, point );
        m_dragging = true;
        m_index = index;
    }

    if ( m_dragging ) {
        QToolTip::showText( mapToGlobal( mapToWidget( point ).toPoint() ), QString( "%1, %2" ).arg( point.x(), 0, 'f', 3 ).arg( point.y(), 0, 'f', 2 ), this );

        emit pointsChanged();
        update();
    }
}

void ShadeWidget::mouseMoveEvent( QMouseEvent* e )
{
    if ( !m_dragging )
        return;

    QPointF point = mapFromWidget( e->pos() );
    point.setX( qBound( (qreal)0, point.x(), (qreal)1 ) );
    point.setY( qBound( (qreal)0, point.y(), (qreal)1 ) ); 

    if ( m_index == 0 ) {
        point.setX( 0.0 );
        m_points[ m_index ] = point;
    } else if ( m_index == m_points.count() - 1 ) {
        point.setX( 1.0 );
        m_points[ m_index ] = point;
    } else {
        if ( m_index >= 0 )
            m_points.remove( m_index );
        if ( rect().adjusted( -30, -30, 30, 30 ).contains( e->pos() ) ) {
            int index = 1;
            while ( index < m_points.count() - 1 && m_points.at( index ).x() < point.x() )
                index++;
            m_points.insert( index, point );
            m_index = index;
        } else {
            m_index = -1;
        }
    }

    if ( m_index >= 0 ) {
        QToolTip::showText( mapToGlobal( mapToWidget( point ).toPoint() ),
            QString( "%1, %2" ).arg( point.x(), 0, 'f', 3 ).arg( point.y(), 0, 'f', 2 ), this );
    } else {
        QToolTip::hideText();
    }

    emit pointsChanged();
    update();
}

void ShadeWidget::mouseReleaseEvent( QMouseEvent* e )
{
    if ( e->button() == Qt::LeftButton )
        m_dragging = false;
}

QPointF ShadeWidget::mapToWidget( const QPointF& point )
{
    return QPointF( point.x() * ( width() - 14.0 ) + 7.0,
        height() - point.y() * ( height() - 14.0 ) - 7.0 );
}

QPointF ShadeWidget::mapFromWidget( const QPointF& point )
{
    return QPointF( ( point.x() - 7.0 ) / ( width() - 14.0 ),
        ( height() - point.y() - 7.0 ) / ( height() - 14.0 ) );
}