Changeset View
Changeset View
Standalone View
Standalone View
src/view/kateviewaccessible.h
Context not available. | |||||
29 | #include <QAccessible> | 29 | #include <QAccessible> | ||
---|---|---|---|---|---|
30 | #include <KLocalizedString> | 30 | #include <KLocalizedString> | ||
31 | #include <QAccessibleWidget> | 31 | #include <QAccessibleWidget> | ||
32 | 32 | | |||
33 | /** | 33 | /** | ||
34 | * This class implements a QAccessible-interface for a KateViewInternal. | 34 | * This class implements a QAccessible-interface for a KateViewInternal. | ||
35 | * | 35 | * | ||
36 | * This is the root class for the kateview. The \a KateCursorAccessible class | 36 | * This is the root class for the kateview. The \a KateCursorAccessible class | ||
37 | * represents the cursor in the kateview and is a child of this class. | 37 | * represents the cursor in the kateview and is a child of this class. | ||
38 | */ | 38 | */ | ||
39 | class KateViewAccessible : public QAccessibleWidget, public QAccessibleTextInterface // FIXME maybe:, public QAccessibleEditableTextInterface | 39 | class KateViewAccessible : public QAccessibleWidget, public QAccessibleTextInterface, public QObject // FIXME maybe:, public QAccessibleEditableTextInterface | ||
40 | { | 40 | { | ||
41 | public: | 41 | public: | ||
42 | explicit KateViewAccessible(KateViewInternal *view) | 42 | explicit KateViewAccessible(KateViewInternal *view); | ||
43 | : QAccessibleWidget(view, QAccessible::EditableText) | | |||
44 | { | | |||
45 | } | | |||
46 | 43 | | |||
47 | void *interface_cast(QAccessible::InterfaceType t) Q_DECL_OVERRIDE | 44 | void *interface_cast(QAccessible::InterfaceType t) Q_DECL_OVERRIDE | ||
48 | { | 45 | { | ||
49 | if (t == QAccessible::TextInterface) | 46 | if (t == QAccessible::TextInterface) | ||
50 | return static_cast<QAccessibleTextInterface*>(this); | 47 | return static_cast<QAccessibleTextInterface*>(this); | ||
51 | return nullptr; | 48 | return nullptr; | ||
52 | } | 49 | } | ||
53 | 50 | | |||
54 | virtual ~KateViewAccessible() | 51 | virtual ~KateViewAccessible() | ||
55 | { | 52 | { | ||
Context not available. | |||||
59 | { | 56 | { | ||
60 | Q_UNUSED(x); | 57 | Q_UNUSED(x); | ||
61 | Q_UNUSED(y); | 58 | Q_UNUSED(y); | ||
62 | return nullptr; | 59 | return nullptr; | ||
63 | } | 60 | } | ||
64 | 61 | | |||
65 | void setText(QAccessible::Text t, const QString &text) Q_DECL_OVERRIDE | 62 | void setText(QAccessible::Text t, const QString &text) Q_DECL_OVERRIDE | ||
66 | { | 63 | { | ||
67 | if (t == QAccessible::Value && view()->view()->document()) { | 64 | if (t == QAccessible::Value && view()->view()->document()) { | ||
68 | view()->view()->document()->setText(text); | 65 | view()->view()->document()->setText(text); | ||
66 | m_lastPosition = -1; | ||||
69 | } | 67 | } | ||
70 | } | 68 | } | ||
71 | 69 | | |||
72 | QAccessible::State state() const Q_DECL_OVERRIDE | 70 | QAccessible::State state() const Q_DECL_OVERRIDE | ||
73 | { | 71 | { | ||
74 | QAccessible::State s = QAccessibleWidget::state(); | 72 | QAccessible::State s = QAccessibleWidget::state(); | ||
75 | s.focusable = view()->focusPolicy() != Qt::NoFocus; | 73 | s.focusable = view()->focusPolicy() != Qt::NoFocus; | ||
76 | s.focused = view()->hasFocus(); | 74 | s.focused = view()->hasFocus(); | ||
77 | s.editable = true; | 75 | s.editable = true; | ||
78 | s.multiLine = true; | 76 | s.multiLine = true; | ||
Context not available. | |||||
174 | view()->view()->setSelection(range); | 172 | view()->view()->setSelection(range); | ||
175 | } | 173 | } | ||
176 | QString text(int startOffset, int endOffset) const Q_DECL_OVERRIDE | 174 | QString text(int startOffset, int endOffset) const Q_DECL_OVERRIDE | ||
177 | { | 175 | { | ||
178 | if (startOffset > endOffset) { | 176 | if (startOffset > endOffset) { | ||
179 | return QString(); | 177 | return QString(); | ||
180 | } | 178 | } | ||
181 | return view()->view()->document()->text().mid(startOffset, endOffset - startOffset); | 179 | return view()->view()->document()->text().mid(startOffset, endOffset - startOffset); | ||
182 | } | 180 | } | ||
183 | 181 | | |||
184 | static int positionFromCursor(KateViewInternal *view, const KTextEditor::Cursor &cursor) | 182 | static int positionFromCursor(KateViewInternal *view, const KTextEditor::Cursor &cursor); | ||
185 | { | | |||
186 | int pos = 0; | | |||
187 | for (int line = 0; line < cursor.line(); ++line) { | | |||
188 | // length of the line plus newline | | |||
189 | pos += view->view()->document()->line(line).size() + 1; | | |||
190 | } | | |||
191 | pos += cursor.column(); | | |||
192 | 183 | | |||
193 | return pos; | 184 | private: | ||
185 | // To be notified when the text is changed | ||||
186 | static void _changed(KTextEditor::Document *document) | ||||
187 | { | ||||
188 | Q_UNUSED(document); | ||||
189 | m_lastPosition = -1; | ||||
194 | } | 190 | } | ||
195 | 191 | | |||
mwolff: I know the old code used int already, but shouldn't this better be a quint64 as it's a file… | |||||
All the methods of QAccesibeTextInrface expect an int. jtamate: All the methods of [[ http://doc.qt.io/qt-5/qaccessibletextinterface.html |… | |||||
196 | private: | | |||
197 | inline KateViewInternal *view() const | 192 | inline KateViewInternal *view() const | ||
mwolff: const auto *doc = | |||||
198 | { | 193 | { | ||
199 | return static_cast<KateViewInternal *>(object()); | 194 | return static_cast<KateViewInternal *>(object()); | ||
200 | } | 195 | } | ||
201 | 196 | | |||
202 | KTextEditor::Cursor cursorFromInt(int position) const | 197 | KTextEditor::Cursor cursorFromInt(int position) const | ||
203 | { | 198 | { | ||
204 | int line = 0; | 199 | int line = 0; | ||
205 | for (;;) { | 200 | for (;;) { | ||
206 | const QString lineString = view()->view()->document()->line(line); | 201 | const QString lineString = view()->view()->document()->line(line); | ||
mwolff: style: join with next line | |||||
Context not available. | |||||
220 | KTextEditor::Cursor pos = cursorFromInt(offset); | 215 | KTextEditor::Cursor pos = cursorFromInt(offset); | ||
mwolff: dito | |||||
221 | pos.setColumn(0); | 216 | pos.setColumn(0); | ||
222 | if (shiftLines) { | 217 | if (shiftLines) { | ||
223 | pos.setLine(pos.line() + shiftLines); | 218 | pos.setLine(pos.line() + shiftLines); | ||
224 | } | 219 | } | ||
225 | *startOffset = positionFromCursor(view(), pos); | 220 | *startOffset = positionFromCursor(view(), pos); | ||
226 | QString line = view()->view()->document()->line(pos.line()) + QLatin1Char('\n'); | 221 | QString line = view()->view()->document()->line(pos.line()) + QLatin1Char('\n'); | ||
227 | *endOffset = *startOffset + line.length(); | 222 | *endOffset = *startOffset + line.length(); | ||
228 | return line; | 223 | return line; | ||
229 | } | 224 | } | ||
225 | | ||||
226 | private: | ||||
227 | | ||||
228 | static KateViewInternal *m_lastView; | ||||
229 | static KTextEditor::Cursor m_lastCursor; | ||||
230 | // m_lastPosition stores the positionFromCursor, but the cursor always in column 0 | ||||
231 | // m_lastPosition < 0 is invalid, so it has to be calculated again from the beginning | ||||
mwolff: unrelated change? | |||||
232 | static int m_lastPosition; | ||||
230 | }; | 233 | }; | ||
231 | 234 | | |||
232 | /** | 235 | /** | ||
233 | * Factory-function used to create \a KateViewAccessible instances for KateViewInternal | 236 | * Factory-function used to create \a KateViewAccessible instances for KateViewInternal | ||
234 | * to make the KateViewInternal accessible. | 237 | * to make the KateViewInternal accessible. | ||
235 | */ | 238 | */ | ||
236 | QAccessibleInterface *accessibleInterfaceFactory(const QString &key, QObject *object) | 239 | QAccessibleInterface *accessibleInterfaceFactory(const QString &key, QObject *object); | ||
237 | { | | |||
238 | Q_UNUSED(key) | | |||
239 | //if (key == QLatin1String("KateViewInternal")) | | |||
240 | if (KateViewInternal *view = qobject_cast<KateViewInternal *>(object)) { | | |||
241 | return new KateViewAccessible(view); | | |||
242 | } | | |||
243 | return nullptr; | | |||
244 | } | | |||
245 | 240 | | |||
246 | #endif | 241 | #endif | ||
247 | #endif | 242 | #endif | ||
Context not available. | |||||
mwolff: maybe also mention that this is a file offset? |
I know the old code used int already, but shouldn't this better be a quint64 as it's a file offset?