示例#1
0
class WidgetTimer(Widget):
	def __init__(self, visitor, name, section):
		Widget.__init__(self, visitor, name, 0, 0, WIDGET_TYPE_TIMER)

		self.expression = Property(self.visitor, section, 'expression', '')

		self.update = visitor.cfg_fetch_num(section, "update", 500)

		self.source_id = None
		self.id_signal = visitor.connect("display-disconnected-before", self.stop)

	def start(self, data=None):
		if self.update is None:
			return
		if self.source_id is None:
			self.source_id = gobject.timeout_add(self.update, self.timer_update)

	def stop(self, data=None):
		if self.source_id is not None:
			gobject.source_remove(self.source_id)
			self.source_id = None

	def timer_update(self):

		self.expression.eval()

		return True
示例#2
0
class WidgetKey(Widget):
	def __init__(self, visitor, name, section):
		Widget.__init__(self, visitor, name, 0, 0, WIDGET_TYPE_KEYPAD)

		self.expression = Property(self.visitor, section, 'expression', '')

		self.key = visitor.cfg_fetch_num(section, "key", 0)

		self.signal_id = None
		self.id_signal = visitor.connect("display-disconnected-before", self.stop)

	def start(self, data=None):
		if self.signal_id is None:
			self.signal_id = self.visitor.connect("key-packet-ready", self.key_packet_ready)

	def stop(self, data=None):
		if self.signal_id is not None:
			self.visitor.disconnect(self.signal_id)
			self.signal_id = None

	def key_packet_ready(self, device, key):
		if key == self.key and self.expression.valid():
			self.expression.eval()

		return True
