~cahr-gr/covergloobus/cahrgr

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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python

import gtk
from gtk import gdk
import string
import os.path
import pango
import xml.dom.minidom

##
## Class to manage the themes, Read its xml and then load each item
##
class UI:    
    def __init__(self, win, colors):
        #self.skin = False
        self.width = 0
        self.height	= 0
        self.draw_background = False
        self.theme_path	= None

        self.items = []						#It will hold all the items :D
        self.albumcover_item = False
        self.titlename_item = False
        self.albumname_item = False
        self.artistname_item = False
        self.rating_item = False
        self.playercontrols_item = False
    
        #gtkwin	= None
        self.gtkwin = win
        self.g = colors

    def get_width(self):
        return self.width

    def get_height(self):
        return self.height

    def get_album_x(self):
	return self.Albumcover_Item.x

	## @param self pointer to the class object
	## @param filename Path to the xml file
	##
	## It reads the xml file and creates a item for each xml entry that corresponds to one of the following:
	## - image
	## - albumcover
	## - rating
	## - titlename
	## - artistname
	## - albumname
    def load_xml(self,filename):
		if os.path.exists(filename):
			self.theme_path = os.path.expanduser(os.path.dirname(filename))
			self.g.debug("Theme path: " + self.theme_path)
			dom 	= xml.dom.minidom.parse(filename)
			skinobj 	= dom.getElementsByTagName("skin")[0]
			self.g.info("Theme: " + skinobj.getAttribute("name"))				#Print Theme Name
			self.width 	= int(skinobj.getAttribute("width"))
			self.height = int(skinobj.getAttribute("height"))
			self.draw_background = skinobj.getAttribute("drawBackground")

			for domitem in skinobj.childNodes:
				if domitem.nodeType == 1:
					#print domitem
					type 	= domitem.nodeName
					x 		= int(domitem.getAttribute("x"))
					y 		= int(domitem.getAttribute("y"))
					w 		= domitem.getAttribute("width")
					h 		= domitem.getAttribute("height")
					display = domitem.getAttribute("display")
					if w: w	= int(w)
					if h: h 	= int(h)
					
					item = False
					if type == "image":
						src  = domitem.getAttribute("src")
						item = ImageItem(self.theme_path + "/" + src, x, y, w, h, display)
					elif type == "albumcover":
						item = ImageItem(None, x, y, w, h, display)
						self.albumcover_item = item
						self.g.debug("Loading AlbumCover xml info")
					elif type== "rating":
						src_star  	= domitem.getAttribute("star")
						src_noStar = domitem.getAttribute("nostar") 
						spacing 	= domitem.getAttribute("spacing")
						if spacing: spacing = int(spacing)
						item = RatingItem(self.theme_path + "/" + src_star, self.theme_path + "/" +src_noStar,x,y,w,h,spacing,display)
						self.rating_item = item
					elif type in ("titlename", "artistname", "albumname"):
						font 			=	domitem.getAttribute("font")
						color			=	domitem.getAttribute("color")
						shadowcolor 	=	domitem.getAttribute("shadowcolor")
						align 			=	domitem.getAttribute("align")
						maxchars 		=	domitem.getAttribute("maxchars")
						item = TextItem("", font, color, x, y, display, shadowcolor, maxchars, align, w, h)
						exec "self."+type+"_item = item"
					elif type == "playercontrols":
						xd = float(x)/self.width
						yd = float(y)/self.height
						spacing = domitem.getAttribute("spacing")
						item = PlayerControls(int(spacing), xd, yd, display, self.gtkwin)
						for controlitem in domitem.childNodes:
							if controlitem.nodeType == 1: 
								src = controlitem.getAttribute("src")
								src = self.theme_path + "/" + src
								x  	= controlitem.getAttribute("x")
								y  	= controlitem.getAttribute("y")
								pause = controlitem.getAttribute("pause")
								if pause:
									pause = self.theme_path + "/" + pause
								ctype = controlitem.nodeName
								item.add(ctype,src,x,y,pause)
						self.playercontrols_item = item
					if item: self.items.append(item)
				
# ============================ GENERIC ITEM =========================== #
##
## Each interface item will be part of this GeneriItem class, everyone will have a position and a display mode
##
class GenericItem:
	type 		= ""
	x     		= 0
	y     		= 0
	width		= 0
	height 		= 0
	display 		= True
	regular_updates	= False

	def __init__(self, type, x, y, w, h, display):
		self.type 	= type
		self.x 		= x
		self.y 		= y
		self.width 	= w
		self.height	= h
		if display: self.display = display

	def draw(self, ctx):
		pass
	
