Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Experior.Activity/widgetIdentifier.py
blob: 169769742be45948a0e8038ea1d0c64684f7c28e (plain)
1
2
3
4
5
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#! /usr/bin/env python
# -*- encoding: utf-8 -*- 

#widgetIdentifier.py

#This file is part of sugarbot.

#sugarbot is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#sugarbot is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with sugarbot.  If not, see <http://www.gnu.org/licenses/>.

import gobject
import sys
import os
import time
import pygtk
pygtk.require('2.0')

import gtk
from gtk import gdk

import sugar
from sugar import graphics
from sugar.graphics.toolbutton import Palette
from sugar.graphics.icon import Icon
from sugar.graphics.toolcombobox import ToolComboBox

class widgetIdentifier:
	"""
	The widgetIdentifier class is used as a basis for classes to identify
	Widgets.  Ideally, all developers would call Widget.set_name() on all
	widgets created by their Activities.  Since this is not always
	practical or worthwhile, we must rely on other methods to identify
	widgets.
	
	This class provides some of the functionality for identifying widgets,
	such as a list of strings that are default Widget names (e.g. 'GtkButton').
	"""
	identifiers = {}

	def __init__(self, widget):
		self.widgetAttribute = "sugarbotWidgetIdentifier"

		self.dontWant = ["GtkToolbar", "GtkToggleButton","GtkButton",
					"GtkEventBox", "GtkNotebook", "GtkViewport",
					"HippoCanvas", "GtkTextView", "GtkInvisible",
					"GtkEntry", "GtkLabel", "GtkVBox", "GtkHBox",
					"SugarIcon","SugarToolButton","GtkAlignment",
					"GtkSeparatorToolItem","GtkTable","SugarToolbox",
					"GtkToggleToolButton","GtkScrolledWindow","GtkCellView",
					"GtkVSeparator","GtkArrow","GtkToolItem","GtkAccelLabel",
					"SugarComboBox","SugarToggleToolButton","GtkHSeparator",
					"GtkHButtonBox","GtkImageMenuItem","GtkSeparatorMenuItem",
					"GtkSpinButton","GtkDrawingArea","GtkFrame",
					"GtkColorButton", ""]

		self.dontWantPrefixes = ["Gtk", "sugar+graphics"]
		self.setWidget(widget)

	def setWidget(self, widget):
		self._widget		= widget
		# self.getIdentifier()

	def getIdentifierSub(self):
		"""
		Overridden by inheriting classes.
		"""
		return None

	def getIdentifier(self):
		"""
		Returns the Identifier of the Widget set with __init__, or None
		if we cannot find an identifier.  Do not override this function!
		"""
		# If we have already set the attribute for this widget, just
		# retrieve it quickly.
		if self.checkStoredIdentifier():
			return self.getStoredIdentifier()

		ident = None
		widget = self._widget

		if hasattr(widget, "get_name"):
			ident = widget.get_name()

		if not self.validateIdentifier(ident):
			ident = self.getIdentifierSub()

		return self.setIdentifier(ident)

	def checkStoredIdentifier(self):
		"""
		Checks to see if we have previously identified this same Widget.
		If we have, the Widget will have an attribute as defined by
		widgetIdentifier.widgetAttribute.
		"""
		if hasattr(self._widget, self.widgetAttribute) \
		 	and getattr(self._widget, self.widgetAttribute) is not None:
			return True
		return False

	def getStoredIdentifier(self):
		"""
		If the Widget has a stored identifier (see checkStoredIdentifier),
		then retrieve the stored identifier's value.

		If the Widget does not have a stored identifier, return None.
		"""
		if self.checkStoredIdentifier():
			return getattr(self._widget, self.widgetAttribute)
		else:
			return None

	def validateIdentifier(self,ident):
		"""
		Checks a proposed identifier against a series of criterium.  For
		example, empty strings, and blacklisted strings, as well as
		blacklisted prefixes, may rule out an identifier for use.
		"""
		if ident is None:
			return False
		elif not isinstance(ident, str):
			return False
		elif len(ident) < 1:
			return False
		elif ident in self.dontWant:
			return False
		else:
			for prefix in self.dontWantPrefixes:
				if ident.startswith(prefix):
					return False
		return True

	def setIdentifier(self, ident):
		"""
		Sets the stored identifier for the Widget assigned by __init__.
		"""
		if self.validateIdentifier(ident):
			setattr(self._widget, self.widgetAttribute, ident)
			return ident
		return None
