def create_event(self, description=_("Estimated date"), type=None, date=None, source=None, note_text="", modifier=None): event = Event() event.set_description(description) note = Note() note.handle = create_id() note.type.set(NoteType.EVENT) note.set(note_text) self.db.add_note(note, self.trans) event.add_note(note.handle) if type: event.set_type(EventType(type)) if date: if modifier: date.set_modifier(modifier) date.set_quality(Date.QUAL_ESTIMATED) date.set_yr_mon_day(date.get_year(), 0, 0) event.set_date_object(date) if source: citation = Citation() citation.set_reference_handle(source.get_handle()) self.db.add_citation(citation, self.trans) event.add_citation(citation.get_handle()) self.db.commit_source(source, self.trans) self.db.add_event(event, self.trans) return event
def find_and_set_citation(self, obj, source): # look for the source in the existing citations for the object LOG.debug("find_and_set_citation: looking for source: %s", source.get_gramps_id()) for citation_handle in obj.get_citation_list(): citation = self.db.get_citation_from_handle(citation_handle) LOG.debug("find_and_set_citation: existing citation: %s", citation.get_gramps_id()) poss_source = self.db.get_source_from_handle( citation.get_reference_handle()) LOG.debug(" compare source %s == %s", source.get_gramps_id(), poss_source.get_gramps_id()) if poss_source.get_handle() == source.get_handle(): # The source is already cited LOG.debug(" source already cited") return # we couldn't find an appropriate citation, so we have to create one. citation = Citation() LOG.debug(" creating citation") citation.set_reference_handle(source.get_handle()) self.db.add_citation(citation, self.trans) LOG.debug(" created citation, citation %s %s" % (citation, citation.get_gramps_id())) obj.add_citation(citation.get_handle())
class CensusEditor(ManagedWindow): """ Census Editor. """ def __init__(self, dbstate, uistate, track, event): self.dbstate = dbstate self.uistate = uistate self.track = track self.db = dbstate.db self.event = event self.citation = get_census_citation(self.db, self.event) if self.citation is None: self.citation = Citation() ManagedWindow.__init__(self, uistate, track, event) self.widgets = {} top = self.__create_gui() self.set_window(top, None, self.get_menu_title()) self._config = config.get_manager('census') width = self._config.get('interface.census-width') height = self._config.get('interface.census-height') self.window.resize(width, height) self.place_field = PlaceEntry(self.dbstate, self.uistate, self.track, self.widgets['place_text'], self.event.set_place_handle, self.event.get_place_handle, self.widgets['place_add'], self.widgets['place_share']) self.ref_field = MonitoredEntry( self.widgets['ref_entry'], self.citation.set_page, self.citation.get_page, self.db.readonly) if self.event.get_handle(): self.widgets['census_combo'].set_sensitive(False) self.__populate_gui(event) self.details.populate_gui(event) def _add_tab(self, notebook, page): notebook.append_page(page, page.get_tab_widget()) page.label.set_use_underline(True) return page def get_menu_title(self): """ Get the menu title. """ if self.event.get_handle(): date = get_date(self.event) if not date: date = 'unknown' dialog_title = _('Census: %s') % date else: dialog_title = _('New Census') return dialog_title def build_menu_names(self, event): """ Build menu names. Overrides method in ManagedWindow. """ return (_('Edit Census'), self.get_menu_title()) def __create_gui(self): """ Create and display the GUI components of the editor. """ root = Gtk.Window(type=Gtk.WindowType.TOPLEVEL) root.set_transient_for(self.uistate.window) vbox = Gtk.VBox() tab = Gtk.Table(4, 4, False) tab.set_row_spacings(10) tab.set_col_spacings(10) census_label = Gtk.Label(_("Source:")) census_label.set_alignment(0.0, 0.5) tab.attach(census_label, 0, 1, 0, 1, xoptions=Gtk.AttachOptions.FILL, xpadding=10) liststore = Gtk.ListStore(str, str, str) for row in get_census_sources(self.db): liststore.append([row[0].decode(), row[1], row[2]]) census_combo = Gtk.ComboBox() census_combo.set_model(liststore) cell = Gtk.CellRendererText() census_combo.pack_start(cell, True) census_combo.add_attribute(cell, 'text', 1) #cell = Gtk.CellRendererText() #census_combo.pack_start(cell, True) #census_combo.add_attribute(cell, 'text', 2) census_combo.connect('changed', self.__census_changed) self.widgets['census_combo'] = census_combo hbox = Gtk.HBox() hbox.pack_start(census_combo, False, True, 0) tab.attach(hbox, 1, 2, 0, 1) date_label = Gtk.Label(_("Date:")) date_label.set_alignment(0.0, 0.5) tab.attach(date_label, 0, 1, 1, 2, xoptions=Gtk.AttachOptions.FILL, xpadding=10) date_text = Gtk.Label() date_text.set_alignment(0.0, 0.5) tab.attach(date_text, 1, 2, 1, 2) self.widgets['date_text'] = date_text ref_label = Gtk.Label(_("Reference:")) ref_label.set_alignment(0.0, 0.5) tab.attach(ref_label, 0, 1, 2, 3, xoptions=Gtk.AttachOptions.FILL, xpadding=10) ref_entry = Gtk.Entry() tab.attach(ref_entry, 1, 2, 2, 3) self.widgets['ref_entry'] = ref_entry place_label = Gtk.Label(_("Place:")) place_label.set_alignment(0.0, 0.5) tab.attach(place_label, 0, 1, 3, 4, xoptions=Gtk.AttachOptions.FILL, xpadding=10) place_text = Gtk.Label() place_text.set_alignment(0.0, 0.5) tab.attach(place_text, 1, 2, 3, 4) self.widgets['place_text'] = place_text image = Gtk.Image() image.set_from_stock(Gtk.STOCK_INDEX, Gtk.IconSize.BUTTON) place_share = Gtk.Button() place_share.set_relief(Gtk.ReliefStyle.NONE) place_share.add(image) tab.attach(place_share, 2, 3, 3, 4, xoptions=0) self.widgets['place_share'] = place_share image = Gtk.Image() image.set_from_stock(Gtk.STOCK_ADD, Gtk.IconSize.BUTTON) place_add = Gtk.Button() place_add.set_relief(Gtk.ReliefStyle.NONE) place_add.add(image) tab.attach(place_add, 3, 4, 3, 4, xoptions=0) self.widgets['place_add'] = place_add button_box = Gtk.HButtonBox() button_box.set_layout(Gtk.ButtonBoxStyle.END) help_btn = Gtk.Button(stock=Gtk.STOCK_HELP) help_btn.connect('clicked', self.help_clicked) button_box.add(help_btn) button_box.set_child_secondary(help_btn, True) cancel_btn = Gtk.Button(stock=Gtk.STOCK_CANCEL) cancel_btn.connect('clicked', self.close) button_box.add(cancel_btn) ok_btn = Gtk.Button(stock=Gtk.STOCK_OK) ok_btn.connect('clicked', self.save) button_box.add(ok_btn) notebook = Gtk.Notebook() self.details = DetailsTab(self.dbstate, self.uistate, self.track, self.event, census_combo) self._add_tab(notebook, self.details) self.headings = HeadingsTab(self.dbstate, self.uistate, self.track, self.event, census_combo) self._add_tab(notebook, self.headings) self.gallery_list = GalleryTab(self.dbstate, self.uistate, self.track, self.citation.get_media_list()) self._add_tab(notebook, self.gallery_list) vbox.pack_start(tab, expand=False, fill=True, padding=10) vbox.pack_start(notebook, expand=True, fill=True, padding=0) vbox.pack_end(button_box, expand=False, fill=True, padding=10) root.add(vbox) root.show_all() notebook.set_current_page(0) return root def __populate_gui(self, event): """ Populate the GUI for a given census event. """ census_combo = self.widgets['census_combo'] for pos, row in enumerate(census_combo.get_model()): if row[0] == self.citation.get_reference_handle(): census_combo.set_active(pos) date_text = self.widgets['date_text'] date_text.set_text(get_date(event)) def __census_changed(self, combo): """ Called when the user selects a new census from the combo box. """ model = combo.get_model() index = combo.get_active() census_id = model[index][2] # Set date census_date = get_census_date(census_id) date_text = self.widgets['date_text'] date_text.set_text(displayer.display(census_date)) self.event.set_date_object(census_date) self.citation.set_date_object(census_date) # Set source self.citation.set_reference_handle(model[index][0]) # Set new columns columns = get_census_columns(census_id) report_columns = get_report_columns(census_id) self.details.create_table(columns, report_columns) heading_list = get_census_headings(census_id) self.headings.create_table(heading_list) def save(self, button): """ Called when the user clicks the OK button. """ if self.widgets['census_combo'].get_active() == -1: ErrorDialog(_('Census Editor'), _('Cannot save this census. First select ' 'a census from the drop-down list.')) return with DbTxn(self.get_menu_title(), self.db) as trans: if not self.event.get_handle(): self.db.add_event(self.event, trans) self.headings.save() self.details.save(trans) citation_handle = self.citation.get_handle() if not citation_handle: citation_handle = self.db.add_citation(self.citation, trans) self.event.add_citation(citation_handle) else: self.db.commit_citation(self.citation, trans) self.db.commit_event(self.event, trans) self.close() def close(self, *args): """ Close the editor window. """ (width, height) = self.window.get_size() self._config.set('interface.census-width', width) self._config.set('interface.census-height', height) self._config.save() self.details.entry_grid.clean_up() self.details.clean_up() self.gallery_list.clean_up() ManagedWindow.close(self) def help_clicked(self, obj): """ Display the relevant portion of GRAMPS manual """ display_help(webpage='Census_Addons')