2015-07-12 16:46:12 +00:00
|
|
|
import sys, os
|
2013-07-03 21:52:16 +00:00
|
|
|
if __name__ == "__main__" and (__package__ is None or __package__==''):
|
|
|
|
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
sys.path.insert(0, parent_dir)
|
|
|
|
import examples
|
|
|
|
__package__ = "examples"
|
2015-07-12 22:13:56 +00:00
|
|
|
import pyqtgraph as pg
|
|
|
|
import subprocess
|
|
|
|
from pyqtgraph.python2_3 import basestring
|
|
|
|
from pyqtgraph.Qt import QtGui, USE_PYSIDE, USE_PYQT5
|
|
|
|
|
2013-04-29 12:13:28 +00:00
|
|
|
|
2015-07-12 20:45:39 +00:00
|
|
|
from .utils import buildFileList, testFile, path, examples
|
|
|
|
|
|
|
|
if USE_PYSIDE:
|
|
|
|
from .exampleLoaderTemplate_pyside import Ui_Form
|
|
|
|
elif USE_PYQT5:
|
|
|
|
from .exampleLoaderTemplate_pyqt5 import Ui_Form
|
|
|
|
else:
|
|
|
|
from .exampleLoaderTemplate_pyqt import Ui_Form
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
self.codeBtn = QtGui.QPushButton('Run Edited Code')
|
|
|
|
self.codeLayout = QtGui.QGridLayout()
|
|
|
|
self.ui.codeView.setLayout(self.codeLayout)
|
|
|
|
self.codeLayout.addItem(QtGui.QSpacerItem(100,100,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding), 0, 0)
|
|
|
|
self.codeLayout.addWidget(self.codeBtn, 1, 1)
|
|
|
|
self.codeBtn.hide()
|
|
|
|
|
|
|
|
global examples
|
|
|
|
self.itemCache = []
|
|
|
|
self.populateTree(self.ui.exampleTree.invisibleRootItem(), examples)
|
|
|
|
self.ui.exampleTree.expandAll()
|
|
|
|
|
|
|
|
self.resize(1000,500)
|
|
|
|
self.show()
|
|
|
|
self.ui.splitter.setSizes([250,750])
|
|
|
|
self.ui.loadBtn.clicked.connect(self.loadFile)
|
|
|
|
self.ui.exampleTree.currentItemChanged.connect(self.showFile)
|
|
|
|
self.ui.exampleTree.itemDoubleClicked.connect(self.loadFile)
|
|
|
|
self.ui.codeView.textChanged.connect(self.codeEdited)
|
|
|
|
self.codeBtn.clicked.connect(self.runEditedCode)
|
|
|
|
|
|
|
|
def populateTree(self, root, examples):
|
|
|
|
for key, val in examples.items():
|
|
|
|
item = QtGui.QTreeWidgetItem([key])
|
|
|
|
self.itemCache.append(item) # PyQt 4.9.6 no longer keeps references to these wrappers,
|
|
|
|
# so we need to make an explicit reference or else the .file
|
|
|
|
# attribute will disappear.
|
|
|
|
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, edited=False):
|
|
|
|
|
|
|
|
extra = []
|
|
|
|
qtLib = str(self.ui.qtLibCombo.currentText())
|
|
|
|
gfxSys = str(self.ui.graphicsSystemCombo.currentText())
|
|
|
|
|
|
|
|
if qtLib != 'default':
|
|
|
|
extra.append(qtLib.lower())
|
|
|
|
elif gfxSys != 'default':
|
|
|
|
extra.append(gfxSys)
|
|
|
|
|
|
|
|
if edited:
|
|
|
|
path = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
proc = subprocess.Popen([sys.executable, '-'] + extra, stdin=subprocess.PIPE, cwd=path)
|
|
|
|
code = str(self.ui.codeView.toPlainText()).encode('UTF-8')
|
|
|
|
proc.stdin.write(code)
|
|
|
|
proc.stdin.close()
|
|
|
|
else:
|
|
|
|
fn = self.currentFile()
|
|
|
|
if fn is None:
|
|
|
|
return
|
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
os.spawnl(os.P_NOWAIT, sys.executable, '"'+sys.executable+'"', '"' + fn + '"', *extra)
|
|
|
|
else:
|
|
|
|
os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, fn, *extra)
|
|
|
|
|
|
|
|
def showFile(self):
|
|
|
|
fn = self.currentFile()
|
|
|
|
if fn is None:
|
|
|
|
self.ui.codeView.clear()
|
|
|
|
return
|
|
|
|
if os.path.isdir(fn):
|
|
|
|
fn = os.path.join(fn, '__main__.py')
|
|
|
|
text = open(fn).read()
|
|
|
|
self.ui.codeView.setPlainText(text)
|
|
|
|
self.ui.loadedFileLabel.setText(fn)
|
|
|
|
self.codeBtn.hide()
|
|
|
|
|
|
|
|
def codeEdited(self):
|
|
|
|
self.codeBtn.show()
|
|
|
|
|
|
|
|
def runEditedCode(self):
|
|
|
|
self.loadFile(edited=True)
|
|
|
|
|
|
|
|
def run():
|
|
|
|
app = QtGui.QApplication([])
|
|
|
|
loader = ExampleLoader()
|
|
|
|
|
|
|
|
app.exec_()
|
2012-11-29 21:50:42 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
if __name__ == '__main__':
|
2015-07-12 16:46:12 +00:00
|
|
|
|
2015-02-28 16:05:57 +00:00
|
|
|
args = sys.argv[1:]
|
|
|
|
|
|
|
|
if '--test' in args:
|
2014-03-05 15:34:46 +00:00
|
|
|
# get rid of orphaned cache files first
|
|
|
|
pg.renamePyc(path)
|
2015-02-28 16:05:57 +00:00
|
|
|
|
2012-11-29 21:50:42 +00:00
|
|
|
files = buildFileList(examples)
|
2015-02-28 16:05:57 +00:00
|
|
|
if '--pyside' in args:
|
2012-12-05 05:25:45 +00:00
|
|
|
lib = 'PySide'
|
2015-02-28 16:05:57 +00:00
|
|
|
elif '--pyqt' in args or '--pyqt4' in args:
|
2012-12-05 05:25:45 +00:00
|
|
|
lib = 'PyQt4'
|
2015-02-28 16:05:57 +00:00
|
|
|
elif '--pyqt5' in args:
|
|
|
|
lib = 'PyQt5'
|
2012-12-05 05:25:45 +00:00
|
|
|
else:
|
|
|
|
lib = ''
|
|
|
|
|
|
|
|
exe = sys.executable
|
|
|
|
print("Running tests:", lib, sys.executable)
|
2012-11-29 21:50:42 +00:00
|
|
|
for f in files:
|
2012-12-05 05:25:45 +00:00
|
|
|
testFile(f[0], f[1], exe, lib)
|
2012-11-29 21:50:42 +00:00
|
|
|
else:
|
|
|
|
run()
|