widgetIdentifier.identifiers[gtk.Widget] = widgetIdentifier


class buttonIdentifier(widgetIdentifier):
	def getIdentifierSub(self):
		ident 	= None
		widget 	= self._widget
		
		if hasattr(widget, "get_label"):
			ident 	= widget.get_label()

		return ident
widgetIdentifier.identifiers[gtk.Button] = buttonIdentifier

# class toolButtonIdentifier(widgetIdentifier):
class toolButtonIdentifier(buttonIdentifier):
	def getIdentifierSub(self):
		ident 	= buttonIdentifier.getIdentifierSub(self)
		widget 	= self._widget

		# Get the identifier using the icon
		if not self.validateIdentifier(ident):
			ico = None
			ico = widget.get_icon_widget()
			if isinstance(ico, Icon):
				ident = ico.props.icon_name

		# Label did not give us a good ident, check the icon name
		if not self.validateIdentifier(ident):
			ident = widget.get_icon_name()

		# Icon did not give us a good ident, try the label
		if not self.validateIdentifier(ident):
			label = widget.get_label_widget()
			if hasattr(label,"get_text"):
				ident = label.get_text()

		return ident
widgetIdentifier.identifiers[gtk.ToolButton] = toolButtonIdentifier

class comboBoxIdentifier(widgetIdentifier):
	def getIdentifierSub(self):
		ident	= None
		widget 	= self._widget

		if hasattr(widget, "get_title"):
			ident = self._widget.get_title()

		return ident
widgetIdentifier.identifiers[gtk.ComboBox] = comboBoxIdentifier


class entryIdentifier(widgetIdentifier):
	def getIdentifierSub(self):	
		ident 	= None
		widget	= self._widget

		if not self.validateIdentifier(ident):
			if hasattr(widget, "get_text"):
				ident = self._widget.get_text()

		return ident
widgetIdentifier.identifiers[gtk.Entry] = entryIdentifier

class paletteIdentifier(widgetIdentifier):
	def getIdentifierSub(self):		
		ident  = None
		widget = self._widget

		if hasattr(widget, "_primary_text"):
			ident = getattr(widget,  "_primary_text")

		elif not self.validateIdentifier(ident):
			if hasattr(widget, "props.primary_text"):
				ident = getattr(widget, "props.primary_text")

		return ident
widgetIdentifier.identifiers[Palette] = paletteIdentifier


class toolComboBoxIdentifier(widgetIdentifier):
	def getIdentifierSub(self):
		ident  = None
		widget = self._widget

		if hasattr(widget, "_label_text"):
			ident = getattr(widget, "_label_text")
			print ident

		# if not self.validateIdentifier(ident):
		# 	try:
		# 		print "@@@"
		# 		ident = widget.get_property("label-text")
		# 		print ident
		# 	except TypeError:
		# 		raise
		
		# <DEPRECATED>
		# if not self.validateIdentifier(ident):
		# 	if hasattr(widget, "_label_text"):
		# 		ident = getattr(widget, "_label_text")
		# 	
		# elif not self.validateIdentifier(ident):
		# 	if hasattr(widget, "label"):
		# 		label = widget.label
		# 		ident = label.get_text()
		# </DEPRECATED> 

		return ident
widgetIdentifier.identifiers[ToolComboBox] = toolComboBoxIdentifier