KDE 4.9 PyKDE API Reference

- KDE's Python API

# View Class Reference

`from PyKDE4.ktexteditor import *`

Inherits: QWidget → QObject,KXMLGUIClient
Namespace: KTextEditor

## Detailed Description

Abstract class:
  This class can be used as a base class for new classes, but can not be instantiated directly.

A text widget with KXMLGUIClient that represents a Document.

Topics: - view_intro - view_hook_into_gui - view_selection - view_cursors - view_mouse_tracking - view_modes - view_extensions

Introduction

The View class represents a single view of a KTextEditor.Document, get the document on which the view operates with document(). A view provides both the graphical representation of the text and the KXMLGUIClient for the actions. The view itself does not provide text manipulation, use the methods from the Document instead. The only method to insert text is insertText(), which inserts the given text at the current cursor position and emits the signal textInserted().

Usually a view is created by using Document.createView(). Furthermore a view can have a context menu. Set it with setContextMenu() and get it with contextMenu().

Merging the View's GUI

A View is derived from the class KXMLGUIClient, so its GUI elements (like menu entries and toolbar items) can be merged into the application's GUI (or into a KXMLGUIFactory) by calling

     // view is of type KTextEditor.View*
     mainWindow()->guiFactory()->addClient( view );

You can add only one view as client, so if you have several views, you first have to remove the current view, and then add the new one, like this

     mainWindow()->guiFactory()->removeClient( currentView );
     mainWindow()->guiFactory()->addClient( newView );

Text Selection

As the view is a graphical text editor it provides normal and block text selection. You can check with selection() whether a selection exists. removeSelection() will remove the selection without removing the text, whereas removeSelectionText() also removes both, the selection and the selected text. Use selectionText() to get the selected text and setSelection() to specify the selected textrange. The signal selectionChanged() is emitted whenever the selecteion changed.

Cursor Positions

A view has one Cursor which represents a line/column tuple. Two different kinds of cursor positions are supported: first is the real cursor position where a tab character only counts one character. Second is the virtual cursor position, where a tab character counts as many spaces as defined. Get the real position with cursorPosition() and the virtual position with cursorPositionVirtual(). Set the real cursor position with setCursorPosition(). You can even get the screen coordinates of the current cursor position in pixel by using cursorPositionCoordinates(). The signal cursorPositionChanged() is emitted whenever the cursor position changed.

Mouse Tracking

It is possible to get notified via the signal mousePositionChanged() for mouse move events, if mouseTrackingEnabled() returns true. Mouse tracking can be turned on/off by calling setMouseTrackingEnabled(). If an editor implementation does not support mouse tracking, mouseTrackingEnabled() will always return false.

Edit Modes

A view supports several edit modes (EditMode). Common edit modes are insert-mode (INS) and overwrite-mode (OVR). Which edit modes the editor supports depends on the implementation, another well-known mode is the command-mode for example in vim and yzis. The getter viewMode() returns a string like INS or OVR and is represented in the user interface for example in the status bar. Further you can get the edit mode as enum by using viewEditMode(). Whenever the edit mode changed the signals viewModeChanged() and viewEditModeChanged() are emitted.

View Extension Interfaces

A simple view represents the text of a Document and provides a text cursor, text selection, edit modes etc. Advanced concepts like code completion and text hints are defined in the extension interfaces. An KTextEditor implementation does not need to support all the extensions. To implement the interfaces multiple inheritance is used.

More information about interfaces for the view can be found in kte_group_view_extensions.

See also:
  KTextEditor.Document, KTextEditor.TemplateInterface, KTextEditor.CodeCompletionInterface, KTextEditor.SessionConfigInterface, KTextEditor.TemplateInterface, KXMLGUIClient

Author:
  Christoph Cullmann <cullmann@kde.org>

## Enumerations

EditMode | { EditInsert, EditOverwrite }

## Signals