# ============================= IMAGE ITEM ============================= #	
##
## Class for the items that are images
##	
class ImageItem(GenericItem):
	src         = ""
	imgobj      = None
	image_type  = None
	image_path  = None

	def __init__(self, src, x, y, w, h, display):
		GenericItem.__init__(self, "image", x, y, w, h, display)
		self.set_image(src)
		self.image_path = src

	def set_image(self, src):
		if not src:
			self.imgobj = None
		elif src != self.image_path:
			if not os.path.exists(src):
				self.imgobj = None
			else:
				try:
					self.image_path = src
					self.imgobj = gtk.gdk.pixbuf_new_from_file(src)
					self.image_scaled = self.imgobj.scale_simple(self.width,self.height,1)
				except:
					self.imgobj = None

		cl 	= self.imgobj.__class__.__name__
		mod = self.imgobj.__class__.__module__
		if    cl == 'Handle': 		self.image_type = "rsvg"
		elif  cl == 'ImageSurface': self.image_type = "surface"
		elif  cl == 'Pixbuf': 		self.image_type = "pixbuf"
		else: 
			self.image_type = None
			
		#print mod+", "+cl

	def draw(self, ctx):
		# Draw accordingly
		#print "Drawing Image :"+self.src + " - " + str(self.image_type)
		if self.imgobj != None:
				ctx.save()
				#pw = float(self.imgobj.get_width())
				#ph = float(self.imgobj.get_height())
				ctx.translate(self.x, self.y)
				#if self.width and self.height:
				#	ctx.scale(self.width/pw, self.height/ph)
				#ctx.set_source_pixbuf(self.imgobj, 0, 0)
				#pixscaled = self.imgobj
				#if self.width and self.height:
				#	pixscaled = self.imgobj.scale_simple(self.width,self.height,1)
				#ctx.set_source_pixbuf(pixscaled, 0, 0)
				ctx.set_source_pixbuf(self.image_scaled, 0, 0)
				ctx.paint()
				ctx.restore()	

# ============================= RATING ITEM ============================== #	
##
## Class for the rating items
##	
class RatingItem(GenericItem):
	src_star 		= ""
	src_noStar 		= ""
	starobj 		= None
	noStarobj 		= None
	spacing 		= 1
	image_type 		= None
	stars 			= 0
	
	def __init__(self, src_star, src_noStar, x, y, w, h, spacing, display):
		GenericItem.__init__(self, "rating", x, y, w, h, display)
		self.set_image(src_star, src_noStar)
		self.spacing = spacing

	def set_image(self, src_star, src_noStar):
		if src_star:
			self.starobj = gtk.gdk.pixbuf_new_from_file(src_star)
		if src_noStar:
			self.noStarobj = gtk.gdk.pixbuf_new_from_file(src_noStar)

		cl 	= self.starobj.__class__.__name__
		mod 	= self.starobj.__class__.__module__
		if    cl == 'Handle': self.image_type = "rsvg"
		elif cl == 'ImageSurface': self.image_type = "surface"
		elif cl == 'Pixbuf': self.image_type = "pixbuf"
		else: self.image_type = None
		#print mod+", "+cl

	def set_rating(self,stars):
		self.stars = stars

	def draw(self, ctx):
		# Draw the rating (stars and the no stars)
		#print "Drawing Rating :"+self.src_star + " - " + str(self.image_type)
		if self.starobj != None and self.noStarobj != None:
			if self.image_type == "pixbuf":
				ctx.save()
				pw 	= float(self.starobj.get_width())
				ph 	= float(self.starobj.get_height())
				p2w 	= float(self.noStarobj.get_width())
				p2h 	= float(self.noStarobj.get_height())
				pixbuf = [None,None,None,None,None]
				
				#if self.width and self.height:
					#ctx.scale(self.width/pw, self.height/ph)
				#ctx.set_source_pixbuf(self.imgobj, 0, 0)

				for i in range(0,self.stars):			#Draw Stars
					ctx.save()
       					pixbuf[i] = self.noStarobj
					if self.width and self.height:
						pixbuf[i] = self.starobj.scale_simple(self.width,self.height,1)
					ctx.translate(self.x + i*(self.width+self.spacing), self.y)
					ctx.set_source_pixbuf(pixbuf[i], 0, 0)
					ctx.paint()
					ctx.restore()

				for i in range(self.stars,5):			#Draw No Stars
					ctx.save()
       					pixbuf[i] = self.noStarobj
					if self.width and self.height:
						pixbuf[i] = self.noStarobj.scale_simple(self.width,self.height,1)
					ctx.translate(self.x + i*(self.width+self.spacing), self.y)
					ctx.set_source_pixbuf(pixbuf[i], 0, 0)
					ctx.paint()
					ctx.restore()
				ctx.restore()	


