__main__.py 3.34 KB
Newer Older
1
2
3
4
5
import sys, os
## make sure this pyqtgraph is importable before any others
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
from pyqtgraph.Qt import QtCore, QtGui

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from exampleLoaderTemplate import Ui_Form
import os, sys
from collections import OrderedDict

examples = OrderedDict([
    ('Command-line usage', 'CLIexample.py'),
    ('Basic Plotting', 'Plotting.py'),
    ('GraphicsItems', OrderedDict([
        ('PlotItem', 'PlotItem.py'),
        ('ImageItem - video', 'ImageItem.py'),
        ('ImageItem - draw', 'Draw.py'),
        ('Region-of-Interest', 'ROItypes.py'),
        ('GraphicsLayout', 'GraphicsLayout.py'),
        ('Scatter Plot', 'ScatterPlot.py'),
        ('ViewBox', 'ViewBox.py'),
        ('Arrow', 'Arrow.py'),
    ])),
    ('Widgets', OrderedDict([
        ('PlotWidget', 'PlotWidget.py'),
        ('SpinBox', '../widgets/SpinBox.py'),
        ('TreeWidget', '../widgets/TreeWidget.py'),
        ('DataTreeWidget', '../widgets/DataTreeWidget.py'),
        ('GradientWidget', '../widgets/GradientWidget.py'),
        ('TableWidget', '../widgets/TableWidget.py'),
        ('ColorButton', '../widgets/ColorButton.py'),
        ('CheckTable', '../widgets/CheckTable.py'),
        ('VerticalLabel', '../widgets/VerticalLabel.py'),
        ('JoystickButton', '../widgets/JoystickButton.py'),
    ])),
    ('ImageView', 'ImageView.py'),
    ('GraphicsScene', 'GraphicsScene.py'),
    ('Flowcharts', 'Flowchart.py'),
    ('ParameterTree', '../parametertree'),
    ('Canvas', '../canvas'),
    ('MultiPlotWidget', 'MultiPlotWidget.py'),
])

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()
        
        self.resize(900,500)
        self.show()
        self.ui.splitter.setSizes([150,750])
        self.ui.loadBtn.clicked.connect(self.loadFile)
        self.ui.exampleTree.currentItemChanged.connect(self.showFile)
        self.ui.exampleTree.itemDoubleClicked.connect(self.loadFile)


    def populateTree(self, root, examples):
        for key, val in examples.iteritems():
            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()
        if fn is None:
            return
        os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, fn)
        
            
    def showFile(self):
        fn = self.currentFile()
        if fn is None:
            self.ui.codeView.clear()
            return
        text = open(fn).read()
        self.ui.codeView.setPlainText(text)

def run():
    app = QtGui.QApplication([])
    loader = ExampleLoader()
    
    if sys.flags.interactive != 1:
        app.exec_()

if __name__ == '__main__':
105
    run()