2013-01-30 20:56:08 +00:00
|
|
|
from .. import functions as fn
|
2013-02-13 04:10:25 +00:00
|
|
|
from .GraphicsObject import GraphicsObject
|
|
|
|
from .ScatterPlotItem import ScatterPlotItem
|
2013-12-22 07:08:39 +00:00
|
|
|
from ..Qt import QtGui, QtCore
|
2013-01-30 20:56:08 +00:00
|
|
|
import numpy as np
|
2013-12-23 15:22:53 +00:00
|
|
|
from .. import getConfigOption
|
2013-01-30 20:56:08 +00:00
|
|
|
|
|
|
|
__all__ = ['GraphItem']
|
|
|
|
|
|
|
|
|
|
|
|
class GraphItem(GraphicsObject):
|
2013-02-10 22:45:16 +00:00
|
|
|
"""A GraphItem displays graph information as
|
|
|
|
a set of nodes connected by lines (as in 'graph theory', not 'graphics').
|
|
|
|
Useful for drawing networks, trees, etc.
|
2013-01-30 20:56:08 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, **kwds):
|
|
|
|
GraphicsObject.__init__(self)
|
|
|
|
self.scatter = ScatterPlotItem()
|
|
|
|
self.scatter.setParentItem(self)
|
|
|
|
self.adjacency = None
|
|
|
|
self.pos = None
|
|
|
|
self.picture = None
|
|
|
|
self.pen = 'default'
|
|
|
|
self.setData(**kwds)
|
|
|
|
|
|
|
|
def setData(self, **kwds):
|
|
|
|
"""
|
|
|
|
Change the data displayed by the graph.
|
|
|
|
|
2014-03-01 14:37:59 +00:00
|
|
|
============== =======================================================================
|
2014-02-05 20:04:33 +00:00
|
|
|
**Arguments:**
|
|
|
|
pos (N,2) array of the positions of each node in the graph.
|
|
|
|
adj (M,2) array of connection data. Each row contains indexes
|
|
|
|
of two nodes that are connected.
|
|
|
|
pen The pen to use when drawing lines between connected
|
|
|
|
nodes. May be one of:
|
2013-02-10 22:45:16 +00:00
|
|
|
|
2014-02-05 20:04:33 +00:00
|
|
|
* QPen
|
|
|
|
* a single argument to pass to pg.mkPen
|
|
|
|
* a record array of length M
|
|
|
|
with fields (red, green, blue, alpha, width). Note
|
|
|
|
that using this option may have a significant performance
|
|
|
|
cost.
|
|
|
|
* None (to disable connection drawing)
|
|
|
|
* 'default' to use the default foreground color.
|
2013-02-10 22:45:16 +00:00
|
|
|
|
2014-03-03 17:56:15 +00:00
|
|
|
symbolPen The pen(s) used for drawing nodes.
|
|
|
|
symbolBrush The brush(es) used for drawing nodes.
|
2014-02-05 20:04:33 +00:00
|
|
|
``**opts`` All other keyword arguments are given to
|
|
|
|
:func:`ScatterPlotItem.setData() <pyqtgraph.ScatterPlotItem.setData>`
|
|
|
|
to affect the appearance of nodes (symbol, size, brush,
|
|
|
|
etc.)
|
2014-03-01 14:37:59 +00:00
|
|
|
============== =======================================================================
|
2013-01-30 20:56:08 +00:00
|
|
|
"""
|
|
|
|
if 'adj' in kwds:
|
|
|
|
self.adjacency = kwds.pop('adj')
|
2014-03-03 17:56:15 +00:00
|
|
|
if self.adjacency.dtype.kind not in 'iu':
|
|
|
|
raise Exception("adjacency array must have int or unsigned type.")
|
|
|
|
self._update()
|
2013-01-30 20:56:08 +00:00
|
|
|
if 'pos' in kwds:
|
|
|
|
self.pos = kwds['pos']
|
2014-03-03 17:56:15 +00:00
|
|
|
self._update()
|
2013-01-30 20:56:08 +00:00
|
|
|
if 'pen' in kwds:
|
|
|
|
self.setPen(kwds.pop('pen'))
|
2014-03-03 17:56:15 +00:00
|
|
|
self._update()
|
|
|
|
|
2013-01-30 20:56:08 +00:00
|
|
|
if 'symbolPen' in kwds:
|
|
|
|
kwds['pen'] = kwds.pop('symbolPen')
|
2014-03-03 17:56:15 +00:00
|
|
|
if 'symbolBrush' in kwds:
|
|
|
|
kwds['brush'] = kwds.pop('symbolBrush')
|
2013-01-30 20:56:08 +00:00
|
|
|
self.scatter.setData(**kwds)
|
|
|
|
self.informViewBoundsChanged()
|
|
|
|
|
2014-03-03 17:56:15 +00:00
|
|
|
def _update(self):
|
|
|
|
self.picture = None
|
|
|
|
self.prepareGeometryChange()
|
|
|
|
self.update()
|
|
|
|
|
2014-02-28 21:16:13 +00:00
|
|
|
def setPen(self, *args, **kwargs):
|
2014-03-01 14:37:59 +00:00
|
|
|
"""
|
|
|
|
Set the pen used to draw graph lines.
|
|
|
|
May be:
|
|
|
|
|
|
|
|
* None to disable line drawing
|
|
|
|
* Record array with fields (red, green, blue, alpha, width)
|
|
|
|
* Any set of arguments and keyword arguments accepted by
|
|
|
|
:func:`mkPen <pyqtgraph.mkPen>`.
|
|
|
|
* 'default' to use the default foreground color.
|
|
|
|
"""
|
|
|
|
if len(args) == 1 and len(kwargs) == 0:
|
|
|
|
self.pen = args[0]
|
|
|
|
else:
|
|
|
|
self.pen = fn.mkPen(*args, **kwargs)
|
2013-01-30 20:56:08 +00:00
|
|
|
self.picture = None
|
2014-03-03 17:56:15 +00:00
|
|
|
self.update()
|
2013-01-30 20:56:08 +00:00
|
|
|
|
|
|
|
def generatePicture(self):
|
2013-12-22 07:08:39 +00:00
|
|
|
self.picture = QtGui.QPicture()
|
2013-01-30 20:56:08 +00:00
|
|
|
if self.pen is None or self.pos is None or self.adjacency is None:
|
|
|
|
return
|
|
|
|
|
2013-12-22 07:08:39 +00:00
|
|
|
p = QtGui.QPainter(self.picture)
|
2013-01-30 20:56:08 +00:00
|
|
|
try:
|
|
|
|
pts = self.pos[self.adjacency]
|
|
|
|
pen = self.pen
|
|
|
|
if isinstance(pen, np.ndarray):
|
|
|
|
lastPen = None
|
|
|
|
for i in range(pts.shape[0]):
|
|
|
|
pen = self.pen[i]
|
|
|
|
if np.any(pen != lastPen):
|
|
|
|
lastPen = pen
|
|
|
|
if pen.dtype.fields is None:
|
2013-12-22 07:08:39 +00:00
|
|
|
p.setPen(fn.mkPen(color=(pen[0], pen[1], pen[2], pen[3]), width=1))
|
2013-01-30 20:56:08 +00:00
|
|
|
else:
|
2013-12-22 07:08:39 +00:00
|
|
|
p.setPen(fn.mkPen(color=(pen['red'], pen['green'], pen['blue'], pen['alpha']), width=pen['width']))
|
|
|
|
p.drawLine(QtCore.QPointF(*pts[i][0]), QtCore.QPointF(*pts[i][1]))
|
2013-01-30 20:56:08 +00:00
|
|
|
else:
|
|
|
|
if pen == 'default':
|
2013-12-22 07:08:39 +00:00
|
|
|
pen = getConfigOption('foreground')
|
|
|
|
p.setPen(fn.mkPen(pen))
|
2013-01-30 20:56:08 +00:00
|
|
|
pts = pts.reshape((pts.shape[0]*pts.shape[1], pts.shape[2]))
|
|
|
|
path = fn.arrayToQPath(x=pts[:,0], y=pts[:,1], connect='pairs')
|
|
|
|
p.drawPath(path)
|
|
|
|
finally:
|
|
|
|
p.end()
|
|
|
|
|
|
|
|
def paint(self, p, *args):
|
|
|
|
if self.picture == None:
|
|
|
|
self.generatePicture()
|
2013-12-22 07:08:39 +00:00
|
|
|
if getConfigOption('antialias') is True:
|
2013-03-26 17:46:26 +00:00
|
|
|
p.setRenderHint(p.Antialiasing)
|
2013-01-30 20:56:08 +00:00
|
|
|
self.picture.play(p)
|
|
|
|
|
|
|
|
def boundingRect(self):
|
|
|
|
return self.scatter.boundingRect()
|
|
|
|
|
2013-11-07 17:05:05 +00:00
|
|
|
def dataBounds(self, *args, **kwds):
|
|
|
|
return self.scatter.dataBounds(*args, **kwds)
|
|
|
|
|
|
|
|
def pixelPadding(self):
|
|
|
|
return self.scatter.pixelPadding()
|
2013-01-30 20:56:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|