exportDialog.py 4.94 KB
Newer Older
1
from pyqtgraph.Qt import QtCore, QtGui, USE_PYSIDE
2 3 4
import pyqtgraph as pg
import pyqtgraph.exporters as exporters

5 6 7 8 9
if USE_PYSIDE:
    from . import exportDialogTemplate_pyside as exportDialogTemplate
else:
    from . import exportDialogTemplate_pyqt as exportDialogTemplate

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

class ExportDialog(QtGui.QWidget):
    def __init__(self, scene):
        QtGui.QWidget.__init__(self)
        self.setVisible(False)
        self.setWindowTitle("Export")
        self.shown = False
        self.currentExporter = None
        self.scene = scene
            
        self.selectBox = QtGui.QGraphicsRectItem()
        self.selectBox.setPen(pg.mkPen('y', width=3, style=QtCore.Qt.DashLine))
        self.selectBox.hide()
        self.scene.addItem(self.selectBox)
        
        self.ui = exportDialogTemplate.Ui_Form()
        self.ui.setupUi(self)
        
        self.ui.closeBtn.clicked.connect(self.close)
        self.ui.exportBtn.clicked.connect(self.exportClicked)
30
        self.ui.copyBtn.clicked.connect(self.copyClicked)
31 32 33 34 35 36
        self.ui.itemTree.currentItemChanged.connect(self.exportItemChanged)
        self.ui.formatList.currentItemChanged.connect(self.exportFormatChanged)
        

    def show(self, item=None):
        if item is not None:
37
            ## Select next exportable parent of the item originally clicked on
38 39
            while not isinstance(item, pg.ViewBox) and not isinstance(item, pg.PlotItem) and item is not None:
                item = item.parentItem()
40 41 42
            ## if this is a ViewBox inside a PlotItem, select the parent instead.
            if isinstance(item, pg.ViewBox) and isinstance(item.parentItem(), pg.PlotItem):
                item = item.parentItem()
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
            self.updateItemList(select=item)
        self.setVisible(True)
        self.activateWindow()
        self.raise_()
        self.selectBox.setVisible(True)
        
        if not self.shown:
            self.shown = True
            vcenter = self.scene.getViewWidget().geometry().center()
            self.setGeometry(vcenter.x()-self.width()/2, vcenter.y()-self.height()/2, self.width(), self.height())
        
    def updateItemList(self, select=None):
        self.ui.itemTree.clear()
        si = QtGui.QTreeWidgetItem(["Entire Scene"])
        si.gitem = self.scene
        self.ui.itemTree.addTopLevelItem(si)
        self.ui.itemTree.setCurrentItem(si)
        si.setExpanded(True)
        for child in self.scene.items():
            if child.parentItem() is None:
                self.updateItemTree(child, si, select=select)
                
    def updateItemTree(self, item, treeItem, select=None):
        si = None
        if isinstance(item, pg.ViewBox):
            si = QtGui.QTreeWidgetItem(['ViewBox'])
        elif isinstance(item, pg.PlotItem):
            si = QtGui.QTreeWidgetItem(['Plot'])
            
        if si is not None:
            si.gitem = item
            treeItem.addChild(si)
            treeItem = si
            if si.gitem is select:
                self.ui.itemTree.setCurrentItem(si)
            
        for ch in item.childItems():
            self.updateItemTree(ch, treeItem, select=select)
        
            
    def exportItemChanged(self, item, prev):
Luke Campagnola's avatar
Luke Campagnola committed
84 85
        if item is None:
            return
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        if item.gitem is self.scene:
            newBounds = self.scene.views()[0].viewRect()
        else:
            newBounds = item.gitem.sceneBoundingRect()
        self.selectBox.setRect(newBounds)
        self.selectBox.show()
        self.updateFormatList()
        
    def updateFormatList(self):
        current = self.ui.formatList.currentItem()
        if current is not None:
            current = str(current.text())
        self.ui.formatList.clear()
        self.exporterClasses = {}
        gotCurrent = False
        for exp in exporters.listExporters():
            self.ui.formatList.addItem(exp.Name)
            self.exporterClasses[exp.Name] = exp
            if exp.Name == current:
                self.ui.formatList.setCurrentRow(self.ui.formatList.count()-1)
                gotCurrent = True
                
        if not gotCurrent:
            self.ui.formatList.setCurrentRow(0)
        
    def exportFormatChanged(self, item, prev):
        if item is None:
            self.currentExporter = None
            self.ui.paramTree.clear()
            return
        expClass = self.exporterClasses[str(item.text())]
        exp = expClass(item=self.ui.itemTree.currentItem().gitem)
        params = exp.parameters()
Luke Campagnola's avatar
Luke Campagnola committed
119 120 121 122
        if params is None:
            self.ui.paramTree.clear()
        else:
            self.ui.paramTree.setParameters(params)
123
        self.currentExporter = exp
124
        self.ui.copyBtn.setEnabled(exp.allowCopy)
125 126 127 128 129
        
    def exportClicked(self):
        self.selectBox.hide()
        self.currentExporter.export()
        
130 131 132 133
    def copyClicked(self):
        self.selectBox.hide()
        self.currentExporter.export(copy=True)
        
134 135 136 137 138 139
    def close(self):
        self.selectBox.setVisible(False)
        self.setVisible(False)