GradientWidget.cpp 11.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: F. Larue.
 *
 *	See licence.txt for additional information.
 */


#include "GradientWidget.h"
#include <cassert>




GradientWidget::GradientWidget( QWidget *parent ) : QWidget(parent)
{
17
    setMinimumSize( 100, 20 );
18
19
20
21
22
23
24
25
26
27
28

    m_BelowCursor  = -1;
    m_SelectedStop = -1;

    setMouseTracking( true );
    setFocusPolicy( Qt::ClickFocus );
}


GradientWidget::GradientWidget( const QColor& start, const QColor& end, QWidget *parent ) : GradientWidget(parent)
{
29
30
    addStop( start, 0.0f );
    addStop( end  , 1.0f );
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
}


GradientWidget::GradientWidget( const QList<QColor>& colors, const QList<float>& values, QWidget *parent ) : GradientWidget(parent)
{
    setStops( colors, values );
}


void GradientWidget::resizeEvent( QResizeEvent *evt )
{
    for( auto &s : m_Stops )
        s.updateX( width()-1 );
}


void GradientWidget::paintEvent( QPaintEvent *evt )
{
    if( !isValid() )
        return;

    const int w = width();
    const int h = height();

    QColor color = this->palette().windowText().color();

    QPainter painter;
    painter.begin( this );

    // Display gradients between every stop pair.

    painter.setPen( Qt::NoPen );

    if( m_Stops.front().value() != 0.0f )
    {
        painter.setBrush( QBrush(m_Stops.front().color()) );
        painter.drawRect( 0, 3, m_Stops.front().x(), h-6 );
    }

    if( m_Stops.back().value() != 1.0f )
    {
        painter.setBrush( QBrush(m_Stops.back().color()) );
        painter.drawRect( m_Stops.back().x(), 3, w-m_Stops.back().x(), h-6 );
    }

    for( int i=0; i<m_Stops.size()-1; ++i )
    {
        Stop &start = m_Stops[i];
        Stop &end   = m_Stops[i+1];

        QLinearGradient g( start.x(), 0.0f, end.x(), 0.0f );
        g.setColorAt( 0.0f, start.color() );
        g.setColorAt( 1.0f, end  .color() );
        painter.setBrush( QBrush(g) );

        painter.drawRect( start.x(), 3, end.x()-start.x()+1, h-6 );
    }

    // Display stop cursors.

    for( int i=0; i<m_Stops.size(); ++i )
    {
        Stop &s = m_Stops[i];

        // Vertical bar.
        painter.setPen( QPen(QColor(255,255,255),1.0f) );
        painter.setOpacity( 0.25f );
        painter.drawLine( s.x()  , 3, s.x()  , h-4 );
        painter.setOpacity( 0.22f );
        painter.drawLine( s.x()-1, 3, s.x()-1, h-4 );
        painter.drawLine( s.x()+1, 3, s.x()+1, h-4 );

        painter.setOpacity( 1.0f );
        if( i == m_SelectedStop )
            painter.setPen( QPen(color,1.0f) );
        else
            painter.setPen( QPen(color,1.0f,Qt::DotLine) );
        painter.drawLine( s.x(), 3, s.x(), h-4 );
        painter.setPen( QPen(color,1.0f) );

        // Top arrow head.
        if( i == m_SelectedStop )
            painter.drawLine( s.x()-3, 0, s.x()+3, 0 );
        painter.drawLine( s.x()-2, 1, s.x()+2, 1 );
        painter.drawLine( s.x()-1, 2, s.x()+1, 2 );

        // Bottom arrow head.
        if( i == m_SelectedStop )
            painter.drawLine( s.x()-3, h-1, s.x()+3, h-1 );
        painter.drawLine( s.x()-2, h-2, s.x()+2, h-2 );
        painter.drawLine( s.x()-1, h-3, s.x()+1, h-3 );
    }

    if( m_SelectedStop >= 0 )
    {
        QString text = m_KeyboardInputValue.isEmpty()?  QString::number(m_Stops[m_SelectedStop].value()) : m_KeyboardInputValue;

        QFontMetrics fm( font() );
        int textW = fm.width( text );

        int textX = m_Stops[m_SelectedStop].x() + 4;
        int textY = ((h+fm.height()) >> 1) - fm.descent();

        if( textX+textW >= w )
            textX -= textW + 7;

        painter.setOpacity( 0.5f );
        painter.setPen( QPen(QColor(255,255,255),1.0f) );
        painter.drawText( textX-1, textY  , text );
        painter.drawText( textX  , textY-1, text );
        painter.drawText( textX  , textY+1, text );
        painter.drawText( textX+1, textY  , text );
        painter.setOpacity( 1.0f );
        painter.setPen( QPen(color,1.0f) );
        painter.drawText( textX, textY, text );
    }

    painter.end();
}