示例#3
0
class WidgetText(Widget):
	def __init__(self, visitor, name, section, row, col):
		Widget.__init__(self, visitor, name, row, col, WIDGET_TYPE_RC)
		if hasattr(visitor, "text_draw"):
			self.draw = visitor.text_draw
		elif hasattr(visitor, "graphic_draw"):
			self.draw = visitor.graphic_draw
		else:
			self.draw = None

		self.row = row
		self.col = col

		self.string = ''
		self.prefix = Property(self.visitor, section, "prefix", '')
		self.postfix = Property(self.visitor, section, 'postfix', '')
		self.style = Property(self.visitor, section, 'style', '')
		self.value = Property(self.visitor, section, 'expression', '')
		self.width = visitor.cfg_fetch_num(section, 'width', 10)
		self.precision = visitor.cfg_fetch_num(section, 'precision', 0xDEAD)
		self.align = visitor.cfg_fetch_raw(section, 'align', 'L')
		self.update = visitor.cfg_fetch_num(section, 'update', 1000)
		self.scroll = visitor.cfg_fetch_num(section, 'scroll', 500)
		self.speed = visitor.cfg_fetch_num(section, 'speed', 500)
		self.fg_color = RGBA()
		self.bg_color = RGBA()
		self.fg_valid = self.widget_color(section, "foreground", self.fg_color)
		self.bg_valid = self.widget_color(section, "background", self.bg_color)
		self.layer = visitor.cfg_fetch_num(section, "layer", 0)
		
		self.id_source = None
		self.scroll_source = None
		self.buffer = ' ' * self.width
		self.id_signal = visitor.connect("display-disconnected-before", self.stop)

	def _get(self):
		return "".join(self._buffer)

	def _set(self, val):
		self._buffer = [x for x in val]

	def _del(self):
		del(self._buffer)
		self._buffer = []

	buffer = property(_get, _set, _del, "WidgetText buffer")

	def start(self, data=None):
		if self.id_source or self.scroll_source:
			self.stop()
		self.id_source = gobject.timeout_add(self.update, self.text_update)
		if self.align == ALIGN_MARQUEE or self.align == ALIGN_AUTOMATIC or self.align == ALIGN_PINGPONG:
			self.scroll_source = gobject.timeout_add(self.speed, self.text_scroll)
		self.text_update()

	def stop(self, data=None):
		if self.id_source: 
			gobject.source_remove(self.id_source)
		self.id_source = None
		if self.scroll_source:
			gobject.source_remove(self.scroll_source)
		self.scroll_source = None
		

	def text_update(self):
		update = 0
		update = update + self.prefix.eval()
		update = update + self.postfix.eval()
		update = update + self.style.eval()

		self.value.eval()

		if self.precision == 0xDEAD:
			string = self.value.P2S()
		else:
			number = self.value.P2N()
			precision = self.precision
			width = self.width - len(self.prefix.P2S()) - len(self.postfix.P2S())
			string = "%.*f" % (precision, number)
			size = len(string)
			if width < 0:
				width = 0
			if size > width and precision > 0:
				delta = size - width
				if delta > precision:
					delta = precision
				precision =  precision - delta
				size = size - delta

			if size > width:
				string = "*" * width

		if not self.string or strcmp(self.string, string) != 0:
			update = update + 1
			self.string = string

		if update:
			self.scroll = 0

		if self.align == ALIGN_PINGPONG:
			self.direction = 0
			self.delay = PINGPONGWAIT

		if self.align != ALIGN_MARQUEE or self.align != ALIGN_AUTOMATIC or self.align != ALIGN_PINGPONG:
			self.text_scroll()

		if self.update == 0:
			return False
		return True

	def text_scroll(self):
		prefix = self.prefix.P2S()
		postfix = self.postfix.P2S()

		string = self.string

		num = 0
		length = len(string)
		width = self.width - len(prefix) - len(postfix)
		if width < 0:
			width = 0

		if self.align == ALIGN_LEFT:
			pad = 0
		elif self.align == ALIGN_CENTER:
			pad = (width - length) / 2
			if pad < 0:
				pad = 0
		elif self.align == ALIGN_RIGHT:
			pad = width - length
			if pad < 0:
				pad = 0
		elif self.align == ALIGN_AUTOMATIC:
			if length <= width:
				pad = 0
		elif self.align == ALIGN_MARQUEE:
			pad = width - self.scroll
			self.scroll = self.scroll + 1
			if self.scroll >= width + length:
				self.scroll = 0
		elif self.align == ALIGN_PINGPONG:
			if length <= width:
				pad = (width - length) / 2
			else:
				if self.direction == 1:
					self.scroll = self.scroll + 1
				else:
					self.scroll = self.scroll - 1

			pad = 0 - self.scroll

			if pad < 0 - (length - width):
				if self.delay < 1:
					self.direction = 0
					self.delay = PINGPONGWAIT
					self.scroll = self.scroll - PINGPONGWAIT
				self.delay = self.delay - 1
				pad = 0 - (length - width)
			elif pad > 0:
				if self.delay < 1:
					self.direction = 1
					self.delay = PINGPONGWAIT
					self.scroll = self.scroll + PINGPONGWAIT
				self.delay = self.delay - 1
				pad = 0

			
		else:
			pad = 0

		dst = 0
		num = 0
		src = prefix
		for i in range(len(src)):
			if dst >= len(self.buffer):
				break
			self._buffer[dst] = src[i]
			dst = dst + 1
			num =  num + 1

		src = string

		while dst < len(self.buffer) and pad > 0 and num < self.width:
			self._buffer[dst] = ' '
			#print "num", num, "pad", pad, "width", self.width
			dst = dst + 1
			num = num + 1
			pad = pad - 1

		while pad < 0:
			src = src[1:] 
			pad = pad + 1

		i = 0
		while dst <  len(self.buffer) and i < len(src) and num < self.width:
			self._buffer[dst] = src[i]
			dst = dst + 1
			i = i + 1
			num = num + 1

		src = postfix
		length = len(src)

		while dst < len(self.buffer) and num < self.width - length:
			self._buffer[dst] = ' '
			dst = dst + 1
			num = num + 1

		i = 0
		while dst < len(self.buffer) and i < length and num < self.width:
			self._buffer[dst] = src[i]
			dst = dst + 1
			i = i + 1
			num = num + 1

		if self.draw is not None:
			self.draw(self)
		else:
			error("WidgetText: No draw method")

		return True
