mejoras varias. mejor manejo de no-imágenes, archivos no encontrados, espacios en nombres, paths vacíos.
This commit is contained in:
parent
d874888f29
commit
20c65a331e
@ -23,6 +23,7 @@ pygtk.require('2.0')
|
|||||||
|
|
||||||
import gtk
|
import gtk
|
||||||
import commands
|
import commands
|
||||||
|
import os
|
||||||
|
|
||||||
# Check for new pygtk: this is new class in PyGtk 2.4
|
# Check for new pygtk: this is new class in PyGtk 2.4
|
||||||
if gtk.pygtk_version < (2,3,90):
|
if gtk.pygtk_version < (2,3,90):
|
||||||
@ -30,7 +31,7 @@ if gtk.pygtk_version < (2,3,90):
|
|||||||
raise SystemExit
|
raise SystemExit
|
||||||
|
|
||||||
class App:
|
class App:
|
||||||
path = "/usr/share/images/desktop-base/moreblue-orbit-gdm.svg"
|
path = None
|
||||||
disp = "stretched"
|
disp = "stretched"
|
||||||
conffile = "/etc/gdm3/greeter.gconf-defaults"
|
conffile = "/etc/gdm3/greeter.gconf-defaults"
|
||||||
image = gtk.Image()
|
image = gtk.Image()
|
||||||
@ -41,125 +42,132 @@ class App:
|
|||||||
def redraw(self):
|
def redraw(self):
|
||||||
|
|
||||||
# new pixbuf from current path
|
# new pixbuf from current path
|
||||||
readimg = gtk.gdk.pixbuf_new_from_file(self.path)
|
try:
|
||||||
imgw = readimg.get_width()
|
readimg = gtk.gdk.pixbuf_new_from_file(self.path)
|
||||||
imgh = readimg.get_height()
|
except:
|
||||||
|
readimg = None
|
||||||
|
print "WARNING: Unable to load image."
|
||||||
|
|
||||||
# calculate selected image and screen proportions (width-to-height ratio)
|
# create an pixbuf for the thumbnail and fill it with a background pattern
|
||||||
disp_ratio = float(self.screenw)/float(self.screenh)
|
|
||||||
img_ratio = float(imgw)/float(imgh)
|
|
||||||
|
|
||||||
# actual width of the image on the screen thumbnail
|
|
||||||
thumbw = -1
|
|
||||||
thumbh = -1
|
|
||||||
|
|
||||||
# create an pixbuf for the thumbn, and fill it with a "background" pattern
|
|
||||||
thumbpixbuf = gtk.gdk.Pixbuf( gtk.gdk.COLORSPACE_RGB, True, 8,
|
thumbpixbuf = gtk.gdk.Pixbuf( gtk.gdk.COLORSPACE_RGB, True, 8,
|
||||||
int(self.screenw/4.0),
|
int(self.screenw/4.0),
|
||||||
int(self.screenh/4.0) )
|
int(self.screenh/4.0) )
|
||||||
thumbpixbuf.fill(0x88888888)
|
thumbpixbuf.fill(0x88888888)
|
||||||
thumbpixbuf.saturate_and_pixelate(thumbpixbuf, 0.0, True)
|
thumbpixbuf.saturate_and_pixelate(thumbpixbuf, 0.0, True)
|
||||||
|
|
||||||
# switch between the different disposition styles
|
if readimg is not None:
|
||||||
if self.disp == "zoom":
|
imgw = readimg.get_width()
|
||||||
|
imgh = readimg.get_height()
|
||||||
|
|
||||||
# if display wider than image, scale image to fit screen width
|
# calculate selected image and screen proportions (width-to-height ratio)
|
||||||
if disp_ratio > img_ratio:
|
disp_ratio = float(self.screenw)/float(self.screenh)
|
||||||
thumbw = int(self.screenw/4.0)
|
img_ratio = float(imgw)/float(imgh)
|
||||||
thumbh = int(imgh*((self.screenw/4.0)/imgw))
|
|
||||||
|
|
||||||
# else scale image to fit screen height
|
# switch between the different disposition styles
|
||||||
else:
|
if self.disp == "zoom":
|
||||||
thumbh = int(self.screenh/4.0)
|
|
||||||
thumbw = int(imgw*((self.screenh/4.0)/imgh))
|
|
||||||
|
|
||||||
# scale previously loaded image and copy to the screen thumbnail pixbuf
|
# if display wider than image, scale image to fit screen width
|
||||||
readimg = readimg.scale_simple(thumbw,thumbh,gtk.gdk.INTERP_BILINEAR)
|
if disp_ratio > img_ratio:
|
||||||
readimg.copy_area( int((thumbw-self.screenw/4.0)/2),
|
thumbw = int(self.screenw/4.0)
|
||||||
|
thumbh = int(imgh*((self.screenw/4.0)/imgw))
|
||||||
|
|
||||||
|
# else scale image to fit screen height
|
||||||
|
else:
|
||||||
|
thumbh = int(self.screenh/4.0)
|
||||||
|
thumbw = int(imgw*((self.screenh/4.0)/imgh))
|
||||||
|
|
||||||
|
# scale previously loaded image and copy to the screen thumbnail pixbuf
|
||||||
|
readimg = readimg.scale_simple(thumbw,thumbh,gtk.gdk.INTERP_BILINEAR)
|
||||||
|
readimg.copy_area( int((thumbw-self.screenw/4.0)/2),
|
||||||
int((thumbh-self.screenh/4.0)/2),
|
int((thumbh-self.screenh/4.0)/2),
|
||||||
int(self.screenw/4), int(self.screenh/4),
|
int(self.screenw/4), int(self.screenh/4),
|
||||||
thumbpixbuf, 0, 0)
|
thumbpixbuf, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
elif self.disp == "scaled":
|
elif self.disp == "scaled":
|
||||||
|
|
||||||
# with the "scaled" disposition, image is scaled
|
# with the "scaled" disposition, image is scaled
|
||||||
# so that it fits entirely onscreen
|
# so that it fits entirely onscreen
|
||||||
xscale = (self.screenw/4.0)/imgw
|
xscale = (self.screenw/4.0)/imgw
|
||||||
yscale = (self.screenh/4.0)/imgh
|
yscale = (self.screenh/4.0)/imgh
|
||||||
|
|
||||||
thumbw = int(imgw*min(xscale, yscale))
|
thumbw = int(imgw*min(xscale, yscale))
|
||||||
thumbh = int(imgh*min(yscale, yscale))
|
thumbh = int(imgh*min(yscale, yscale))
|
||||||
|
|
||||||
readimg = readimg.scale_simple(thumbw,thumbh,gtk.gdk.INTERP_BILINEAR)
|
readimg = readimg.scale_simple(thumbw,thumbh,gtk.gdk.INTERP_BILINEAR)
|
||||||
readimg.copy_area( 0, 0, thumbw, thumbh, thumbpixbuf,
|
readimg.copy_area( 0, 0, thumbw, thumbh, thumbpixbuf,
|
||||||
int((self.screenw/4-thumbw)/2),
|
int((self.screenw/4-thumbw)/2),
|
||||||
int((self.screenh/4-thumbh)/2))
|
int((self.screenh/4-thumbh)/2))
|
||||||
|
|
||||||
|
|
||||||
elif self.disp == "centered":
|
elif self.disp == "centered":
|
||||||
|
|
||||||
# scale image to 1/4 its original size
|
# scale image to 1/4 its original size
|
||||||
# (the screen thumbnail is 1/4 of the real screen size)
|
# (the screen thumbnail is 1/4 of the real screen size)
|
||||||
readimg = readimg.scale_simple( int(imgw/4), int(imgh/4),
|
readimg = readimg.scale_simple( int(imgw/4), int(imgh/4),
|
||||||
gtk.gdk.INTERP_BILINEAR)
|
gtk.gdk.INTERP_BILINEAR)
|
||||||
|
|
||||||
# the visible area coordinates of the image
|
# the visible area coordinates of the image
|
||||||
# (if negative, the image is bigger than the screen)
|
# (if negative, the image is bigger than the screen)
|
||||||
x0 = int((self.screenw-imgw)/8)
|
x0 = int((self.screenw-imgw)/8)
|
||||||
y0 = int((self.screenh-imgh)/8)
|
y0 = int((self.screenh-imgh)/8)
|
||||||
|
|
||||||
readimg.copy_area( max(-x0,0), max(-y0,0),
|
readimg.copy_area( max(-x0,0), max(-y0,0),
|
||||||
int(min(self.screenw/4,imgw/4)),
|
int(min(self.screenw/4,imgw/4)),
|
||||||
int(min(self.screenh/4,imgh/4)),
|
int(min(self.screenh/4,imgh/4)),
|
||||||
thumbpixbuf, max(x0,0), max(y0,0))
|
thumbpixbuf, max(x0,0), max(y0,0))
|
||||||
|
|
||||||
|
|
||||||
elif self.disp == "stretched":
|
elif self.disp == "stretched":
|
||||||
|
|
||||||
# in this case, simply stretch image so that both width and height
|
# in this case, simply stretch image so that both width and height
|
||||||
# are the same as the screen's
|
# are the same as the screen's
|
||||||
readimg = readimg.scale_simple( int(self.screenw/4),
|
readimg = readimg.scale_simple( int(self.screenw/4),
|
||||||
int(self.screenh/4),
|
int(self.screenh/4),
|
||||||
gtk.gdk.INTERP_BILINEAR)
|
gtk.gdk.INTERP_BILINEAR)
|
||||||
readimg.copy_area( 0, 0, int(self.screenw/4), int(self.screenh/4),
|
readimg.copy_area( 0, 0, int(self.screenw/4), int(self.screenh/4),
|
||||||
thumbpixbuf, 0, 0)
|
thumbpixbuf, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
elif self.disp == "wallpaper":
|
elif self.disp == "wallpaper":
|
||||||
|
|
||||||
# scale image to 1/4 as in centered
|
# scale image to 1/4 as in centered
|
||||||
readimg = readimg.scale_simple( int(imgw/4),
|
readimg = readimg.scale_simple( int(imgw/4),
|
||||||
int(imgh/4),
|
int(imgh/4),
|
||||||
gtk.gdk.INTERP_BILINEAR)
|
gtk.gdk.INTERP_BILINEAR)
|
||||||
|
|
||||||
# copy image as tiles until we fill the entire screen
|
# copy image as tiles until we fill the entire screen
|
||||||
x0=0
|
x0=0
|
||||||
while( x0 < self.screenw ):
|
while( x0 < self.screenw ):
|
||||||
y0=0
|
y0=0
|
||||||
while( y0 < self.screenh):
|
while( y0 < self.screenh):
|
||||||
readimg.copy_area( 0, 0,
|
readimg.copy_area( 0, 0,
|
||||||
int(min(self.screenw/4-x0,imgw/4)),
|
int(min(self.screenw/4-x0,imgw/4)),
|
||||||
int(min(self.screenh/4-y0,imgh/4)),
|
int(min(self.screenh/4-y0,imgh/4)),
|
||||||
thumbpixbuf, x0, y0)
|
thumbpixbuf, x0, y0)
|
||||||
y0 += imgh/4
|
y0 += imgh/4
|
||||||
x0 += imgw/4
|
x0 += imgw/4
|
||||||
|
|
||||||
# finally draw the gtk.Image from the pixbuf
|
# finally draw the gtk.Image from the pixbuf
|
||||||
self.image.set_from_pixbuf(thumbpixbuf)
|
self.image.set_from_pixbuf(thumbpixbuf)
|
||||||
|
|
||||||
|
|
||||||
# read GDM3 configuration file, and set correspondig variables
|
# read GDM3 configuration file, and set correspondig variables
|
||||||
def read_config( self ):
|
def read_config( self ):
|
||||||
|
|
||||||
with open( self.conffile, 'r') as conf:
|
with open( self.conffile, 'r') as conf:
|
||||||
|
|
||||||
for line in conf:
|
for line in conf:
|
||||||
m1 = re.match(r"\s*/desktop/gnome/background/picture_filename\s+((?:\S*(?:(?<=\\)\s)*)+)", line)
|
m1 = re.match(r"^\s*/desktop/gnome/background/picture_filename\s+(\S.*)$", line)
|
||||||
m2 = re.match(r"\s*/desktop/gnome/background/picture_options\s+(\w+)", line)
|
m2 = re.match(r"^\s*/desktop/gnome/background/picture_options\s+(\w+)", line)
|
||||||
|
|
||||||
if m1:
|
if m1:
|
||||||
print "Image file option read: " + m1.group(1)
|
if os.path.isfile( m1.group(1)):
|
||||||
self.path = m1.group(1)
|
print "Image file option read: " + m1.group(1)
|
||||||
|
self.path = m1.group(1)
|
||||||
|
else:
|
||||||
|
print "Image file not found"
|
||||||
|
self.path = None
|
||||||
|
|
||||||
elif m2:
|
elif m2:
|
||||||
print "Image disposition option read: " + m2.group(1)
|
print "Image disposition option read: " + m2.group(1)
|
||||||
@ -172,19 +180,19 @@ class App:
|
|||||||
out = ""
|
out = ""
|
||||||
|
|
||||||
for line in conf:
|
for line in conf:
|
||||||
m1 = re.match(r"\s*/desktop/gnome/background/picture_filename\s+((?:\S*(?:(?<=\\)\s)*)+)", line)
|
m1 = re.match(r"^\s*/desktop/gnome/background/picture_filename", line)
|
||||||
m2 = re.match(r"\s*/desktop/gnome/background/picture_options\s+(\w+)", line)
|
m2 = re.match(r"^\s*/desktop/gnome/background/picture_options", line)
|
||||||
|
|
||||||
# if the current line matchs the picture_filename option, replace
|
# if the current line matchs the picture_filename option, replace
|
||||||
# it with a new one containing the current value of path
|
# it with a new one containing the current value of path
|
||||||
if m1:
|
if m1:
|
||||||
out += "/desktop/gnome/background/picture_filename " \
|
out += "/desktop/gnome/background/picture_filename " \
|
||||||
+ self.path + "\n"
|
+ self.path + "\n"
|
||||||
|
|
||||||
# else if it macthes the picture_options option, put the
|
# else if it macthes the picture_options option, put the
|
||||||
# value of disp instead
|
# value of disp instead
|
||||||
elif m2:
|
elif m2:
|
||||||
out += "/desktop/gnome/background/picture_options "\
|
out += "/desktop/gnome/background/picture_options "\
|
||||||
+ self.disp + "\n"
|
+ self.disp + "\n"
|
||||||
|
|
||||||
# else copy the line as-is
|
# else copy the line as-is
|
||||||
@ -274,7 +282,8 @@ class App:
|
|||||||
i+=1
|
i+=1
|
||||||
self.combo.set_active( i )
|
self.combo.set_active( i )
|
||||||
|
|
||||||
self.filedialog.set_filename(self.path)
|
if self.path is not None:
|
||||||
|
self.filedialog.set_filename(self.path)
|
||||||
|
|
||||||
#self.window.set_border_width(4)
|
#self.window.set_border_width(4)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user