# ============================= TEXT ITEM ============================== #	
##
## Class for the items that are text
##		
class TextItem(GenericItem):
	font 			= "Sans 10"
	text 			= ""
	align			= ""
	color 		= (0,0,0,1)
	shadowcolor 	= False
	maxchars 		= False
	layout = 0
	

 	def __init__(self, text, font, color, x, y, display="", 
					shadowcolor="", maxchars="", align="left", 
					w=0, h=0):       
		GenericItem.__init__(self, "text", x, y, w, h, display)
		if font: self.font = font
		if color: self.color = self.get_rgba(color)
		if shadowcolor: self.shadowcolor = self.get_rgba(shadowcolor)
		if maxchars: self.maxchars = int(maxchars)
		self.align = align
		self.set_text(text)
	
	def get_rgba(self, color):
		return (
            int(color[1:3], 16)/255.0,
            int(color[3:5], 16)/255.0,
            int(color[5:7], 16)/255.0,
            int(color[7:9], 16)/255.0
        )

	def set_text(self, text):
		#print "resetting text"
		if self.maxchars and len(text) > self.maxchars:
			text = text[0:self.maxchars] + "..."
		self.text = text
		
	def draw(self, ctx):
        	# Do some Pango Magic here
        	# Draw Shadow Text
		text = self.text
		ctx.save()
		if self.shadowcolor: 
			self.draw_text(ctx, text, self.shadowcolor, self.x-1, self.y-1)
        	# Draw Text
		self.draw_text(ctx, text, self.color, self.x, self.y)
		ctx.restore()

	def draw_text(self, ctx, text, c, x, y):
		ctx.save()
		p_layout = ctx.create_layout()
		p_fdesc = pango.FontDescription(self.font)
		p_layout.set_font_description(p_fdesc)
		p_layout.set_text(text)
		self.set_clip_region(ctx, p_layout, x, y)
		if text: 
			(x,y) = self.get_xy_from_alignment(p_layout, x, y)
		ctx.move_to(x,y)
		ctx.set_source_rgba(c[0],c[1],c[2],c[3])
		ctx.show_layout(p_layout)
		ctx.restore()

	def get_extents(self, p_layout):
		(extents, lextents) = p_layout.get_pixel_extents()
		return extents

	def get_xy_from_alignment(self, p_layout, x, y):
		if not self.width: return (x, y)
		extents = self.get_extents(p_layout)
		fw = extents[2]+extents[0]
		fh = extents[3]+extents[1]

		if self.align == "left":
			pass
		
		elif self.align == "center":
			if self.layout==0:
				x = x + self.width/2.0 - fw/2.0
			elif self.layout==1:
				y = y - self.width/2.0 + fw/2.0
			elif self.layout==2:
				x = x - self.width/2.0 + fw/2.0
			elif self.layout==3:
				y = y + self.width/2.0 - fw/2.0
		
		elif self.align == "right":
			if self.layout==0:
				x = x + self.width - fw
			elif self.layout==1:
				y = y - self.width + fw
			elif self.layout==2:
				x = x - self.width + fw
			elif self.layout==3:
				y = y + self.width - fw
		return (x, y)
		
	def set_clip_region(self, ctx, p_layout, x, y):
		extents = self.get_extents(p_layout)
		fw = extents[2]+extents[0]
		fh = extents[3]+extents[1]
		if self.width:
			if self.layout == 0:
				ctx.rectangle(x, y, self.width, fh)
			if self.layout == 1:
				ctx.rectangle(x, y-self.width, fh, self.width)
			if self.layout == 2:
				ctx.rectangle(x-self.width, y-fh, self.width, fh)
			if self.layout == 3:
				ctx.rectangle(x-fh, y, fh, self.width)
			#ctx.stroke()
			ctx.clip()

# ============================= PlayerControls ITEM ============================== #	
##
## Class for the player controls
##			
class PlayerControls(GenericItem):
	type = "playercontrols"

	fixed				= False
	box 				= False
	window 				= False
	theme 				= False
	player 				= False

	prev_button 		= False
	play_pause_button 	= False
	next_button 		= False
	
	display = True

	image_play = None
	src_play = ''
	src_pause = ''
	state = None
        
	def __init__(self, spacing, display,x ,y , gtkwin , w=0, h=0,):
		GenericItem.__init__(self, "text", x, y, w, h, display)
		self.box 	= gtk.HBox(spacing=spacing)
		self.fixed 	= gtk.Fixed()
		self.fixed.add(self.box)
		self.window = gtkwin
		self.window.add(self.fixed)
		
		if display: self.display = display       
		
	def add(self, image_type, src, x, y, pause=''):
		image = gtk.Image()
		image.set_from_file(src)
		
		ebox = gtk.EventBox()
		ebox.set_visible_window(False)
		ebox.add(image)
		
		if image_type == "prev":
			self.prev_button = ebox
			self.fixed.put(self.prev_button, int(x),int(y))
		elif image_type == "play":
			self.play_pause_button = ebox
			self.fixed.put(self.play_pause_button, int(x),int(y))
			self.src_play = src
			self.src_pause = pause
			self.image_play = image
		elif image_type == "next":
			self.next_button = ebox
			self.fixed.put(self.next_button, int(x),int(y))

	def change_state(self, state):
		if self.src_pause and state != self.state:
			if state == 'playing':
				self.image_play.set_from_file(self.src_pause)
				self.state = state
			elif state == 'paused':
				self.image_play.set_from_file(self.src_play)
				self.state = state