int GradientWidget::getInterval( float value ) const
{
    int beg = 0, end = m_Stops.size();

    while( end != beg )
    {
        int p = beg + (end-beg)/2;

        if( value <= m_Stops[p].value() )
            end = p;
        else
            beg = p+1;
    }

    return end;
}


QColor GradientWidget::getColor( int interval, float value ) const
{
    assert( isValid() );

    if( interval == 0 )
        return m_Stops.front().color();
    else if( interval == m_Stops.size() )
        return m_Stops.back().color();
    else
    {
        const Stop &s0 = m_Stops[interval-1];
        const Stop &s1 = m_Stops[interval  ];

        float alpha = (value - s0.value()) / (s1.value() - s0.value());

        const QColor &c0 = s0.color();
        const QColor &c1 = s1.color();

        float r = c0.redF  () + alpha*(c1.redF  () - c0.redF  ());
        float g = c0.greenF() + alpha*(c1.greenF() - c0.greenF());
        float b = c0.blueF () + alpha*(c1.blueF () - c0.blueF ());

        return QColor::fromRgbF( r, g, b );
    }
}


float GradientWidget::clampValueToStopInterval( int n, float value ) const
{
    value = std::max( 0.0f, std::min(value,1.0f) );

    if( n > 0  &&  value < m_Stops[n-1].value() )
        value = m_Stops[n-1].value();
    else if( n < m_Stops.size()-1  &&  value > m_Stops[n+1].value() )
        value = m_Stops[n+1].value();

    return value;
}


void GradientWidget::addStop( const QColor& color, float value )
{
    Stop stop( color, value );
    stop.updateX( width()-1 );

    m_Stops.insert( getInterval(value), stop );

217
    emit gradientModified();
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    update();
}


void GradientWidget::setStops( const QList<QColor>& colors, const QList<float>& values )
{
    assert( colors.size() == values.size() );
    assert( !colors.empty() );

    m_Stops.clear();

    auto c = colors.begin();
    auto v = values.begin();

    while( c != colors.end() )
    {
        addStop( *c, *v );
        ++ c;
        ++ v;
    }
}


241
QString GradientWidget::getShaderFunctionBodyGLSL( int beg, int end, const QString& indent ) const
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
{
    QString res;
    if( end != beg )
    {
        int p = beg + (end-beg)/2;
        res += indent + "if( value <= " + QString::number( m_Stops[p].value() ) + " )\n";
        res += getShaderFunctionBodyGLSL( beg, p, indent+"  " );
        res += indent + "else\n";
        res += getShaderFunctionBodyGLSL( p+1, end, indent+"  " );
    }
    else
    {
        if( beg == 0 )
        {
            QColor c = m_Stops.front().color();
            res = indent + "return vec3(" + QString::number(c.redF()) + "," + QString::number(c.greenF()) + "," + QString::number(c.blueF()) + ");\n";
        }
        else if( beg == m_Stops.size() )
        {
            QColor c = m_Stops.back().color();
            res = indent + "return vec3(" + QString::number(c.redF()) + "," + QString::number(c.greenF()) + "," + QString::number(c.blueF()) + ");\n";
        }
        else
        {
            const Stop &s0 = m_Stops[beg-1];
            const Stop &s1 = m_Stops[beg  ];

            const QColor &c0 = s0.color();
            const QColor &c1 = s1.color();

            res += indent + "return mix( ";
            res += "vec3(" + QString::number(c0.redF()) + "," + QString::number(c0.greenF()) + "," + QString::number(c0.blueF()) + "), ";
            res += "vec3(" + QString::number(c1.redF()) + "," + QString::number(c1.greenF()) + "," + QString::number(c1.blueF()) + "), ";
            res += "(value-" + QString::number(s0.value()) + ")*" + QString::number(1.0f / (s1.value() - s0.value()));
            res += " );\n";
        }
    }

    return res;
}


284
QString GradientWidget::getShaderFunctionGLSL( const QString& funcName ) const
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{
    QString func =
        "vec3 " + funcName + "( float value )\n"
        "{\n"
        + getShaderFunctionBodyGLSL( 0, m_Stops.size(), "    " ) +
        "}\n";

    return func;
}


