#!/usr/bin/python

import os,sys,importlib
os.environ['QT_API'] = 'pyqt'
import sip
sip.setapi("QString", 2)
sip.setapi("QVariant", 2)

import time,string
import Labtools.Apps as Apps
from Labtools.templates import template_experiments

# Import the core and GUI elements of Qt
from PyQt4.QtGui  import *
from PyQt4.QtCore import *
import PyQt4.QtGui as Widgets

import pkgutil


class MyMainWindow(QMainWindow, template_experiments.Ui_MainWindow):
	def __init__(self, parent=None):
		self.pkgpath = os.path.dirname(Apps.__file__)
		apps = [name for _, name, _ in pkgutil.iter_modules([self.pkgpath])]
		self.app = QApplication(sys.argv)
		super(MyMainWindow, self).__init__(parent)
		self.setupUi(self)
		for a in apps: self.filenames.addItem(a+'.py')

	def run_exp(self):
		self.loadFile(self.filenames.currentItem().text())

	def loadFile(self, fn):
		if fn is None:
			return
		fn = self.pkgpath+'/'+fn
		if sys.platform.startswith('win'):
			os.spawnl(os.P_NOWAIT, sys.executable, '"'+sys.executable+'"', '"' + fn + '"')
		else:
			os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, fn)		

	def selection_changed(self):
		pass

	def __del__(self):
		print 'bye'

        		
if __name__ == "__main__":
	myapp = MyMainWindow()
	myapp.show()
	myapp.app.exec_()