示例#4
0
class WidgetBigNumbers(Widget):
	def __init__(self, visitor, name, section, row, col):
		Widget.__init__(self, visitor, name, row, col, WIDGET_TYPE_RC)

		if hasattr(visitor, "text_bignums_draw"):
			self.draw = visitor.text_bignums_draw
		elif hasattr(visitor, "graphic_bignums_draw"):
			self.draw = visitor.graphic_bignums_draw
		else:
			self.draw = None

		self.FB = Buffer()
		self.FB.buffer = ' ' * 16 * 24

		self.min = 0
		self.max = 0
		self.string = ''
		self.expression = Property(self.visitor, section, 'expression', '')
		self.expr_min = Property(self.visitor, section, 'min', None)
		self.expr_max = Property(self.visitor, section, 'max', None)

		self.length = visitor.cfg_fetch_num(section, 'length', 10)

		self.update = visitor.cfg_fetch_num(section, 'update', 500)

		self.fg_color = RGBA()
		self.fg_valid = self.widget_color(section, "foreground", self.fg_color)
		self.bg_color = RGBA()
		self.bg_valid = self.widget_color(section, "background", self.bg_color)

		self.layer = visitor.cfg_fetch_num(section, "layer", 0)
		
		self.ch = {}

		self.id_source = None
		self.id_signal = visitor.connect("display-disconnected-before", self.stop)

	def setup_chars(self):
		for i in range(8):
			if len(self.visitor.chars) >= self.visitor.CHARS:
				error("Can not allot char for widget: %s" % name)
				self.update = None
				return
			self.visitor.chars.append(CHAR[i])
			self.ch[i] = len(self.visitor.chars) - 1

	def start(self, data=None):
		if self.update is None:
			return
		if self.id_source is not None:
			self.stop()
		self.id_source = gobject.timeout_add(self.update, self.bignums_update)
		self.bignums_update()

	def stop(self, data=None):
		if self.id_source is not None:
			gobject.source_remove(self.id_source)
		self.id_source = None

	def bignums_update(self):

		self.expression.eval()
		val = self.expression.P2N()

		if self.expr_min.valid():
			self.expr_min.eval()
			min = self.expr_min.P2N()
		else:
			min = self.min
			if val < min:
				min = val

		if self.expr_max.valid():
			self.expr_max.eval()
			max = self.expr_max.P2N()
		else:
			max = self.max
			if val > max:
				max = val	

		self.min = min
		self.max = max
		if max > min:
			self.val = int((val - min) / (max - min) * 100)
		else:
			self.val = 0

		
		text = str(self.val)
		pad = 3 - len(text)
		self.FB.buffer = ' ' * 16 * 24
		for i in range(len(text)):
			c = ord(text[i]) - ord('0')
			for row in range(16):
				for col in range(8):
					if (CHAR[c][row] & 1<<7-col) != 0:
						self.FB[row * 24 + (i + pad) * 8 + col] = '.'

		if self.draw is not None:
			self.draw(self)
		else:
			error( "WidgetBigNumbers: no draw method" )

		
		return True
