def getDefaultStyle(cls): if cls.__defstyle is None: _s = style.Style(u'Polyline Default Style', linetype.Linetype(u'Solid', None), color.Color(0xffffff), 1.0) cls.__defstyle = _s return cls.__defstyle
def __init__(self, st=None, lt=None, col=None, t=None, **kw): """Initialize a GraphicObject. GraphicObject([st, lt, col, t]) Optional arguments: style: A style object that overrides the class default linetype: A Linetype object that overrides the value in the Style color: A Color object that overrides the value in the Style thickness: A positive float that overrides the value in the Style """ super(GraphicObject, self).__init__(**kw) _st = st if _st is None: _st = self.getDefaultStyle() if not isinstance(_st, style.Style): raise TypeError, "Invalid style type: " + ` type(_st) ` _col = _lt = _t = None if 'color' in kw: _col = kw['color'] if _col is None: _col = col if _col is not None: if not isinstance(_col, color.Color): _col = color.Color(_col) if _col == _st.getColor(): _col = None if 'linetype' in kw: _lt = kw['linetype'] if _lt is None: _lt = lt if _lt is not None: if not isinstance(_lt, linetype.Linetype): raise TypeError, "Invalid linetype type: " + ` type(_lt) ` if _lt == _st.getLinetype(): _lt = None if 'thickness' in kw: _t = kw['thickness'] if _t is None: _t = t if _t is not None: if not isinstance(_t, float): _t = util.get_float(_t) if _t < 0.0: raise ValueError, "Invalid thickness: %g" % _t if abs(_t - _st.getThickness()) < 1e-10: _t = None self.__style = _st self.__color = _col self.__linetype = _lt self.__thickness = _t
def _initialize_colors(): _color = color.Color(255, 0, 0) # red globals.colors[_color] = _color _color = color.Color(0, 255, 0) # green globals.colors[_color] = _color _color = color.Color(0, 0, 255) # blue globals.colors[_color] = _color _color = color.Color(255, 0, 255) # violet globals.colors[_color] = _color _color = color.Color(255, 255, 0) # yellow globals.colors[_color] = _color _color = color.Color(0, 255, 255) # cyan globals.colors[_color] = _color _color = color.Color(255, 255, 255) # white globals.colors[_color] = _color _color = color.Color(0, 0, 0) # black globals.colors[_color] = _color
def setColor(self, c=None): """Set the color of the object. setColor([c]) Setting the color overrides the value in the Style. Setting the color to None or invoking this method without arguments restores the color value defined in the Style. """ if self.isLocked(): raise RuntimeError, "Color change not allowed - object locked." _c = c if _c is not None: if not isinstance(_c, color.Color): _c = color.Color(c) _oc = self.getColor() if ((_c is None and self.__color is not None) or (_c is not None and _c != _oc)): self.startChange('color_changed') self.__color = _c self.endChange('color_changed') self.sendMessage('color_changed', _oc) self.modified()
def __init__(self, name, lt=None, col=None, t=None): """Instatiate a Style object. Style(name [, lt, col, t]) name: A string giving the style a name Option arguments: lt: A Linetype object - defaults to a solid line Linetype col: A Color object - defaults to the default Color object t: A positive float value - defaults to 1.0 """ if not isinstance(name, types.StringTypes): raise TypeError, "Invalid Style name: " + ` name ` _n = name if not isinstance(_n, unicode): _n = unicode(name) _lt = lt if _lt is None: _lt = linetype.Linetype('Default_Solid', None) if not isinstance(_lt, linetype.Linetype): raise TypeError, "Invalid linetype: " + ` _lt ` _c = col if _c is None: _c = Style.__defcolor if not isinstance(_c, color.Color): _c = color.Color(color) _t = t if _t is None: _t = 1.0 _t = util.get_float(_t) if _t < 0.0: raise ValueError, "Invalid line thickness: %g" % _t self.__name = _n self.__linetype = _lt self.__color = _c self.__thickness = _t
class Style(object): """A class storing a particular of Linetype, Color, and Thickness. A Style consists of four attributes: name: The Style name linetype: A Linetype object color: A Color object thickness: A positive float value giving the line thickness A Style has the following methods: getName(): Get the Style name. getColor(): Get the Style color. getLinetype(): Get the Style Linetype. getThickness(): Get the Style line thickness. clone(): Return an identical copy of the Style. Once a Style is created, the values in that object cannot be changed. """ __defcolor = color.Color(0xffffff) def __init__(self, name, lt=None, col=None, t=None): """Instatiate a Style object. Style(name [, lt, col, t]) name: A string giving the style a name Option arguments: lt: A Linetype object - defaults to a solid line Linetype col: A Color object - defaults to the default Color object t: A positive float value - defaults to 1.0 """ if not isinstance(name, types.StringTypes): raise TypeError, "Invalid Style name: " + ` name ` _n = name if not isinstance(_n, unicode): _n = unicode(name) _lt = lt if _lt is None: _lt = linetype.Linetype('Default_Solid', None) if not isinstance(_lt, linetype.Linetype): raise TypeError, "Invalid linetype: " + ` _lt ` _c = col if _c is None: _c = Style.__defcolor if not isinstance(_c, color.Color): _c = color.Color(color) _t = t if _t is None: _t = 1.0 _t = util.get_float(_t) if _t < 0.0: raise ValueError, "Invalid line thickness: %g" % _t self.__name = _n self.__linetype = _lt self.__color = _c self.__thickness = _t def __eq__(self, obj): """Compare a Style object to another Style for equality. Comparing two styles is really comparing that the linetypes are the same, then the colors are the same, and that the line thickness are the same (within a tiny tolerance). If all three are the same, the comparison returns True. Otherwise, the comparison returns False. """ if not isinstance(obj, Style): return False if obj is self: return True return (self.__name == obj.getName() and self.__linetype == obj.getLinetype() and self.__color == obj.getColor() and abs(self.__thickness - obj.getThickness()) < 1e-10) def __ne__(self, obj): """Compare a Style object to another Style for non-equality. Comparing two styles is really comparing that the linetypes are the same, then the colors are the same, and that the line thickness are the same (within a tiny tolerance). If all three are the same, the comparison returns False. Otherwise, the comparison returns True. """ return not self == obj def __hash__(self): """Return a hash value for the Style. Defining this method allows Styles to be stored in dictionaries. """ _val = hash(self.__color) _val = _val ^ hash(self.__linetype) _val = _val ^ hash(long(self.__thickness * 1e10)) return _val def getName(self): """Return the name of the Style. getName() """ return self.__name name = property(getName, None, None, "Style name.") def getLinetype(self): """Return the Linetype used by this Style. getLinetype() """ return self.__linetype linetype = property(getLinetype, None, None, "Style Linetype") def getColor(self): """Return the Color used by this Style. getColor() """ return self.__color color = property(getColor, None, None, "Style Color") def getThickness(self): """Return the line thickness used by this style. getThickness() """ return self.__thickness thickness = property(getThickness, None, None, "Style Thickness.") def getStyleValues(self): _n = self.__name _l = self.__linetype.getName(), self.__linetype.getList() _c = self.__color.getColors() _t = self.thickness return _n, _l, _c, _t def clone(self): """Return an identical copy of a Style. clone() """ _name = self.__name[:] _linetype = self.__linetype.clone() _color = self.__color.clone() _thickness = self.__thickness return Style(_name, _linetype, _color, _thickness)
def textstyle_dialog(gtkimage, textstyles): _window = gtkimage.getWindow() _dialog = gtk.Dialog( _('TextStyle Settings'), _window, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)) _image = gtkimage.getImage() _ts = _image.getOption('TEXT_STYLE') _hbox = gtk.HBox(False, 5) _hbox.set_border_width(5) _label = gtk.Label(_('Active TextStyle:')) _hbox.pack_start(_label, False, False, 5) _idx = 0 if hasattr(gtk, 'ComboBox'): _widget = gtk.combo_box_new_text() for _i in range(len(textstyles)): _textstyle = textstyles[_i] if (_ts is _textstyle or _ts == _textstyle): _idx = _i _widget.append_text(_textstyle.getName()) _widget.set_active(_idx) else: _menu = gtk.Menu() for _i in range(len(textstyles)): _textstyle = textstyles[_i] if (_ts is _textstyle or _ts == _textstyle): _idx = _i _item = gtk.MenuItem(_textstyle.getName()) _menu.append(_item) _widget = gtk.OptionMenu() _widget.set_menu(_menu) _widget.set_history(_idx) _hbox.pack_start(_widget, False, False, 0) _dialog.vbox.pack_start(_hbox, True, True) # _gts = GtkTextStyle(_window) for _textstyle in textstyles: _gts.addTextStyle(_textstyle) _gts.setTextStyle(_ts) _gts.setImageSettings(_image) _fill_dialog(_dialog.vbox, _gts) _widget.connect('changed', _widget_changed, _gts) _dialog.show_all() _response = _dialog.run() if _response == gtk.RESPONSE_OK: _nts = _gts.getTextStyle() if _nts != _ts: _image.setOption('TEXT_STYLE', _nts) for _opt, _val in _gts.getValues(): if _opt == 'FONT_FAMILY': if _val != _image.getOption(_opt): _image.setOption(_opt, _val) elif _opt == 'FONT_STYLE': if _val != _image.getOption(_opt): _image.setOption(_opt, _val) elif _opt == 'FONT_WEIGHT': if _val != _image.getOption(_opt): _image.setOption(_opt, _val) elif _opt == 'FONT_COLOR': if _val != _image.getOption(_opt).getColors(): _r, _g, _b = _val _image.setOption(_opt, color.Color(_r, _g, _b)) elif _opt == 'TEXT_SIZE': if abs(_val - _image.getOption(_opt)) > 1e-10: _image.setOption(_opt, _val) elif _opt == 'TEXT_ANGLE': if abs(_val - _image.getOption(_opt)) > 1e-10: _image.setOption(_opt, _val) elif _opt == 'TEXT_ALIGNMENT': if _val != _image.getOption(_opt): _image.setOption(_opt, _val) else: raise RuntimeError, "Unexpected TextStyle option '%s'" % _opt _gts.clear() _dialog.destroy()