Qt 4.8
qaccessible2.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qaccessible2.h"
43 #include "qapplication.h"
44 #include "qclipboard.h"
45 #include "qtextboundaryfinder.h"
46 
47 #ifndef QT_NO_ACCESSIBILITY
48 
50 
162  int *startOffset, int *endOffset, const QString& text)
163 {
165  switch (boundaryType) {
168  break;
171  break;
174  break;
175  default:
176  // in any other case return the whole line
177  *startOffset = 0;
178  *endOffset = text.length();
179  return text;
180  }
181 
182  QTextBoundaryFinder boundary(type, text);
183  boundary.setPosition(offset);
184 
185  if (!boundary.isAtBoundary()) {
186  boundary.toPreviousBoundary();
187  }
188  boundary.toPreviousBoundary();
189  *startOffset = boundary.position();
190  boundary.toNextBoundary();
191  *endOffset = boundary.position();
192 
193  return text.mid(*startOffset, *endOffset - *startOffset);
194 }
195 
200  int *startOffset, int *endOffset, const QString& text)
201 {
203  switch (boundaryType) {
206  break;
209  break;
212  break;
213  default:
214  // in any other case return the whole line
215  *startOffset = 0;
216  *endOffset = text.length();
217  return text;
218  }
219 
220  QTextBoundaryFinder boundary(type, text);
221  boundary.setPosition(offset);
222 
223  boundary.toNextBoundary();
224  *startOffset = boundary.position();
225  boundary.toNextBoundary();
226  *endOffset = boundary.position();
227 
228  return text.mid(*startOffset, *endOffset - *startOffset);
229 }
230 
235  int *startOffset, int *endOffset, const QString& text)
236 {
238  switch (boundaryType) {
241  break;
244  break;
247  break;
248  default:
249  // in any other case return the whole line
250  *startOffset = 0;
251  *endOffset = text.length();
252  return text;
253  }
254 
255  QTextBoundaryFinder boundary(type, text);
256  boundary.setPosition(offset);
257 
258  if (!boundary.isAtBoundary()) {
259  boundary.toPreviousBoundary();
260  }
261  *startOffset = boundary.position();
262  boundary.toNextBoundary();
263  *endOffset = boundary.position();
264 
265  return text.mid(*startOffset, *endOffset - *startOffset);
266 }
267 
269  QAccessibleInterface *accessibleInterface)
270  : iface(accessibleInterface)
271 {
272  Q_ASSERT(iface);
273 }
274 
275 #ifndef QT_NO_CLIPBOARD
276 static QString textForRange(QAccessibleInterface *iface, int startOffset, int endOffset)
277 {
278  return iface->text(QAccessible::Value, 0).mid(startOffset, endOffset - startOffset);
279 }
280 #endif
281 
282 void QAccessibleSimpleEditableTextInterface::copyText(int startOffset, int endOffset)
283 {
284 #ifdef QT_NO_CLIPBOARD
285  Q_UNUSED(startOffset);
286  Q_UNUSED(endOffset);
287 #else
288  QApplication::clipboard()->setText(textForRange(iface, startOffset, endOffset));
289 #endif
290 }
291 
292 void QAccessibleSimpleEditableTextInterface::deleteText(int startOffset, int endOffset)
293 {
295  txt.remove(startOffset, endOffset - startOffset);
296  iface->setText(QAccessible::Value, 0, txt);
297 }
298 
300 {
302  txt.insert(offset, text);
303  iface->setText(QAccessible::Value, 0, txt);
304 }
305 
306 void QAccessibleSimpleEditableTextInterface::cutText(int startOffset, int endOffset)
307 {
308 #ifdef QT_NO_CLIPBOARD
309  Q_UNUSED(startOffset);
310  Q_UNUSED(endOffset);
311 #else
312  QString sub = textForRange(iface, startOffset, endOffset);
313  deleteText(startOffset, endOffset);
315 #endif
316 }
317 
319 {
320 #ifdef QT_NO_CLIPBOARD
321  Q_UNUSED(offset);
322 #else
324  txt.insert(offset, QApplication::clipboard()->text());
325  iface->setText(QAccessible::Value, 0, txt);
326 #endif
327 }
328 
329 void QAccessibleSimpleEditableTextInterface::replaceText(int startOffset, int endOffset, const QString &text)
330 {
332  txt.replace(startOffset, endOffset - startOffset, text);
333  iface->setText(QAccessible::Value, 0, txt);
334 }
335 
337 
338 #endif // QT_NO_ACCESSIBILITY
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void cutText(int startOffset, int endOffset)
QString Q_GUI_EXPORT qTextAtOffsetFromString(int offset, QAccessible2::BoundaryType boundaryType, int *startOffset, int *endOffset, const QString &text)
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
int toPreviousBoundary()
Moves the QTextBoundaryFinder to the previous boundary position and returns that position.
QAccessibleSimpleEditableTextInterface(QAccessibleInterface *accessibleInterface)
The QString class provides a Unicode character string.
Definition: qstring.h:83
void setText(const QString &, Mode mode=Clipboard)
Copies text into the clipboard as plain text.
Definition: qclipboard.cpp:375
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int position() const
Returns the current position of the QTextBoundaryFinder.
static QString textForRange(QAccessibleInterface *iface, int startOffset, int endOffset)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static QClipboard * clipboard()
Returns a pointer to the application global clipboard.
QString Q_GUI_EXPORT qTextAfterOffsetFromString(int offset, QAccessible2::BoundaryType boundaryType, int *startOffset, int *endOffset, const QString &text)
void setPosition(int position)
Sets the current position of the QTextBoundaryFinder to position.
QString Q_GUI_EXPORT qTextBeforeOffsetFromString(int offset, QAccessible2::BoundaryType boundaryType, int *startOffset, int *endOffset, const QString &text)
void deleteText(int startOffset, int endOffset)
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
bool isAtBoundary() const
Returns true if the object's position() is currently at a valid text boundary.
void copyText(int startOffset, int endOffset)
The QAccessibleInterface class defines an interface that exposes information about accessible objects...
Definition: qaccessible.h:370
int toNextBoundary()
Moves the QTextBoundaryFinder to the next boundary position and returns that position.
virtual QString text(Text t, int child) const =0
Returns the value of the text property t of the object, or of the object's child if child is not 0...
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
QString & insert(int i, QChar c)
Definition: qstring.cpp:1671
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
void insertText(int offset, const QString &text)
The QTextBoundaryFinder class provides a way of finding Unicode text boundaries in a string...
virtual void setText(Text t, int child, const QString &text)=0
Sets the text property t of the object, or of the object's child if child is not 0, to text.
#define text
Definition: qobjectdefs.h:80
void replaceText(int startOffset, int endOffset, const QString &text)