Skip to content

eort/QProgEdit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

module QProgEdit

QProgEdit

v2.0.0

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) 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 sip
sip.setapi('QString', 1)

import sys
from PyQt4 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):

	treeWidgetItem.activate()

def main():
	
	"""Runs a simple QProgEdit demonstration."""

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

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

	tabManager = QTabManager(handlerButtonText=u'apply')
	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)

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

	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)
	tab.setText(
		u'def test():\n\tprint undefined_var\n\tbuiltin_var\n\ntest()\n')
	print tab.symbols()

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

	sys.exit(app.exec_())

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:

  • line -- The line number.
    • Type: int
  • margin -- The margin 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.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 the 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.
    • Default: <class 'QProgEdit._qsymboltreewidgetitem.QSymbolTreeWidgetItem'>
    • Type: type

function QProgEdit.QEditor.setText(text)

Sets the editor contents.

Arguments:

  • text -- A text string. This can be a str object, which is assumed to be in utf-8 encoding, a Unicode object, or a QString.
    • Type: str, unicode, QString

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.
    • Default: None
    • Type: QWidget, NoneType

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.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.
    • Default: u''
    • Type: unicode

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.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(index=0, line=0)

Updates the cursor position.

Keywords:

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

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

class QProgEdit.QLexer

A themeable wrapper around the standard Lexer system.

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

Constructor.

Arguments:

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

Keywords:

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

function QProgEdit.QLexer.description(style)

Gives a style description for the generic Lexer.

Arguments:

  • style -- The style number.
    • Type: int

Returns:

The 'Default' QString for style 0 and empty QStrings for all other styles.

  • Type: QString

class QProgEdit.QProgEdit

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

function QProgEdit.QProgEdit.__init__(tabManager, title=u'Empty document', dPrint=None, **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:

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

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:

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

function QProgEdit.QSymbolTreeWidgetItem.activate()

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

class QProgEdit.QTabCornerWidget

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

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

Constructor.

Arguments:

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

Keywords:

  • msg -- An informative text message.
    • Default: None
    • Type: str, unicode, NoneType
  • handlerButtonText -- Text for a top-right button, which can be clicked to call the handler, or None for no button.
    • Default: None
    • Type: str, unicode, NoneType

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 QProgEdit.QTabManager

A tab manager that contains multiple QProgEdit tabs.

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

Constructor.

Keywords:

  • tabsClosable -- Indicates whether a close button should be shown on tabs.
    • Default: False
    • Type: bool
  • handlerButtonText -- Text for a top-right button, which can be clicked to call the handler, or None for no button.
    • Default: None
    • Type: str, unicode, NoneType
  • parent -- The parent widget.
    • Default: None
    • Type: QWidget
  • msg -- An informative message for the corner widget.
    • Default: None
    • Type: str, unicode, NoneType
  • 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 0x7f56df4a6c30>
  • tabsMovable -- Indicates whether tabs can be re-ordered.
    • Default: False
    • Type: bool

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

Adds an empty document tab.

Keywords:

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

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.selectTab(index)

Switches to a specific tab.

Arguments:

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

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.
    • Default: None
    • Type: int, str, unicode, NoneType

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.
    • Default: None
    • Type: int, str, unicode, NoneType

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

module QProgEdit.py3

This modules defines some built-ins that are not available in Python 3, but are assumed to exist by QProgEdit.

About

QProgEdit is a PyQt4 widget that implements a full-featured text editor component

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 99.8%
  • Shell 0.2%