File: frames.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 (337 lines) | stat: -rw-r--r-- 12,700 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
/*
 *   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 <QListView>
#include <QTableView>
#include <QTreeView>
#include <QTextEdit>

#if QT_VERSION >= 0x040500
#include <QStyleOptionFrameV3>
#endif

#include "visualframe.h"
#include "draw.h"

#include "debug.h"

#include <QtDebug>

static const QWidget *last_widget = 0;
static bool last_isSpecialFrame = false;

bool
Style::isSpecialFrame(const QWidget *widget)
{
    if (!widget)
        return false;
    if ( widget == last_widget )
        return last_isSpecialFrame;
    last_widget = widget;
    if (appType == Opera)
        return (last_isSpecialFrame = true);
    if IS_HTML_WIDGET
        return (last_isSpecialFrame = true);
    if (widget->minimumSize() == widget->maximumSize()) // koffice uses this... and miscalculates margins.
        return false;
    if (const QListView *view = qobject_cast<const QListView*>(widget))
    {
        last_isSpecialFrame = (view->viewMode() == QListView::IconMode || view->inherits("KCategorizedView"));
        return last_isSpecialFrame;
    }
    return (last_isSpecialFrame = bool(qobject_cast<const QTextEdit*>(widget)));
//     || (w->parentWidget() && w->parentWidget()->inherits("KateView")); // kate repaints the frame anyway
}

void
Style::drawFocusFrame(const QStyleOption *option, QPainter *painter, const QWidget *w) const
{
    if (option->state & State_Selected || option->state & State_MouseOver)
        return; // looks crap...
    if ( w && w->style() != this && w->inherits("QAbstractButton"))
        return; // from QtCssStyle...

    painter->save();
    painter->setBrush(Qt::NoBrush);
    painter->setPen(FCOLOR(Highlight));
    painter->drawLine(RECT.bottomLeft(), RECT.bottomRight());
    painter->restore();
}

void
Style::drawFrame(const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
    OPT_SUNKEN OPT_ENABLED OPT_FOCUS
    bool raised = option->state & State_Raised;

#if QT_VERSION >= 0x040500
    // lines via CE_ShapedFrame instead the ugly eventfilter override
    if HAVE_OPTION(v3frame, FrameV3)
    {
        if (v3frame->frameShape == QFrame::NoFrame)
            return;
        const bool v = (v3frame->frameShape == QFrame::VLine);
        if (v || v3frame->frameShape == QFrame::HLine)
        {
            shadows.line[v][sunken?Sunken:raised?Raised:Relief].render(RECT, painter);
            return;
        }
        if ( v3frame->frameShape == QFrame::Box )
            sunken = raised = false;
    }
#endif
    // all other frame kinds look the same
    if (!widget || (appType == GTK))
    {   // fallback, we cannot paint shaped frame contents
        if (sunken)
            shadows.fallback.render(RECT,painter);
        else if (raised)
            shadows.fallback.render(RECT,painter);
            // TODO !
//          shadows.raised.render(RECT,painter);
        else
        {
            //horizontal
            shadows.line[false][Sunken].render(RECT, painter, Tile::Full, false);
            shadows.line[false][Sunken].render(RECT, painter, Tile::Full, true);
            //vertical
            shadows.line[true][Sunken].render(RECT, painter, Tile::Full, false);
            shadows.line[true][Sunken].render(RECT, painter, Tile::Full, true);
        }
        return;
    }

    const QColor *brush = 0;
    QRect rect = RECT;
    bool fastFrame = false;
    if (qobject_cast<const QFrame*>(widget))
    {   // frame, can be killed unless...
        fastFrame = isSpecialFrame(widget);
        if (fastFrame)
        {   // ...TextEdit, ...
            if (const QAbstractItemView *view = qobject_cast<const QAbstractItemView*>(widget))
            if (view->viewport())
                brush = &view->viewport()->palette().color(view->viewport()->backgroundRole());
            if (!brush)
                brush = &PAL.color(QPalette::Base);
        }
        else
        {   // usually painted on visual frame, but...
            if (config.menu.shadow && widget->inherits("QComboBoxPrivateContainer"))
            {   // a decent combobox dropdown frame...
                SAVE_PEN;
                painter->setPen(Colors::mid(FCOLOR(Base),FCOLOR(Text),4,1));
                painter->drawRect(RECT.adjusted(0,0,-1,-1));
                RESTORE_PEN;
            }
            return;
        }
    }

    if (sunken)
        rect.setBottom(rect.bottom() - F(2));
    else if (raised)
        rect.adjust(F(2), F(1), -F(2), -F(4));
    else
        rect.adjust(F(2), F(2), -F(2), -F(2));

    const Tile::Set *mask = &masks.rect[false], *shadow = 0L;
    if (sunken)
        shadow = &shadows.sunken[false][isEnabled];
    else if (raised)
        shadow = &shadows.group;

    if (brush)
    {
        QRegion rgn = painter->clipRegion();
        bool hadClip = painter->hasClipping();
        painter->setClipRegion(QRegion(RECT) - RECT.adjusted(F(4),F(4),-F(4),-F(4)));
        mask->render(rect, painter, *brush);
        if (hadClip)
            painter->setClipRegion(rgn);
        else
            painter->setClipping(false);
    }
    if (shadow)
        shadow->render(RECT, painter);
    else
    {   // plain frame
        //horizontal
        shadows.line[0][Sunken].render(RECT, painter, Tile::Full, false);
        shadows.line[0][Sunken].render(RECT, painter, Tile::Full, true);
        //vertical
        shadows.line[1][Sunken].render(RECT, painter, Tile::Full, false);
        shadows.line[1][Sunken].render(RECT, painter, Tile::Full, true);
    }
    if (hasFocus)
    {
        rect = RECT;
        if (!fastFrame)
        if (const VisualFramePart* vfp = qobject_cast<const VisualFramePart*>(widget))
        {   // Looks somehow dull if a views header get's surrounded by the focus, ...but it
            // still should inside the frame: don't dare!
            Tile::setShape(Tile::Ring);
            QWidget *vHeader = 0, *hHeader = 0;
            if (const QTreeView* tv = qobject_cast<const QTreeView*>(vfp->frame()))
                hHeader = (QWidget*)tv->header();
            else if (const QTableView* table = qobject_cast<const QTableView*>(vfp->frame()))
            {
                hHeader = (QWidget*)table->horizontalHeader();
                vHeader = (QWidget*)table->verticalHeader();
            }
            if (vHeader && vHeader->isVisible())
            {
                Tile::setShape(Tile::shape() & ~Tile::Left);
                rect.setLeft(rect.left() + vHeader->width());
            }
            if (hHeader && hHeader->isVisible())
            {
                Tile::setShape(Tile::shape() & ~Tile::Top);
                rect.setTop(rect.top() + hHeader->height());
            }
        }
        lights.glow[false].render(rect, painter, FCOLOR(Highlight));
        Tile::reset();
    }
}

void
Style::drawGroupBox(const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const
{
    ASSURE_OPTION(groupBox, GroupBox);
    OPT_ENABLED

    // Frame
    if (groupBox->subControls & QStyle::SC_GroupBoxFrame)
    {
        QStyleOptionFrameV2 frame;
        frame.QStyleOption::operator=(*groupBox);
        frame.features = groupBox->features;
        frame.lineWidth = groupBox->lineWidth;
        frame.midLineWidth = groupBox->midLineWidth;
        frame.rect = subControlRect(CC_GroupBox, option, SC_GroupBoxFrame, widget);
        drawGroupBoxFrame(&frame, painter, widget);
    }

    // Title
    if ((groupBox->subControls & QStyle::SC_GroupBoxLabel) && !groupBox->text.isEmpty())
    {
        QColor textColor = groupBox->textColor;
        QPalette::ColorRole role = QPalette::WindowText;
        // NOTICE, WORKAROUND: groupBox->textColor is black by def. and should be invalid - but it's not
        // so assuming everything is optimized for a black on white world, we assume the
        // CUSTOM groupBox->textColor to be only valid if it's != Qt::black
        // THIS IS A HACK!
        if (textColor.isValid() && textColor != Qt::black)
        {
            if (!isEnabled)
                textColor.setAlpha(48);
            painter->setPen(textColor);
            role = QPalette::NoRole;
        }
        setTitleFont(painter, groupBox->text, RECT.width());
        QStyleOptionGroupBox copy = *groupBox;
        copy.fontMetrics = QFontMetrics(painter->font());
        QRect textRect = subControlRect(CC_GroupBox, &copy, SC_GroupBoxLabel, widget);
        drawItemText(painter, textRect, BESPIN_MNEMONIC, groupBox->palette, isEnabled, groupBox->text, role);
        if (groupBox->features & QStyleOptionFrameV2::Flat)
        {
            Tile::PosFlags pf = Tile::Center;
            if (option->direction == Qt::LeftToRight)
            {
                textRect.setLeft(RECT.left());
                textRect.setRight(textRect.right() + (RECT.right()-textRect.right())/2);
                pf |= Tile::Right;
            }
            else
            {
                textRect.setRight(RECT.right());
                textRect.setLeft(textRect.left() - (textRect.left() - RECT.left())/2);
                pf |= Tile::Left;
            }
            shadows.line[0][Sunken].render(textRect, painter, pf, true);

//             const int x = textRect.right();
//             textRect.setRight(RECT.right()); textRect.setLeft(x);
//             shadows.line[0][Sunken].render(textRect, painter, Tile::Center | Tile::Right, true);
        }
        else if (config.groupBoxMode)
        {
            const int x = textRect.width()/8;
            textRect.adjust(x,0,-x,0);
            shadows.line[0][Sunken].render(textRect, painter, Tile::Full, true);
        }
    }

    // Checkbox
    // TODO: doesn't hover - yet.
    if (groupBox->subControls & SC_GroupBoxCheckBox)
    {
        QStyleOptionButton box;
        box.QStyleOption::operator=(*groupBox);
        box.rect = subControlRect(CC_GroupBox, option, SC_GroupBoxCheckBox, widget);
//       box.state |= State_HasFocus; // focus to signal this to the user
        if (groupBox->activeSubControls & SC_GroupBoxCheckBox)
            box.state |= State_MouseOver;
        drawRadio(&box, painter, 0L);
    }
}

void
Style::drawGroupBoxFrame(const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
    const QStyleOptionFrameV2 *groupBox = qstyleoption_cast<const QStyleOptionFrameV2 *>(option);

    if (groupBox && groupBox->features == QStyleOptionFrameV2::Flat)
    {
        const QRect r = (option->direction == Qt::LeftToRight) ?
                        RECT.adjusted(RECT.width()/2,0,0,0) : RECT.adjusted(0,0,-RECT.width()/2,0);
        shadows.line[0][Sunken].render(r, painter, Tile::Full, true);
//         Tile::setShape(Tile::Bottom);
//         shadows.relief[true][false].render(RECT, painter);
//         Tile::reset();
        return;
    }
    if (config.groupBoxMode)
    {
        QRect rect = RECT.adjusted(F(4), F(2), -F(4), 0);
        rect.setHeight(qMin(2*F(32), RECT.height()));
        Tile::setShape(Tile::Full & ~Tile::Bottom);
        if ( config.groupBoxMode != 3 )
            masks.rect[false].render(rect, painter, Gradients::light(rect.height()) );
        rect.setBottom(RECT.bottom()-F(32));
        Tile::setShape(Tile::Full);
        shadows.group.render(RECT, painter);
        Tile::reset();
    }
    else
    {
#if BESPIN_ARGB_WINDOWS
        if (config.bg.opacity != 0xff)
            masks.rect[false].render( RECT.adjusted(0,0,0,-F(2)), painter, QColor(0,0,0,48) );
        else
#endif
            if (config.bg.mode == Scanlines && !(widget && widget->window() && widget->window()->testAttribute(Qt::WA_MacBrushedMetal)))
            masks.rect[false].render( RECT.adjusted(0,0,0,-F(2)), painter,
                                      Gradients::structure(FCOLOR(Window).darker(108)),
                                      widget ? widget->mapTo(widget->window(), RECT.topLeft()) : QPoint() );
        else
            masks.rect[false].render( RECT.adjusted(0,0,0,-F(2)), painter, FCOLOR(Window).darker(105));
        shadows.sunken[false][true].render(RECT, painter);
    }
}