| contextMenuAboutToShow (KTextEditor.View view, QMenu menu)
| cursorPositionChanged (KTextEditor.View view, KTextEditor.Cursor newPosition)
| focusIn (KTextEditor.View view)
| focusOut (KTextEditor.View view)
| horizontalScrollPositionChanged (KTextEditor.View view)
| informationMessage (KTextEditor.View view, QString message)
| mousePositionChanged (KTextEditor.View view, KTextEditor.Cursor newPosition)
| selectionChanged (KTextEditor.View view)
| textInserted (KTextEditor.View view, KTextEditor.Cursor position, QString text)
| verticalScrollPositionChanged (KTextEditor.View view, KTextEditor.Cursor newPos)
| viewEditModeChanged (KTextEditor.View view, KTextEditor.View.EditMode mode)
| viewModeChanged (KTextEditor.View view)

## Methods

| __init__ (self, QWidget parent)
bool | blockSelection (self)
QMenu | contextMenu (self)
KTextEditor.Cursor | cursorPosition (self)
QPoint | cursorPositionCoordinates (self)
KTextEditor.Cursor | cursorPositionVirtual (self)
QPoint | cursorToCoordinate (self, KTextEditor.Cursor cursor)
QMenu | defaultContextMenu (self, QMenu menu=0)
KTextEditor.Document | document (self)
bool | insertText (self, QString text)
bool | isActiveView (self)
bool | mouseTrackingEnabled (self)
bool | removeSelection (self)
bool | removeSelectionText (self)
bool | selection (self)
KTextEditor.Range | selectionRange (self)
QString | selectionText (self)
bool | setBlockSelection (self, bool on)
| setContextMenu (self, QMenu menu)
bool | setCursorPosition (self, KTextEditor.Cursor position)
bool | setMouseTrackingEnabled (self, bool enable)
bool | setSelection (self, KTextEditor.Range range)
bool | setSelection (self, KTextEditor.Cursor position, int length, bool wrap=1)
KTextEditor.View.EditMode | viewEditMode (self)
QString | viewMode (self)

## Signal Documentation

contextMenuAboutToShow | ( | KTextEditor.View | view,
|  | QMenu | menu
| ) |  |  |

Signal which is emitted immediately prior to showing the current context menu.

Signal syntax:
  `QObject.connect(source, SIGNAL("contextMenuAboutToShow(KTextEditor::View*, QMenu*)"), target_slot)`

cursorPositionChanged | ( | KTextEditor.View | view,
|  | KTextEditor.Cursor | newPosition
| ) |  |  |

This signal is emitted whenever the view's cursor position changed.

Parameters:

  | view | view which emitted the signal

Parameters:

  | newPosition | new position of the cursor (Kate will pass the real cursor potition, not the virtual)

See also:
  cursorPosition(), cursorPositionVirtual()

Signal syntax:
  `QObject.connect(source, SIGNAL("cursorPositionChanged(KTextEditor::View*, const KTextEditor::Cursor&)"), target_slot)`

focusIn | ( | KTextEditor.View | view
| ) |  |  |

This signal is emitted whenever the view gets the focus.

Parameters:

  | view | view which gets focus

See also:
  focusOut()

Signal syntax:
  `QObject.connect(source, SIGNAL("focusIn(KTextEditor::View*)"), target_slot)`

focusOut | ( | KTextEditor.View | view
| ) |  |  |

This signal is emitted whenever the view loses the focus.

Parameters:

  | view | view which lost focus

See also:
  focusIn()

Signal syntax:
  `QObject.connect(source, SIGNAL("focusOut(KTextEditor::View*)"), target_slot)`

horizontalScrollPositionChanged | ( | KTextEditor.View | view
| ) |  |  |

This signal should be emitted whenever the view is scrolled horizontally.

Parameters:

  | view | view which emitted the signal

Signal syntax:
  `QObject.connect(source, SIGNAL("horizontalScrollPositionChanged(KTextEditor::View*)"), target_slot)`

informationMessage | ( | KTextEditor.View | view,
|  | QString | message
| ) |  |  |

