summaryrefslogtreecommitdiffstats
path: root/chalk/ui/kis_gradient_slider_widget.cc
blob: 3325517a33f2fbc59186903eae224e15d77dbf18 (plain)
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
/*
 *  Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
 *                2004 Sven Langkamp <longamp@reallygood.de>
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_gradient_slider_widget.h"

#include <tqpainter.h>

#include <kdebug.h>
#include <tdepopupmenu.h>
#include <klocale.h>

#include "kis_autogradient_resource.h"

#define MARGIN 5
#define HANDLE_SIZE 10

KisGradientSliderWidget::KisGradientSliderWidget(TQWidget *parent, const char* name, WFlags f )
    : TQWidget( parent, name, f),
    m_currentSegment(0),
    m_selectedSegment(0),
    m_drag(0)
{
    setMinimumHeight(30);

    m_segmentMenu = new TDEPopupMenu();
    m_segmentMenu->insertItem(i18n("Split Segment"), SPLIT_SEGMENT);
    m_segmentMenu->insertItem(i18n("Duplicate Segment"), DUPLICATE_SEGMENT);
    m_segmentMenu->insertItem(i18n("Mirror Segment"), MIRROR_SEGMENT);
    m_segmentMenu->insertItem(i18n("Remove Segment"), REMOVE_SEGMENT);
    connect( m_segmentMenu, TQT_SIGNAL( activated(int) ), TQT_SLOT( slotMenuAction(int) ) );
}

void KisGradientSliderWidget::setGradientResource( KisAutogradientResource* agr)
{
    m_autogradientResource = agr;
    m_selectedSegment = m_autogradientResource->segmentAt(0.0);
    emit sigSelectedSegment( m_selectedSegment );
}

void KisGradientSliderWidget::paintEvent ( TQPaintEvent* pe )
{
    TQWidget::paintEvent( pe );
    TQPixmap pixmap( width(), height() );
    pixmap.fill( colorGroup().background() );
    TQPainter painter( &pixmap );
    painter.setPen( TQt::black );
    painter.drawRect( MARGIN, MARGIN, width() - 2 * MARGIN, height()- 2 * MARGIN - HANDLE_SIZE );
    if(m_autogradientResource)
    {
        TQImage img = m_autogradientResource->generatePreview(width()- 2* MARGIN - 2, height()- 2* MARGIN - HANDLE_SIZE - 2);
        TQPixmap pixmap(img.width(), img.height());
        if (!img.isNull()) {
            m_pixmapIO.putImage(&pixmap, 0, 0, &img);
            painter.drawPixmap( MARGIN + 1, MARGIN + 1, pixmap, 0, 0, pixmap.width(), pixmap.height());
        }

        painter.fillRect( MARGIN + 1, height()- MARGIN - HANDLE_SIZE, width() - 2 * MARGIN, HANDLE_SIZE, TQBrush( TQt::white ) );
        if( m_selectedSegment )
        {
            TQRect selection( tqRound( m_selectedSegment->startOffset()*(double)(width()- 2 * MARGIN - 2) ) + 6,
                    height()- HANDLE_SIZE - MARGIN,
                    tqRound( ( m_selectedSegment->endOffset() - m_selectedSegment->startOffset() )*(double)(width()-12) ),
                    HANDLE_SIZE );
            painter.fillRect( selection, TQBrush( colorGroup().highlight() ) );
        }

        TQPointArray triangle(3);
        TQValueVector<double> handlePositions = m_autogradientResource->getHandlePositions();
        int position;
        painter.setBrush( TQBrush( TQt::black) );
        for (uint i = 0; i < handlePositions.count(); i++)
        {
            position = tqRound( handlePositions[i] * (double)( width()-12) ) + 6;
            triangle[0] = TQPoint(position, height() - HANDLE_SIZE - MARGIN );
            triangle[1] = TQPoint(position + (HANDLE_SIZE / 2 - 1), height() - MARGIN );
            triangle[2] = TQPoint(position - (HANDLE_SIZE / 2 - 1), height() - MARGIN );
            painter.drawPolygon(triangle);
        }
        painter.setBrush( TQBrush( TQt::white ) );
        TQValueVector<double> middleHandlePositions = m_autogradientResource->getMiddleHandlePositions();
        for (uint i = 0; i < middleHandlePositions.count(); i++)
        {
            position = tqRound( middleHandlePositions[i] * (double)(width()-12) ) + 6;
            triangle[0] = TQPoint(position, height()-HANDLE_SIZE - MARGIN);
            triangle[1] = TQPoint(position + (HANDLE_SIZE / 2 - 2), height() - MARGIN);
            triangle[2] = TQPoint(position - (HANDLE_SIZE / 2 - 2), height() - MARGIN);
            painter.drawPolygon(triangle);
        }
    }
    bitBlt( this, 0, 0, &pixmap, 0, 0, pixmap.width(), pixmap.height(), TQt::CopyROP);
}

void KisGradientSliderWidget::mousePressEvent( TQMouseEvent * e )
{
    TQWidget::mousePressEvent( e );
    if( ( e->y() < MARGIN || e->y() > height() - MARGIN ) || ( e->x() < MARGIN || e->x() > width() - MARGIN ) || e-> button() != Qt::LeftButton )
        return;
    double t = (double)(e->x() - MARGIN) / (double)(width() - 2 * MARGIN);
    KisGradientSegment* segment = 0;
    segment = m_autogradientResource->segmentAt(t);
    if(segment != 0)
    {
        m_currentSegment = segment;
        TQRect leftHandle( tqRound(m_currentSegment->startOffset() * (double)(width()-2*MARGIN-2)+ MARGIN - (HANDLE_SIZE/2 - 1 )),
                    height() - HANDLE_SIZE,
                    HANDLE_SIZE - 1,
                    HANDLE_SIZE);
        TQRect middleHandle( tqRound(m_currentSegment->middleOffset() * (double)(width()-2*MARGIN-2)+ MARGIN - (HANDLE_SIZE/2 -2) ),
                    height() - HANDLE_SIZE - MARGIN,
                    HANDLE_SIZE - 1,
                    HANDLE_SIZE);
        TQRect rightHandle( tqRound(m_currentSegment->endOffset() * (double)(width()-2*MARGIN-2)+ MARGIN - (HANDLE_SIZE/2 - 1 )),
                    height() - HANDLE_SIZE,
                    HANDLE_SIZE - 1,
                    HANDLE_SIZE);
        // Change the activation order of the handles to avoid deadlocks
        if( t > 0.5 )
        {
            if( leftHandle.contains( e->pos() ) )
                m_drag = LEFT_DRAG;
            else if( middleHandle.contains( e->pos() ) )
                m_drag = MIDDLE_DRAG;
            else if( rightHandle.contains( e->pos() ) )
                m_drag = RIGHT_DRAG;
        }
        else
        {
            if( rightHandle.contains( e->pos() ) )
                m_drag = RIGHT_DRAG;
            else if( middleHandle.contains( e->pos() ) )
                m_drag = MIDDLE_DRAG;
            else if( leftHandle.contains( e->pos() ) )
                m_drag = LEFT_DRAG;
        }

        if( m_drag == NO_DRAG )
        {
            m_selectedSegment = m_currentSegment;
            emit sigSelectedSegment( m_selectedSegment );
        }
    }
    repaint(false);
}

void KisGradientSliderWidget::mouseReleaseEvent ( TQMouseEvent * e )
{
    TQWidget::mouseReleaseEvent( e );
    m_drag = NO_DRAG;
}

void KisGradientSliderWidget::mouseMoveEvent( TQMouseEvent * e )
{
    TQWidget::mouseMoveEvent( e );
    if( ( e->y() < MARGIN || e->y() > height() - MARGIN ) || ( e->x() < MARGIN || e->x() > width() - MARGIN ) )
        return;
    double t = (double)(e->x() - MARGIN) / (double)(width() - 2 * MARGIN);
    switch( m_drag )
    {
        case RIGHT_DRAG:
            m_autogradientResource->moveSegmentEndOffset( m_currentSegment, t );
            break;
        case LEFT_DRAG:
            m_autogradientResource->moveSegmentStartOffset( m_currentSegment, t );
            break;
        case MIDDLE_DRAG:
            m_autogradientResource->moveSegmentMiddleOffset( m_currentSegment, t );
            break;
    }

    if ( m_drag != NO_DRAG)
        emit sigChangedSegment( m_currentSegment );

    repaint(false);
}

void KisGradientSliderWidget::contextMenuEvent( TQContextMenuEvent * e )
{
    m_segmentMenu->setItemEnabled( REMOVE_SEGMENT, m_autogradientResource->removeSegmentPossible() );
    m_segmentMenu->popup( e->globalPos());
}

void KisGradientSliderWidget::slotMenuAction( int id )
{
    switch( id )
    {
        case SPLIT_SEGMENT:
            m_autogradientResource->splitSegment( m_selectedSegment );
            break;
        case DUPLICATE_SEGMENT:
            m_autogradientResource->duplicateSegment( m_selectedSegment );
            break;
        case MIRROR_SEGMENT:
            m_autogradientResource->mirrorSegment( m_selectedSegment );
            break;
        case REMOVE_SEGMENT:
            m_selectedSegment = m_autogradientResource->removeSegment( m_selectedSegment );
            break;
    }
    emit sigSelectedSegment( m_selectedSegment );
    repaint(false);
}

#include "kis_gradient_slider_widget.moc"