File: stdpix.cpp

package info (click to toggle)
bespin 0.r1552-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,848 kB
  • sloc: cpp: 24,647; sh: 523; xml: 459; makefile: 79
file content (415 lines) | stat: -rw-r--r-- 14,043 bytes parent folder | download | duplicates (2)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 *   Bespin style for Qt4
 *   Copyright 2007-2012 by Thomas Lübking <thomas.luebking@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2
 *
 *   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 <QStyleOptionTitleBar>
#include <QApplication>
#include <QDockWidget>
#include <QImageReader>
#include <QPainter>
#include <QPen>

#include "blib/colors.h"
#include "blib/gradients.h"
#include "blib/shapes.h"

#include "bespin.h"

#include <QtDebug>

#define COLOR(_TYPE_) pal.color(QPalette::_TYPE_)

using namespace Bespin;

static void
setIconFont(QPainter &painter, const QRect &rect, float f = 0.75)
{
    QFont fnt = painter.font();
    fnt.setPixelSize ( (int)(f*rect.height()) );
    fnt.setBold(true); painter.setFont(fnt);
}

static inline uint qt_intensity(uint r, uint g, uint b)
{
    // 30% red, 59% green, 11% blue
    return (77 * r + 150 * g + 28 * b) / 255;
}

#if 0
static
QPainterPath arrow( const QRect &rect, bool right = false )
{
    int cy = rect.center().y();
    int x1 = rect.right();
    int s = -1;
    if (right)
        { s = 1; x1 = rect.left(); }

    QPainterPath shape;
    shape.moveTo( x1, rect.top() );
    shape.quadTo( x1+s*18*rect.width()/10, cy, x1, rect.bottom() );
    shape.quadTo( x1+s*rect.width()/5, cy, x1, rect.top() );
    return shape;
}
#endif
QPixmap
Style::standardPixmap(StandardPixmap standardPixmap, const QStyleOption *option, const QWidget *widget ) const
{
    bool sunken = false, isEnabled = false, hover = false;
    if (option)
    {
        sunken = option->state & State_Sunken;
        isEnabled = option->state & State_Enabled;
        hover = isEnabled && (option->state & State_MouseOver);
    }

    QRect rect; QPalette pal;
//    const QStyleOptionTitleBar *opt =
//       qstyleoption_cast<const QStyleOptionTitleBar *>(option);
    if (option)
    {
        if (option->rect.isNull()) // THIS SHOULD NOT!!!! happen... but unfortunately does on dockwidgets...
            rect = QRect(0,0,14,14);
        else
        {
            rect = option->rect; rect.moveTo(0,0);
            if (rect.width() > rect.height())
                rect.setWidth(rect.height());
            else
                rect.setHeight(rect.width());
        }
        pal = option->palette;
    }
    else
    {
        rect = QRect(0,0,14,14);
        pal = widget ? widget->palette() : qApp->palette();
    }

    QPalette::ColorRole bg = QPalette::Window, fg = QPalette::WindowText;
    if (widget)
        { bg = widget->backgroundRole(); fg = widget->foregroundRole(); }

    const QDockWidget *dock = qobject_cast<const QDockWidget*>(widget);
    const int sz = dock ? 14 : rect.height();
    QPixmap pm(sz, sz);
    pm.fill(Qt::transparent);
    QPainter painter(&pm);
    QPainterPath shape;
    Shapes::Style style = (Shapes::Style)config.winBtnStyle;

    switch (standardPixmap)
    {
#if 0
    case SP_ArrowBack:
    case SP_ArrowLeft:
        shape = arrow( pm.rect() ).subtracted(arrow( pm.rect().translated(pm.rect().width()/2, 0) ));
        goto paint;
    case SP_ArrowRight:
    case SP_ArrowForward:
        shape = arrow( pm.rect(), true ).subtracted(arrow( pm.rect().translated(-pm.rect().width()/2, 0), true ));
        goto paint;
    case SP_MediaPlay:
        shape = arrow( pm.rect(), true );
        goto paint;
    case SP_MediaPause:
        shape = Shapes::unAboveBelow(pm.rect());
        goto paint;
    case SP_BrowserReload:
    {
        QRectF rect = pm.rect();
        const float s = rect.width()/3.0;
        QPointF c = rect.center();
        QRectF box(rect.x(), c.y()-s/2, s, s);

        shape.arcMoveTo(rect, 90);
        shape.arcTo(rect, 90, 90);
        shape.arcTo(box, 180, 180);
        shape.arcTo(rect.adjusted(s,s,-s,-s), 180, -90);
        box.moveTo(c.x()-s/2, rect.y());
        shape.arcTo(box, -90, 180);
        shape.closeSubpath();

        shape.arcMoveTo(rect, -90);
        shape.arcTo(rect, -90, 90);
        box.moveBottomRight(QPointF(rect.right(), c.y()+s/2));
        shape.arcTo(box, 0, 180);
        shape.arcTo(rect.adjusted(s,s,-s,-s), 0, -90);
        box.moveBottomRight(QPointF(c.x()+s/2, rect.bottom()));
        shape.arcTo(box, 90, 180);
        shape.closeSubpath();
/*
        int d5 = rect.height()/5;
        rect.setWidth( 4*rect.width()/5 );
        rect.setHeight( rect.height()/4 );
        rect.moveTop( rect.y() + d5 );
        shape.addRoundRect( rect, 50, 50 );
        rect.moveBottom( pm.rect().bottom() - d5 );
        rect.moveRight( pm.rect().right() );
        shape.addRoundRect( rect, 50, 50 );
        */
        goto paint;
    }
#endif
//         SP_MediaSkipForward 63  Icon indicating that media should skip forward.
//         SP_MediaSkipBackward    64  Icon indicating that media should skip backward.
//         SP_MediaSeekForward 65  Icon indicating that media should seek forward.
//         SP_MediaSeekBackward    66  Icon indicating that media should seek backward.

    case SP_DockWidgetCloseButton:
    case SP_TitleBarCloseButton:
//     case SP_BrowserStop:
//     case SP_MediaStop:
        shape = Shapes::close(pm.rect(), style);
        goto paint;
    case SP_TitleBarMinButton:
        shape = Shapes::min(pm.rect(), style);
        goto paint;
    case SP_TitleBarMaxButton:
        shape = Shapes::max(pm.rect(), style);
        goto paint;
    case SP_TitleBarMenuButton:
        shape = Shapes::menu(pm.rect(), false, style);
        goto paint;
    case SP_TitleBarShadeButton:
    case SP_TitleBarUnshadeButton:
        shape = Shapes::shade(pm.rect(), style);
        goto paint;
    case SP_TitleBarNormalButton:
        if (dock)
            shape = Shapes::dockControl(pm.rect(), dock->isFloating(), style);
        else
            shape = Shapes::restore(pm.rect(), style);
        goto paint;
    case SP_TitleBarContextHelpButton:
    {
        shape = Shapes::help(pm.rect(), style);
#if 0
        goto paint;
    case SP_ArrowDown:
    case SP_ArrowUp:
    case SP_FileDialogToParent: //  30
    {
        const float d = sz/2.0;
        shape = arrow( pm.rect()).subtracted(arrow( pm.rect().translated(d, 0)));
        painter.translate(d, d);
        painter.rotate(standardPixmap == SP_ArrowDown ? -90 : 90);
        painter.translate(-d, -d);
        goto paint;
    }
    case SP_MediaVolume:
    case SP_MediaVolumeMuted:
    {
        {
            QRectF r = pm.rect(); float d = r.width()/8.0;
            r.adjust(d,d,-d,-d);
            float x = r.width()/2.0, y = r.height()/2.0;
            shape.moveTo(0, r.y() + y);
            shape.quadTo(0,r.y() + y/2.0, x, r.y());
            shape.quadTo(r.width() - x/2.0, r.y()+y, x, r.bottom());
            shape.quadTo(0,r.bottom()-y/2.0, 0, r.y()+y);
            if (standardPixmap == SP_MediaVolume)
            {
                r = pm.rect();
                int st = -75, sw = 150;
                shape.moveTo(x, y);
                shape.arcTo(r, st, sw);
                r.adjust(d,d,-d,-d);
                shape.moveTo(x, y); shape.arcTo(r, st, sw);
                d = r.width()/8.0; r.adjust(d,d,-d,-d);
                shape.moveTo(x, y); shape.arcTo(r, st, sw);
                d = r.width()/8.0; r.adjust(d,d,-d,-d);
                shape.moveTo(x, y); shape.arcTo(r, st, sw);
                shape.closeSubpath();
            }
            else
            {
                shape.moveTo(r.topLeft());
                shape.lineTo(r.bottomRight());
                shape.moveTo(r.topRight());
                shape.lineTo(r.bottomLeft());
                shape.closeSubpath();
            }
        }
#endif
paint:
        const QColor c = Colors::mid(pal.color(fg), pal.color(bg), (sz > 16) ? 16 : 2, sunken ? 2 : (hover ? 4 : 2) );
        painter.setRenderHint ( QPainter::Antialiasing );
        if (sz > 16)
            painter.setPen(pal.color(bg));
        else
            painter.setPen(Qt::NoPen);
#if 0
        if (sz > 16)
            painter.setBrush( Gradients::brush( c, sz, Qt::Vertical, config.btn.gradient ) );
        else
#endif
            painter.setBrush(c);
        painter.drawPath(shape);
        break;
    }
    case SP_MessageBoxInformation:
    { //  9  The "information" icon
        const int bs = rect.height()/14;
        rect.adjust(bs,bs,-bs,-bs);
        setIconFont(painter, rect);
        painter.setRenderHint ( QPainter::Antialiasing );
        painter.setPen(QPen(Qt::white, bs));
        painter.setBrush(QColor(0,102,255));
        painter.drawEllipse(rect);
        painter.setPen(Qt::white);
        painter.drawText(rect, Qt::AlignHCenter | Qt::AlignBottom, "i");
        break;
    }
    case SP_MessageBoxWarning:
    { //  10  The "warning" icon
        int bs = rect.width()/14;
        rect.adjust(bs,bs,-bs,-bs);
        int hm = rect.x()+rect.width()/2;
        const QPoint points[3] = {
            QPoint(hm, rect.top()),
            QPoint(rect.left(), rect.bottom()),
            QPoint(rect.right(), rect.bottom())
        };
        setIconFont(painter, rect);
        painter.setRenderHint ( QPainter::Antialiasing);
        painter.setPen(QPen(QColor(227,173,0), bs, Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
        painter.setBrush(QColor(255,235,85));
        painter.drawPolygon(points, 3);
        painter.setPen(Qt::black);
        painter.drawText(rect, Qt::AlignHCenter | Qt::AlignBottom, "!");
        break;
    }
    case SP_MessageBoxCritical:
    { //  11  The "critical" icon
        const int bs = rect.height()/14;
        rect.adjust(bs,bs,-bs,-bs);
        setIconFont(painter, rect);
        painter.setRenderHint ( QPainter::Antialiasing );
        painter.setPen(QPen(Qt::white/*Color(226,8,0)*/, bs));
        painter.setBrush(QColor(156,15,15));
        painter.drawEllipse(rect);
        painter.setPen(Qt::white);
        painter.drawText(rect, Qt::AlignCenter, "X");
        break;
    }
    case SP_MessageBoxQuestion:
    { //  12  The "question" icon
        setIconFont(painter, rect, 1);
        QColor c = COLOR(WindowText); c.setAlpha(128);
        painter.setPen(c);
        painter.drawText(rect, Qt::AlignCenter, "?");
        break;
    }
//    case SP_DesktopIcon: //  13
//    case SP_TrashIcon: //  14
//    case SP_ComputerIcon: //  15
//    case SP_DriveFDIcon: //  16
//    case SP_DriveHDIcon: //  17
//    case SP_DriveCDIcon: //  18
//    case SP_DriveDVDIcon: //  19
//    case SP_DriveNetIcon: //  20
//     case SP_DirOpenIcon: //  21
//     case SP_DirClosedIcon: //  22
//     case SP_DirLinkIcon: //  23
//         break;
#if 0
    case SP_FileDialogNewFolder: //  31
    {
        const float t = rect.width()/8.0;
        const float half = rect.width()/2.0;
        const float third = rect.width()/3.0;
        painter.setPen(QPen(Colors::mid(pal.color(bg), pal.color(fg)), t/2.0));
        painter.setBrush(Qt::NoBrush);
        painter.setRenderHint ( QPainter::Antialiasing );
        QRectF mother(t/2.0,t/2.0,half,half);
        painter.drawArc(mother, 10*16,80*16);
        painter.drawArc(mother, 270*16,80*16);
        mother.adjust(t,t,-t,-t);
        painter.drawEllipse(mother);

        QRectF child(sz-third,sz-third,third,third);
        painter.drawArc(child, 75*16,110*16);
        child.adjust(t,t,-t,-t);
        painter.drawEllipse(child);

        QRectF baby(sz-third,t/2.0,third,third);
//         painter.drawArc(baby, 110*16,110*16);
//         baby.adjust(t,t,-t,-t);
        QPointF c = baby.center();
        painter.drawLine(baby.x(), c.y(), baby.right(), c.y());
        painter.drawLine(c.x(), baby.y(), c.x(), baby.bottom());

        painter.drawLine(mother.center(), child.center());
        break;
    }
//    case SP_FileIcon: //  24
//    case SP_FileLinkIcon: //  25

//    case SP_FileDialogStart: //  28
//    case SP_FileDialogEnd: //  29

    case SP_FileDialogDetailedView: //  32
    {
        const float t = rect.width()/8.0;
        painter.setPen(QPen(Colors::mid(pal.color(bg), pal.color(fg)), t));
        painter.setBrush(Qt::NoBrush);
        painter.setRenderHint ( QPainter::Antialiasing );
        float y = t;
        while (y <= rect.height()-t)
        {
            painter.drawPoint(t, y); painter.drawLine(3*t, y, rect.right(), y);
            y += 2*t;
        }
        break;
    }

//    case SP_FileDialogInfoView: //  33
//    case SP_FileDialogContentsView: //  34
    case SP_FileDialogListView: //  35
    {
        QRectF r(0,0,sz/3.0,sz/3.0);
        painter.setPen(Qt::NoPen);
        painter.setBrush(Colors::mid(pal.color(bg), pal.color(fg)));
        painter.setRenderHint(QPainter::Antialiasing);
        painter.drawEllipse(r);
        r.moveRight(pm.rect().right()); painter.drawEllipse(r);
        r.moveBottom(pm.rect().bottom()); painter.drawEllipse(r);
        r.moveLeft(pm.rect().left()); painter.drawEllipse(r);
        break;
    }
#endif
//     case SP_FileDialogBack: //  36
//         break;

    case SP_ToolBarHorizontalExtensionButton: //  26  Extension button for horizontal toolbars
    case SP_ToolBarVerticalExtensionButton: //  27  Extension button for vertical toolbars
        painter.setPen(Qt::NoPen);
        painter.setBrush(Colors::mid(COLOR(Window), COLOR(WindowText)));
        drawSolidArrow(standardPixmap == SP_ToolBarHorizontalExtensionButton ? Navi::E : Navi::S, rect, &painter);
        break;
    default:
        return QCommonStyle::standardPixmap ( standardPixmap, option, widget );
    }
    painter.end();
#if 0
    QPixmapCache::insert(key, pm);
#endif
    return pm;
}

#undef COLOR