doc/src/snippets/accessibilityslidersnippet.cpp
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 
       
    43 //! [0]
       
    44 QAccessibleSlider::QAccessibleSlider(QWidget *w)
       
    45 : QAccessibleAbstractSlider(w)
       
    46 {
       
    47     Q_ASSERT(slider());
       
    48     addControllingSignal(QLatin1String("valueChanged(int)"));
       
    49 }
       
    50 //! [0]
       
    51 
       
    52 QSlider *QAccessibleSlider::slider() const
       
    53 {
       
    54     return qobject_cast<QSlider*>(object());
       
    55 }
       
    56 
       
    57 //! [1]
       
    58 QRect QAccessibleSlider::rect(int child) const
       
    59 {
       
    60 //! [1]
       
    61     QRect rect;
       
    62     if (!slider()->isVisible())
       
    63         return rect;
       
    64     const QStyleOptionSlider option = qt_qsliderStyleOption(slider());
       
    65     QRect srect = slider()->style()->subControlRect(QStyle::CC_Slider, &option,
       
    66                                                     QStyle::SC_SliderHandle, slider());
       
    67 
       
    68 //! [2]
       
    69     switch (child) {
       
    70     case PageLeft:
       
    71         if (slider()->orientation() == Qt::Vertical)
       
    72             rect = QRect(0, 0, slider()->width(), srect.y());
       
    73         else
       
    74             rect = QRect(0, 0, srect.x(), slider()->height());
       
    75         break;
       
    76     case Position:
       
    77         rect = srect;
       
    78         break;
       
    79     case PageRight:
       
    80         if (slider()->orientation() == Qt::Vertical)
       
    81             rect = QRect(0, srect.y() + srect.height(), slider()->width(), slider()->height()- srect.y() - srect.height());
       
    82         else
       
    83             rect = QRect(srect.x() + srect.width(), 0, slider()->width() - srect.x() - srect.width(), slider()->height());
       
    84         break;
       
    85     default:
       
    86         return QAccessibleAbstractSlider::rect(child);
       
    87     }
       
    88 //! [2] //! [3]
       
    89 
       
    90     QPoint tp = slider()->mapToGlobal(QPoint(0,0));
       
    91     return QRect(tp.x() + rect.x(), tp.y() + rect.y(), rect.width(), rect.height());
       
    92 }
       
    93 //! [3]
       
    94 
       
    95 int QAccessibleSlider::childCount() const
       
    96 {
       
    97     if (!slider()->isVisible())
       
    98         return 0;
       
    99     return PageRight;
       
   100 }
       
   101 
       
   102 //! [4]
       
   103 QString QAccessibleSlider::text(Text t, int child) const
       
   104 {
       
   105     if (!slider()->isVisible())
       
   106         return QString();
       
   107     switch (t) {
       
   108     case Value:
       
   109         if (!child || child == 2)
       
   110             return QString::number(slider()->value());
       
   111         return QString();
       
   112     case Name:
       
   113         switch (child) {
       
   114         case PageLeft:
       
   115             return slider()->orientation() == Qt::Horizontal ?
       
   116                 QSlider::tr("Page left") : QSlider::tr("Page up");
       
   117         case Position:
       
   118             return QSlider::tr("Position");
       
   119         case PageRight:
       
   120             return slider()->orientation() == Qt::Horizontal ?
       
   121                 QSlider::tr("Page right") : QSlider::tr("Page down");
       
   122         }
       
   123         break;
       
   124     default:
       
   125         break;
       
   126     }
       
   127     return QAccessibleAbstractSlider::text(t, child);
       
   128 }
       
   129 //! [4]
       
   130 
       
   131 //! [5]
       
   132 QAccessible::Role QAccessibleSlider::role(int child) const
       
   133 {
       
   134     switch (child) {
       
   135     case PageLeft:
       
   136     case PageRight:
       
   137         return PushButton;
       
   138     case Position:
       
   139         return Indicator;
       
   140     default:
       
   141         return Slider;
       
   142     }
       
   143 }
       
   144 //! [5]
       
   145 
       
   146 //! [6]
       
   147 QAccessible::State QAccessibleSlider::state(int child) const
       
   148 {
       
   149     const State parentState = QAccessibleAbstractSlider::state(0);
       
   150 //! [6]
       
   151 
       
   152     if (child == 0)
       
   153         return parentState;
       
   154 
       
   155     // Inherit the Invisible state from parent.
       
   156     State state = parentState & QAccessible::Invisible;
       
   157 
       
   158     // Disable left/right if we are at the minimum/maximum.
       
   159     const QSlider * const slider = QAccessibleSlider::slider();
       
   160 //! [7]
       
   161     switch (child) {
       
   162     case PageLeft:
       
   163         if (slider->value() <= slider->minimum())
       
   164             state |= Unavailable;
       
   165         break;
       
   166     case PageRight:
       
   167         if (slider->value() >= slider->maximum())
       
   168             state |= Unavailable;
       
   169         break;
       
   170     case Position:
       
   171     default:
       
   172         break;
       
   173     }
       
   174 
       
   175     return state;
       
   176 }
       
   177 //! [7]
       
   178 
       
   179 int QAccessibleSlider::defaultAction(int child) const
       
   180 {
       
   181     switch (child) {
       
   182         case SliderSelf:
       
   183             return SetFocus;
       
   184         case PageLeft:
       
   185             return Press;
       
   186         case PageRight:
       
   187             return Press;
       
   188     }
       
   189 
       
   190     return 0;
       
   191 }
       
   192 
       
   193 // Name, Description, Value, Help, Accelerator
       
   194 static const char * const actionTexts[][5] =
       
   195 {
       
   196     {"Press", "Decreases the value of the slider", "", "", "Ctrl+L"},
       
   197     {"Press", "Increaces the value of the slider", "", "", "Ctrl+R"}
       
   198 };
       
   199 
       
   200 QString QAccessibleSlider::actionText(int action, Text text, int child) const
       
   201 {
       
   202     if (action != Press || child < 1 || child > 2)
       
   203         return QAccessibleAbstractSlider::actionText(action, text, child);        
       
   204 
       
   205     return actionTexts[child - 1][t];
       
   206 }
       
   207 
       
   208 bool QAccessibleSlider::doAction(int action, int child)
       
   209 {
       
   210     if (action != Press || child < 1 || child > 2)
       
   211         return false;
       
   212     
       
   213     if (child == PageLeft)
       
   214         slider()->setValue(slider()->value() - slider()->pageStep());
       
   215     else
       
   216         slider()->setValue(slider()->value() + slider()->pageStep());
       
   217 }
       
   218 
       
   219 QAccessibleAbstractSlider::QAccessibleAbstractSlider(QWidget *w, Role r)
       
   220     : QAccessibleWidgetEx(w, r)
       
   221 {
       
   222     Q_ASSERT(qobject_cast<QAbstractSlider *>(w));
       
   223 }
       
   224 
       
   225 QVariant QAccessibleAbstractSlider::invokeMethodEx(Method method, int child, const QVariantList &params)
       
   226 {
       
   227     switch (method) {
       
   228     case ListSupportedMethods: {
       
   229         QSet<QAccessible::Method> set;
       
   230         set << ListSupportedMethods;
       
   231         return qVariantFromValue(set | qvariant_cast<QSet<QAccessible::Method> >(
       
   232                     QAccessibleWidgetEx::invokeMethodEx(method, child, params)));
       
   233     }
       
   234     default:
       
   235         return QAccessibleWidgetEx::invokeMethodEx(method, child, params);
       
   236     }
       
   237 }
       
   238 
       
   239 QVariant QAccessibleAbstractSlider::currentValue()
       
   240 {
       
   241     return abstractSlider()->value();
       
   242 }
       
   243 
       
   244 void QAccessibleAbstractSlider::setCurrentValue(const QVariant &value)
       
   245 {
       
   246     abstractSlider()->setValue(value.toInt());
       
   247 }
       
   248 
       
   249 QVariant QAccessibleAbstractSlider::maximumValue()
       
   250 {
       
   251     return abstractSlider()->maximum();
       
   252 }
       
   253 
       
   254 QVariant QAccessibleAbstractSlider::minimumValue()
       
   255 {
       
   256     return abstractSlider()->minimum();
       
   257 }
       
   258 
       
   259 QAbstractSlider *QAccessibleAbstractSlider::abstractSlider() const
       
   260 {
       
   261     return static_cast<QAbstractSlider *>(object());
       
   262 }