# eric7.Templates.TemplateViewer

Module implementing a template viewer and associated classes.

### Global Attributes

None

### Classes

TemplateEntry | Class immplementing a template entry.
TemplateGroup | Class implementing a template group.
TemplateViewer | Class implementing the template viewer.

### Functions

None

## TemplateEntry

Class immplementing a template entry.

### Derived from

QTreeWidgetItem

### Class Attributes

None

### Class Methods

None

### Methods

TemplateEntry | Constructor
__displayText | Private method to generate the display text.
__expandFormattedVariable | Private method to expand a template variable with special formatting.
__extractVariables | Private method to retrieve the list of variables.
getDescription | Public method to get the description of the entry.
getExpandedText | Public method to get the template text with all variables expanded.
getGroupName | Public method to get the name of the group this entry belongs to.
getName | Public method to get the name of the entry.
getTemplateText | Public method to get the template text.
getVariables | Public method to get the list of variables.
setDescription | Public method to update the description of the entry.
setName | Public method to update the name of the entry.
setTemplateText | Public method to update the template text.

### Static Methods

None

#### TemplateEntry (Constructor)

TemplateEntry(parent, name, description, templateText)

Constructor

parent (QWidget)
  parent widget of the template entry
name (str)
  name of the entry
description (str)
  descriptive text for the template
templateText (str)
  text of the template entry

#### TemplateEntry.__displayText

__displayText()

Private method to generate the display text.

Return:
  display text

Return Type:
  str

#### TemplateEntry.__expandFormattedVariable

__expandFormattedVariable(var, val, txt)

Private method to expand a template variable with special formatting.

var (str)
  template variable name
val (str)
  value of the template variable
txt (str)
  template text

Return:
  expanded and formatted variable

Return Type:
  str

#### TemplateEntry.__extractVariables

__extractVariables()

Private method to retrieve the list of variables.

#### TemplateEntry.getDescription

getDescription()

Public method to get the description of the entry.

Return:
  description of the entry

Return Type:
  str

#### TemplateEntry.getExpandedText

getExpandedText(varDict, indent)

Public method to get the template text with all variables expanded.

varDict (dict)
  dictionary containing the texts of each variable with the variable name as key
indent (str)
  indentation of the line receiving he expanded template text

Return:
  a tuple of the expanded template text, the number of lines and the length of the last line

Return Type:
  tuple of (str, int, int)

#### TemplateEntry.getGroupName

getGroupName()

Public method to get the name of the group this entry belongs to.

Return:
  name of the group containing this entry

Return Type:
  str

#### TemplateEntry.getName

getName()

Public method to get the name of the entry.

Return:
  name of the entry

Return Type:
  str

#### TemplateEntry.getTemplateText

getTemplateText()

Public method to get the template text.

Return:
  the template text

Return Type:
  str

#### TemplateEntry.getVariables

getVariables()

Public method to get the list of variables.

Return:
  list of variables

Return Type:
  list of str

#### TemplateEntry.setDescription

setDescription(description)

Public method to update the description of the entry.

description (str)
  description of the entry

#### TemplateEntry.setName

setName(name)

Public method to update the name of the entry.

name (str)
  name of the entry

#### TemplateEntry.setTemplateText

setTemplateText(templateText)

Public method to update the template text.

templateText (str)
  text of the template entry

Up

## TemplateGroup

Class implementing a template group.

### Derived from

QTreeWidgetItem

### Class Attributes

None

### Class Methods

None

### Methods

TemplateGroup | Constructor
addEntry | Public method to add a template entry to this group.
getAllEntries | Public method to retrieve all entries.
getEntry | Public method to get an entry.
getEntryNames | Public method to get the names of all entries, who's name starts with the given string.
getLanguage | Public method to get the name of the group.
getName | Public method to get the name of the group.
hasEntry | Public method to check, if the group has an entry with the given name.
removeAllEntries | Public method to remove all template entries of this group.
removeEntry | Public method to remove a template entry from this group.
setLanguage | Public method to update the name of the group.
setName | Public method to update the name of the group.

### Static Methods

None

#### TemplateGroup (Constructor)

TemplateGroup(parent, name, language="All")

Constructor

parent (QWidget)
  parent widget of the template group
name (str)
  name of the group
