2007-11-24 Benny Malengier <benny.malengier@gramps-project.org>

* src/GrampsWidgets.py: ObjEntry, PlaceEntry and NoteEntry inherit from 
it.


svn: r9398
This commit is contained in:
Benny Malengier 2007-11-24 16:57:45 +00:00
parent 3d2b06a66a
commit 40d9440e86
2 changed files with 190 additions and 42 deletions

View File

@ -1,3 +1,6 @@
2007-11-24 Benny Malengier <benny.malengier@gramps-project.org>
* src/GrampsWidgets.py: ObjEntry, PlaceEntry and NoteEntry inherit from it.
2007-11-24 Douglas S. Blank <dblank@cs.brynmawr.edu> 2007-11-24 Douglas S. Blank <dblank@cs.brynmawr.edu>
* src/gen/lib/date.py: had to import calendar by path.name * src/gen/lib/date.py: had to import calendar by path.name
(so that the import of date_test would work) (so that the import of date_test would work)

View File

@ -736,14 +736,23 @@ class MonitoredDate:
field.set_editable(not readonly) field.set_editable(not readonly)
button.set_sensitive(not readonly) button.set_sensitive(not readonly)
class PlaceEntry: class ObjEntry:
""" """
Handles the selection of a existing or new Place. Supports Drag and Drop Handles the selection of a existing or new Object. Supports Drag and Drop
to select a place. to select the object.
This is the base class to create a real entry
""" """
def __init__(self, dbstate, uistate, track, obj, set_val, def __init__(self, dbstate, uistate, track, obj, set_val,
get_val, add_del, share): get_val, add_del, share):
'''Pass the dbstate and uistate and present track.
obj is a gtk.Label that shows the persent value
set_val is function that is called when handle changes, use it
to update the calling module
get_val is function that is called to obtain handle from calling
module
add_del is the gtk.Button with add or delete value
share is the gtk.Button to call the object selector
'''
self.obj = obj self.obj = obj
self.add_del = add_del self.add_del = add_del
self.share = share self.share = share
@ -755,14 +764,15 @@ class PlaceEntry:
self.track = track self.track = track
self.tooltips = gtk.Tooltips() self.tooltips = gtk.Tooltips()
self.obj.drag_dest_set(gtk.DEST_DEFAULT_ALL, [DdTargets.PLACE_LINK.target()], #connect drag and drop
gtk.gdk.ACTION_COPY) self._init_dnd()
self.obj.connect('drag_data_received', self.drag_data_received) #set the object specific code
self._init_object()
if get_val(): if get_val():
self.set_button(True) self.set_button(True)
p = self.db.get_place_from_handle(self.get_val()) p = self.get_from_handle(self.get_val())
name = "%s [%s]" % (p.get_title(), p.gramps_id) name = self.get_label(p)
else: else:
name = u"" name = u""
self.set_button(False) self.set_button(False)
@ -778,61 +788,84 @@ class PlaceEntry:
self.share.connect('clicked', self.share_clicked) self.share.connect('clicked', self.share_clicked)
if not self.db.readonly and not name: if not self.db.readonly and not name:
obj.set_text("<i>%s</i>" % _('To select a place, use drag-and-drop or use the buttons')) obj.set_text(self.EMPTY_TEXT)
obj.set_use_markup(True) obj.set_use_markup(True)
else: else:
obj.set_text(name) obj.set_text(name)
def after_edit(self, place): def _init_dnd(self):
name = "%s [%s]" % (place.get_title(), place.gramps_id) '''inheriting objects must set this
'''
pass
def _init_object(self):
'''inheriting objects can use this to set extra variables
'''
pass
def get_from_handle(self, handle):
''' return the object given the hande
inheriting objects must set this
'''
pass
def get_label(self, object):
''' return the label
inheriting objects must set this
'''
pass
def after_edit(self, obj):
name = self.get_label(obj)
self.obj.set_text(name) self.obj.set_text(name)
def add_del_clicked(self, obj): def add_del_clicked(self, obj):
''' if value, delete, if no value, call editor
'''
if self.get_val(): if self.get_val():
self.set_val(None) self.set_val(None)
self.obj.set_text(u'') self.obj.set_text(u'')
self.set_button(False) self.set_button(False)
else: else:
from gen.lib import Place self.call_editor()
from Editors import EditPlace
place = Place() def call_editor(self, obj):
try: '''inheriting objects must set this
EditPlace(self.dbstate, self.uistate, self.track, '''
place, self.place_added) pass
except WindowActiveError:
def call_selector(self):
'''inheriting objects must set this
'''
pass pass
def drag_data_received(self, widget, context, x, y, selection, info, time): def drag_data_received(self, widget, context, x, y, selection, info, time):
(drag_type, idval, obj, val) = pickle.loads(selection.data) (drag_type, idval, obj, val) = pickle.loads(selection.data)
data = self.db.get_place_from_handle(obj) data = self.db.get_place_from_handle(obj)
self.place_added(data) self.obj_added(data)
def place_added(self, data): def obj_added(self, data):
''' callback from adding an object to the entry'''
self.set_val(data.handle) self.set_val(data.handle)
self.obj.set_text("%s [%s]" % (data.get_title(), data.gramps_id)) self.obj.set_text(self.get_label(data))
self.set_button(True) self.set_button(True)
def share_clicked(self, obj): def share_clicked(self, obj):
''' if value, edit object, in no value, select existing object
'''
if self.get_val(): if self.get_val():
from Editors import EditPlace obj = self.get_from_handle(self.get_val())
self.call_editor(obj)
place = self.db.get_place_from_handle(self.get_val())
try:
EditPlace(self.dbstate, self.uistate, self.track, place,
self.after_edit)
except WindowActiveError:
pass
else: else:
from Selectors import selector_factory select = self.call_selector()
cls = selector_factory('Place') obj = select.run()
select = cls(self.dbstate, self.uistate, self.track) if obj:
place = select.run() self.obj_added(obj)
if place:
self.place_added(place)
def set_button(self, use_add): def set_button(self, use_add):
''' This sets the correct image to the two buttons
'''
for i in self.add_del.get_children(): for i in self.add_del.get_children():
self.add_del.remove(i) self.add_del.remove(i)
for i in self.share.get_children(): for i in self.share.get_children():
@ -847,8 +880,8 @@ class PlaceEntry:
image.set_from_stock(gtk.STOCK_EDIT, gtk.ICON_SIZE_BUTTON) image.set_from_stock(gtk.STOCK_EDIT, gtk.ICON_SIZE_BUTTON)
image.show() image.show()
self.share.add(image) self.share.add(image)
self.tooltips.set_tip(self.share, _('Edit place')) self.tooltips.set_tip(self.share, self.EDIT_STR)
self.tooltips.set_tip(self.add_del, _('Remove place')) self.tooltips.set_tip(self.add_del, self.DEL_STR)
else: else:
image = gtk.Image() image = gtk.Image()
image.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON) image.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON)
@ -858,8 +891,120 @@ class PlaceEntry:
image.set_from_stock(gtk.STOCK_INDEX, gtk.ICON_SIZE_BUTTON) image.set_from_stock(gtk.STOCK_INDEX, gtk.ICON_SIZE_BUTTON)
image.show() image.show()
self.share.add(image) self.share.add(image)
self.tooltips.set_tip(self.share, _('Select an existing place')) self.tooltips.set_tip(self.share, self.SHARE_STR)
self.tooltips.set_tip(self.add_del, _('Add a new place')) self.tooltips.set_tip(self.add_del, self.ADD_STR)
class PlaceEntry(ObjEntry):
"""
Handles the selection of a existing or new Place. Supports Drag and Drop
to select a place.
"""
EMPTY_TEXT = "<i>%s</i>" % _('To select a place, use drag-and-drop '
'or use the buttons')
EDIT_STR = _('Edit place')
SHARE_STR = _('Select an existing place')
ADD_STR = _('Add a new place')
DEL_STR = _('Remove place')
def __init__(self, dbstate, uistate, track, obj, set_val,
get_val, add_del, share):
ObjEntry.__init__(self, dbstate, uistate, track, obj, set_val,
get_val, add_del, share)
def _init_dnd(self):
'''connect drag and drop of places
'''
self.obj.drag_dest_set(gtk.DEST_DEFAULT_ALL, [DdTargets.PLACE_LINK.target()],
gtk.gdk.ACTION_COPY)
self.obj.connect('drag_data_received', self.drag_data_received)
def get_from_handle(self, handle):
''' return the object given the hande
'''
return self.db.get_place_from_handle(handle)
def get_label(self, place):
return "%s [%s]" % (place.get_title(), place.gramps_id)
def call_editor(self, obj=None):
from Editors import EditPlace
if obj is None:
from gen.lib import Place
place = Place()
func = self.obj_added
else:
place = obj
func = self.after_edit
try:
EditPlace(self.dbstate, self.uistate, self.track,
place, func)
except WindowActiveError:
pass
def call_selector(self):
from Selectors import selector_factory
cls = selector_factory('Place')
return cls(self.dbstate, self.uistate, self.track)
class NoteEntry(ObjEntry):
"""
Handles the selection of a existing or new Note. Supports Drag and Drop
to select a Note.
"""
EMPTY_TEXT = "<i>%s</i>" % _('To select a note, use drag-and-drop '
'or use the buttons')
EDIT_STR = _('Edit Note')
SHARE_STR = _('Select an existing note')
ADD_STR = _('Add a new note')
DEL_STR = _('Remove note')
def __init__(self, dbstate, uistate, track, obj, set_val,
get_val, add_del, share):
ObjEntry.__init__(self, dbstate, uistate, track, obj, set_val,
get_val, add_del, share)
def _init_dnd(self):
'''connect drag and drop of places
'''
self.obj.drag_dest_set(gtk.DEST_DEFAULT_ALL, [DdTargets.NOTE_LINK.target()],
gtk.gdk.ACTION_COPY)
self.obj.connect('drag_data_received', self.drag_data_received)
def get_from_handle(self, handle):
''' return the object given the hande
'''
return self.db.get_note_from_handle(handle)
def get_label(self, note):
note = " ".join(note.get(markup=False).split())
if len(note) > 35:
txt = note[:35]+"..."
else:
txt = note
return "%s [%s]" % (txt, p.gramps_id)
def call_editor(self, obj=None):
from Editors import EditNote
if obj is None:
from gen.lib import Note, Notetype
note = Note()
note.set_type(NoteType.REPORT)
func = self.obj_added
else:
note = obj
func = self.after_edit
try:
EditNote(self.dbstate, self.uistate, self.track,
note, func)
except WindowActiveError:
pass
def call_selector(self):
from Selectors import selector_factory
cls = selector_factory('Note')
return cls(self.dbstate, self.uistate, self.track)
class Statusbar(gtk.HBox): class Statusbar(gtk.HBox):
"""Custom Statusbar with flexible number of "bars". """Custom Statusbar with flexible number of "bars".