Kate
kateviewaccessible.h
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2010 Sebastian Sauer <mail@dipe.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #ifndef _KATE_VIEW_ACCESSIBLE_ 00021 #define _KATE_VIEW_ACCESSIBLE_ 00022 00023 #include "kateviewinternal.h" 00024 #include "katetextcursor.h" 00025 00026 #include <QtGui/QAccessible> 00027 #include <QtGui/QAccessibleInterface> 00028 #include <klocale.h> 00029 00045 class KateCursorAccessible : public QAccessibleInterface 00046 { 00047 public: 00048 00049 enum { ChildId = 1 }; 00050 00051 explicit KateCursorAccessible(KateViewInternal *view) 00052 : QAccessibleInterface() 00053 , m_view(view) 00054 { 00055 } 00056 00057 virtual ~KateCursorAccessible() 00058 { 00059 } 00060 00061 virtual QString actionText(int action, QAccessible::Text t, int) const 00062 { 00063 if (t == QAccessible::Name) { 00064 switch(action) { 00065 case 0: return i18n("Move To..."); 00066 case 1: return i18n("Move Left"); 00067 case 2: return i18n("Move Right"); 00068 case 3: return i18n("Move Up"); 00069 case 4: return i18n("Move Down"); 00070 default: break; 00071 } 00072 } 00073 return QString(); 00074 } 00075 00076 virtual bool doAction(int action, int, const QVariantList & params = QVariantList() ) 00077 { 00078 bool ok = true; 00079 KTextEditor::Cursor c = m_view->getCursor(); 00080 switch(action) { 00081 case 0: { 00082 if (params.count() < 2) ok = false; 00083 const int line = ok ? params[0].toInt(&ok) : 0; 00084 const int column = ok ? params[1].toInt(&ok) : 0; 00085 if (ok) c.setPosition(line, column); 00086 } break; 00087 case 1: c.setPosition(c.line(), c.column() - 1); break; 00088 case 2: c.setPosition(c.line(), c.column() + 1); break; 00089 case 3: c.setPosition(c.line() - 1, c.column()); break; 00090 case 4: c.setPosition(c.line() + 1, c.column()); break; 00091 default: ok = false; break; 00092 } 00093 return ok; 00094 } 00095 00096 virtual int userActionCount(int) const 00097 { 00098 return 5; 00099 } 00100 00101 virtual int childAt(int, int) const 00102 { 00103 return 0; 00104 } 00105 00106 virtual int childCount() const 00107 { 00108 return 0; 00109 } 00110 00111 virtual int indexOfChild(const QAccessibleInterface *) const 00112 { 00113 return 0; 00114 } 00115 00116 virtual bool isValid() const 00117 { 00118 KTextEditor::Cursor c = m_view->getCursor(); 00119 return c.isValid(); 00120 } 00121 00122 virtual int navigate(QAccessible::RelationFlag relation, int entry, QAccessibleInterface **target) const 00123 { 00124 Q_UNUSED(relation); 00125 Q_UNUSED(entry); 00126 *target = 0; 00127 return -1; 00128 } 00129 00130 virtual QObject* object() const 00131 { 00132 return m_view; 00133 } 00134 00135 virtual QRect rect(int) const 00136 { 00137 // return the exact position of the cursor with no width and height defined, 00138 QPoint p = m_view->view()->cursorPositionCoordinates(); 00139 return QRect(m_view->mapToGlobal(p), QSize(0,0)); 00140 } 00141 00142 virtual QAccessible::Relation relationTo(int child, const QAccessibleInterface *other, int otherChild) const 00143 { 00144 Q_UNUSED(child); 00145 Q_UNUSED(other); 00146 Q_UNUSED(otherChild); 00147 return QAccessible::Unrelated; 00148 } 00149 00150 virtual QAccessible::Role role(int) const 00151 { 00152 return QAccessible::Cursor; 00153 } 00154 00155 virtual void setText(QAccessible::Text, int, const QString &) 00156 { 00157 } 00158 00159 virtual QAccessible::State state(int) const 00160 { 00161 QAccessible::State s = QAccessible::Focusable | QAccessible::Focused; 00162 return s; 00163 } 00164 00165 virtual QString text(QAccessible::Text, int) const 00166 { 00167 return QString(); 00168 } 00169 00170 private: 00171 KateViewInternal *m_view; 00172 }; 00173 00180 class KateViewAccessible : public QAccessibleInterface 00181 { 00182 public: 00183 explicit KateViewAccessible(KateViewInternal *view) 00184 : QAccessibleInterface() 00185 , m_view(view) 00186 , m_cursor(new KateCursorAccessible(view)) 00187 { 00188 } 00189 00190 virtual ~KateViewAccessible() 00191 { 00192 } 00193 00194 virtual QString actionText(int action, QAccessible::Text t, int child) const 00195 { 00196 if (child == KateCursorAccessible::ChildId) 00197 return m_cursor->actionText(action, t, 0); 00198 return QString(); 00199 } 00200 00201 virtual bool doAction(int action, int child, const QVariantList & params = QVariantList() ) 00202 { 00203 if (child == KateCursorAccessible::ChildId) 00204 return m_cursor->doAction(action, 0, params); 00205 return false; 00206 } 00207 00208 virtual int userActionCount(int child) const 00209 { 00210 if (child == KateCursorAccessible::ChildId) 00211 return m_cursor->userActionCount(0); 00212 return 0; 00213 } 00214 00215 virtual int childAt(int x, int y) const 00216 { 00217 Q_UNUSED(x); 00218 Q_UNUSED(y); 00219 return 0; 00220 } 00221 00222 virtual int childCount() const 00223 { 00224 return 1; 00225 } 00226 00227 virtual int indexOfChild(const QAccessibleInterface *child) const 00228 { 00229 return dynamic_cast<const KateCursorAccessible*>(child) ? KateCursorAccessible::ChildId : 0; 00230 } 00231 00232 virtual bool isValid() const 00233 { 00234 return true; 00235 } 00236 00237 virtual int navigate(QAccessible::RelationFlag relation, int entry, QAccessibleInterface **target) const 00238 { 00239 if ((relation == QAccessible::Child || QAccessible::FocusChild) && entry == KateCursorAccessible::ChildId) { 00240 *target = new KateCursorAccessible(m_view); 00241 return KateCursorAccessible::ChildId; 00242 } 00243 *target = 0; 00244 return -1; 00245 } 00246 00247 virtual QObject* object() const 00248 { 00249 return m_view; 00250 } 00251 00252 virtual QRect rect(int child) const 00253 { 00254 if (child == KateCursorAccessible::ChildId) 00255 return m_cursor->rect(0); 00256 return QRect(m_view->mapToGlobal(QPoint(0,0)), m_view->size()); 00257 } 00258 00259 virtual QAccessible::Relation relationTo(int child, const QAccessibleInterface *other, int otherChild) const 00260 { 00261 Q_UNUSED(child); 00262 Q_UNUSED(other); 00263 Q_UNUSED(otherChild); 00264 return QAccessible::Unrelated; 00265 } 00266 00267 virtual QAccessible::Role role(int child) const 00268 { 00269 if (child == KateCursorAccessible::ChildId) 00270 return QAccessible::Cursor; 00271 return QAccessible::Document; 00272 } 00273 00274 virtual void setText(QAccessible::Text t, int child, const QString & text) 00275 { 00276 if (t == QAccessible::Value && child == 0 && m_view->view()->document()) 00277 m_view->view()->document()->setText(text); 00278 } 00279 00280 virtual QAccessible::State state(int child) const 00281 { 00282 if (child == KateCursorAccessible::ChildId) 00283 return m_cursor->state(0); 00284 QAccessible::State s = QAccessible::Focusable | QAccessible::Focused; 00285 return s; 00286 } 00287 00288 virtual QString text(QAccessible::Text t, int child) const 00289 { 00290 if (child == KateCursorAccessible::ChildId) 00291 return m_cursor->text(t, 0); 00292 QString s; 00293 if (m_view->view()->document()) { 00294 if (t == QAccessible::Name) 00295 s = m_view->view()->document()->documentName(); 00296 if (t == QAccessible::Value) 00297 s = m_view->view()->document()->text(); 00298 } 00299 return s; 00300 } 00301 00302 private: 00303 KateViewInternal *m_view; 00304 KateCursorAccessible *m_cursor; 00305 }; 00306 00311 QAccessibleInterface* accessibleInterfaceFactory(const QString &key, QObject *object) 00312 { 00313 Q_UNUSED(key) 00314 //if (key == QLatin1String("KateViewInternal")) 00315 if (KateViewInternal *view = qobject_cast<KateViewInternal*>(object)) 00316 return new KateViewAccessible(view); 00317 return 0; 00318 } 00319 00320 #endif
KDE 4.6 API Reference