language (str)
  programming language for the group

#### TemplateGroup.addEntry

addEntry(name, description, template, quiet=False)

Public method to add a template entry to this group.

name (str)
  name of the entry
description (str)
  description of the entry to add
template (str)
  template text of the entry
quiet (bool)
  flag indicating quiet operation

#### TemplateGroup.getAllEntries

getAllEntries()

Public method to retrieve all entries.

Return:
  list of all entries

Return Type:
  list of TemplateEntry

#### TemplateGroup.getEntry

getEntry(name)

Public method to get an entry.

name (str)
  name of the entry to retrieve

Return:
  reference to the entry

Return Type:
  TemplateEntry

#### TemplateGroup.getEntryNames

getEntryNames(beginning)

Public method to get the names of all entries, who's name starts with the given string.

beginning (str)
  string denoting the beginning of the template name

Return:
  list of entry names found

Return Type:
  list of str

#### TemplateGroup.getLanguage

getLanguage()

Public method to get the name of the group.

Return:
  language of the group

Return Type:
  str

#### TemplateGroup.getName

getName()

Public method to get the name of the group.

Return:
  name of the group

Return Type:
  str

#### TemplateGroup.hasEntry

hasEntry(name)

Public method to check, if the group has an entry with the given name.

name (str)
  name of the entry to check for

Return:
  flag indicating existence

Return Type:
  bool

#### TemplateGroup.removeAllEntries

removeAllEntries()

Public method to remove all template entries of this group.

#### TemplateGroup.removeEntry

removeEntry(name)

Public method to remove a template entry from this group.

name (str)
  name of the entry to be removed

#### TemplateGroup.setLanguage

setLanguage(language)

Public method to update the name of the group.

language (str)
  programming language for the group

#### TemplateGroup.setName

setName(name)

Public method to update the name of the group.

name (str)
  name of the group

Up

## TemplateViewer

Class implementing the template viewer.

### Derived from

QTreeWidget

### Class Attributes

None

### Class Methods

None

### Methods

TemplateViewer | Constructor
__addEntry | Private slot to handle the Add Entry context menu action.
__addGroup | Private slot to handle the Add Group context menu action.
__configure | Private method to open the configuration dialog.
__edit | Private slot to handle the Edit context menu action.
__export | Private slot to handle the Export context menu action.
__getPredefinedVars | Private method to return predefined variables.
__import | Private slot to handle the Import context menu action.
__reload | Private slot to reload the templates.
__remove | Private slot to handle the Remove context menu action.
__resort | Private method to resort the tree.
__showContextMenu | Private slot to show the context menu of the list.
__showHelp | Private method to show some help.
__templateItemActivated | Private slot to handle the activation of an item.
addEntry | Public method to add a template entry.
addGroup | Public method to add a group.
applyNamedTemplate | Public method to apply a template given a template name.
applyTemplate | Public method to apply the template.
changeEntry | Public method to change a template entry.
changeGroup | Public method to rename a group.
getAllGroups | Public method to get all groups.
getGroupNames | Public method to get all group names.
getTemplateNames | Public method to get the names of templates starting with the given string.
hasGroup | Public method to check, if a group with the given name exists.
hasTemplate | Public method to check, if an entry of the given name exists.
readTemplates | Public method to read in the templates file (.ecj).
removeEntry | Public method to remove a template entry.
removeGroup | Public method to remove a group.
save | Public slot to save the templates.
writeTemplates | Public method to write the templates data to a JSON file (.ecj).

### Static Methods

None

#### TemplateViewer (Constructor)

TemplateViewer(parent, viewmanager)

Constructor

parent (QWidget)
  the parent
viewmanager (ViewManager)
  reference to the viewmanager object

#### TemplateViewer.__addEntry

__addEntry()

Private slot to handle the Add Entry context menu action.

#### TemplateViewer.__addGroup

__addGroup()

Private slot to handle the Add Group context menu action.

#### TemplateViewer.__configure

__configure()

Private method to open the configuration dialog.

#### TemplateViewer.__edit

__edit()

Private slot to handle the Edit context menu action.

#### TemplateViewer.__export

__export()

Private slot to handle the Export context menu action.

#### TemplateViewer.__getPredefinedVars

__getPredefinedVars()

