Update copyright headers
[qt:qt.git] / doc / src / snippets / accessibilityslidersnippet.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41
42 //! [0]
43 QAccessibleSlider::QAccessibleSlider(QWidget *w)
44 : QAccessibleAbstractSlider(w)
45 {
46     Q_ASSERT(slider());
47     addControllingSignal(QLatin1String("valueChanged(int)"));
48 }
49 //! [0]
50
51 QSlider *QAccessibleSlider::slider() const
52 {
53     return qobject_cast<QSlider*>(object());
54 }
55
56 //! [1]
57 QRect QAccessibleSlider::rect(int child) const
58 {
59 //! [1]
60     QRect rect;
61     if (!slider()->isVisible())
62         return rect;
63     const QStyleOptionSlider option = qt_qsliderStyleOption(slider());
64     QRect srect = slider()->style()->subControlRect(QStyle::CC_Slider, &option,
65                                                     QStyle::SC_SliderHandle, slider());
66
67 //! [2]
68     switch (child) {
69     case PageLeft:
70         if (slider()->orientation() == Qt::Vertical)
71             rect = QRect(0, 0, slider()->width(), srect.y());
72         else
73             rect = QRect(0, 0, srect.x(), slider()->height());
74         break;
75     case Position:
76         rect = srect;
77         break;
78     case PageRight:
79         if (slider()->orientation() == Qt::Vertical)
80             rect = QRect(0, srect.y() + srect.height(), slider()->width(), slider()->height()- srect.y() - srect.height());
81         else
82             rect = QRect(srect.x() + srect.width(), 0, slider()->width() - srect.x() - srect.width(), slider()->height());
83         break;
84     default:
85         return QAccessibleAbstractSlider::rect(child);
86     }
87 //! [2] //! [3]
88
89     QPoint tp = slider()->mapToGlobal(QPoint(0,0));
90     return QRect(tp.x() + rect.x(), tp.y() + rect.y(), rect.width(), rect.height());
91 }
92 //! [3]
93
94 int QAccessibleSlider::childCount() const
95 {
96     if (!slider()->isVisible())
97         return 0;
98     return PageRight;
99 }
100
101 //! [4]
102 QString QAccessibleSlider::text(Text t, int child) const
103 {
104     if (!slider()->isVisible())
105         return QString();
106     switch (t) {
107     case Value:
108         if (!child || child == 2)
109             return QString::number(slider()->value());
110         return QString();
111     case Name:
112         switch (child) {
113         case PageLeft:
114             return slider()->orientation() == Qt::Horizontal ?
115                 QSlider::tr("Page left") : QSlider::tr("Page up");
116         case Position:
117             return QSlider::tr("Position");
118         case PageRight:
119             return slider()->orientation() == Qt::Horizontal ?
120                 QSlider::tr("Page right") : QSlider::tr("Page down");
121         }
122         break;
123     default:
124         break;
125     }
126     return QAccessibleAbstractSlider::text(t, child);
127 }
128 //! [4]
129
130 //! [5]
131 QAccessible::Role QAccessibleSlider::role(int child) const
132 {
133     switch (child) {
134     case PageLeft:
135     case PageRight:
136         return PushButton;
137     case Position:
138         return Indicator;
139     default:
140         return Slider;
141     }
142 }
143 //! [5]
144
145 //! [6]
146 QAccessible::State QAccessibleSlider::state(int child) const
147 {
148     const State parentState = QAccessibleAbstractSlider::state(0);
149 //! [6]
150
151     if (child == 0)
152         return parentState;
153
154     // Inherit the Invisible state from parent.
155     State state = parentState & QAccessible::Invisible;
156
157     // Disable left/right if we are at the minimum/maximum.
158     const QSlider * const slider = QAccessibleSlider::slider();
159 //! [7]
160     switch (child) {
161     case PageLeft:
162         if (slider->value() <= slider->minimum())
163             state |= Unavailable;
164         break;
165     case PageRight:
166         if (slider->value() >= slider->maximum())
167             state |= Unavailable;
168         break;
169     case Position:
170     default:
171         break;
172     }
173
174     return state;
175 }
176 //! [7]
177
178 int QAccessibleSlider::defaultAction(int child) const
179 {
180     switch (child) {
181         case SliderSelf:
182             return SetFocus;
183         case PageLeft:
184             return Press;
185         case PageRight:
186             return Press;
187     }
188
189     return 0;
190 }
191
192 // Name, Description, Value, Help, Accelerator
193 static const char * const actionTexts[][5] =
194 {
195     {"Press", "Decreases the value of the slider", "", "", "Ctrl+L"},
196     {"Press", "Increaces the value of the slider", "", "", "Ctrl+R"}
197 };
198
199 QString QAccessibleSlider::actionText(int action, Text text, int child) const
200 {
201     if (action != Press || child < 1 || child > 2)
202         return QAccessibleAbstractSlider::actionText(action, text, child);        
203
204     return actionTexts[child - 1][t];
205 }
206
207 bool QAccessibleSlider::doAction(int action, int child)
208 {
209     if (action != Press || child < 1 || child > 2)
210         return false;
211     
212     if (child == PageLeft)
213         slider()->setValue(slider()->value() - slider()->pageStep());
214     else
215         slider()->setValue(slider()->value() + slider()->pageStep());
216 }
217
218 QAccessibleAbstractSlider::QAccessibleAbstractSlider(QWidget *w, Role r)
219     : QAccessibleWidgetEx(w, r)
220 {
221     Q_ASSERT(qobject_cast<QAbstractSlider *>(w));
222 }
223
224 QVariant QAccessibleAbstractSlider::invokeMethodEx(Method method, int child, const QVariantList &params)
225 {
226     switch (method) {
227     case ListSupportedMethods: {
228         QSet<QAccessible::Method> set;
229         set << ListSupportedMethods;
230         return QVariant::fromValue(set | qvariant_cast<QSet<QAccessible::Method> >(
231                     QAccessibleWidgetEx::invokeMethodEx(method, child, params)));
232     }
233     default:
234         return QAccessibleWidgetEx::invokeMethodEx(method, child, params);
235     }
236 }
237
238 QVariant QAccessibleAbstractSlider::currentValue()
239 {
240     return abstractSlider()->value();
241 }
242
243 void QAccessibleAbstractSlider::setCurrentValue(const QVariant &value)
244 {
245     abstractSlider()->setValue(value.toInt());
246 }
247
248 QVariant QAccessibleAbstractSlider::maximumValue()
249 {
250     return abstractSlider()->maximum();
251 }
252
253 QVariant QAccessibleAbstractSlider::minimumValue()
254 {
255     return abstractSlider()->minimum();
256 }
257
258 QAbstractSlider *QAccessibleAbstractSlider::abstractSlider() const
259 {
260     return static_cast<QAbstractSlider *>(object());
261 }