void GradientWidget::mouseDoubleClickEvent( QMouseEvent *evt )
{
    if( m_SelectedStop >= 0 )
    {
        Stop &stop = m_Stops[m_SelectedStop];
        QColor color = QColorDialog::getColor( stop.color(), this, "Select gradient stop color" );
302
        if( color.isValid()  &&  color != stop.color() )
303
304
        {
            stop.setColor( color );
305
            emit gradientModified();
306
307
308
309
310
311
312
313
314
315
316
317
            update();
        }
    }
    else
    {
        float value = float(evt->x()) / (width()-1);
        int interval = getInterval( value );

        Stop stop( getColor(interval,value), value );
        stop.updateX( width()-1 );
        m_Stops.insert( interval, stop );

318
        emit gradientModified();
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
        update();
    }
}


void GradientWidget::mousePressEvent( QMouseEvent *evt )
{
    m_SelectedStop = m_BelowCursor;
    update();
}


void GradientWidget::mouseMoveEvent( QMouseEvent *evt )
{
    if( (evt->buttons() & Qt::LeftButton)  &&  m_SelectedStop >= 0 )
    {
        float value = clampValueToStopInterval( m_SelectedStop, float(evt->x()) / (width()-1) );
336
337
338
339
340
341
342
        if( value != m_Stops[m_SelectedStop].value() )
        {
            m_Stops[m_SelectedStop].setValue( value );
            m_Stops[m_SelectedStop].updateX( width()-1 );
            emit gradientModified();
            update();
        }
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
    }
    else
    {
        int belowCursor = -1;
        for( int i=0; i<m_Stops.size(); ++i )
            if( evt->x() >= m_Stops[i].x()-2  &&  evt->x() <= m_Stops[i].x()+2 )
            {
                belowCursor = i;
                break;
            }

        if( belowCursor != m_BelowCursor )
        {
            m_BelowCursor = belowCursor;
            setCursor( belowCursor<0? Qt::ArrowCursor : Qt::PointingHandCursor );
        }
    }
}


void GradientWidget::keyPressEvent( QKeyEvent *evt )
{
    if( m_SelectedStop < 0 )
    {
        m_KeyboardInputValue = QString();
        QWidget::keyPressEvent( evt );
        return;
    }

    switch( evt->key() )
    {
        case Qt::Key_0:
        case Qt::Key_1:
        case Qt::Key_2:
        case Qt::Key_3:
        case Qt::Key_4:
        case Qt::Key_5:
        case Qt::Key_6:
        case Qt::Key_7:
        case Qt::Key_8:
        case Qt::Key_9:
        case Qt::Key_Period:
        {
            bool ok;
            QString tmp = m_KeyboardInputValue + (char) evt->key();
            if( tmp == "." )
                tmp = "0.";
390
            float value = tmp.toFloat( &ok );
391
392
            if( ok )
            {
393
394
395
396
397
398
399
400
401
402
                if( value >= 2.0f )
                {
                    m_KeyboardInputValue = "0." + tmp;
                    update();
                }
                else if( value <= 1.0f )
                {
                    m_KeyboardInputValue = tmp;
                    update();
                }
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
            }
            break;
        }
        case Qt::Key_Backspace:
        {
            m_KeyboardInputValue = m_KeyboardInputValue.left( m_KeyboardInputValue.size()-1 );
            update();
            break;
        }
        case Qt::Key_Delete:
        {
            if( m_Stops.size() > 1 )
            {
                m_Stops.remove( m_SelectedStop );
                m_SelectedStop = -1;
                m_KeyboardInputValue = QString();
419
                emit gradientModified();
420
421
422
423
424
425
426
427
428
429
                update();
            }
            break;
        }
        case Qt::Key_Enter:
        case Qt::Key_Return:
        {
            if( m_KeyboardInputValue.isEmpty() )
                break;

430
            float value = clampValueToStopInterval( m_SelectedStop, m_KeyboardInputValue.toFloat() );
431
432
            m_KeyboardInputValue = QString();

433
434
435
436
437
438
            if( value != m_Stops[m_SelectedStop].value() )
            {
                m_Stops[m_SelectedStop].setValue( value );
                m_Stops[m_SelectedStop].updateX( width()-1 );
                emit gradientModified();
            }
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455

            update();
            break;
        }
        default: QWidget::keyPressEvent( evt );
    }
}


void GradientWidget::leaveEvent( QEvent *evt )
{
    if( m_SelectedStop >= 0 )
    {
        m_SelectedStop = -1;
        update();
    }
}