Yup I have done that with get and set attr... here is what I have done... But it doesn't work... if you try it out... Jason On Thu, 2008-01-10 at 16:30 -0600, Gryc Ueusp wrote: > Why not use `model.feat = gtk.ListStore(str, gtk.gdk.Pixbuf)`? > > Lines 8-62 scream to me to be one, maybe two classes with some iteration to > set all the appropriate attributes. I see a LOT of duplication there, and > that should be about 10 lines at most. > > And help(setattr) tells me it needs to be a string, not a variable. You > should really re-evaluate your use of set/getattr. It doesnt really help > readability... > > On Thursday 10 January 2008 4:40:35 am Jason Brower wrote: > > http://pastebin.com/d394aac1d > > I want to take all the code and put it in a loop with get and set attr > > but I guess I am missing something. It tells me I need to declare > > model... do I really? > > > > :( > > > > Jason Brower > > > > _______________________________________________ > > Discussion mailing list > > [EMAIL PROTECTED] > > http://lists.memaker.org/listinfo.cgi/discussion-memaker.org > > > > _______________________________________________ > Discussion mailing list > [EMAIL PROTECTED] > http://lists.memaker.org/listinfo.cgi/discussion-memaker.org
#!/usr/bin/env python #-*- coding: utf-8 -*- # memaker.py - A gtk based avatar creation program. # Copyright 2007 Jason Brower [EMAIL PROTECTED], # Christopher Denter (motor AT the-space-station DOT com), # Aaron C Spike <[EMAIL PROTECTED]>, # Matthew Brennan Jones <[EMAIL PROTECTED]> # Og Maciel # Gryc Ueusp <[EMAIL PROTECTED]> # # This program 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. # # This program 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 this program. See the file LICENSE. # If not, see <http://www.gnu.org/licenses/>. import sys , os, fileinput, shutil, rsvg from motor import * import pygtk import gtk import gtk.glade import cairo import StringIO import pynotify try: import Image, ImageChops except ImportError: print 'Couldnt find PIL, install `python-imaging` for autogenerated thumbnails.' #Could be a dialog from image_loader_dummy import * else: from image_loader import * os.chdir(os.sys.path[0]) """Check directoris if they exist and creat them if they don't""" def mkdirCheck(newdir): if os.path.isdir(newdir): pass elif os.path.isfile(newdir): raise OSError("a file with the same name as the desired " \ "dir, '%s', already exists." % newdir) else: head, tail = os.path.split(newdir) if head and not os.path.isdir(head): print "A required Directory in your Home Directory was needed.... making:", head os.mkdir(head) if tail: print "A required Directory in your Home Directory was needed.... making:", newdir os.mkdir(newdir) class MeMakerGui: def __init__(self): self.featureList = ["Head","Hair","Eye","Mouth","Ear","Glasses","Nose","Eyebrow","Hat","Accessory","Beard"] gladefile = "memaker.glade" self.windowname = "meMaker" self.currentFace = ObjectStack() self.meMakerWin = gtk.glade.XML( gladefile, self.windowname ) #Create the default directory structure if not there now.... locationMkDir = os.path.expanduser("~/.MeMaker/features") mkdirCheck(locationMkDir) for featureDir in self.featureList: mkdirCheck(os.path.join(locationMkDir,featureDir.lower())) #Make a directory for the cache if it doesn't exist. mkdirCheck(os.path.expanduser("~/.MeMaker/cache")) #Create the window system for memaker windowMain = self.meMakerWin.get_widget("meMaker") windowMain.set_app_paintable(True) windowMain.hide() self.featListLoading = [] self.themesIHad = [] # leave empty. this is for remembering which themes we had. #Now that our main gtk window is ready we can show it. windowMain.show() avatarPicture = self.meMakerWin.get_widget("imageAvatar") loadAbout = self.meMakerWin.get_widget("buttonAbout") loadAbout.connect('clicked', self.loadAbout) avatarSaveAs = self.meMakerWin.get_widget("buttonSaveAs") #avatarSaveAs.connect('clicked', self.saveAsAvatar) resetAvatar = self.meMakerWin.get_widget("buttonReset") resetAvatar.connect('clicked', self.avatarReset) quitMeMaker = self.meMakerWin.get_widget("meMaker") quitMeMaker.connect('destroy', gtk.main_quit) buttonUp = self.meMakerWin.get_widget("buttonUp") buttonUp.connect('clicked', self.arrowUpClicked) arrowDown = self.meMakerWin.get_widget("buttonDown") arrowDown.connect('clicked', self.arrowDownClicked) #Adding the save as combobox comboboxSaveAs = self.meMakerWin.get_widget("comboboxSaveAs") comboboxSaveAs.set_active(0) comboboxSaveAs.connect('changed',self.saveAsChanged) #Adding the buttons for removing features... for feat in self.featureList: buttons = self.meMakerWin.get_widget("buttonRemove"+feat) buttons.connect('clicked', self.removeFeature) #Connect all feature iconviews with there proper button presses. self.modelHead = (gtk.ListStore(str, gtk.gdk.Pixbuf)) self.modelEye = (gtk.ListStore(str, gtk.gdk.Pixbuf)) self.modelEar = (gtk.ListStore(str, gtk.gdk.Pixbuf)) self.modelNose = (gtk.ListStore(str, gtk.gdk.Pixbuf)) self.modelMouth = (gtk.ListStore(str, gtk.gdk.Pixbuf)) self.modelBeard = (gtk.ListStore(str, gtk.gdk.Pixbuf)) self.modelHair = (gtk.ListStore(str, gtk.gdk.Pixbuf)) self.modelEyebrow = (gtk.ListStore(str, gtk.gdk.Pixbuf)) self.modelGlasses = (gtk.ListStore(str, gtk.gdk.Pixbuf)) self.modelHat = (gtk.ListStore(str, gtk.gdk.Pixbuf)) self.modelAccessory = (gtk.ListStore(str, gtk.gdk.Pixbuf)) iconviewHead = self.meMakerWin.get_widget('iconviewHead') iconviewEye = self.meMakerWin.get_widget('iconviewEye') iconviewEar = self.meMakerWin.get_widget('iconviewEar') iconviewNose = self.meMakerWin.get_widget('iconviewNose') iconviewMouth = self.meMakerWin.get_widget('iconviewMouth') iconviewBeard = self.meMakerWin.get_widget('iconviewBeard') iconviewHair = self.meMakerWin.get_widget('iconviewHair') iconviewEyebrow = self.meMakerWin.get_widget('iconviewEyebrow') iconviewGlasses = self.meMakerWin.get_widget('iconviewGlasses') iconviewHat = self.meMakerWin.get_widget('iconviewHat') iconviewAccessory = self.meMakerWin.get_widget('iconviewAccessory') iconviewHead.set_model(self.modelHead) iconviewEye.set_model(self.modelEye) iconviewEar.set_model(self.modelEar) iconviewNose.set_model(self.modelNose) iconviewMouth.set_model(self.modelMouth) iconviewBeard.set_model(self.modelBeard) iconviewHair.set_model(self.modelHair) iconviewEyebrow.set_model(self.modelEyebrow) iconviewGlasses.set_model(self.modelGlasses) iconviewHat.set_model(self.modelHat) iconviewAccessory.set_model(self.modelAccessory) iconviewHead.set_pixbuf_column(1) iconviewEye.set_pixbuf_column(1) iconviewEar.set_pixbuf_column(1) iconviewNose.set_pixbuf_column(1) iconviewMouth.set_pixbuf_column(1) iconviewBeard.set_pixbuf_column(1) iconviewHair.set_pixbuf_column(1) iconviewEyebrow.set_pixbuf_column(1) iconviewGlasses.set_pixbuf_column(1) iconviewHat.set_pixbuf_column(1) iconviewAccessory.set_pixbuf_column(1) iconviewHead.connect('selection-changed', self.featureClicked, self.modelHead) iconviewEye.connect('selection-changed', self.featureClicked, self.modelEye) iconviewEar.connect('selection-changed', self.featureClicked, self.modelEar) iconviewNose.connect('selection-changed', self.featureClicked, self.modelNose) iconviewMouth.connect('selection-changed', self.featureClicked, self.modelMouth) iconviewBeard.connect('selection-changed', self.featureClicked, self.modelBeard) iconviewHair.connect('selection-changed', self.featureClicked, self.modelHair) iconviewEyebrow.connect('selection-changed', self.featureClicked, self.modelEyebrow) iconviewGlasses.connect('selection-changed', self.featureClicked, self.modelGlasses) iconviewHat.connect('selection-changed', self.featureClicked, self.modelHat) iconviewAccessory.connect('selection-changed', self.featureClicked, self.modelAccessory) comboboxThemePicker = self.meMakerWin.get_widget("comboboxThemes") location = "../themes/" listStore = gtk.ListStore(gtk.gdk.Pixbuf, str) comboboxThemePicker.set_model(listStore) px = gtk.CellRendererPixbuf() text = gtk.CellRendererText() comboboxThemePicker.pack_start(px, False) comboboxThemePicker.pack_start(text, False) comboboxThemePicker.add_attribute(px, "pixbuf", 0) comboboxThemePicker.add_attribute(text, "text", 1) themesList = [] for items in os.listdir(location): if os.path.isdir(location+items): themesList.append(items) for items in (themesList): if (os.path.isdir(location + items)): image = gtk.gdk.pixbuf_new_from_file(location+items +".png") listStore.append((image, items))
comboboxThemePicker.connect('changed',self.themeChanged, location, themesList) linkbuttonDownload = self.meMakerWin.get_widget("linkbuttonDownload") self.avatarPicture = self.meMakerWin.get_widget("imageAvatar") self.clearIt = rsvg.Handle("clearIt.svg") self.avatarPicture.set_size_request(400,400) self.avatarPicture.connect("expose-event", self.draw_scene) #Now to load the last theme used memakerConf = os.path.expanduser("~/.MeMaker/conf.conf") try: confFile = file(memakerConf, "r") themeToLoad = confFile.read() except IOError: print "No conf.conf file found. Creating a new one with the default theme." themeToLoad = "../themes/cocoHead/" if os.path.isdir(themeToLoad): self.loadFeatures(themeToLoad) else: print "Theme to load was not found. Was it deleted?" print "Falling back to default theme..." themeToLoad = "../themes/cocoHead/" self.loadFeatures(themeToLoad) self.themesIHad.append(themeToLoad) print "Loading previous theme:", themeToLoad if len(themesList) <= 1: comboboxThemePicker.hide() linkbuttonDownload.show() else: linkbuttonDownload.hide() def saveAsChanged(self, comboboxSaveAs): if self.currentFace.amIEmpty(): return fileType = comboboxSaveAs.get_active_text() if fileType == "SVG": locationSave = self.saveLoadFeatures(gtk.FILE_CHOOSER_ACTION_SAVE, "svg",FILE_EXT = {"Scalable Vector Graphics | SVG":"svg"}) file = open(locationSave, "w") file.write(self.currentFace.printMe()) if fileType == "PNG": locationSave = self.saveLoadFeatures(gtk.FILE_CHOOSER_ACTION_SAVE, "png",FILE_EXT = {"Scalable Vector Graphics | PNG":"png"}) self.svgHandle = rsvg.Handle(data = self.currentFace.printMe()) cr = self.avatarPicture.window.cairo_create() self.clearIt.render_cairo(cr) pixbuf = self.svgHandle.get_pixbuf() pixbuf.save(locationSave, 'png') if fileType == "BMP": locationSave = self.saveLoadFeatures(gtk.FILE_CHOOSER_ACTION_SAVE, "bmp",FILE_EXT = {"Bitmap Image | BMP":"bmp"}) self.svgHandle = rsvg.Handle(data = self.currentFace.printMe()) cr = self.avatarPicture.window.cairo_create() self.clearIt.render_cairo(cr) pixbuf = self.svgHandle.get_pixbuf() pixbuf.save(locationSave, 'bmp') if fileType == ".face": self.svgHandle = rsvg.Handle(data = self.currentFace.printMe()) cr = self.avatarPicture.window.cairo_create() self.clearIt.render_cairo(cr) pixbuf = self.svgHandle.get_pixbuf() pixbuf = pixbuf.scale_simple(256,256,gtk.gdk.INTERP_BILINEAR) pixbufNot = pixbuf pixbuf.save(os.path.expanduser("~/.face"), 'png') pynotify.init("MeMaker Notify") notifier = pynotify.Notification("Profile Updated", "Your profile avatar has been successfully updated. Changes will take place when you login again.") helper = gtk.Button() notifier.set_icon_from_pixbuf(pixbuf.scale_simple(50,50,gtk.gdk.INTERP_BILINEAR)) notifier.show() if fileType == "Launchpad Logo": locationSave = self.saveLoadFeatures(gtk.FILE_CHOOSER_ACTION_SAVE, "png",FILE_EXT = {"Scalable Vector Graphics | PNG":"png"}) self.svgHandle = rsvg.Handle(data = self.currentFace.printMe()) cr = self.avatarPicture.window.cairo_create() self.clearIt.render_cairo(cr) pixbuf = self.svgHandle.get_pixbuf() pixbuf = pixbuf.scale_simple(64,64,gtk.gdk.INTERP_BILINEAR) pixbuf.save(locationSave, 'png') if fileType == "Launchpad Mugshot": locationSave = self.saveLoadFeatures(gtk.FILE_CHOOSER_ACTION_SAVE, "png",FILE_EXT = {"Scalable Vector Graphics | PNG":"png"}) self.svgHandle = rsvg.Handle(data = self.currentFace.printMe()) cr = self.avatarPicture.window.cairo_create() self.clearIt.render_cairo(cr) pixbuf = self.svgHandle.get_pixbuf() pixbuf = pixbuf.scale_simple(192,192,gtk.gdk.INTERP_BILINEAR) pixbuf.save(locationSave, 'png') if fileType == "Save As...": pass comboboxSaveAs.set_active(0) def themeChanged(self, comboboxThemePicker, featureLocation, themesList): themeName = comboboxThemePicker.get_active() themeLocation = featureLocation+themesList[themeName]+"/" print "Loading theme...", themeLocation fileSave = file(os.path.expanduser("~/.MeMaker/conf.conf"), "w") fileSave.write(themeLocation) self.loadFeatures(themeLocation) def loadFeatures(self, themeLocation = ""): notebookFeatures = self.meMakerWin.get_widget("notebookFeatures") notebookFeatures.set_current_page(0) self.avatarReset() progressbarLoading = self.meMakerWin.get_widget("progressbarLoading") progressbarLoading.show() comboboxThemePicker = self.meMakerWin.get_widget("comboboxThemes") comboboxThemePicker.hide() #for feat in self.featureList: # getattr(self,'model%s' % feat).clear() self.modelHead.clear() self.modelEye.clear() self.modelNose.clear() self.modelMouth.clear() self.modelBeard.clear() self.modelHair.clear() self.modelEyebrow.clear() self.modelGlasses.clear() self.modelHat.clear() self.modelAccessory.clear() self.modelEar.clear() self.featListLoading = [] for feat in self.featureList: self.featListLoading.append({'filename':'loading'+feat+'.png','text':'Loading '+feat+'(s)','widget':'iconview'+feat, 'path':themeLocation + feat+'/', 'featureType':feat}) """This is going to now show the progress window...""" #progressDialogWindow = self.progressDialog.get_widget("windowLoading") #progressDialogWindow.show() #progressbarLoading = self.progressDialog.get_widget("progressbarLoading") #loadingImage = self.progressDialog.get_widget("imageLoading") cnt = 0.0 loaderLength = len(self.featListLoading) + 1.0 image_loader = ImageLoader(50, 50, '#000000') for feature in self.featListLoading: cnt = cnt + 1 if feature['filename'] != '': #loadingImage.set_from_file(feature['filename']) pass progressbarLoading.set_text(feature['text']) progressbarLoading.set_fraction(cnt/loaderLength) while gtk.events_pending(): gtk.main_iteration() #iconviewWidget= self.meMakerWin.get_widget(feature['widget']) locationsToLoad = [os.path.expanduser(feature['path'])] for locationTheme in locationsToLoad: for items in (os.listdir(locationTheme)): if os.path.isfile(locationTheme+items) == True: pixbuf = image_loader.load_cached_image(locationTheme+items) scaled_buf = pixbuf.scale_simple(50,50,gtk.gdk.INTERP_BILINEAR) pictureLocation = (locationTheme+items) if feature['featureType'] == "Head": self.modelHead.append([pictureLocation , scaled_buf]) if feature['featureType'] == "Eye": self.modelEye.append([pictureLocation , scaled_buf]) if feature['featureType'] == "Ear": self.modelEar.append([pictureLocation , scaled_buf]) if feature['featureType'] == "Nose": self.modelNose .append([pictureLocation , scaled_buf]) if feature['featureType'] == "Mouth": self.modelMouth.append([pictureLocation , scaled_buf]) if feature['featureType'] == "Beard": self.modelBeard.append([pictureLocation , scaled_buf]) if feature['featureType'] == "Hair": self.modelHair.append([pictureLocation , scaled_buf]) if feature['featureType'] == "Eyebrow": self.modelEyebrow.append([pictureLocation , scaled_buf]) if feature['featureType'] == "Glasses": self.modelGlasses.append([pictureLocation , scaled_buf]) if feature['featureType'] == "Hat": self.modelHat.append([pictureLocation , scaled_buf]) if feature['featureType'] == "Accessory": self.modelAccessory.append([pictureLocation , scaled_buf]) else: print("Found a folder, but I'm not loading what's inside.") #Hide out the loader image_loader.close() progressbarLoading.hide() comboboxThemePicker.show() #progressDialogWindow.hide() def removeFeature(self, widget): """Remove a feature from the stack""" self.currentFace.deleteFeature(self.getSelectedPage()) self.applyChanges() def arrowUpClicked(self, widget): """Apply changes after the up-arrow has been clicked""" self.currentFace.raiseFeature(self.getSelectedPage()) self.applyChanges() def arrowDownClicked(self, widget): """Apply changes after the down-arrow has been clicked""" self.currentFace.lowerFeature(self.getSelectedPage()) self.applyChanges() def getSelectedPage(self): """Take the current notbook page and return it back to the caller with the name of the notebook""" notebookFeatures = self.meMakerWin.get_widget("notebookFeatures") testing = notebookFeatures.get_current_page() return self.featureList[testing].lower() def avatarReset(self, widget = None): """Reset the avatar image to nothing""" self.currentFace.clearMe() self.applyChanges() def loadAbout(self, widget): """Open the about diolog""" aboutDialog = gtk.glade.XML( "memaker.glade", "aboutdialogMeMaker") frmAbout = aboutDialog.get_widget("aboutdialogMeMaker") result = frmAbout.run() frmAbout.destroy() return result def featureClicked(self, iconviewList, modelTheme): indexFeature = iconviewList.get_selected_items() try: selectedItems = iconviewList.get_selected_items()[0][0] locationFeature = modelTheme[selectedItems][0] except IndexError: return self.currentFace.addFeature(locationFeature,self.getSelectedPage()) iconviewList.unselect_all() self.applyChanges() def applyChanges(self): self.avatarPicture.queue_draw() def draw_scene(self, a, v): self.svgHandle = rsvg.Handle(data = self.currentFace.printMe()) cr = self.avatarPicture.window.cairo_create() self.clearIt.render_cairo(cr) self.svgHandle.render_cairo(cr) def saveLoadFeatures(self, dialog_action, file_type, file_name="MyAvatar", FILE_EXT = {"Scalable Vector Graphics | SVG":"svg","Bitmap Format | BMP":"bmp","Gnome About Me | .face":".face","JPeg Format | JPG":"JPG","Portable Network Graphic | PNG":"PNG"}): """dialog_action - The open or save mode for the dialog either gtk.FILE_CHOOSER_ACTION_OPEN, gtk.FILE_CHOOSER_ACTION_SAVE file_name - Default name when doing a save""" if (dialog_action==gtk.FILE_CHOOSER_ACTION_OPEN): dialog_buttons = (gtk.STOCK_CANCEL , gtk.RESPONSE_CANCEL , gtk.STOCK_OPEN , gtk.RESPONSE_OK) else: dialog_buttons = (gtk.STOCK_CANCEL , gtk.RESPONSE_CANCEL , gtk.STOCK_SAVE , gtk.RESPONSE_OK) file_dialog = gtk.FileChooserDialog(title="MeMaker Save Avatar" , action=dialog_action , buttons=dialog_buttons) if (dialog_action==gtk.FILE_CHOOSER_ACTION_SAVE): file_dialog.set_current_name(file_name+"."+file_type) for extension in FILE_EXT.keys(): filters = gtk.FileFilter() filters.set_name(extension) filters.add_pattern("*." + FILE_EXT[extension]) file_dialog.add_filter(filters) filters = gtk.FileFilter() filters.set_name("All files") filters.add_pattern("*") file_dialog.add_filter(filters) """Init the return value""" result = "" if file_dialog.run() == gtk.RESPONSE_OK: result = file_dialog.get_filename() file_dialog.destroy() return result app = MeMakerGui() gtk.main()
-- ubuntu-art mailing list ubuntu-art@lists.ubuntu.com https://lists.ubuntu.com/mailman/listinfo/ubuntu-art