1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 //! [0]
52     opt.initFrom(q);
53         if (down)
54         opt.state |= QStyle::State_Sunken;
55     if (tristate && noChange)
56         opt.state |= QStyle::State_NoChange;
57     else
58         opt.state |= checked ? QStyle::State_On :
59         QStyle::State_Off;
60     if (q->testAttribute(Qt::WA_Hover) &&  q->underMouse()) {
61         if (hovering)
62         opt.state |= QStyle::State_MouseOver;
63         else
64         opt.state &= ~QStyle::State_MouseOver;
65     }
66     opt.text = text;
67     opt.icon = icon;
68     opt.iconSize = q->iconSize();
69 //! [0]
70 
71 
72 //! [1]
73     state = QStyle::State_None;
74     if (widget->isEnabled())
75         state |= QStyle::State_Enabled;
76     if (widget->hasFocus())
77         state |= QStyle::State_HasFocus;
78     if (widget->window()->testAttribute(Qt::WA_KeyboardFocusChange))
79         state |= QStyle::State_KeyboardFocusChange;
80     if (widget->underMouse())
81         state |= QStyle::State_MouseOver;
82     if (widget->window()->isActiveWindow())
83         state |= QStyle::State_Active;
84 #ifdef QT_KEYPAD_NAVIGATION
85     if (widget->hasEditFocus())
86         state |= QStyle::State_HasEditFocus;
87 #endif
88 
89     direction = widget->layoutDirection();
90     rect = widget->rect();
91     palette = widget->palette();
92     fontMetrics = widget->fontMetrics();
93 //! [1]
94 
95 
96 //! [2]
97     QStylePainter p(this);
98     QStyleOptionButton opt = d->getStyleOption();
99     p.drawControl(QStyle::CE_CheckBox, opt);
100 //! [2]
101 
102 
103 //! [3]
104     QStyleOptionButton subopt = *btn;
105     subopt.rect = subElementRect(SE_CheckBoxIndicator, btn, widget);
106     drawPrimitive(PE_IndicatorCheckBox, &subopt, p, widget);
107     subopt.rect = subElementRect(SE_CheckBoxContents, btn, widget);
108     drawControl(CE_CheckBoxLabel, &subopt, p, widget);
109 
110     if (btn->state & State_HasFocus) {
111         QStyleOptionFocusRect fropt;
112         fropt.QStyleOption::operator=(*btn);
113         fropt.rect = subElementRect(SE_CheckBoxFocusRect, btn, widget);
114         drawPrimitive(PE_FrameFocusRect, &fropt, p, widget);
115     }
116 //! [3]
117 
118 
119 //! [4]
120     const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt);
121     uint alignment = visualAlignment(btn->direction, Qt::AlignLeft | Qt::AlignVCenter);
122 
123     if (!styleHint(SH_UnderlineShortcut, btn, widget))
124         alignment |= Qt::TextHideMnemonic;
125     QPixmap pix;
126     QRect textRect = btn->rect;
127     if (!btn->icon.isNull()) {
128         pix = btn->icon.pixmap(btn->iconSize, btn->state & State_Enabled ? QIcon::Normal : QIcon::Disabled);
129         drawItemPixmap(p, btn->rect, alignment, pix);
130         if (btn->direction == Qt::RightToLeft)
131             textRect.setRight(textRect.right() - btn->iconSize.width() - 4);
132         else
133             textRect.setLeft(textRect.left() + btn->iconSize.width() + 4);
134     }
135     if (!btn->text.isEmpty()){
136         drawItemText(p, textRect, alignment | Qt::TextShowMnemonic,
137             btn->palette, btn->state & State_Enabled, btn->text, QPalette::WindowText);
138     }
139 //! [4]
140