~ubuntu-branches/ubuntu/edgy/digikam/edgy-proposed

« back to all changes in this revision

Viewing changes to digikam/libs/themeengine/texture.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Achim Bohnet
  • Date: 2006-05-15 01:15:02 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515011502-kpyuz7766hpbuia8
Tags: 0.8.2~rc1-0ubuntu1
* sync with debian (UVF see #44102)
  0.8.2~rc1-0ubuntu1 is identical to debian's 0.8.1+0.8.2-rc1-1.
  Version was changed due to latest 0.8.1.ubuntu-0ubuntu1 upload.
  This version is unfortunately bigger than debian's 0.8.1+0.8.2-rc1-1
* Merge in ubuntu changelog

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* ============================================================
 
2
 * Author: Renchi Raju <renchi@pooh.tam.uiuc.edu>
2
3
 * Date  : 2004-07-26
3
4
 * Description : 
4
 
 * 
 
5
 *
 
6
 * Copyright 2004 by Renchi Raju
 
7
 *
5
8
 * Adapted from fluxbox: Texture/TextureRender
6
9
 *
7
10
 * Texture.cc for Fluxbox Window Manager
28
31
 * 
29
32
 * ============================================================ */
30
33
 
 
34
// C++ includes.
 
35
 
 
36
#include <cstring>
 
37
#include <cstdio>
 
38
 
 
39
// Qt includes.
 
40
 
31
41
#include <qpainter.h>
32
42
#include <qimage.h>
33
 
 
34
 
#include <cstring>
35
 
#include <cstdio>
36
 
 
 
43
#include <qpixmap.h>
 
44
 
 
45
// Local includes.
 
46
 
 
47
#include "theme.h"
37
48
#include "texture.h"
38
49
 
39
 
 
 
50
namespace Digikam
 
51
{
 
52
 
 
53
class TexturePriv
 
54
{
 
55
public:
 
56
 
 
57
    TexturePriv()
 
58
    {
 
59
        red   = 0;
 
60
        green = 0;
 
61
        blue  = 0;
 
62
    }
 
63
 
 
64
    bool            border;
 
65
 
 
66
    unsigned char  *red;
 
67
    unsigned char  *green;
 
68
    unsigned char  *blue;
 
69
    
 
70
    int             width;
 
71
    int             height;
 
72
        
 
73
    QPixmap         pixmap;
 
74
 
 
75
    QColor          color0;
 
76
    QColor          color1;
 
77
    QColor          borderColor;
 
78
 
 
79
    Theme::Bevel    bevel;
 
80
    Theme::Gradient gradient;
 
81
};
 
82
    
40
83
Texture::Texture(int w, int h, const QColor& from, const QColor& to,
41
84
                 Theme::Bevel bevel, Theme::Gradient gradient,
42
85
                 bool border, const QColor& borderColor)
43
86
{
44
 
    m_bevel       = bevel;
45
 
    m_gradient    = gradient;
46
 
    m_red         = 0;
47
 
    m_green       = 0;
48
 
    m_blue        = 0;
49
 
    m_border      = border;
50
 
    m_borderColor = borderColor;
 
87
    d = new TexturePriv;
 
88
    
 
89
    d->bevel       = bevel;
 
90
    d->gradient    = gradient;
 
91
    d->border      = border;
 
92
    d->borderColor = borderColor;
51
93
 
52
94
    if (!border)
53
95
    {
54
 
        m_width  = w;
55
 
        m_height = h;
 
96
        d->width  = w;
 
97
        d->height = h;
56
98
    }
57
99
    else
58
100
    {
59
 
        m_width  = w-2;
60
 
        m_height = h-2;
 
101
        d->width  = w-2;
 
102
        d->height = h-2;
61
103
    }
62
104
 
63
 
    if (m_width <= 0 || m_height <= 0)
 
105
    if (d->width <= 0 || d->height <= 0)
64
106
        return;
65
107
    
66
 
    
67
108
    if (bevel & Theme::SUNKEN)
68
109
    {
69
 
        m_color0 = to;
70
 
        m_color1 = from;
 
110
        d->color0 = to;
 
111
        d->color1 = from;
71
112
    }
72
113
    else
73
114
    {
74
 
        m_color0 = from;
75
 
        m_color1 = to;
 
115
        d->color0 = from;
 
116
        d->color1 = to;
76
117
    }
77
118
 
78
119
    if (gradient == Theme::SOLID)
81
122
    }
82
123
    else
83
124
    {
84
 
        m_red   = new unsigned char[w*h];
85
 
        m_green = new unsigned char[w*h];
86
 
        m_blue  = new unsigned char[w*h];
 
125
        d->red   = new unsigned char[w*h];
 
126
        d->green = new unsigned char[w*h];
 
127
        d->blue  = new unsigned char[w*h];
87
128
 
88
129
        if (gradient == Theme::HORIZONTAL)
89
130
            doHgradient();
101
142
 
102
143
Texture::~Texture()
103
144
{
104
 
    if (m_red)
105
 
        delete [] m_red;
106
 
    if (m_green)
107
 
        delete [] m_green;
108
 
    if (m_blue)
109
 
        delete [] m_blue;
 
145
    if (d->red)
 
146
        delete [] d->red;
 
147
    if (d->green)
 
148
        delete [] d->green;
 
149
    if (d->blue)
 
150
        delete [] d->blue;
 
151
 
 
152
    delete d;    
110
153
}
111
154
 
112
155
QPixmap Texture::renderPixmap() const
113
156
{
114
 
    if (m_width <= 0 || m_height <= 0)
 
157
    if (d->width <= 0 || d->height <= 0)
115
158
        return QPixmap();
116
159
 
117
 
    if (!m_border)
118
 
        return m_pixmap;
 
160
    if (!d->border)
 
161
        return d->pixmap;
119
162
 
120
 
    QPixmap pix(m_width+2, m_height+2);
121
 
    bitBlt(&pix, 1, 1, &m_pixmap, 0, 0);
 
163
    QPixmap pix(d->width+2, d->height+2);
 
164
    bitBlt(&pix, 1, 1, &d->pixmap, 0, 0);
122
165
    QPainter p(&pix);
123
 
    p.setPen(m_borderColor);
124
 
    p.drawRect(0, 0, m_width+2, m_height+2);
 
166
    p.setPen(d->borderColor);
 
167
    p.drawRect(0, 0, d->width+2, d->height+2);
125
168
    p.end();
126
169
 
127
170
    return pix;
129
172
 
130
173
void Texture::doSolid()
131
174
{
132
 
    m_pixmap.resize(m_width, m_height);
133
 
    QPainter p(&m_pixmap);
134
 
    p.fillRect(0, 0, m_width, m_height, m_color0);
135
 
    if (m_bevel == Theme::RAISED)
 
175
    d->pixmap.resize(d->width, d->height);
 
176
    QPainter p(&d->pixmap);
 
177
    p.fillRect(0, 0, d->width, d->height, d->color0);
 
178
    if (d->bevel == Theme::RAISED)
136
179
    {
137
 
        p.setPen(m_color0.light(120));
138
 
        p.drawLine(0, 0, m_width-1, 0);  // top
139
 
        p.drawLine(0, 0, 0, m_height-1); // left
140
 
        p.setPen(m_color0.dark(120));
141
 
        p.drawLine(0, m_height-1, m_width-1, m_height-1); // bottom
142
 
        p.drawLine(m_width-1, 0, m_width-1, m_height-1);  // right
 
180
        p.setPen(d->color0.light(120));
 
181
        p.drawLine(0, 0, d->width-1, 0);  // top
 
182
        p.drawLine(0, 0, 0, d->height-1); // left
 
183
        p.setPen(d->color0.dark(120));
 
184
        p.drawLine(0, d->height-1, d->width-1, d->height-1); // bottom
 
185
        p.drawLine(d->width-1, 0, d->width-1, d->height-1);  // right
143
186
    }
144
 
    else if (m_bevel == Theme::SUNKEN)
 
187
    else if (d->bevel == Theme::SUNKEN)
145
188
    {
146
 
        p.setPen(m_color0.dark(120));
147
 
        p.drawLine(0, 0, m_width-1, 0);  // top
148
 
        p.drawLine(0, 0, 0, m_height-1); // left
149
 
        p.setPen(m_color0.light(120));
150
 
        p.drawLine(0, m_height-1, m_width-1, m_height-1); // bottom
151
 
        p.drawLine(m_width-1, 0, m_width-1, m_height-1);  // right
 
189
        p.setPen(d->color0.dark(120));
 
190
        p.drawLine(0, 0, d->width-1, 0);  // top
 
191
        p.drawLine(0, 0, 0, d->height-1); // left
 
192
        p.setPen(d->color0.light(120));
 
193
        p.drawLine(0, d->height-1, d->width-1, d->height-1); // bottom
 
194
        p.drawLine(d->width-1, 0, d->width-1, d->height-1);  // right
152
195
    }
153
196
    p.end();
154
197
}
156
199
void Texture::doHgradient()
157
200
{
158
201
    float drx, dgx, dbx,
159
 
        xr = (float) m_color0.red(),
160
 
        xg = (float) m_color0.green(),
161
 
        xb = (float) m_color0.blue();
162
 
    unsigned char *pr = m_red, *pg = m_green, *pb = m_blue;
 
202
        xr = (float) d->color0.red(),
 
203
        xg = (float) d->color0.green(),
 
204
        xb = (float) d->color0.blue();
 
205
    unsigned char *pr = d->red, *pg = d->green, *pb = d->blue;
163
206
 
164
207
    register int x, y;
165
208
 
166
 
    drx = (float) (m_color1.red()   - m_color0.red());
167
 
    dgx = (float) (m_color1.green() - m_color0.green());
168
 
    dbx = (float) (m_color1.blue()  - m_color0.blue());
169
 
 
170
 
    drx /= m_width;
171
 
    dgx /= m_width;
172
 
    dbx /= m_width;
173
 
 
174
 
    for (x = 0; x < m_width; x++) {
 
209
    drx = (float) (d->color1.red()   - d->color0.red());
 
210
    dgx = (float) (d->color1.green() - d->color0.green());
 
211
    dbx = (float) (d->color1.blue()  - d->color0.blue());
 
212
 
 
213
    drx /= d->width;
 
214
    dgx /= d->width;
 
215
    dbx /= d->width;
 
216
 
 
217
    for (x = 0; x < d->width; x++)
 
218
    {
175
219
        *(pr++) = (unsigned char) (xr);
176
220
        *(pg++) = (unsigned char) (xg);
177
221
        *(pb++) = (unsigned char) (xb);
181
225
        xb += dbx;
182
226
    }
183
227
 
184
 
    for (y = 1; y < m_height; y++, pr += m_width, pg += m_width, pb += m_width) {
185
 
        memcpy(pr, m_red, m_width);
186
 
        memcpy(pg, m_green, m_width);
187
 
        memcpy(pb, m_blue, m_width);
 
228
    for (y = 1; y < d->height; y++, pr += d->width, pg += d->width, pb += d->width)
 
229
    {
 
230
        memcpy(pr, d->red, d->width);
 
231
        memcpy(pg, d->green, d->width);
 
232
        memcpy(pb, d->blue, d->width);
188
233
    }
189
234
}
190
235
 
191
236
void Texture::doVgradient()
192
237
{
193
238
    float dry, dgy, dby,
194
 
        yr = (float) m_color0.red(),
195
 
        yg = (float) m_color0.green(),
196
 
        yb = (float) m_color0.blue();
197
 
 
198
 
    dry = (float) (m_color1.red()   - m_color0.red());
199
 
    dgy = (float) (m_color1.green() - m_color0.green());
200
 
    dby = (float) (m_color1.blue()  - m_color0.blue());
201
 
 
202
 
    dry /= m_height;
203
 
    dgy /= m_height;
204
 
    dby /= m_height;
205
 
 
206
 
    unsigned char *pr = m_red, *pg = m_green, *pb = m_blue;
 
239
        yr = (float) d->color0.red(),
 
240
        yg = (float) d->color0.green(),
 
241
        yb = (float) d->color0.blue();
 
242
 
 
243
    dry = (float) (d->color1.red()   - d->color0.red());
 
244
    dgy = (float) (d->color1.green() - d->color0.green());
 
245
    dby = (float) (d->color1.blue()  - d->color0.blue());
 
246
 
 
247
    dry /= d->height;
 
248
    dgy /= d->height;
 
249
    dby /= d->height;
 
250
 
 
251
    unsigned char *pr = d->red, *pg = d->green, *pb = d->blue;
207
252
    register int y;
208
253
    
209
 
    for (y = 0; y < m_height; y++, pr += m_width, pg += m_width, pb += m_width) {
210
 
        memset(pr, (unsigned char) yr, m_width);
211
 
        memset(pg, (unsigned char) yg, m_width);
212
 
        memset(pb, (unsigned char) yb, m_width);
 
254
    for (y = 0; y < d->height; y++, pr += d->width, pg += d->width, pb += d->width) {
 
255
        memset(pr, (unsigned char) yr, d->width);
 
256
        memset(pg, (unsigned char) yg, d->width);
 
257
        memset(pb, (unsigned char) yb, d->width);
213
258
 
214
259
        yr += dry;
215
260
        yg += dgy;
217
262
    }
218
263
}
219
264
 
220
 
 
221
265
void Texture::doDgradient()
222
266
{
223
 
    unsigned int* xtable = new unsigned int[m_width*3]; 
224
 
    unsigned int* ytable = new unsigned int[m_height*3];
 
267
    unsigned int* xtable = new unsigned int[d->width*3];
 
268
    unsigned int* ytable = new unsigned int[d->height*3];
225
269
 
226
270
    float drx, dgx, dbx, dry, dgy, dby, yr = 0.0, yg = 0.0, yb = 0.0,
227
 
                                        xr = (float) m_color0.red(),
228
 
                                        xg = (float) m_color0.green(),
229
 
                                        xb = (float) m_color0.blue();
230
 
    unsigned char *pr = m_red, *pg = m_green, *pb = m_blue;
231
 
    unsigned int w = m_width * 2, h = m_height * 2;
 
271
                                        xr = (float) d->color0.red(),
 
272
                                        xg = (float) d->color0.green(),
 
273
                                        xb = (float) d->color0.blue();
 
274
    unsigned char *pr = d->red, *pg = d->green, *pb = d->blue;
 
275
    unsigned int w = d->width * 2, h = d->height * 2;
232
276
    unsigned int *xt = xtable; 
233
277
    unsigned int *yt = ytable; 
234
278
 
235
 
 
236
279
    register int x, y;
237
280
 
238
 
    dry = drx = (float) (m_color1.red()   - m_color0.red());
239
 
    dgy = dgx = (float) (m_color1.green() - m_color0.green());
240
 
    dby = dbx = (float) (m_color1.blue()  - m_color0.blue());
 
281
    dry = drx = (float) (d->color1.red()   - d->color0.red());
 
282
    dgy = dgx = (float) (d->color1.green() - d->color0.green());
 
283
    dby = dbx = (float) (d->color1.blue()  - d->color0.blue());
241
284
 
242
285
    // Create X table
243
286
    drx /= w;
244
287
    dgx /= w;
245
288
    dbx /= w;
246
289
 
247
 
    for (x = 0; x < m_width; x++) {
 
290
    for (x = 0; x < d->width; x++)
 
291
    {
248
292
        *(xt++) = (unsigned char) (xr);
249
293
        *(xt++) = (unsigned char) (xg);
250
294
        *(xt++) = (unsigned char) (xb);
259
303
    dgy /= h;
260
304
    dby /= h;
261
305
 
262
 
    for (y = 0; y < m_height; y++) {
 
306
    for (y = 0; y < d->height; y++)
 
307
    {
263
308
        *(yt++) = ((unsigned char) yr);
264
309
        *(yt++) = ((unsigned char) yg);
265
310
        *(yt++) = ((unsigned char) yb);
271
316
 
272
317
    // Combine tables to create gradient
273
318
 
274
 
    for (yt = ytable, y = 0; y < m_height; y++, yt += 3)
 
319
    for (yt = ytable, y = 0; y < d->height; y++, yt += 3)
275
320
    {
276
 
        for (xt = xtable, x = 0; x < m_width; x++)
 
321
        for (xt = xtable, x = 0; x < d->width; x++)
277
322
        {
278
323
            *(pr++) = *(xt++) + *(yt);
279
324
            *(pg++) = *(xt++) + *(yt + 1);
287
332
 
288
333
void Texture::doBevel()
289
334
{
290
 
    unsigned char *pr = m_red, *pg = m_green, *pb = m_blue;
 
335
    unsigned char *pr = d->red, *pg = d->green, *pb = d->blue;
291
336
 
292
337
    register unsigned char r, g, b, rr ,gg ,bb;
293
 
    register unsigned int w = m_width, h = m_height - 1, wh = w * h;
 
338
    register unsigned int w = d->width, h = d->height - 1, wh = w * h;
294
339
 
295
 
    while (--w) {
 
340
    while (--w)
 
341
    {
296
342
        r = *pr;
297
343
        rr = r + (r >> 1);
298
344
        if (rr < r) rr = ~0;
350
396
    *(pg + wh) = gg;
351
397
    *(pb + wh) = bb;
352
398
 
353
 
    pr = m_red   + m_width;
354
 
    pg = m_green + m_width;
355
 
    pb = m_blue  + m_width;
 
399
    pr = d->red   + d->width;
 
400
    pg = d->green + d->width;
 
401
    pb = d->blue  + d->width;
356
402
 
357
 
    while (--h) {
 
403
    while (--h)
 
404
    {
358
405
        r = *pr;
359
406
        rr = r + (r >> 1);
360
407
        if (rr < r) rr = ~0;
369
416
        *pg = gg;
370
417
        *pb = bb;
371
418
 
372
 
        pr += m_width - 1;
373
 
        pg += m_width - 1;
374
 
        pb += m_width - 1;
 
419
        pr += d->width - 1;
 
420
        pg += d->width - 1;
 
421
        pb += d->width - 1;
375
422
 
376
423
        r = *pr;
377
424
        rr = (r >> 2) + (r >> 1);
402
449
    *pg = gg;
403
450
    *pb = bb;
404
451
 
405
 
    pr += m_width - 1;
406
 
    pg += m_width - 1;
407
 
    pb += m_width - 1;
 
452
    pr += d->width - 1;
 
453
    pg += d->width - 1;
 
454
    pb += d->width - 1;
408
455
 
409
456
    r = *pr;
410
457
    rr = (r >> 2) + (r >> 1);
423
470
 
424
471
void Texture::buildImage()
425
472
{
426
 
    unsigned char *pr = m_red, *pg = m_green, *pb = m_blue;
 
473
    unsigned char *pr = d->red, *pg = d->green, *pb = d->blue;
427
474
 
428
 
    QImage image(m_width, m_height, 32);
 
475
    QImage image(d->width, d->height, 32);
429
476
 
430
477
    unsigned int* bits = (unsigned int*) image.bits();
431
478
    
432
479
    register int p;
433
 
    for (p =0; p < m_width*m_height; p++)
 
480
    for (p =0; p < d->width*d->height; p++)
434
481
    {
435
482
        *bits = 0xff << 24 | *pr << 16 | *pg << 8 | *pb;
436
483
        bits++;
439
486
        pb++;
440
487
    }
441
488
 
442
 
    m_pixmap = QPixmap(image);
 
489
    d->pixmap = QPixmap(image);
443
490
}
 
491
 
 
492
}  // NameSpace Digikam