QProgEdit is a PyQt4 widget that implements a full-featured text editor component
Python Shell
smathot 4.0.11
- Fix keyboard shortcut for preferences
Latest commit 1e898d7 Apr 6, 2017
Permalink
Failed to load latest commit information.
QProgEdit
debian
dev-scripts
COPYING
copyright Update setup and bump to 4.0.0 Mar 28, 2016
example.py PyQt4 compatibility Feb 24, 2016
readme.md PyQt4 compatibility Feb 24, 2016
readme.py
setup.cfg
setup.py
stdeb.cfg Bump to 4.0.8 Jan 30, 2017

readme.md

module QProgEdit

QProgEdit

v3.0.0~pre2

QProgEdit is a PyQt4 widget that implements a full-featured text editor component. It's primary target at the moment is OpenSesame, a graphical experiment builder.

Copyright (2013-2015) Sebastiaan Mathôt

http://www.cogsci.nl/smathot

Overview

Dependencies

  • PyQt4
  • Qscintilla2

Example

#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
This file is part of QProgEdit.

QProgEdit is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

QProgEdit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with QProgEdit.  If not, see <http://www.gnu.org/licenses/>.
"""

import os
import sys
from qtpy import QtGui, QtCore
from QProgEdit import QTabManager, validate

def cursorRowChanged(index, rowFrom, rowTo):

	print(u'curorRowChanged(): %d, %d, %d' % (index, rowFrom, rowTo))

def focusLost(index):

	print(u'focusOut(): %s' % index)

def focusReceived(index):

	print(u'focusReceived(): %s' % index)

def handlerButtonClicked(index):

	print(u'handlerButtonClicked(): %s' % index)

def activateSymbolTree(treeWidgetItem):

	if hasattr(treeWidgetItem, u'activate'):
		treeWidgetItem.activate()

def runSelectedText(s):

	print('run:\n%s' % s)

def main():

	"""Runs a simple QProgEdit demonstration."""

	validate.addPythonBuiltins(['builtin_var'])
	app = QtGui.QApplication(sys.argv)

	treeWidgetItem1 = QtWidgets.QTreeWidgetItem([u'Tab 1'])
	treeWidgetItem3 = QtWidgets.QTreeWidgetItem([u'Tab 3'])
	symbolTree = QtWidgets.QTreeWidget()
	symbolTree.addTopLevelItem(treeWidgetItem1)
	symbolTree.addTopLevelItem(treeWidgetItem3)
	symbolTree.itemActivated.connect(activateSymbolTree)

	tabManager = QTabManager(handlerButtonText=u'apply', runButton=True)
	tabManager.setWindowIcon(QtGui.QIcon.fromTheme(u'accessories-text-editor'))
	tabManager.setWindowTitle(u'QProgEdit')
	tabManager.resize(800, 600)

	tabManager.cursorRowChanged.connect(cursorRowChanged)
	tabManager.focusLost.connect(focusLost)
	tabManager.focusReceived.connect(focusReceived)
	tabManager.handlerButtonClicked.connect(handlerButtonClicked)
	tabManager.execute.connect(runSelectedText)

	tab = tabManager.addTab(u'Tab 1')
	tab.setLang(u'Python')
	tab.setSymbolTree(treeWidgetItem1)
	tab.setText(open(__file__).read())

	tab = tabManager.addTab(u'Tab 2')
	tab.setText(u'Some plain text')

	tab = tabManager.addTab(u'Tab 3')
	tab.setLang(u'Python')
	tab.setSymbolTree(treeWidgetItem3)
	if os.path.exists(u'content.txt'):
		tab.setText(open(u'content.txt').read())

	layout = QtWidgets.QHBoxLayout()
	layout.addWidget(symbolTree)
	layout.addWidget(tabManager)
	container = QtWidgets.QWidget()
	container.setLayout(layout)
	container.show()

	res = app.exec_()
	open(u'content.txt', u'w').write(tab.text())
	sys.exit(res)

if __name__ == '__main__':
	main()

class QProgEdit.QEditor

A single editor widget, which is embedded in a QProgEdit widget.

function QProgEdit.QEditor.__init__(qProgEdit)

Constructor.

Arguments:

  • qProgEdit -- The parent QProgEdit.
    • Type: QProgEdit

function QProgEdit.QEditor.applyCfg()

Applies the configuration.

function QProgEdit.QEditor.commentSelection()

Comments out the currently selected text.

function QProgEdit.QEditor.cursorMoved()

Is called whenever the cursor moves, checks whether the cursor has jumped from one line to the next, and, if so, calls the relevant functions.

function QProgEdit.QEditor.focusInEvent(e)

Called when the editor receives focus.

Arguments:

  • e -- No description

function QProgEdit.QEditor.focusOutEvent(e)

Called when the editor loses focus.

Arguments:

  • e -- No description

function QProgEdit.QEditor.highlightSelection()

Highlights all parts of the text that match the current selection.

function QProgEdit.QEditor.keyPressEvent(event)

Intercepts certain keypress events to implement custom copy-pasting and zooming.

Arguments:

  • event -- No description
    • Type: QKeyPressEvent

function QProgEdit.QEditor.lang()

No description specified.

Returns:

The language of the editor.

  • Type: unicode

function QProgEdit.QEditor.onMarginClick(margin, line, state)

Shows validation errors when the margin symbol is clicked.

Arguments:

  • margin -- The margin number.
    • Type: int
  • line -- The line number.
    • Type: int
  • state -- The keyboard state.
    • Type: int

function QProgEdit.QEditor.paste()

Re-implements the paste method to allow modification of paste content.

function QProgEdit.QEditor.selectedText(currentLineFallback=False)

Returns the selected text.

Keywords:

  • currentLineFallback -- Indicates whether the current line should be returned if no text has been selected. Otherwise, an empty string is returned.
    • Type: bool
    • Default: False

Returns:

The selected text.

  • Type: str

function QProgEdit.QEditor.setKeyBindings()

Sets keybindings so that they don't interfere with the default keybindings of OpenSesame, and are more atom-like.

function QProgEdit.QEditor.setLang(lang=u'text')

Sets the editor language.

Keywords:

  • lang -- A language, used to select a lexer for syntax highlighting, validation, cleaning, etc. if an appropriate lexer isn't found, no error is generated, but syntax highlighting is disabled. For a list of available lexers, refer to theQsci.QsciScintilla documentation.
    • Default: u'text'

function QProgEdit.QEditor.setSymbolTree(symbolTree, symbolTreeWidgetItemClass=<class 'QProgEdit._qsymboltreewidgetitem.QSymbolTreeWidgetItem'>)

Sets the symbol-tree widget.

Arguments:

  • symbolTree -- A symbol-tree widget.
    • Type: QTreeWidgetItem

Keywords:

  • symbolTreeWidgetItemClass -- The class to use for symbol-tree widgets. This should derive from QSymbolTreeWidgetItem.
    • Type: type
    • Default: <class 'QProgEdit._qsymboltreewidgetitem.QSymbolTreeWidgetItem'>

function QProgEdit.QEditor.setText(text)

Sets the editor contents.

Arguments:

  • text -- A text string. This can be a str object or unicode object.
    • Type: str, unicode

function QProgEdit.QEditor.symbols()

Returns an up-to-date list of symbols.

Returns:

A list of symbols.

  • Type: list

function QProgEdit.QEditor.text()

Retrieves the editor contents.

Returns:

The editor contents.

  • Type: unicode

function QProgEdit.QEditor.uncommentSelection()

Uncomments the currently selected text.

function QProgEdit.QEditor.updateMarginWidth()

Updates the width of the margin containing the line numbers.

function QProgEdit.QEditor.updateSymbolTree()

Updates the symbol tree, if any has been specified and a symbol parser is available for the langauage.

function QProgEdit.QEditor.validate()

Validates the content.

function QProgEdit.QEditor.wheelEvent(event)

Implements scroll-to-zoom functionality.

Arguments:

  • event -- No description
    • Type: QWheelEvent

class QProgEdit.QEditorCfg

A non-persistent configuration object.

function QProgEdit.QEditorCfg.__init__(parent=None)

Constructor.

Keywords:

  • parent -- The parent widget.
    • Type: QWidget, NoneType
    • Default: None

function QProgEdit.QEditorCfg.version()

Returns the config version.

class QProgEdit.QEditorFind

A find/ replace widget.

function QProgEdit.QEditorFind.__init__(qProgEdit)

Constructor.

Arguments:

  • qProgEdit -- The parent QProgEdit.
    • Type: QProgEdit

function QProgEdit.QEditorFind.caseSensitive()

No description specified.

Returns:

True or False, depending on whether we should search case sensitive.

  • Type: bool

function QProgEdit.QEditorFind.find()

Finds the current text in the document.

Returns:

True if matching text has been found, False otherwise.

  • Type: bool

function QProgEdit.QEditorFind.findText()

No description specified.

Returns:

The find text.

  • Type: unicode

function QProgEdit.QEditorFind.loadUi(name)

Load a UI.

Arguments:

  • name -- The name of the UI file, without the .ui extension.
    • Type: unicode

function QProgEdit.QEditorFind.lock()

Locks the editor and find widget, so that we don't get into recursion problems during replace actions.

function QProgEdit.QEditorFind.matchWhole()

No description specified.

Returns:

True or False, depending on whether we should match whole words only.

  • Type: bool

function QProgEdit.QEditorFind.replace()

Replaces the first occurence in the document.

Returns:

True if text has been replaced, False otherwise.

  • Type: bool

function QProgEdit.QEditorFind.replaceAll()

Replaces all occurences in the document.

function QProgEdit.QEditorFind.replaceText()

No description specified.

Returns:

The replace text.

  • Type: unicode

function QProgEdit.QEditorFind.setFindText(txt=u'')

Sets the text of the find widget.

Keywords:

  • txt -- The text to set.
    • Type: unicode
    • Default: u''

function QProgEdit.QEditorFind.unlock()

Unlocks the editor and find widget, to resume normal operations after replacing.

function QProgEdit.QEditorFind.unshow()

Hides the widget.

class QProgEdit.QEditorPrefs

An editor preferences widget.

function QProgEdit.QEditorPrefs.__init__(qProgEdit)

Constructor.

Arguments:

  • qProgEdit -- The parent QProgEdit.
    • Type: QProgEdit

function QProgEdit.QEditorPrefs.apply(dummy=None)

Applies the controls.

Keywords:

  • dummy -- No description
    • Default: None

function QProgEdit.QEditorPrefs.loadUi(name)

Load a UI.

Arguments:

  • name -- The name of the UI file, without the .ui extension.
    • Type: unicode

function QProgEdit.QEditorPrefs.refresh()

Refreshes the controls.

class QProgEdit.QEditorStatus

A simple widget that indicates the editor status, which currently corresponds only to the cursor position.

function QProgEdit.QEditorStatus.__init__(qProgEdit)

Constructor.

Arguments:

  • qProgEdit -- The parent QProgEdit.
    • Type: QProgEdit

function QProgEdit.QEditorStatus.updateCursorPos(line=0, index=0)

Updates the cursor position.

Keywords:

  • line -- The line number.
    • Type: int
    • Default: 0
  • index -- The column number.
    • Type: int
    • Default: 0

class QProgEdit.QLangMenu

The language selection menu.

function QProgEdit.QLangMenu.__init__(tabCornerWidget)

Constructor.

Arguments:

  • tabCornerWidget -- The parent QTabCornerWidget.
    • Type: QTabCornerWidget

function QProgEdit.QLangMenu.setLang(action)

Select a new language for the selected tab.

Arguments:

  • action -- No description
    • Type: QAction

function QProgEdit.QLexer(editor, lang=u'text', colorScheme=u'Default')

A factory for a lexer.

Arguments:

  • editor -- The parent QEditor.
    • Type: QEditor

Keywords:

  • lang -- The language.
    • Type: unicode
    • Default: u'text'
  • colorScheme -- The color scheme.
    • Type: unicode
    • Default: u'Default'

class QProgEdit.QProgEdit

A single editor window, with preferences widget and search functionality.

function QProgEdit.QProgEdit.__init__(tabManager, dPrint=None, title=u'Empty document', **editorParams)

Constructor.

Arguments:

  • tabManager -- A tab manager.
    • Type: QTabManager

Keywords:

  • dPrint -- A function to be used for debug printing. Should accept a single parameter, which is the debug message. If no debug function is specified, the standard output is used.
    • Default: None
  • title -- A title for the document.
    • Default: u'Empty document'

Keyword dict:

  • **editorParams: A dictionary with keywords to be passed to QEditor.

function QProgEdit.QProgEdit.dPrint(msg)

Prints a debug message.

Arguments:

  • msg -- A debug message.
    • Type: unicode, str

function QProgEdit.QProgEdit.focusTab()

Focuses the current tab.

function QProgEdit.QProgEdit.tabIndex()

Gets the index of the current tab.

Returns:

The tab index.

  • Type: int

function QProgEdit.QProgEdit.toggle(widget, visible)

Toggles the visibility of a widget with a smooth animation.

Arguments:

  • widget -- A QWidget.
  • visible -- A boolean indicating the visibility of the widget.

function QProgEdit.QProgEdit.toggleFind(visible)

Toggles the visibility of the find widget.

Arguments:

  • visible -- A boolean indicating the visibility of the widget.

function QProgEdit.QProgEdit.togglePrefs(visible)

Toggles the visibility of the preferences widget

Arguments:

  • visible -- A boolean indicating the visibility of the widget.

class QProgEdit.QSymbolTreeWidgetItem

A symbol-tree widget item to use for symbol overviews.

function QProgEdit.QSymbolTreeWidgetItem.__init__(editor, lineNo, _type, name, argSpec)

Constructor.

Arguments:

  • editor -- The editor widget.
    • Type: QEditor
  • lineNo -- A line number.
    • Type: int
  • _type -- The symbol type, such as 'class' or 'def'
    • Type: unicode
  • name -- The symbol name
    • Type: unicode
  • argSpec -- The symbol's argument specification.
    • Type: unicode

function QProgEdit.QSymbolTreeWidgetItem.activate()

Is called when the symbol is activated, to focus the symbol in the editor.

class qtpyabCornerWidget

Contains a number of buttons that are displayed in the tab bar.

function QProgEdit.QTabCornerWidget.__init__(tabManager, msg=None, handlerButtonText=None, runButton=False)

Constructor.

Arguments:

  • tabManager -- A tab manager.
    • Type: QTabManager

Keywords:

  • msg -- An informative text message.
    • Type: str, unicode, NoneType
    • Default: None
  • handlerButtonText -- Text for a top-right button, which can be clicked to call the handler, or None for no button.
    • Type: str, unicode, NoneType
    • Default: None
  • runButton -- Indicates whether a run-selected-text button should be shown.
    • Type: bool
    • Default: False

function QProgEdit.QTabCornerWidget.handlerButtonClicked()

Is called when the handler button is clicked and emits the relevant signals.

function QProgEdit.QTabCornerWidget.update()

Updates widget to reflect document contents.

class qtpyabManager

A tab manager that contains multiple QProgEdit tabs.

function QProgEdit.QTabManager.__init__(parent=None, cfg=<QProgEdit._qeditorcfg.QEditorCfg object at 0x7f73885d59d0>, tabsClosable=False, tabsMovable=False, msg=None, handlerButtonText=None, runButton=False)

Constructor.

Keywords:

  • parent -- The parent widget.
    • Type: QWidget
    • Default: None
  • cfg -- A configuration backend. By default QEditorCfg is used. Custom backends must have the same API for getting and setting options.
    • Default: <QProgEdit._qeditorcfg.QEditorCfg object at 0x7f73885d59d0>
  • tabsClosable -- Indicates whether a close button should be shown on tabs.
    • Type: bool
    • Default: False
  • tabsMovable -- Indicates whether tabs can be re-ordered.
    • Type: bool
    • Default: False
  • msg -- An informative message for the corner widget.
    • Type: str, unicode, NoneType
    • Default: None
  • handlerButtonText -- Text for a top-right button, which can be clicked to call the handler, or None for no button.
    • Type: str, unicode, NoneType
    • Default: None
  • runButton -- Indicates whether a run-selected-text button should be shown.
    • Type: bool
    • Default: False

function QProgEdit.QTabManager.addTab(title=u'Empty document', select=True)

Adds an empty document tab.

Keywords:

  • title -- A tab title.
    • Type: unicode, str
    • Default: u'Empty document'
  • select -- Indicates whether the newly created tab should be selected.
    • Type: bool
    • Default: True

Returns:

The newly added tab widget.

  • Type: QProgEdit

function QProgEdit.QTabManager.applyCfg()

Applies the configuration.

function QProgEdit.QTabManager.closeTab(index=None)

Closes a tab.

Keywords:

  • index -- A tab index (see tabIndex).
    • Default: None

function QProgEdit.QTabManager.isAnyModified()

Checks if one or more of the tabs have been modified.

Returns:

True if (one of) the tab(s) is modified, False otherwise.

  • Type: bool

function QProgEdit.QTabManager.runSelectedText()

Emits the execute signal with the selected text.

function QProgEdit.QTabManager.runText()

Emits the execute signal with the current text.

function QProgEdit.QTabManager.selectTab(index)

Switches to a specific tab.

Arguments:

  • index -- A tab index, as understood by tabIndex.

function QProgEdit.QTabManager.selectedText(index=None, currentLineFallback=False)

Returns the selected text for a specific tab. For details, see QProgEdit.QEditor.

Keywords:

  • index -- A tab index, as understood by tabIndex.
    • Default: None
  • currentLineFallback -- No description
    • Default: False

Returns:

The selected text.

function QProgEdit.QTabManager.setFocus(index=None)

Focuses a specific tab.

Keywords:

  • index -- A tab index, as understood by tabIndex.
    • Default: None

function QProgEdit.QTabManager.setText(text, index=None)

Sets the text on a specific tab.

Arguments:

  • text -- The new text.

Keywords:

  • index -- A tab index, as understood by tabIndex.
    • Default: None

function QProgEdit.QTabManager.switchTabLeft()

Switches to the tab on the left.

function QProgEdit.QTabManager.switchTabRight()

Switches to the tab on the left.

function QProgEdit.QTabManager.tab(index=None)

Returns the QProgEdit instance for a given tab.

Keywords:

  • index -- Specifies the tab, either by a name (i.e. the name on a tab), an index, or None to get the current tab.
    • Type: int, str, unicode, NoneType
    • Default: None

Returns:

A tab, or None if no matching tab was found.

  • Type: QProgEdit, NoneType

function QProgEdit.QTabManager.tabChanged(index)

Is called when the current tab must be updated, for example because a new tab is selected.

Arguments:

  • index -- The index of the newly selected tab.
    • Type: int

function QProgEdit.QTabManager.tabIndex(index=None)

Returns the index for a given tab.

Keywords:

  • index -- Specifies the tab, either by a name (i.e. the name on a tab), an index, or None to get the current tab.
    • Type: int, str, unicode, NoneType
    • Default: None

Returns:

A tab index, or None if no matching tab was found.

  • Type: int, NoneType

function QProgEdit.QTabManager.tabs()

Gets all tabs.

Returns:

A list of all tab widgets.

  • Type: list

function QProgEdit.QTabManager.text(index=None)

Gets the text on a specific tab.

Keywords:

  • index -- A tab index, as understood by tabIndex.
    • Default: None

Returns:

The text or None if the tab does not exist.

function QProgEdit.QTabManager.toggleFind(visible)

Toggle the visibility of the find widget.

Arguments:

  • visible -- Visibility status.
    • Type: bool

function QProgEdit.QTabManager.togglePrefs(visible)

Toggle the visibility of the preferences widget.

Arguments:

  • visible -- Visibility status.
    • Type: bool

class QProgEdit.QUiLoader

A simple base class that implements dynamic UI loading for widgets.

function QProgEdit.QUiLoader.loadUi(name)

Load a UI.

Arguments:

  • name -- The name of the UI file, without the .ui extension.
    • Type: unicode