#!/usr/bin/python
# -*- coding: utf-8 -*-

__author__      = "Ricardo Ribeiro"
__credits__     = ["Ricardo Ribeiro"]
__license__     = "MIT"
__version__     = "0.0"
__maintainer__  = "Ricardo Ribeiro"
__email__       = "ricardojvr@gmail.com"
__status__      = "Development"


from __init__ import *
import random, time
from PyQt4 import QtCore

class SimpleExample(BaseWidget):

	def __init__(self):
		super(SimpleExample,self).__init__('Simple example')

		#Definition of the forms fields
		self._control0 = ControlEventsGraph('Check me')
		self._control1 = ControlEventsGraph('Check me')
		self._control2 = ControlEventsGraph('Check me')
		self._control3 = ControlEventsGraph('Check me')

		self._txt = ControlText('Time')

		self._btn = ControlButton('Click')
		self._btn1 = ControlButton('Click 1')
		self._save = ControlButton('Save button')
		self._load = ControlButton('Load button')
		
		self.formset = [
			('_btn','_btn1'),
			('_control0','_control1'),
			('_control2','_control3'),
			'_txt',
			('_save','_load')]
			
		self._btn.value = self.__btn
		self._btn1.value = self.__btn1
		self._save.value = self.save_window
		self._load.value = self.load_window

		self._start = time.time()

		self.INTERVAL = 500
		self.N_TRACKS = 8

		
		

	def __btn(self):	

		for i in range(40):
			s = random.randint( 0, 10000 )
			o = random.randint( 0, 1000  )
			self._control0.add_event( s, s+o, track=random.randint(0,self.N_TRACKS) )
			#self._control0.add_event( random.randint(0, 10000), s+o, track=random.randint(0,self.N_TRACKS), color="#00FFDD")

		self._control0.value = 5000

	def __addEvent0(self):
		b = self._control0.value
		e = b+self.INTERVAL
		self._control0.add_event( b, e, track=random.randint(0,self.N_TRACKS) )
		self._control0.value = e

		self._txt.value = str(time.time() - self._start)

	def __addEvent1(self):
		b = self._control1.value
		e = b+self.INTERVAL
		self._control1.add_event( b, e, track=random.randint(0,self.N_TRACKS) )
		self._control1.value = e

	def __addEvent2(self):
		b = self._control2.value
		e = b+self.INTERVAL
		self._control2.add_event( b, e, track=random.randint(0,self.N_TRACKS) )
		self._control2.value = e

	def __addEvent3(self):
		b = self._control3.value
		e = b+self.INTERVAL
		self._control3.add_event( b, e, track=random.randint(0,self.N_TRACKS) )
		self._control3.value = e

		

	def __btn1(self):

		self._start = time.time()
		
		timer = QtCore.QTimer(self.form)
		timer.timeout.connect(self.__addEvent0)
		timer.start(self.INTERVAL)

		timer = QtCore.QTimer(self.form)
		timer.timeout.connect(self.__addEvent1)
		timer.start(self.INTERVAL)

		timer = QtCore.QTimer(self.form)
		timer.timeout.connect(self.__addEvent2)
		timer.start(self.INTERVAL)

		timer = QtCore.QTimer(self.form)
		timer.timeout.connect(self.__addEvent3)
		timer.start(self.INTERVAL)
				

##################################################################################################################
##################################################################################################################
##################################################################################################################

#Execute the application
if __name__ == "__main__":	 pyforms.start_app( SimpleExample )
	
[((47, 16, 47, 27), 'time.time', 'time.time', ({}, {}), '()', False, 'import random, time\n'), ((95, 16, 95, 27), 'time.time', 'time.time', ({}, {}), '()', False, 'import random, time\n'), ((97, 10, 97, 34), 'PyQt4.QtCore.QTimer', 'QtCore.QTimer', ({(97, 24, 97, 33): 'self.form'}, {}), '(self.form)', False, 'from PyQt4 import QtCore\n'), ((101, 10, 101, 34), 'PyQt4.QtCore.QTimer', 'QtCore.QTimer', ({(101, 24, 101, 33): 'self.form'}, {}), '(self.form)', False, 'from PyQt4 import QtCore\n'), ((105, 10, 105, 34), 'PyQt4.QtCore.QTimer', 'QtCore.QTimer', ({(105, 24, 105, 33): 'self.form'}, {}), '(self.form)', False, 'from PyQt4 import QtCore\n'), ((109, 10, 109, 34), 'PyQt4.QtCore.QTimer', 'QtCore.QTimer', ({(109, 24, 109, 33): 'self.form'}, {}), '(self.form)', False, 'from PyQt4 import QtCore\n'), ((58, 7, 58, 33), 'random.randint', 'random.randint', ({(58, 23, 58, 24): '0', (58, 26, 58, 31): '10000'}, {}), '(0, 10000)', False, 'import random, time\n'), ((59, 7, 59, 33), 'random.randint', 'random.randint', ({(59, 23, 59, 24): '0', (59, 26, 59, 30): '1000'}, {}), '(0, 1000)', False, 'import random, time\n'), ((68, 40, 68, 71), 'random.randint', 'random.randint', ({(68, 55, 68, 56): '(0)', (68, 57, 68, 70): 'self.N_TRACKS'}, {}), '(0, self.N_TRACKS)', False, 'import random, time\n'), ((71, 24, 71, 35), 'time.time', 'time.time', ({}, {}), '()', False, 'import random, time\n'), ((76, 40, 76, 71), 'random.randint', 'random.randint', ({(76, 55, 76, 56): '(0)', (76, 57, 76, 70): 'self.N_TRACKS'}, {}), '(0, self.N_TRACKS)', False, 'import random, time\n'), ((82, 40, 82, 71), 'random.randint', 'random.randint', ({(82, 55, 82, 56): '(0)', (82, 57, 82, 70): 'self.N_TRACKS'}, {}), '(0, self.N_TRACKS)', False, 'import random, time\n'), ((88, 40, 88, 71), 'random.randint', 'random.randint', ({(88, 55, 88, 56): '(0)', (88, 57, 88, 70): 'self.N_TRACKS'}, {}), '(0, self.N_TRACKS)', False, 'import random, time\n'), ((60, 43, 60, 74), 'random.randint', 'random.randint', ({(60, 58, 60, 59): '(0)', (60, 60, 60, 73): 'self.N_TRACKS'}, {}), '(0, self.N_TRACKS)', False, 'import random, time\n')]