This signal is emitted whenever the view wants to display a information message. The message can be displayed in the status bar for example.

Parameters:

  | view | view which sends out information

Parameters:

  | message | information message

Signal syntax:
  `QObject.connect(source, SIGNAL("informationMessage(KTextEditor::View*, const QString&)"), target_slot)`

mousePositionChanged | ( | KTextEditor.View | view,
|  | KTextEditor.Cursor | newPosition
| ) |  |  |

This signal is emitted whenever the position of the mouse changes over this view. If the mouse moves off the view, an invalid cursor position should be emitted, i.e. Cursor.invalid().

Note:
  If mouseTrackingEnabled() returns false, this signal is never emitted.

Parameters:

  | view | view which emitted the signal

Parameters:

  | newPosition | new position of the mouse or Cursor.invalid(), if the mouse moved out of the view.

See also:
  mouseTrackingEnabled()

Signal syntax:
  `QObject.connect(source, SIGNAL("mousePositionChanged(KTextEditor::View*, const KTextEditor::Cursor&)"), target_slot)`

selectionChanged | ( | KTextEditor.View | view
| ) |  |  |

This signal is emitted whenever the view's selection changes.

Note:
  If the mode switches from block selection to normal selection or vice versa this signal should also be emitted.

Parameters:

  | view | view in which the selection changed

See also:
  selection(), selectionRange(), selectionText()

Signal syntax:
  `QObject.connect(source, SIGNAL("selectionChanged(KTextEditor::View*)"), target_slot)`

textInserted | ( | KTextEditor.View | view,
|  | KTextEditor.Cursor | position,
|  | QString | text
| ) |  |  |

This signal is emitted from view whenever the users inserts text at position, that means the user typed/pasted text.

Parameters:

  | view | view in which the text was inserted

Parameters:

  | position | position where the text was inserted

Parameters:

  | text | the text the user has typed into the editor

See also:
  insertText()

Signal syntax:
  `QObject.connect(source, SIGNAL("textInserted(KTextEditor::View*, const KTextEditor::Cursor&, const QString&)"), target_slot)`

verticalScrollPositionChanged | ( | KTextEditor.View | view,
|  | KTextEditor.Cursor | newPos
| ) |  |  |

This signal should be emitted whenever the view is scrolled vertically.

Parameters:

  | view | view which emitted the signal

Parameters:

  | newPos | the new scroll position

Signal syntax:
  `QObject.connect(source, SIGNAL("verticalScrollPositionChanged(KTextEditor::View*, const KTextEditor::Cursor&)"), target_slot)`

viewEditModeChanged | ( | KTextEditor.View | view,
|  | KTextEditor.View.EditMode | mode
| ) |  |  |

This signal is emitted whenever the view's edit mode changed from either EditInsert to EditOverwrite or vice versa.

Parameters:

  | view | view which changed its edit mode

Parameters:

  | mode | new edit mode

See also:
  viewEditMode()

Signal syntax:
  `QObject.connect(source, SIGNAL("viewEditModeChanged(KTextEditor::View*, KTextEditor::View::EditMode)"), target_slot)`

viewModeChanged | ( | KTextEditor.View | view
| ) |  |  |

This signal is emitted whenever the view mode of view changes.

Parameters:

  | view | the view which changed its mode

See also:
  viewMode()

Signal syntax:
  `QObject.connect(source, SIGNAL("viewModeChanged(KTextEditor::View*)"), target_slot)`

## Method Documentation

__init__ | ( | self, |
|  | QWidget | parent
| ) |  |  |

Constructor.

Create a view attached to the widget parent.

Parameters:

  | parent | parent widget

See also:
  Document.createView()

bool blockSelection | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Get the status of the selection mode. true indicates that block selection mode is on. If this is true, selections applied via the SelectionInterface are handled as block selections and the Copy&Paste functions work on rectangular blocks of text rather than normal.

Returns:
  true, if block selection mode is enabled, otherwise false

See also:
  setBlockSelection()

