__main__.py 7.66 KB
Newer Older
1
import sys, os, subprocess, time
2 3

import initExample
4
from pyqtgraph.Qt import QtCore, QtGui, USE_PYSIDE
5

6 7 8 9 10
if USE_PYSIDE:
    from exampleLoaderTemplate_pyside import Ui_Form
else:
    from exampleLoaderTemplate_pyqt import Ui_Form
    
11
import os, sys
12
from pyqtgraph.pgcollections import OrderedDict
13 14 15 16

examples = OrderedDict([
    ('Command-line usage', 'CLIexample.py'),
    ('Basic Plotting', 'Plotting.py'),
Luke Campagnola's avatar
Luke Campagnola committed
17
    ('ImageView', 'ImageView.py'),
18
    ('ParameterTree', 'parametertree.py'),
19
    ('Crosshair / Mouse interaction', 'crosshair.py'),
Luke Campagnola's avatar
Luke Campagnola committed
20
    ('Data Slicing', 'DataSlicing.py'),
21
    ('Plot Customization', 'customPlot.py'),
Luke Campagnola's avatar
Luke Campagnola committed
22
    ('Dock widgets', 'dockarea.py'),
23
    ('Console', 'ConsoleWidget.py'),
Luke Campagnola's avatar
Luke Campagnola committed
24
    ('Histograms', 'histogram.py'),
25
    ('Remote Plotting', 'RemoteSpeedTest.py'),
26
    ('GraphicsItems', OrderedDict([
Luke Campagnola's avatar
Luke Campagnola committed
27
        ('Scatter Plot', 'ScatterPlot.py'),
Luke Campagnola's avatar
Luke Campagnola committed
28
        #('PlotItem', 'PlotItem.py'),
29
        ('IsocurveItem', 'isocurve.py'),
30 31
        ('ImageItem - video', 'ImageItem.py'),
        ('ImageItem - draw', 'Draw.py'),
Luke Campagnola's avatar
Luke Campagnola committed
32
        ('Region-of-Interest', 'ROIExamples.py'),
33
        ('GraphicsLayout', 'GraphicsLayout.py'),
34
        ('LegendItem', 'Legend.py'),
Luke Campagnola's avatar
Luke Campagnola committed
35
        ('Text Item', 'text.py'),
36
        ('Linked Views', 'linkedViews.py'),
37
        ('Arrow', 'Arrow.py'),
Luke Campagnola's avatar
Luke Campagnola committed
38 39
        ('ViewBox', 'ViewBox.py'),
    ])),
Luke Campagnola's avatar
Luke Campagnola committed
40 41 42 43 44
    ('Benchmarks', OrderedDict([
        ('Video speed test', 'VideoSpeedTest.py'),
        ('Line Plot update', 'PlotSpeedTest.py'),
        ('Scatter Plot update', 'ScatterPlotSpeedTest.py'),
    ])),
Luke Campagnola's avatar
Luke Campagnola committed
45 46
    ('3D Graphics', OrderedDict([
        ('Volumetric', 'GLVolumeItem.py'),
47 48
        ('Isosurface', 'GLIsosurface.py'),
        ('Surface Plot', 'GLSurfacePlot.py'),
49
        ('Scatter Plot', 'GLScatterPlotItem.py'),
50
        ('Shaders', 'GLshaders.py'),
51
        ('Line Plot', 'GLLinePlotItem.py'),
52 53
        ('Mesh', 'GLMeshItem.py'),
        ('Image', 'GLImageItem.py'),
54 55 56
    ])),
    ('Widgets', OrderedDict([
        ('PlotWidget', 'PlotWidget.py'),
Luke Campagnola's avatar
Luke Campagnola committed
57
        ('SpinBox', 'SpinBox.py'),
58
        ('ConsoleWidget', 'ConsoleWidget.py'),
Luke Campagnola's avatar
Luke Campagnola committed
59 60
        ('TreeWidget', 'TreeWidget.py'),
        ('DataTreeWidget', 'DataTreeWidget.py'),
61
        ('GradientWidget', 'GradientWidget.py'),
Luke Campagnola's avatar
Luke Campagnola committed
62
        #('TableWidget', '../widgets/TableWidget.py'),
Luke Campagnola's avatar
Luke Campagnola committed
63
        ('ColorButton', 'ColorButton.py'),
Luke Campagnola's avatar
Luke Campagnola committed
64 65
        #('CheckTable', '../widgets/CheckTable.py'),
        #('VerticalLabel', '../widgets/VerticalLabel.py'),
66
        ('JoystickButton', 'JoystickButton.py'),
67
    ])),
68
    
69 70
    ('GraphicsScene', 'GraphicsScene.py'),
    ('Flowcharts', 'Flowchart.py'),
Luke Campagnola's avatar
Luke Campagnola committed
71
    ('Custom Flowchart Nodes', 'FlowchartCustomNode.py'),
Luke Campagnola's avatar
Luke Campagnola committed
72 73
    #('Canvas', '../canvas'),
    #('MultiPlotWidget', 'MultiPlotWidget.py'),
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
])

path = os.path.abspath(os.path.dirname(__file__))