示例#5
0
class WidgetHistogram(Widget):
	def __init__(self, visitor, name, section, row, col):
		Widget.__init__(self, visitor, name, row, col, WIDGET_TYPE_RC)

		if hasattr(visitor, "text_histogram_draw"):
			self.draw = visitor.text_histogram_draw
		elif hasattr(visitor, "graphic_histogram_draw"):
			self.draw = visitor.graphic_histogram_draw
		else:
			self.draw = None

		
		self.min = 0
		self.max = 0
		self.string = ''
		self.expression = Property(self.visitor, section, 'expression', '')
		self.expr_min = Property(self.visitor, section, 'min', None)
		self.expr_max = Property(self.visitor, section, 'max', None)

		self.length = visitor.cfg_fetch_num(section, 'length', 10)

		self.height = visitor.cfg_fetch_num(section, 'height', 1)

		self.gap = visitor.cfg_fetch_num(section, "gap", 0)

		self.layer = visitor.cfg_fetch_num(section, "layer", 0)

		self.history = resizeList([], self.length, int)

		c = visitor.cfg_fetch_raw(section, "direction", "E")

		if c.upper() == "E":
			self.direction = DIR_EAST
		elif c.upper() == "W":
			self.direction = DIR_WEST
		else:
			error("widget %s has unknown direction '%s'; Use E(ast) or W(est). Using E." % (self.name, c))
			self.direction = DIR_EAST

		self.update = visitor.cfg_fetch_num(section, 'update', 1000)

		self.fg_color = RGBA()
		self.fg_valid = self.widget_color(section, "foreground", self.fg_color)
		self.bg_color = RGBA()
		self.bg_valid = self.widget_color(section, "background", self.bg_color)

		self.ch = {}

		self.id_source = None
		self.id_signal = visitor.connect("display-disconnected-before", self.stop)

	def setup_chars(self):
		self.ch = {}

		for char in range(self.visitor.YRES):
			buffer = []
			i = self.visitor.YRES - 1
			while i >= 0:  
				buffer.append( [0, 2**(self.visitor.XRES-self.gap)-1][i < char] )
				i = i - 1

			reused = False

			for i in range(len(self.visitor.chars)):
				if buffer == self.visitor.chars[i]:
					self.ch[char] = i
					reused = True
					break

			if char not in self.ch.keys():	
				if self.visitor.CHARS - len(self.visitor.chars) < self.visitor.YRES-char:
					self.update = None
					error("widget %s unable to allocate special chars" % self.name)
					return

			if reused: continue
			self.visitor.chars.append(buffer)
			self.ch[char] = len(self.visitor.chars)-1

	def start(self, data=None):
		if self.update is None:
			return
		if self.id_source is not None:
			self.stop()
		self.id_source = gobject.timeout_add(self.update, self.histogram_update)
		self.histogram_update()

	def stop(self, data=None):
		if self.id_source is not None:
			gobject.source_remove(self.id_source)
		self.id_source = None

	def histogram_update(self):

		self.expression.eval()
		val = self.expression.P2N()

		if self.expr_min.valid():
			self.expr_min.eval()
			min = self.expr_min.P2N()
		else:
			min = self.min
			if val < min:
				min = val

		if self.expr_max.valid():
			self.expr_max.eval()
			max = self.expr_max.P2N()
		else:
			max = self.max
			if val > max:
				max = val	

		self.min = min
		self.max = max
		if max > min:
			val = (val - min) / (max - min)
		else:
			val = 0.0

		if self.direction == DIR_EAST:
			self.history[1:] = self.history[:-1]
			self.history[0] = val
		elif self.direction == DIR_WEST:
			self.history[:-1] = self.history[1:]
			self.history[self.length-1] = val

		if self.draw is not None:
			self.draw(self)
		else:
			error( "WidgetHistogram: no draw method" )

		
		return True