QMenu contextMenu | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Get the context menu for this view. The return value can be NULL if no context menu object was set and kxmlgui is not initialized yet. If there is no user set menu, the kxmlgui menu is returned. Do not delete this menu, if if it is the xmlgui menu.

Returns:
  context menu object

See also:
  setContextMenu()

KTextEditor.Cursor cursorPosition | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Get the view's current cursor position. A TAB character is handeled as only one character.

Returns:
  current cursor position

See also:
  setCursorPosition()

QPoint cursorPositionCoordinates | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Get the screen coordinates (x/y) of the cursor position in pixels.

Returns:
  cursor screen coordinates

KTextEditor.Cursor cursorPositionVirtual | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Get the current virtual cursor position, virtual means the tabulator character (TAB) counts multiple characters, as configured by the user (e.g. one TAB is 8 spaces). The virtual cursor position provides access to the user visible values of the current cursor position.

Returns:
  virtual cursor position

See also:
  cursorPosition()

QPoint cursorToCoordinate | ( | self, |
|  | KTextEditor.Cursor | cursor
| ) |  |  |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Get the screen coordinates (x, y) of the supplied cursor relative to the view widget in pixels. Thus, 0,0 represents the top left hand of the view widget.

Parameters:

  | cursor | cursor to determine coordinate for.

Returns:
  cursor screen coordinates relative to the view widget

QMenu defaultContextMenu | ( | self, |
|  | QMenu | menu=0
| ) |  |  |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Populate menu with default text editor actions. If menu is null, a menu will be created with the view as its parent.

Note:
  to use this menu, you will next need to call setContextMenu(), as this does not assign the new context menu.

Warning:
  This contains only basic options from the editor component (katepart). Plugins are not merged/integrated into it! If you want to be a better citizen and take full advantage of KTextEditor plugins do something like:

     KXMLGUIClient* client = view;
     // search parent XmlGuiClient
     while (client->parentClient()) {
       client = client->parentClient();
     }

     if (client->factory()) {
       QList<QWidget*> conts = client->factory()->containers("menu");
       foreach (QWidget *w, conts) {
         if (w->objectName() == "ktexteditor_popup") {
           // do something with the menu (ie adding an onshow handler)
           break;
         }
       }
     }

Warning:
  or simply use the aboutToShow, aboutToHide signals !!!!!

Parameters:

  | menu | the menu to be populated, or null to create a new menu.

Returns:
  the menu, whether created or passed initially

KTextEditor.Document document | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Get the view's document, that means the view is a view of the returned document.

Returns:
  the view's document

bool insertText | ( | self, |
|  | QString | text
| ) |  |  |

This is a convenience function which inserts text at the view's current cursor position. You do not necessarily need to reimplement it, except you want to do some special things.

Parameters:

  | text | Text to be inserted

Returns:
  true on success of insertion, otherwise false

See also:
  textInserted()

bool isActiveView | ( |  | self ) |

Check whether this view is the document's active view. This is equal to the code:

     document()->activeView() == view

bool mouseTrackingEnabled | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Check, whether mouse tracking is enabled.

Mouse tracking is required to have the signal mousePositionChanged() emitted.

Returns:
  true, if mouse tracking is enabled, otherwise false

See also:
  setMouseTrackingEnabled(), mousePositionChanged()

bool removeSelection | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Remove the view's current selection, without deleting the selected text.

Returns:
  true on success, otherwise false

See also:
  removeSelectionText()

bool removeSelectionText | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Remove the view's current selection including the selected text.

Returns:
  true on success, otherwise false

See also:
  removeSelection()

bool selection | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Query the view whether it has selected text, i.e. whether a selection exists.

Returns:
  true if a text selection exists, otherwise false

See also:
  setSelection(), selectionRange()

KTextEditor.Range selectionRange | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Get the range occupied by the current selection.

Returns:
  selection range, valid only if a selection currently exists.

See also:
  setSelection()

QString selectionText | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Get the view's selected text.

Returns:
  the selected text

See also:
  setSelection()

