2013-12-23 12:51:33 +00:00
|
|
|
from ..Qt import QtCore, QtGui
|
|
|
|
from ..widgets.TreeWidget import TreeWidget
|
2012-09-13 14:12:59 +00:00
|
|
|
import os, weakref, re
|
2013-08-19 03:02:01 +00:00
|
|
|
from .ParameterItem import ParameterItem
|
2012-03-02 02:55:32 +00:00
|
|
|
#import functions as fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParameterTree(TreeWidget):
|
2014-05-10 19:30:51 +00:00
|
|
|
"""Widget used to display or control data from a hierarchy of Parameters"""
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2012-09-09 23:13:30 +00:00
|
|
|
def __init__(self, parent=None, showHeader=True):
|
2014-05-10 19:30:51 +00:00
|
|
|
"""
|
|
|
|
============== ========================================================
|
|
|
|
**Arguments:**
|
|
|
|
parent (QWidget) An optional parent widget
|
|
|
|
showHeader (bool) If True, then the QTreeView header is displayed.
|
|
|
|
============== ========================================================
|
|
|
|
"""
|
2012-03-02 02:55:32 +00:00
|
|
|
TreeWidget.__init__(self, parent)
|
|
|
|
self.setVerticalScrollMode(self.ScrollPerPixel)
|
|
|
|
self.setHorizontalScrollMode(self.ScrollPerPixel)
|
|
|
|
self.setAnimated(False)
|
|
|
|
self.setColumnCount(2)
|
|
|
|
self.setHeaderLabels(["Parameter", "Value"])
|
|
|
|
self.setAlternatingRowColors(True)
|
|
|
|
self.paramSet = None
|
|
|
|
self.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)
|
2012-09-09 23:13:30 +00:00
|
|
|
self.setHeaderHidden(not showHeader)
|
2012-03-02 02:55:32 +00:00
|
|
|
self.itemChanged.connect(self.itemChangedEvent)
|
|
|
|
self.lastSel = None
|
|
|
|
self.setRootIsDecorated(False)
|
|
|
|
|
2012-03-11 15:59:45 +00:00
|
|
|
def setParameters(self, param, showTop=True):
|
2014-05-10 19:30:51 +00:00
|
|
|
"""
|
|
|
|
Set the top-level :class:`Parameter <pyqtgraph.parametertree.Parameter>`
|
|
|
|
to be displayed in this ParameterTree.
|
|
|
|
|
|
|
|
If *showTop* is False, then the top-level parameter is hidden and only
|
|
|
|
its children will be visible. This is a convenience method equivalent
|
|
|
|
to::
|
|
|
|
|
|
|
|
tree.clear()
|
|
|
|
tree.addParameters(param, showTop)
|
|
|
|
"""
|
2012-03-11 15:59:45 +00:00
|
|
|
self.clear()
|
|
|
|
self.addParameters(param, showTop=showTop)
|
|
|
|
|
|
|
|
def addParameters(self, param, root=None, depth=0, showTop=True):
|
2014-05-10 19:30:51 +00:00
|
|
|
"""
|
|
|
|
Adds one top-level :class:`Parameter <pyqtgraph.parametertree.Parameter>`
|
|
|
|
to the view.
|
|
|
|
|
|
|
|
============== ==========================================================
|
|
|
|
**Arguments:**
|
|
|
|
param The :class:`Parameter <pyqtgraph.parametertree.Parameter>`
|
|
|
|
to add.
|
|
|
|
root The item within the tree to which *param* should be added.
|
|
|
|
By default, *param* is added as a top-level item.
|
|
|
|
showTop If False, then *param* will be hidden, and only its
|
|
|
|
children will be visible in the tree.
|
|
|
|
============== ==========================================================
|
|
|
|
"""
|
2012-03-02 02:55:32 +00:00
|
|
|
item = param.makeTreeItem(depth=depth)
|
|
|
|
if root is None:
|
|
|
|
root = self.invisibleRootItem()
|
|
|
|
## Hide top-level item
|
|
|
|
if not showTop:
|
|
|
|
item.setText(0, '')
|
|
|
|
item.setSizeHint(0, QtCore.QSize(1,1))
|
|
|
|
item.setSizeHint(1, QtCore.QSize(1,1))
|
|
|
|
depth -= 1
|
|
|
|
root.addChild(item)
|
|
|
|
item.treeWidgetChanged()
|
|
|
|
|
|
|
|
for ch in param:
|
2012-03-11 15:59:45 +00:00
|
|
|
self.addParameters(ch, root=item, depth=depth+1)
|
|
|
|
|
|
|
|
def clear(self):
|
2014-05-10 19:30:51 +00:00
|
|
|
"""
|
|
|
|
Remove all parameters from the tree.
|
|
|
|
"""
|
|
|
|
self.invisibleRootItem().takeChildren()
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
def focusNext(self, item, forward=True):
|
2014-05-10 19:30:51 +00:00
|
|
|
"""Give input focus to the next (or previous) item after *item*
|
|
|
|
"""
|
2012-03-02 02:55:32 +00:00
|
|
|
while True:
|
|
|
|
parent = item.parent()
|
|
|
|
if parent is None:
|
|
|
|
return
|
|
|
|
nextItem = self.nextFocusableChild(parent, item, forward=forward)
|
|
|
|
if nextItem is not None:
|
|
|
|
nextItem.setFocus()
|
|
|
|
self.setCurrentItem(nextItem)
|
|
|
|
return
|
|
|
|
item = parent
|
|
|
|
|
|
|
|
def focusPrevious(self, item):
|
|
|
|
self.focusNext(item, forward=False)
|
|
|
|
|
|
|
|
def nextFocusableChild(self, root, startItem=None, forward=True):
|
|
|
|
if startItem is None:
|
|
|
|
if forward:
|
|
|
|
index = 0
|
|
|
|
else:
|
|
|
|
index = root.childCount()-1
|
|
|
|
else:
|
|
|
|
if forward:
|
|
|
|
index = root.indexOfChild(startItem) + 1
|
|
|
|
else:
|
|
|
|
index = root.indexOfChild(startItem) - 1
|
|
|
|
|
|
|
|
if forward:
|
2012-05-11 22:05:41 +00:00
|
|
|
inds = list(range(index, root.childCount()))
|
2012-03-02 02:55:32 +00:00
|
|
|
else:
|
2012-05-11 22:05:41 +00:00
|
|
|
inds = list(range(index, -1, -1))
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
for i in inds:
|
|
|
|
item = root.child(i)
|
|
|
|
if hasattr(item, 'isFocusable') and item.isFocusable():
|
|
|
|
return item
|
|
|
|
else:
|
|
|
|
item = self.nextFocusableChild(item, forward=forward)
|
|
|
|
if item is not None:
|
|
|
|
return item
|
|
|
|
return None
|
|
|
|
|
|
|
|
def contextMenuEvent(self, ev):
|
|
|
|
item = self.currentItem()
|
|
|
|
if hasattr(item, 'contextMenuEvent'):
|
|
|
|
item.contextMenuEvent(ev)
|
|
|
|
|
|
|
|
def itemChangedEvent(self, item, col):
|
|
|
|
if hasattr(item, 'columnChangedEvent'):
|
|
|
|
item.columnChangedEvent(col)
|
|
|
|
|
|
|
|
def selectionChanged(self, *args):
|
|
|
|
sel = self.selectedItems()
|
|
|
|
if len(sel) != 1:
|
|
|
|
sel = None
|
2013-08-19 03:02:01 +00:00
|
|
|
if self.lastSel is not None and isinstance(self.lastSel, ParameterItem):
|
2012-03-02 02:55:32 +00:00
|
|
|
self.lastSel.selected(False)
|
|
|
|
if sel is None:
|
|
|
|
self.lastSel = None
|
|
|
|
return
|
|
|
|
self.lastSel = sel[0]
|
|
|
|
if hasattr(sel[0], 'selected'):
|
|
|
|
sel[0].selected(True)
|
|
|
|
return TreeWidget.selectionChanged(self, *args)
|
|
|
|
|
2012-06-22 01:52:34 +00:00
|
|
|
def wheelEvent(self, ev):
|
|
|
|
self.clearSelection()
|
|
|
|
return TreeWidget.wheelEvent(self, ev)
|