doc/src/snippets/code/doc_src_styles.qdoc
branchRCL_3
changeset 7 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 //! [0]
       
    43     opt.initFrom(q);
       
    44         if (down)
       
    45         opt.state |= QStyle::State_Sunken;
       
    46     if (tristate && noChange)
       
    47         opt.state |= QStyle::State_NoChange;
       
    48     else
       
    49         opt.state |= checked ? QStyle::State_On :
       
    50         QStyle::State_Off;
       
    51     if (q->testAttribute(Qt::WA_Hover) &&  q->underMouse()) {
       
    52         if (hovering)
       
    53         opt.state |= QStyle::State_MouseOver;
       
    54         else
       
    55         opt.state &= ~QStyle::State_MouseOver;
       
    56     }
       
    57     opt.text = text;
       
    58     opt.icon = icon;
       
    59     opt.iconSize = q->iconSize();
       
    60 //! [0]
       
    61 
       
    62 
       
    63 //! [1]
       
    64     state = QStyle::State_None;
       
    65     if (widget->isEnabled())
       
    66         state |= QStyle::State_Enabled;
       
    67     if (widget->hasFocus())
       
    68         state |= QStyle::State_HasFocus;
       
    69     if (widget->window()->testAttribute(Qt::WA_KeyboardFocusChange))
       
    70         state |= QStyle::State_KeyboardFocusChange;
       
    71     if (widget->underMouse())
       
    72         state |= QStyle::State_MouseOver;
       
    73     if (widget->window()->isActiveWindow())
       
    74         state |= QStyle::State_Active;
       
    75 #ifdef Q_WS_MAC
       
    76     extern bool qt_mac_can_clickThrough(const QWidget *w); //qwidget_mac.cpp
       
    77     if (!(state & QStyle::State_Active) && !qt_mac_can_clickThrough(widget))
       
    78         state &= ~QStyle::State_Enabled;
       
    79 #endif
       
    80 #ifdef QT_KEYPAD_NAVIGATION
       
    81     if (widget->hasEditFocus())
       
    82         state |= QStyle::State_HasEditFocus;
       
    83 #endif
       
    84 
       
    85     direction = widget->layoutDirection();
       
    86     rect = widget->rect();
       
    87     palette = widget->palette();
       
    88     fontMetrics = widget->fontMetrics();
       
    89 //! [1]
       
    90 
       
    91 
       
    92 //! [2]
       
    93     QStylePainter p(this);
       
    94     QStyleOptionButton opt = d->getStyleOption();
       
    95     p.drawControl(QStyle::CE_CheckBox, opt);
       
    96 //! [2]
       
    97 
       
    98 
       
    99 //! [3]
       
   100     QStyleOptionButton subopt = *btn;
       
   101     subopt.rect = subElementRect(SE_CheckBoxIndicator, btn, widget);
       
   102     drawPrimitive(PE_IndicatorCheckBox, &subopt, p, widget);
       
   103     subopt.rect = subElementRect(SE_CheckBoxContents, btn, widget);
       
   104     drawControl(CE_CheckBoxLabel, &subopt, p, widget);
       
   105 
       
   106     if (btn->state & State_HasFocus) {
       
   107         QStyleOptionFocusRect fropt;
       
   108         fropt.QStyleOption::operator=(*btn);
       
   109         fropt.rect = subElementRect(SE_CheckBoxFocusRect, btn, widget);
       
   110         drawPrimitive(PE_FrameFocusRect, &fropt, p, widget);
       
   111     }
       
   112 //! [3]
       
   113 
       
   114 
       
   115 //! [4]
       
   116     const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt);
       
   117     uint alignment = visualAlignment(btn->direction, Qt::AlignLeft | Qt::AlignVCenter);
       
   118 
       
   119     if (!styleHint(SH_UnderlineShortcut, btn, widget))
       
   120         alignment |= Qt::TextHideMnemonic;
       
   121     QPixmap pix;
       
   122     QRect textRect = btn->rect;
       
   123     if (!btn->icon.isNull()) {
       
   124         pix = btn->icon.pixmap(btn->iconSize, btn->state & State_Enabled ? QIcon::Normal : QIcon::Disabled);
       
   125         drawItemPixmap(p, btn->rect, alignment, pix);
       
   126         if (btn->direction == Qt::RightToLeft)
       
   127             textRect.setRight(textRect.right() - btn->iconSize.width() - 4);
       
   128         else
       
   129             textRect.setLeft(textRect.left() + btn->iconSize.width() + 4);
       
   130     }
       
   131     if (!btn->text.isEmpty()){
       
   132         drawItemText(p, textRect, alignment | Qt::TextShowMnemonic,
       
   133             btn->palette, btn->state & State_Enabled, btn->text, QPalette::WindowText);
       
   134     }
       
   135 //! [4]