def __init__ (self, title = 'Save file', parent = None, action = gtk.FILE_CHOOSER_ACTION_SAVE, buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK), path = None, filetypes = [], default_filetype = None ): super (FileChooserDialog, self).__init__ (title, parent, action, buttons) self.set_default_response (gtk.RESPONSE_OK) if not path: path = os.getcwd() + os.sep # create an extra widget to list supported image formats self.set_current_folder (path) self.set_current_name ('image.' + default_filetype) hbox = gtk.HBox (spacing=10) hbox.pack_start (gtk.Label ("File Format:"), expand=False) liststore = gtk.ListStore(gobject.TYPE_STRING) cbox = gtk.ComboBox(liststore) cell = gtk.CellRendererText() cbox.pack_start(cell, True) cbox.add_attribute(cell, 'text', 0) hbox.pack_start (cbox) self.filetypes = filetypes self.sorted_filetypes = filetypes.items() self.sorted_filetypes.sort() default = 0 for i, (ext, name) in enumerate(self.sorted_filetypes): cbox.append_text ("%s (*.%s)" % (name, ext)) if ext == default_filetype: default = i cbox.set_active(default) self.ext = default_filetype def cb_cbox_changed (cbox, data=None): """File extension changed""" head, filename = os.path.split(self.get_filename()) root, ext = os.path.splitext(filename) ext = ext[1:] new_ext = self.sorted_filetypes[cbox.get_active()][0] self.ext = new_ext if ext in self.filetypes: filename = root + '.' + new_ext elif ext == '': filename = filename.rstrip('.') + '.' + new_ext self.set_current_name (filename) cbox.connect ("changed", cb_cbox_changed) hbox.show_all() self.set_extra_widget(hbox)
def toggled(item, data=None): if item == self.itemAll: for item in items: item.set_active(True) elif item == self.itemInvert: for item in items: item.set_active(not item.get_active()) ind = [i for i,item in enumerate(items) if item.get_active()] self.set_active(ind)
def _draw_mathtext(self, gc, x, y, s, prop, angle): if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) # mathtext using the gtk/gdk method if numx.which[0] == "numarray": warnings.warn("_draw_mathtext() currently works for numpy, but " "not numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo with Numeric support is required for " "_draw_mathtext()") return size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font( s, self.dpi.get(), size) if angle==90: width, height = height, width x -= width y -= height imw, imh, s = fonts[0].image_as_str() N = imw*imh # a numpixels by num fonts array Xall = numx.zeros((N,len(fonts)), typecode=numx.UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:,i] = numx.fromstring(s, numx.UInt8) # get the max alpha at each pixel Xs = numx.mlab.max (Xall,1) # convert it to it's proper shape Xs.shape = imh, imw pa = numx.zeros(shape=(imh,imw,4), typecode=numx.UInt8) rgb = gc.get_rgb() pa[:,:,0] = int(rgb[0]*255) pa[:,:,1] = int(rgb[1]*255) pa[:,:,2] = int(rgb[2]*255) pa[:,:,3] = Xs # works for numpy pa, not a numarray pa surface = cairo.ImageSurface.create_for_array (pa) gc.ctx.set_source_surface (surface, x, y) gc.ctx.paint()
def _draw_mathtext(self, gc, x, y, s, prop, angle): size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size) if angle == 90: width, height = height, width x -= width y -= height imw, imh, image_str = fonts[0].image_as_str() N = imw * imh # a numpixels by num fonts array Xall = zeros((N, len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, image_str = font.image_as_str() Xall[:, i] = fromstring(image_str, UInt8) # get the max alpha at each pixel Xs = numerix.mlab.max(Xall, 1) # convert it to it's proper shape Xs.shape = imh, imw pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=True, bits_per_sample=8, width=imw, height=imh) array = pixbuf_get_pixels_array(pixbuf) rgb = gc.get_rgb() array[:, :, 0] = int(rgb[0] * 255) array[:, :, 1] = int(rgb[1] * 255) array[:, :, 2] = int(rgb[2] * 255) array[:, :, 3] = Xs try: # new in 2.2 # can use None instead of gc.gdkGC, if don't need clipping self.gdkDrawable.draw_pixbuf(gc.gdkGC, pixbuf, 0, 0, int(x), int(y), imw, imh, gdk.RGB_DITHER_NONE, 0, 0) except AttributeError: # deprecated in 2.2 pixbuf.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0, int(x), int(y), imw, imh, gdk.RGB_DITHER_NONE, 0, 0)
def _draw_mathtext(self, gc, x, y, s, prop, angle): if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) # mathtext using the gtk/gdk method if numx.which[0] == "numarray": warnings.warn("_draw_mathtext() currently works for numpy, but " "not numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo with Numeric support is required for " "_draw_mathtext()") return size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size) if angle == 90: width, height = height, width x -= width y -= height imw, imh, s = fonts[0].image_as_str() N = imw * imh # a numpixels by num fonts array Xall = numx.zeros((N, len(fonts)), typecode=numx.UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:, i] = numx.fromstring(s, numx.UInt8) # get the max alpha at each pixel Xs = numx.mlab.max(Xall, 1) # convert it to it's proper shape Xs.shape = imh, imw pa = numx.zeros(shape=(imh, imw, 4), typecode=numx.UInt8) rgb = gc.get_rgb() pa[:, :, 0] = int(rgb[0] * 255) pa[:, :, 1] = int(rgb[1] * 255) pa[:, :, 2] = int(rgb[2] * 255) pa[:, :, 3] = Xs # works for numpy pa, not a numarray pa surface = cairo.ImageSurface.create_for_array(pa) gc.ctx.set_source_surface(surface, x, y) gc.ctx.paint()
def _draw_mathtext(self, gc, x, y, s, prop, angle): if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name() # mathtext using the gtk/gdk method if numerix.which[0] == "numarray": warnings.warn("_draw_mathtext() currently works for numpy, but not numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo.numpy module required for _draw_mathtext()") return size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font( s, self.dpi.get(), size) if angle==90: width, height = height, width x -= width y -= height imw, imh, s = fonts[0].image_as_str() N = imw*imh # a numpixels by num fonts array Xall = zeros((N,len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:,i] = fromstring(s, UInt8) # get the max alpha at each pixel Xs = numerix.mlab.max(Xall,1) # convert it to it's proper shape Xs.shape = imh, imw pa = zeros(shape=(imh,imw,4), typecode=UInt8) rgb = gc.get_rgb() pa[:,:,0] = int(rgb[0]*255) pa[:,:,1] = int(rgb[1]*255) pa[:,:,2] = int(rgb[2]*255) pa[:,:,3] = Xs # works for numpy pa, not a numarray pa surface = cairo.numpy.surface_create_for_array(pa) gc.ctx.translate (x,y) gc.ctx.show_surface (surface, imw, imh)
def _draw_mathtext(self, gc, x, y, s, prop, angle): size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font( s, self.dpi.get(), size) if angle==90: width, height = height, width x -= width y -= height imw, imh, image_str = fonts[0].image_as_str() N = imw*imh # a numpixels by num fonts array Xall = zeros((N,len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, image_str = font.image_as_str() Xall[:,i] = fromstring(image_str, UInt8) # get the max alpha at each pixel Xs = numerix.mlab.max(Xall,1) # convert it to it's proper shape Xs.shape = imh, imw pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=True, bits_per_sample=8, width=imw, height=imh) array = pixbuf_get_pixels_array(pixbuf) rgb = gc.get_rgb() array[:,:,0]=int(rgb[0]*255) array[:,:,1]=int(rgb[1]*255) array[:,:,2]=int(rgb[2]*255) array[:,:,3]=Xs try: # new in 2.2 # can use None instead of gc.gdkGC, if don't need clipping self.gdkDrawable.draw_pixbuf (gc.gdkGC, pixbuf, 0, 0, int(x), int(y), imw, imh, gdk.RGB_DITHER_NONE, 0, 0) except AttributeError: # deprecated in 2.2 pixbuf.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0, int(x), int(y), imw, imh, gdk.RGB_DITHER_NONE, 0, 0)
def draw_mathtext(self, gc, x, y, s, prop, angle): size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size) if angle == 90: width, height = height, width x = int(x) y = int(y) rgb = gc.get_rgb() #rgba = (rgb[0], rgb[1], rgb[2], gc.get_alpha()) imw, imh, s = fonts[0].image_as_str() N = imw * imh # a numpixels by num fonts array Xall = zeros((N, len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:, i] = fromstring(s, UInt8) # get the max alpha at each pixel Xs = numerix.max(Xall, 1) # convert it to it's proper shape Xs.shape = imh, imw pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=1, bits_per_sample=8, width=imw, height=imh) try: pa = pb.get_pixels_array() except AttributeError: pa = pb.pixel_array except RuntimeError, exc: # pygtk was not compiled with Numeric Python support print >> sys.stderr, 'Error:', exc return
def _draw_mathtext(self, gc, x, y, s, prop, angle): size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size) if angle == 90: width, height = height, width x -= width y -= height imw, imh, s = fonts[0].image_as_str() N = imw * imh # a numpixels by num fonts array Xall = zeros((N, len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:, i] = fromstring(s, UInt8) # get the max alpha at each pixel Xs = numerix.max(Xall, 1) # convert it to it's proper shape Xs.shape = imh, imw pb = gtk.gdk.Pixbuf( gtk.gdk.COLORSPACE_RGB, #has_alpha=1, bits_per_sample=8, width=imw, height=imh) has_alpha=True, bits_per_sample=8, width=imw, height=imh) try: pa = pb.get_pixels_array() except AttributeError: pa = pb.pixel_array except RuntimeError, exc: # 'pygtk was not compiled with Numeric Python support' verbose.report_error('mathtext not supported: %s' % exc) return
def save_figure(ptr, base): filetypes = base.canvas.get_supported_filetypes() default_filetype = base.canvas.get_default_filetype() sorted_filetypes = filetypes.items() sorted_filetypes.sort() selected_filter = 0 filters = [] for i, (ext, name) in enumerate(sorted_filetypes): filter = '%s (*.%s)' % (name, ext) filters.append(filter) if ext == default_filetype: selected_filter = i filters = '\t'.join(filters) file_chooser = base._fc file_chooser.filter(filters) file_chooser.filter_value(selected_filter) file_chooser.show() while file_chooser.visible(): Fltk.Fl.wait() fname = None if (file_chooser.count() and file_chooser.value(0) != None): fname = "" (status, fname) = Fltk.fl_filename_absolute(fname, 1024, file_chooser.value(0)) if fname is None: # Cancel return #start from last directory lastDir = os.path.dirname(fname) file_chooser.directory(lastDir) format = sorted_filetypes[file_chooser.filter_value()][0] try: base.canvas.print_figure(fname, format=format) except IOError, msg: err = '\n'.join(map(str, msg)) msg = 'Failed to save %s: Error msg was\n\n%s' % (fname, err) error_msg_fltk(msg)
def save_figure(ptr,base): filetypes = base.canvas.get_supported_filetypes() default_filetype = base.canvas.get_default_filetype() sorted_filetypes = filetypes.items() sorted_filetypes.sort() selected_filter = 0 filters = [] for i, (ext, name) in enumerate(sorted_filetypes): filter = '%s (*.%s)' % (name, ext) filters.append(filter) if ext == default_filetype: selected_filter = i filters = '\t'.join(filters) file_chooser=base._fc file_chooser.filter(filters) file_chooser.filter_value(selected_filter) file_chooser.show() while file_chooser.visible() : Fltk.Fl.wait() fname=None if(file_chooser.count() and file_chooser.value(0) != None): fname="" (status,fname)=Fltk.fl_filename_absolute(fname, 1024, file_chooser.value(0)) if fname is None: # Cancel return #start from last directory lastDir = os.path.dirname(fname) file_chooser.directory(lastDir) format = sorted_filetypes[file_chooser.filter_value()][0] try: base.canvas.print_figure(fname, format=format) except IOError, msg: err = '\n'.join(map(str, msg)) msg = 'Failed to save %s: Error msg was\n\n%s' % ( fname, err) error_msg_fltk(msg)
def _draw_mathtext(self, gc, x, y, s, prop, angle): size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font( s, self.dpi.get(), size) if angle==90: width, height = height, width x -= width y -= height imw, imh, s = fonts[0].image_as_str() N = imw*imh # a numpixels by num fonts array Xall = zeros((N,len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:,i] = fromstring(s, UInt8) # get the max alpha at each pixel Xs = numerix.mlab.max(Xall,1) # convert it to it's proper shape Xs.shape = imh, imw pb=gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, #has_alpha=1, bits_per_sample=8, width=imw, height=imh) has_alpha=True, bits_per_sample=8, width=imw, height=imh) try: pa = pb.get_pixels_array() except AttributeError: pa = pb.pixel_array except RuntimeError, exc: # 'pygtk was not compiled with Numeric Python support' verbose.report_error('mathtext not supported: %s' % exc) return
class DialogLineprops: """ A GUI dialog for controlling lineprops """ signals = ( 'on_combobox_lineprops_changed', 'on_combobox_linestyle_changed', 'on_combobox_marker_changed', 'on_colorbutton_linestyle_color_set', 'on_colorbutton_markerface_color_set', 'on_dialog_lineprops_okbutton_clicked', 'on_dialog_lineprops_cancelbutton_clicked', ) linestyles = ( '-', '--', '-.', ',', 'steps', 'None', ) linestyled = dict([(s, i) for i, s in enumerate(linestyles)]) markers = ( 'None', '.', ',', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', 's', 'p', 'h', 'H', '+', 'x', 'D', 'd', '|', '_', ) markerd = dict([(s, i) for i, s in enumerate(markers)]) def __init__(self, lines): datadir = matplotlib.get_data_path() gladefile = os.path.join(datadir, 'lineprops.glade') if not os.path.exists(gladefile): raise IOError('Could not find gladefile lineprops.glade in %s' % datadir) self._inited = False self._updateson = True # suppress updates when setting widgets manually self.wtree = gtk.glade.XML(gladefile, 'dialog_lineprops') self.wtree.signal_autoconnect( dict([(s, getattr(self, s)) for s in self.signals])) self.dlg = self.wtree.get_widget('dialog_lineprops') self.lines = lines cbox = self.wtree.get_widget('combobox_lineprops') cbox.set_active(0) self.cbox_lineprops = cbox cbox = self.wtree.get_widget('combobox_linestyles') for ls in self.linestyles: cbox.append_text(ls) cbox.set_active(0) self.cbox_linestyles = cbox cbox = self.wtree.get_widget('combobox_markers') for m in self.markers: cbox.append_text(m) cbox.set_active(0) self.cbox_markers = cbox self._lastcnt = 0 self._inited = True def show(self): 'populate the combo box' self._updateson = False # flush the old cbox = self.cbox_lineprops for i in range(self._lastcnt - 1, -1, -1): cbox.remove_text(i) # add the new for line in self.lines: cbox.append_text(line.get_label()) cbox.set_active(0) self._updateson = True self._lastcnt = len(self.lines) self.dlg.show() def get_active_line(self): 'get the active line' ind = self.cbox_lineprops.get_active() line = self.lines[ind] return line def get_active_linestyle(self): 'get the active lineinestyle' ind = self.cbox_linestyles.get_active() ls = self.linestyles[ind] return ls def get_active_marker(self): 'get the active lineinestyle' ind = self.cbox_markers.get_active() m = self.markers[ind] return m def _update(self): 'update the active line props from the widgets' if not self._inited or not self._updateson: return line = self.get_active_line() ls = self.get_active_linestyle() marker = self.get_active_marker() line.set_linestyle(ls) line.set_marker(marker) button = self.wtree.get_widget('colorbutton_linestyle') color = button.get_color() r, g, b = [val / 65535. for val in color.red, color.green, color.blue] line.set_color((r, g, b)) button = self.wtree.get_widget('colorbutton_markerface') color = button.get_color() r, g, b = [val / 65535. for val in color.red, color.green, color.blue] line.set_markerfacecolor((r, g, b)) line.figure.canvas.draw() def on_combobox_lineprops_changed(self, item): 'update the widgets from the active line' if not self._inited: return self._updateson = False line = self.get_active_line() ls = line.get_linestyle() if ls is None: ls = 'None' self.cbox_linestyles.set_active(self.linestyled[ls]) marker = line.get_marker() if marker is None: marker = 'None' self.cbox_markers.set_active(self.markerd[marker]) r, g, b = colorConverter.to_rgb(line.get_color()) color = gtk.gdk.Color(*[int(val * 65535) for val in r, g, b]) button = self.wtree.get_widget('colorbutton_linestyle') button.set_color(color) r, g, b = colorConverter.to_rgb(line.get_markerfacecolor()) color = gtk.gdk.Color(*[int(val * 65535) for val in r, g, b]) button = self.wtree.get_widget('colorbutton_markerface') button.set_color(color) self._updateson = True def on_combobox_linestyle_changed(self, item): self._update() def on_combobox_marker_changed(self, item): self._update() def on_colorbutton_linestyle_color_set(self, button): self._update() def on_colorbutton_markerface_color_set(self, button): 'called colorbutton marker clicked' self._update() def on_dialog_lineprops_okbutton_clicked(self, button): self._update() self.dlg.hide() def on_dialog_lineprops_cancelbutton_clicked(self, button): self.dlg.hide()