bool setBlockSelection | ( | self, |
|  | bool | on
| ) |  |  |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Set block selection mode to state on.

Parameters:

  | on | if true, block selection mode is turned on, otherwise off

Returns:
  true on success, otherwise false

See also:
  blockSelection()

setContextMenu | ( | self, |
|  | QMenu | menu
| ) |  |  |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Set a context menu for this view to menu.

Note:
  any previously assigned menu is not deleted. If you are finished with the previous menu, you may delete it.

Warning:
  Use this with care! Plugin xml gui clients are not merged into this menu!

Warning:
  !!!!!! DON'T USE THIS FUNCTION, UNLESS YOU ARE SURE YOU DON'T WANT PLUGINS TO WORK !!!!!!

Parameters:

  | menu | new context menu object for this view

See also:
  contextMenu()

bool setCursorPosition | ( | self, |
|  | KTextEditor.Cursor | position
| ) |  |  |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Set the view's new cursor to position. A TAB character is handeled as only on character.

Parameters:

  | position | new cursor position

Returns:
  true on success, otherwise false

See also:
  cursorPosition()

bool setMouseTrackingEnabled | ( | self, |
|  | bool | enable
| ) |  |  |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Try to enable or disable mouse tracking according to enable. The return value contains the state of mouse tracking after the request. Mouse tracking is required to have the mousePositionChanged() signal emitted.

Note:
  Implementation Notes: An implementation is not forced to support this, and should always return false if it does not have support.

Parameters:

  | enable | if true, try to enable mouse tracking, otherwise disable it.

Returns:
  the current state of mouse tracking

See also:
  mouseTrackingEnabled(), mousePositionChanged()

bool setSelection | ( | self, |
|  | KTextEditor.Range | range
| ) |  |  |

Abstract method:
  This method is abstract and can be overridden but not called directly.

This is an overloaded member function, provided for convenience, it differs from the above function only in what argument(s) it accepts. An existing old selection will be discarded. If possible you should reimplement the default implementation with a more efficient one.

Parameters:

  | position | start or end position of the selection, depending on the length parameter

Parameters:

  | length | if >0 position defines the start of the selection, if <0 position specifies the end

Parameters:

  | wrap | if false the selection does not wrap lines and reaches only to start/end of the cursors line. Default: true

See also:
  selectionRange(), selection()

To do: rodda - is this really needed? it can now be accomplished with SmartCursor.advance()

bool setSelection | ( | self, |
|  | KTextEditor.Cursor | position,
|  | int | length,
|  | bool | wrap=1
| ) |  |  |

This is an overloaded member function, provided for convenience, it differs from the above function only in what argument(s) it accepts. An existing old selection will be discarded. If possible you should reimplement the default implementation with a more efficient one.

Parameters:

  | position | start or end position of the selection, depending on the length parameter

Parameters:

  | length | if >0 position defines the start of the selection, if <0 position specifies the end

Parameters:

  | wrap | if false the selection does not wrap lines and reaches only to start/end of the cursors line. Default: true

See also:
  selectionRange(), selection()

To do: rodda - is this really needed? it can now be accomplished with SmartCursor.advance()

KTextEditor.View.EditMode viewEditMode | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Get the view's current edit mode. The current mode can be insert mode, replace mode or any other the editor supports, e.g. a vim like command mode. If in doubt return EditInsert.

Returns:
  the current edit mode of this view

See also:
  viewEditModeChanged()

QString viewMode | ( |  | self ) |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Get the current view mode/state. This can be used to visually indicate the view's current mode, for example INSERT mode, OVERWRITE mode or COMMAND mode - or whatever other edit modes are supported. The string should be translated (i18n), as this is a user aimed representation of the view state, which should be shown in the GUI, for example in the status bar.

Returns:

See also:
  viewModeChanged()

## Enumeration Documentation

EditMode

Possible edit modes. These correspond to various modes the text editor might be in.

Enumerator:

  EditInsert = 0 |
  EditOverwrite = 1 |