Private method to return predefined variables.

Return:
  dictionary of predefined variables and their values

Return Type:
  dict

#### TemplateViewer.__import

__import()

Private slot to handle the Import context menu action.

#### TemplateViewer.__reload

__reload()

Private slot to reload the templates.

#### TemplateViewer.__remove

__remove()

Private slot to handle the Remove context menu action.

#### TemplateViewer.__resort

__resort()

Private method to resort the tree.

#### TemplateViewer.__showContextMenu

__showContextMenu(coord)

Private slot to show the context menu of the list.

coord (QPoint)
  the position of the mouse pointer

#### TemplateViewer.__showHelp

__showHelp()

Private method to show some help.

#### TemplateViewer.__templateItemActivated

__templateItemActivated(itm=None, _col=0)

Private slot to handle the activation of an item.

itm (QTreeWidgetItem)
  reference to the activated item
_col (int)
  column the item was activated in (unused)

#### TemplateViewer.addEntry

addEntry(groupName, name, description, template, quiet=False)

Public method to add a template entry.

groupName (str)
  name of the group to add to
name (str)
  name of the entry to add
description (str)
  description of the entry to add
template (str)
  template text of the entry
quiet (bool)
  flag indicating quiet operation

#### TemplateViewer.addGroup

addGroup(name, language="All")

Public method to add a group.

name (str)
  name of the group to be added
language (str)
  programming language for the group

#### TemplateViewer.applyNamedTemplate

applyNamedTemplate(templateName, groupName=None)

Public method to apply a template given a template name.

templateName (str)
  name of the template item to apply
groupName (str)
  name of the group to get the entry from. None or empty means to apply the first template found with the given name.

#### TemplateViewer.applyTemplate

applyTemplate(itm)

Public method to apply the template.

itm (TemplateEntry)
  reference to the template item to apply

#### TemplateViewer.changeEntry

changeEntry(itm, name, groupName, description, template)

Public method to change a template entry.

itm (TemplateEntry)
  template entry to be changed
name (str)
  new name for the entry
groupName (str)
  name of the group the entry should belong to
description (str)
  description of the entry
template (str)
  template text of the entry

#### TemplateViewer.changeGroup

changeGroup(oldname, newname, language="All")

Public method to rename a group.

oldname (str)
  old name of the group
newname (str)
  new name of the group
language (str)
  programming language for the group

#### TemplateViewer.getAllGroups

getAllGroups()

Public method to get all groups.

Return:
  list of all groups

Return Type:
  list of TemplateGroup

#### TemplateViewer.getGroupNames

getGroupNames()

Public method to get all group names.

Return:
  list of all group names

Return Type:
  list of str

#### TemplateViewer.getTemplateNames

getTemplateNames(start, groupName=None)

Public method to get the names of templates starting with the given string.

start (str)
  start string of the name
groupName (str)
  name of the group to get the entry from. None or empty means to look in all groups.

Return:
  sorted list of matching template names

Return Type:
  list of str

#### TemplateViewer.hasGroup

hasGroup(name)

Public method to check, if a group with the given name exists.

name (str)
  name of the group to be checked for

Return:
  flag indicating an existing group

Return Type:
  bool

#### TemplateViewer.hasTemplate

hasTemplate(entryName, groupName=None)

Public method to check, if an entry of the given name exists.

entryName (str)
  name of the entry to check for
groupName (str)
  name of the group to check for the entry. None or empty means to check all groups.

Return:
  flag indicating the existence

Return Type:
  bool

#### TemplateViewer.readTemplates

readTemplates(filename=None)

Public method to read in the templates file (.ecj).

filename (str)
  name of a templates file to read

#### TemplateViewer.removeEntry

removeEntry(itm)

Public method to remove a template entry.

itm (TemplateEntry)
  template entry to be removed

#### TemplateViewer.removeGroup

removeGroup(itm)

Public method to remove a group.

itm (TemplateGroup)
  template group to be removed

#### TemplateViewer.save

save()

Public slot to save the templates.

#### TemplateViewer.writeTemplates

writeTemplates(filename=None)

Public method to write the templates data to a JSON file (.ecj).

filename (str)
  name of a templates file to write

Return:
  flag indicating success

Return Type:
  bool

Up