示例#6
0
class WidgetBar(Widget):
	def __init__(self, visitor, name, section, row, col):
		Widget.__init__(self, visitor, name, row, col, WIDGET_TYPE_RC)

		if hasattr(visitor, "text_bar_draw"):
			self.draw = visitor.text_bar_draw
		elif hasattr(visitor, "graphic_bar_draw"):
			self.draw = visitor.graphic_bar_draw
		else:
			self.draw = None

		self.min = 0
		self.max = 0
		self.string = ''
		self.expression = Property(self.visitor, section, 'expression', '')
		self.expression2 = Property(self.visitor, section, 'expression2', '')
		self.expr_min = Property(self.visitor, section, 'min', None)
		self.expr_max = Property(self.visitor, section, 'max', None)

		self.color_valid = resizeList([], 2, int)
		self.color = resizeList([], 2, RGBA)
		self.color_valid[0] = self.widget_color(section, "barcolor0", self.color[0])
		self.color_valid[1] = self.widget_color(section, "barcolor1", self.color[1])

		self.layer = visitor.cfg_fetch_num(section, 'layer', 0)

		self.length = visitor.cfg_fetch_num(section, 'length', 10)
		c = visitor.cfg_fetch_raw(section, "direction", "E")

		if c.upper() == "E":
			self.direction = DIR_EAST
		elif c.upper() == "W":
			self.direction = DIR_WEST
		else:
			error("widget %s has unknown direction '%s'; Use E(ast) or W(est). Using E." % (self.name, c))
			self.direction = DIR_EAST

		self.update = visitor.cfg_fetch_num(section, 'update', 1000)

		c = visitor.cfg_fetch_raw(section, "style", "O")

		if c.upper() == "H":
			self.style = STYLE_HOLLOW
		elif c.upper() == "N" or c.upper == "O":
			self.style = STYLE_NORMAL
		else:
			error("widget %s has unknown style '%s'; known styles 'N' or 'H'; using 'N'" % (self.name, c))
			self.style = STYLE_NORMAL

		self.ch = {}

		self.id_source = None
		self.id_signal = visitor.connect("display-disconnected-before", self.stop)

	def setup_chars(self):
		self.ch = {}
		if self.style == STYLE_HOLLOW and not self.expression2.valid():
			for i in range(4):
				for j in range(len(self.visitor.chars)):
					if self.visitor.chars[j] == CHAR[i]:
						self.ch[i] = j

			for i in range(6):
				if i not in self.ch.keys():
					if len(self.visitor.chars) >= self.visitor.CHARS:
						error("Can not allot char for widget: %s" % name)
						self.update = None
						return
					self.visitor.chars.append(CHAR[i])
					self.ch[i] = len(self.visitor.chars) - 1
		elif self.style == STYLE_NORMAL:
			for j in range(len(self.visitor.chars)):
				if self.visitor.chars[j] == CHAR[0]:
					self.ch[0] = j
				if self.visitor.chars[j] == CHAR[4]:
					self.ch[1] = j
				if self.visitor.chars[j] == CHAR[5]:
					self.ch[2] = j
			if 0 not in self.ch.keys():
				if len(self.visitor.chars) >= self.visitor.CHARS:
					error("Can not allot char for bar widget: %s" % self.widget_base)
					self.update = None
					return
				self.visitor.chars.append(CHAR[0])
				self.ch[0] = len(self.visitor.chars) - 1
			if 1 not in self.ch.keys() and self.expression2.valid():
				if len(self.visitor.chars) >= self.visitor.CHARS:
					error("Can not allot char for bar widget: %s" % self.widget_base)
					return
				self.visitor.chars.append(CHAR[4])
				self.ch[1] = len(self.visitor.chars) - 1
			if 2 not in self.ch.keys() and self.expression2.valid():
				if len(self.visitor.chars) >= self.visitor.CHARS:
					error("Can not allot char for bar widget: %s" % self.widget_base)
					return
				self.visitor.chars.append(CHAR[5])
				self.ch[2] = len(self.visitor.chars) - 1

		else:
			error("%s: Either choose style hollow or have a 2nd expression, not both" % self.widget_base)
			self.update = None

	def start(self, data=None):
		if self.update is None:
			return
		if self.id_source is not None:
			self.stop()
		self.id_source = gobject.timeout_add(self.update, self.bar_update)
		self.bar_update()

	def stop(self, data=None):
		if self.id_source is not None:
			gobject.source_remove(self.id_source)
		self.id_source = None

	def bar_update(self):
		self.expression.eval()
		val1 = self.expression.P2N()
		if self.expression2.valid():
			self.expression2.eval()
			val2 = self.expression2.P2N()
		else:
			val2 = val1

		if self.expr_min.valid():
			self.expr_min.eval()
			min = self.expr_min.P2N()
		else:
			min = self.min
			if val1 < min:
				min = val1
			if val2 < min:
				min = val2

		if self.expr_max.valid():
			self.expr_max.eval()
			max = self.expr_max.P2N()
		else:
			max = self.max
			if val1 > max:
				max = val1
			if val2 > max:
				max = val2

		self.min = min
		self.max = max
		if max > min:
			self.val1 = (val1 - min) / (max - min)
			self.val2 = (val2 - min) / (max - min)
		else:
			self.val1 = 0.0
			self.val2 = 0.0

		if self.draw is not None:
			self.draw(self)
		else:
			error( "WidgetBar: no draw method" )

		
		return True