• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • view
kateviewaccessible.h
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2010 Sebastian Sauer <mail@dipe.org>
3  Copyright (C) 2012 Frederik Gladhorn <gladhorn@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #ifndef _KATE_VIEW_ACCESSIBLE_
22 #define _KATE_VIEW_ACCESSIBLE_
23 #ifndef QT_NO_ACCESSIBILITY
24 #if QT_VERSION >= 0x040800
25 
26 #include "kateviewinternal.h"
27 #include "katetextcursor.h"
28 #include "katedocument.h"
29 
30 #include <QtGui/QAccessible>
31 #include <QtGui/qaccessible2.h>
32 #include <klocale.h>
33 #include <qaccessiblewidget.h>
34 
35 
36 
37 QString Q_GUI_EXPORT qTextBeforeOffsetFromString(int offset, QAccessible2::BoundaryType boundaryType,
38  int *startOffset, int *endOffset, const QString& text);
39 QString Q_GUI_EXPORT qTextAtOffsetFromString(int offset, QAccessible2::BoundaryType boundaryType,
40  int *startOffset, int *endOffset, const QString& text);
41 QString Q_GUI_EXPORT qTextAfterOffsetFromString(int offset, QAccessible2::BoundaryType boundaryType,
42  int *startOffset, int *endOffset, const QString& text);
43 
44 
60 class KateCursorAccessible : public QAccessibleInterface
61 {
62  public:
63 
64  enum { ChildId = 1 };
65 
66  explicit KateCursorAccessible(KateViewInternal *view)
67  : QAccessibleInterface()
68  , m_view(view)
69  {
70  }
71 
72  virtual ~KateCursorAccessible()
73  {
74  }
75 
76  virtual QString actionText(int action, QAccessible::Text t, int) const
77  {
78  if (t == QAccessible::Name) {
79  switch(action) {
80  case 0: return i18n("Move To...");
81  case 1: return i18n("Move Left");
82  case 2: return i18n("Move Right");
83  case 3: return i18n("Move Up");
84  case 4: return i18n("Move Down");
85  default: break;
86  }
87  }
88  return QString();
89  }
90 
91  virtual bool doAction(int action, int, const QVariantList & params = QVariantList() )
92  {
93  bool ok = true;
94  KTextEditor::Cursor c = m_view->getCursor();
95  switch(action) {
96  case 0: {
97  if (params.count() < 2) ok = false;
98  const int line = ok ? params[0].toInt(&ok) : 0;
99  const int column = ok ? params[1].toInt(&ok) : 0;
100  if (ok) c.setPosition(line, column);
101  } break;
102  case 1: c.setPosition(c.line(), c.column() - 1); break;
103  case 2: c.setPosition(c.line(), c.column() + 1); break;
104  case 3: c.setPosition(c.line() - 1, c.column()); break;
105  case 4: c.setPosition(c.line() + 1, c.column()); break;
106  default: ok = false; break;
107  }
108 
109  return ok;
110  }
111 
112  virtual int userActionCount(int) const
113  {
114  return 5;
115  }
116 
117  virtual int childAt(int, int) const
118  {
119  return 0;
120  }
121 
122  virtual int childCount() const
123  {
124  return 0;
125  }
126 
127  virtual int indexOfChild(const QAccessibleInterface *) const
128  {
129  return 0;
130  }
131 
132  virtual bool isValid() const
133  {
134  KTextEditor::Cursor c = m_view->getCursor();
135  return c.isValid();
136  }
137 
138  virtual int navigate(QAccessible::RelationFlag relation, int entry, QAccessibleInterface **target) const
139  {
140  Q_UNUSED(relation);
141  Q_UNUSED(entry);
142  *target = 0;
143  return -1;
144  }
145 
146  virtual QObject* object() const
147  {
148  return m_view;
149  }
150 
151  virtual QRect rect(int) const
152  {
153  // return the exact position of the cursor with no width and height defined,
154  QPoint p = m_view->view()->cursorPositionCoordinates();
155  return QRect(m_view->mapToGlobal(p), QSize(0,0));
156  }
157 
158  virtual QAccessible::Relation relationTo(int child, const QAccessibleInterface *other, int otherChild) const
159  {
160  Q_UNUSED(child);
161  Q_UNUSED(other);
162  Q_UNUSED(otherChild);
163  return QAccessible::Unrelated;
164  }
165 
166  virtual QAccessible::Role role(int) const
167  {
168  return QAccessible::Cursor;
169  }
170 
171  virtual void setText(QAccessible::Text, int, const QString &)
172  {
173  }
174 
175  virtual QAccessible::State state(int) const
176  {
177  QAccessible::State s = QAccessible::Focusable | QAccessible::Focused;
178  return s;
179  }
180 
181  virtual QString text(QAccessible::Text, int) const
182  {
183  return QString();
184  }
185 
186  private:
187  KateViewInternal *m_view;
188 };
189 
196 class KateViewAccessible : public QAccessibleWidgetEx, public QAccessibleTextInterface, public QAccessibleSimpleEditableTextInterface
197 {
198  Q_ACCESSIBLE_OBJECT
199 
200  public:
201  explicit KateViewAccessible(KateViewInternal *view)
202  : QAccessibleWidgetEx(view), QAccessibleSimpleEditableTextInterface(this)
203  , m_cursor(new KateCursorAccessible(view))
204  {
205  }
206 
207  virtual ~KateViewAccessible()
208  {
209  }
210 
211  virtual QString actionText(int action, QAccessible::Text t, int child) const
212  {
213  if (child == KateCursorAccessible::ChildId)
214  return m_cursor->actionText(action, t, 0);
215  return QString();
216  }
217 
218  virtual bool doAction(int action, int child, const QVariantList & params = QVariantList() )
219  {
220  if (child == KateCursorAccessible::ChildId)
221  return m_cursor->doAction(action, 0, params);
222  return false;
223  }
224 
225  virtual int userActionCount(int child) const
226  {
227  if (child == KateCursorAccessible::ChildId)
228  return m_cursor->userActionCount(0);
229  return 0;
230  }
231 
232  virtual int childAt(int x, int y) const
233  {
234  Q_UNUSED(x);
235  Q_UNUSED(y);
236  return 0;
237  }
238 
239  virtual int childCount() const
240  {
241  return 1;
242  }
243 
244  virtual int indexOfChild(const QAccessibleInterface *child) const
245  {
246  return dynamic_cast<const KateCursorAccessible*>(child) ? KateCursorAccessible::ChildId : 0;
247  }
248 
249  virtual int navigate(QAccessible::RelationFlag relation, int entry, QAccessibleInterface **target) const
250  {
251  if ((relation == QAccessible::Child || QAccessible::FocusChild) && entry == KateCursorAccessible::ChildId) {
252  *target = new KateCursorAccessible(view());
253  return KateCursorAccessible::ChildId;
254  }
255  *target = 0;
256  return -1;
257  }
258 
259  virtual QRect rect(int child) const
260  {
261  if (child == KateCursorAccessible::ChildId)
262  return m_cursor->rect(0);
263  return QRect(view()->mapToGlobal(QPoint(0,0)), view()->size());
264  }
265 
266  virtual QAccessible::Relation relationTo(int child, const QAccessibleInterface *other, int otherChild) const
267  {
268  Q_UNUSED(child);
269  Q_UNUSED(other);
270  Q_UNUSED(otherChild);
271  return QAccessible::Unrelated;
272  }
273 
274  virtual QAccessible::Role role(int child) const
275  {
276  if (child == KateCursorAccessible::ChildId)
277  return QAccessible::Cursor;
278  return QAccessible::EditableText;
279  }
280 
281  virtual void setText(QAccessible::Text t, int child, const QString & text)
282  {
283  if (t == QAccessible::Value && child == 0 && view()->view()->document())
284  view()->view()->document()->setText(text);
285  }
286 
287  virtual QAccessible::State state(int child) const
288  {
289  if (child == KateCursorAccessible::ChildId)
290  return m_cursor->state(0);
291 
292  QAccessible::State s = QAccessibleWidgetEx::state(0);
293  s |= QAccessible::Focusable;
294 
295  if (view()->hasFocus())
296  s |= QAccessible::Focused;
297 
298  s |= QAccessible::HasInvokeExtension;
299  return s;
300  }
301 
302  virtual QString text(QAccessible::Text t, int child) const
303  {
304  if (child == KateCursorAccessible::ChildId)
305  return m_cursor->text(t, 0);
306  QString s;
307  if (view()->view()->document()) {
308  if (t == QAccessible::Name)
309  s = view()->view()->document()->documentName();
310  if (t == QAccessible::Value)
311  s = view()->view()->document()->text();
312  }
313  return s;
314  }
315 
316  virtual int characterCount()
317  {
318  return view()->view()->document()->text().size();
319  }
320 
321  virtual void addSelection(int startOffset, int endOffset)
322  {
323  KTextEditor::Range range;
324  range.setRange(cursorFromInt(startOffset), cursorFromInt(endOffset));
325  view()->view()->setSelection(range);
326  view()->view()->setCursorPosition(cursorFromInt(endOffset));
327  }
328 
329  virtual QString attributes(int offset, int* startOffset, int* endOffset)
330  {
331  Q_UNUSED(offset);
332  *startOffset = 0;
333  *endOffset = characterCount();
334  return QString();
335  }
336  virtual QRect characterRect(int offset, QAccessible2::CoordinateType /*coordType*/)
337  {
338  KTextEditor::Cursor c = cursorFromInt(offset);
339  if (!c.isValid())
340  return QRect();
341  QPoint p = view()->view()->cursorToCoordinate(c);
342  KTextEditor::Cursor endCursor = KTextEditor::Cursor(c.line(), c.column() + 1);
343  QPoint size = view()->view()->cursorToCoordinate(endCursor) - p;
344  return QRect(view()->mapToGlobal(p), QSize(size.x(), size.y()));
345  }
346  virtual int cursorPosition()
347  {
348  KTextEditor::Cursor c = view()->getCursor();
349  return positionFromCursor(c);
350  }
351  virtual int offsetAtPoint(const QPoint& /*point*/, QAccessible2::CoordinateType /*coordType*/)
352  {
353  return 0;
354  }
355  virtual void removeSelection(int selectionIndex)
356  {
357  if (selectionIndex != 0)
358  return;
359  view()->view()->clearSelection();
360  }
361  virtual void scrollToSubstring(int /*startIndex*/, int /*endIndex*/)
362  {
363  // FIXME
364  }
365  virtual void selection(int selectionIndex, int *startOffset, int *endOffset)
366  {
367  if (selectionIndex != 0 || !view()->view()->selection()) {
368  *startOffset = 0;
369  *endOffset = 0;
370  return;
371  }
372  KTextEditor::Range range = view()->view()->selectionRange();
373  *startOffset = positionFromCursor(range.start());
374  *endOffset = positionFromCursor(range.end());
375  }
376 
377  virtual int selectionCount()
378  {
379  return view()->view()->selection() ? 1 : 0;
380  }
381  virtual void setCursorPosition(int position)
382  {
383  view()->view()->setCursorPosition(cursorFromInt(position));
384  }
385  virtual void setSelection(int selectionIndex, int startOffset, int endOffset)
386  {
387  if (selectionIndex != 0)
388  return;
389  KTextEditor::Range range = KTextEditor::Range(cursorFromInt(startOffset), cursorFromInt(endOffset));
390  view()->view()->setSelection(range);
391  }
392  virtual QString text(int startOffset, int endOffset)
393  {
394  if (startOffset > endOffset)
395  return QString();
396  return view()->view()->document()->text().mid(startOffset, endOffset - startOffset);
397  }
398 
399  virtual QString textAfterOffset(int offset, QAccessible2::BoundaryType boundaryType, int* startOffset, int* endOffset)
400  {
401  if (boundaryType == QAccessible2::LineBoundary)
402  return textLine(1, offset + 1, startOffset, endOffset);
403 
404  const QString text = view()->view()->document()->text();
405  return qTextAfterOffsetFromString(offset, boundaryType, startOffset, endOffset, text);
406  }
407  virtual QString textAtOffset(int offset, QAccessible2::BoundaryType boundaryType, int* startOffset, int* endOffset)
408  {
409  if (boundaryType == QAccessible2::LineBoundary)
410  return textLine(0, offset, startOffset, endOffset);
411 
412  const QString text = view()->view()->document()->text();
413  return qTextAtOffsetFromString(offset, boundaryType, startOffset, endOffset, text);
414  }
415  virtual QString textBeforeOffset(int offset, QAccessible2::BoundaryType boundaryType, int* startOffset, int* endOffset)
416  {
417  if (boundaryType == QAccessible2::LineBoundary)
418  return textLine(-1, offset - 1, startOffset, endOffset);
419 
420  const QString text = view()->view()->document()->text();
421  return qTextBeforeOffsetFromString(offset, boundaryType, startOffset, endOffset, text);
422  }
423 
424  virtual QVariant invokeMethodEx(Method, int, const QVariantList&)
425  { return QVariant(); }
426 
427  private:
428  KateViewInternal *view() const
429  {
430  return static_cast<KateViewInternal*>(object());
431  }
432 
433  KTextEditor::Cursor cursorFromInt(int position) const
434  {
435  int line = 0;
436  for (;;) {
437  const QString lineString = view()->view()->document()->line(line);
438  if (position > lineString.length()) {
439  // one is the newline
440  position -= lineString.length() + 1;
441  ++line;
442  } else {
443  break;
444  }
445  }
446  return KTextEditor::Cursor(line, position);
447  }
448 
449  int positionFromCursor(const KTextEditor::Cursor &cursor) const
450  {
451  int pos = 0;
452  for (int line = 0; line < cursor.line(); ++line) {
453  // length of the line plus newline
454  pos += view()->view()->document()->line(line).size() + 1;
455  }
456  pos += cursor.column();
457 
458  return pos;
459  }
460 
461  QString textLine(int shiftLines, int offset, int* startOffset, int* endOffset) const
462  {
463  KTextEditor::Cursor pos = cursorFromInt(offset);
464  pos.setColumn(0);
465  if (shiftLines)
466  pos.setLine(pos.line() + shiftLines);
467  *startOffset = positionFromCursor(pos);
468  QString line = view()->view()->document()->line(pos.line()) + '\n';
469  *endOffset = *startOffset + line.length();
470  return line;
471  }
472 
473  KateCursorAccessible *m_cursor;
474 };
475 
480 QAccessibleInterface* accessibleInterfaceFactory(const QString &key, QObject *object)
481 {
482  Q_UNUSED(key)
483  //if (key == QLatin1String("KateViewInternal"))
484  if (KateViewInternal *view = qobject_cast<KateViewInternal*>(object))
485  return new KateViewAccessible(view);
486  return 0;
487 }
488 
489 #endif
490 #endif
491 
492 #endif
QAccessibleInterface::indexOfChild
virtual int indexOfChild(const QAccessibleInterface *child) const =0
Kate::Script::i18n
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
Definition: katescripthelpers.cpp:186
QAccessibleInterface::doAction
virtual bool doAction(int action, int child, const QVariantList &params)=0
QAccessibleInterface::childCount
virtual int childCount() const =0
QAccessibleInterface::setText
virtual void setText(Text t, int child, const QString &text)=0
QAccessibleInterface
QAccessibleInterface::rect
virtual QRect rect(int child) const =0
QAccessibleInterface::text
virtual QString text(Text t, int child) const =0
QString::size
int size() const
katedocument.h
QAccessibleInterface::state
virtual State state(int child) const =0
QPoint
QRect
QAccessible::Relation
typedef Relation
QObject
QAccessibleInterface::userActionCount
virtual int userActionCount(int child) const =0
KateViewInternal
Definition: kateviewinternal.h:58
kateviewinternal.h
QString
QAccessibleInterface::isValid
virtual bool isValid() const =0
QSize
QAccessibleInterface::role
virtual Role role(int child) const =0
QAccessible::State
typedef State
QAccessibleInterface::relationTo
virtual Relation relationTo(int child, const QAccessibleInterface *other, int otherChild) const =0
QAccessibleInterface::childAt
virtual int childAt(int x, int y) const =0
QAccessibleInterface::object
virtual QObject * object() const =0
QString::length
int length() const
QAccessibleInterface::navigate
virtual int navigate(RelationFlag relation, int entry, QAccessibleInterface **target) const =0
QAccessibleInterface::actionText
virtual QString actionText(int action, Text t, int child) const =0
katetextcursor.h
QVariant
QRect::rect
void rect(int *x, int *y, int *width, int *height) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal