Changeset View
Changeset View
Standalone View
Standalone View
src/view/kateviewaccessible.cpp
1 | /* This file is part of the KDE libraries | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2018 Jaime Torres <jtamate@gmail.com> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #ifndef QT_NO_ACCESSIBILITY | ||||
21 | | ||||
22 | #include "kateviewaccessible.h" | ||||
23 | | ||||
24 | //static variables instances | ||||
25 | KateViewInternal *KateViewAccessible::m_lastView; | ||||
26 | KTextEditor::Cursor KateViewAccessible::m_lastCursor; | ||||
27 | int KateViewAccessible::m_lastPosition; | ||||
28 | | ||||
29 | KateViewAccessible::KateViewAccessible(KateViewInternal *view) | ||||
30 | : QAccessibleWidget(view, QAccessible::EditableText) | ||||
31 | { | ||||
32 | QObject::connect(view->view()->document(), &KTextEditor::Document::textChanged, this, &KateViewAccessible::_changed); | ||||
33 | } | ||||
34 | | ||||
35 | int KateViewAccessible::positionFromCursor(KateViewInternal *view, const KTextEditor::Cursor &cursor) | ||||
36 | { | ||||
37 | int pos = m_lastPosition; | ||||
38 | const KTextEditor::Document *doc = view->view()->document(); | ||||
39 | | ||||
40 | // m_lastPosition < 0 is invalid, so it has to be calculated again from the beginning | ||||
41 | if (m_lastPosition < 0 || view != m_lastView) { | ||||
42 | pos = 0; | ||||
43 | // Default (worst) case | ||||
44 | for (int line = 0; line < cursor.line(); ++line) { | ||||
45 | pos += doc->line(line).size(); | ||||
46 | } | ||||
47 | // new line for each line | ||||
48 | pos += cursor.line(); | ||||
49 | m_lastView = view; | ||||
50 | } | ||||
51 | else { | ||||
52 | // if the lines are the same, just add the cursor.column(), otherwise | ||||
53 | if (cursor.line() != m_lastCursor.line()) { | ||||
54 | // If the cursor is after the previous cursor | ||||
55 | if (m_lastCursor.line() < cursor.line()) { | ||||
56 | for (int line = m_lastCursor.line(); line < cursor.line(); ++line) { | ||||
57 | pos += doc->line(line).size(); | ||||
58 | } | ||||
59 | // add new line character for each line | ||||
60 | pos += cursor.line() - m_lastCursor.line(); | ||||
61 | } | ||||
62 | else { | ||||
63 | for (int line = cursor.line(); line < m_lastCursor.line(); ++line) { | ||||
64 | pos -= doc->line(line).size(); | ||||
65 | } | ||||
66 | // remove new line character for each line | ||||
67 | pos -= m_lastCursor.line() - cursor.line(); | ||||
68 | } | ||||
69 | } | ||||
70 | } | ||||
71 | m_lastCursor = cursor; | ||||
72 | m_lastPosition = pos; | ||||
73 | | ||||
74 | return pos + cursor.column(); | ||||
75 | } | ||||
76 | | ||||
77 | | ||||
78 | /** | ||||
79 | * Factory-function used to create \a KateViewAccessible instances for KateViewInternal | ||||
80 | * to make the KateViewInternal accessible. | ||||
81 | */ | ||||
82 | QAccessibleInterface *accessibleInterfaceFactory(const QString &key, QObject *object) | ||||
83 | { | ||||
84 | Q_UNUSED(key) | ||||
85 | //if (key == QLatin1String("KateViewInternal")) | ||||
86 | if (KateViewInternal *view = qobject_cast<KateViewInternal *>(object)) { | ||||
87 | return new KateViewAccessible(view); | ||||
88 | } | ||||
89 | return nullptr; | ||||
90 | } | ||||
91 | | ||||
92 | | ||||
93 | #endif |