class ExampleLoader(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_Form()
        self.cw = QtGui.QWidget()
        self.setCentralWidget(self.cw)
        self.ui.setupUi(self.cw)
        
        global examples
        self.populateTree(self.ui.exampleTree.invisibleRootItem(), examples)
        self.ui.exampleTree.expandAll()
        
Luke Campagnola's avatar
Luke Campagnola committed
90
        self.resize(1000,500)
91
        self.show()
Luke Campagnola's avatar
Luke Campagnola committed
92
        self.ui.splitter.setSizes([250,750])
93 94 95
        self.ui.loadBtn.clicked.connect(self.loadFile)
        self.ui.exampleTree.currentItemChanged.connect(self.showFile)
        self.ui.exampleTree.itemDoubleClicked.connect(self.loadFile)
96 97
        self.ui.pyqtCheck.toggled.connect(self.pyqtToggled)
        self.ui.pysideCheck.toggled.connect(self.pysideToggled)
98

99 100 101 102 103 104 105 106
    def pyqtToggled(self, b):
        if b:
            self.ui.pysideCheck.setChecked(False)
        
    def pysideToggled(self, b):
        if b:
            self.ui.pyqtCheck.setChecked(False)
        
107 108

    def populateTree(self, root, examples):
109
        for key, val in examples.items():
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
            item = QtGui.QTreeWidgetItem([key])
            if isinstance(val, basestring):
                item.file = val
            else:
                self.populateTree(item, val)
            root.addChild(item)
            
    
    def currentFile(self):
        item = self.ui.exampleTree.currentItem()
        if hasattr(item, 'file'):
            global path
            return os.path.join(path, item.file)
        return None
    
    def loadFile(self):
        fn = self.currentFile()
127 128 129 130 131
        extra = []
        if self.ui.pyqtCheck.isChecked():
            extra.append('pyqt')
        elif self.ui.pysideCheck.isChecked():
            extra.append('pyside')
132 133 134
        
        if self.ui.forceGraphicsCheck.isChecked():
            extra.append(str(self.ui.forceGraphicsCombo.currentText()))
135

136 137
        if fn is None:
            return
138
        if sys.platform.startswith('win'):
139
            os.spawnl(os.P_NOWAIT, sys.executable, '"'+sys.executable+'"', '"' + fn + '"', *extra)
140
        else:
141
            os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, fn, *extra)
142 143 144 145 146 147 148
        
            
    def showFile(self):
        fn = self.currentFile()
        if fn is None:
            self.ui.codeView.clear()
            return
Luke Campagnola's avatar
Luke Campagnola committed
149 150
        if os.path.isdir(fn):
            fn = os.path.join(fn, '__main__.py')
151 152 153 154 155 156 157
        text = open(fn).read()
        self.ui.codeView.setPlainText(text)

def run():
    app = QtGui.QApplication([])
    loader = ExampleLoader()
    
Luke Campagnola's avatar
Luke Campagnola committed
158
    app.exec_()
159

160 161 162 163 164 165 166 167 168 169 170 171
def buildFileList(examples, files=None):
    if files == None:
        files = []
    for key, val in examples.items():
        #item = QtGui.QTreeWidgetItem([key])
        if isinstance(val, basestring):
            #item.file = val
            files.append((key,val))
        else:
            buildFileList(val, files)
    return files
            
Luke Campagnola's avatar
Luke Campagnola committed
172
def testFile(name, f, exe, lib, graphicsSystem=None):
173 174 175
    global path
    fn =  os.path.join(path,f)
    #print "starting process: ", fn
176
    os.chdir(path)
177 178 179
    sys.stdout.write(name)
    sys.stdout.flush()
    
180 181 182
    import1 = "import %s" % lib if lib != '' else ''
    import2 = os.path.splitext(os.path.split(fn)[1])[0]
    graphicsSystem = '' if graphicsSystem is None else "pg.QtGui.QApplication.setGraphicsSystem('%s')" % graphicsSystem
183 184
    code = """
try:
185
    %s
186
    import initExample
187
    import pyqtgraph as pg
188 189
    %s
    import %s
Luke Campagnola's avatar
Luke Campagnola committed
190
    import sys
191
    print("test complete")
Luke Campagnola's avatar
Luke Campagnola committed
192 193
    sys.stdout.flush()
    import time
194 195 196 197 198 199 200
    while True:  ## run a little event loop
        pg.QtGui.QApplication.processEvents()
        time.sleep(0.01)
except:
    print("test failed")
    raise

Luke Campagnola's avatar
Luke Campagnola committed
201 202
"""  % (import1, graphicsSystem, import2)

Luke Campagnola's avatar
Luke Campagnola committed
203
    process = subprocess.Popen(['exec %s -i' % (exe)], shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
204 205 206 207 208 209 210 211 212 213 214 215 216 217
    process.stdin.write(code.encode('UTF-8'))
    #process.stdin.close()
    output = ''
    fail = False
    while True:
        c = process.stdout.read(1).decode()
        output += c
        #sys.stdout.write(c)
        #sys.stdout.flush()
        if output.endswith('test complete'):
            break
        if output.endswith('test failed'):
            fail = True
            break
218
    time.sleep(1)
Luke Campagnola's avatar
Luke Campagnola committed
219 220
    process.kill()
    #process.wait()
221
    res = process.communicate()
Luke Campagnola's avatar
Luke Campagnola committed
222 223
    
    if fail or 'exception' in res[1].decode().lower() or 'error' in res[1].decode().lower():
224 225 226
        print('.' * (50-len(name)) + 'FAILED')
        print(res[0].decode())
        print(res[1].decode())
227
    else:
228
        print('.' * (50-len(name)) + 'passed')
229 230 231
    


232
if __name__ == '__main__':
233 234
    if '--test' in sys.argv[1:]:
        files = buildFileList(examples)
235 236 237 238 239 240 241 242 243
        if '--pyside' in sys.argv[1:]:
            lib = 'PySide'
        elif '--pyqt' in sys.argv[1:]:
            lib = 'PyQt4'
        else:
            lib = ''
            
        exe = sys.executable
        print("Running tests:", lib, sys.executable)
244
        for f in files:
245
            testFile(f[0], f[1], exe, lib)
246 247
    else: 
        run()