def applyEntriesCB(self, *args): newdic = {} for k, e in self.entries.items(): txt = e.get_text() if txt: if k == 'amount': try: newdic[k] = convert.frac_to_float(txt) except: de.show_amount_error(txt) return else: newdic[k] = txt if not newdic: print 'We called applyEntriesCB with no text -- that shouldn\'t be possible' return mod, rows = self.treeview.get_selection().get_selected_rows() if not de.getBoolean( label=_("Change all selected rows?"), sublabel= (_('This action will not be undoable. Are you that for all %s selected rows, you want to set the following values:' ) % len(rows) + (newdic.has_key('ingkey') and _('\nKey to %s') % newdic['ingkey'] or '') + (newdic.has_key('item') and _('\nItem to %s') % newdic['item'] or '') + (newdic.has_key('unit') and _('\nUnit to %s') % newdic['unit'] or '') + (newdic.has_key('amount') and _('\nAmount to %s') % newdic['amount'] or ''))): return # Now actually apply our lovely new logic... changed_iters = True updated_iters = [] for path in rows: itr = self.treeModel.get_iter(path) # We check to see if we've updated the parent of our iter, # in which case the changes would already be inherited by # the current row (i.e. if the tree has been expanded and # all rows have been selected). parent = mod.iter_parent(itr) already_updated = False while parent: if parent in updated_iters: already_updated = True else: parent = mod.iter_parent(parent) if already_updated: continue # Now that we're sure we really need to update... curdic, field = self.get_dic_describing_iter(itr) curkey = self.treeModel.get_value(itr, self.VALUE_COL) if not already_updated: self.rd.update_by_criteria( self.rd.ingredients_table, curdic, newdic, ) if curdic.has_key('ingkey') and newdic.has_key('ingkey'): self.rd.delete_by_criteria(self.rd.keylookup_table, {'ingkey': curdic['ingkey']}) self.resetTree()
def applyEntriesCB (self, *args): newdic = {} for k,e in self.entries.items(): txt = e.get_text() if txt: if k=='amount': try: newdic[k]=convert.frac_to_float(txt) except: print 'Problem with amount:',txt import traceback; traceback.print_exc() de.show_amount_error(txt) return else: newdic[k]=txt if not newdic: print 'We called applyEntriesCB with no text -- that shouldn\'t be possible' return mod,rows = self.treeview.get_selection().get_selected_rows() if not de.getBoolean( label=_("Change all selected rows?"), sublabel=(_('This action will not be undoable. Are you that for all %s selected rows, you want to set the following values:')%len(rows) + (newdic.has_key('ingkey') and _('\nKey to %s')%newdic['ingkey'] or '') + (newdic.has_key('item') and _('\nItem to %s')%newdic['item'] or '') + (newdic.has_key('unit') and _('\nUnit to %s')%newdic['unit'] or '') + (newdic.has_key('amount') and _('\nAmount to %s')%newdic['amount'] or ''))): return # Now actually apply our lovely new logic... changed_iters = True updated_iters = [] for path in rows: itr=self.treeModel.get_iter(path) # We check to see if we've updated the parent of our iter, # in which case the changes would already be inherited by # the current row (i.e. if the tree has been expanded and # all rows have been selected). parent = mod.iter_parent(itr); already_updated = False while parent: if parent in updated_iters: already_updated = True else: parent = mod.iter_parent(parent) if already_updated: continue # Now that we're sure we really need to update... curdic,field = self.get_dic_describing_iter(itr) curkey = self.treeModel.get_value(itr,self.VALUE_COL) if not already_updated: self.rd.update_by_criteria( self.rd.ingredients_table, curdic, newdic, ) if curdic.has_key('ingkey') and newdic.has_key('ingkey'): self.rd.delete_by_criteria( self.rd.keylookup_table, {'ingkey':curdic['ingkey']} ) self.resetTree()
def tree_edited (self, renderer, path_string, text, n, head): indices = path_string.split(':') path = tuple( map(int, indices)) itr = self.treeModel.get_iter(path) curdic,field = self.get_dic_describing_iter(itr) value = curdic[field] if value == text: return if field=='ingkey': key = curdic['ingkey'] if de.getBoolean(label=_('Change all keys "%s" to "%s"?')%(key,text), sublabel=_("You won't be able to undo this action. If there are already ingredients with the key \"%s\", you won't be able to distinguish between those items and the items you are changing now."%text) ): self.rd.update_by_criteria( self.rd.ingredients_table, curdic, {'ingkey':text} ) self.rd.delete_by_criteria( self.rd.keylookup_table, {'ingkey':key} ) elif field=='item': if de.getBoolean(label=_('Change all items "%s" to "%s"?')%(curdic['item'],text), sublabel=_("You won't be able to undo this action. If there are already ingredients with the item \"%s\", you won't be able to distinguish between those items and the items you are changing now.")%text ): self.rd.update_by_criteria( self.rd.ingredients_table, curdic, {'item':text} ) elif field=='unit': unit = curdic['unit']; key = curdic['ingkey']; item = curdic['item'] val = de.getRadio(label='Change unit', options=[ [_('Change _all instances of "%(unit)s" to "%(text)s"')%locals(),1], [_('Change "%(unit)s" to "%(text)s" only for _ingredients "%(item)s" with key "%(key)s"')%locals(),2], ], default = 2, ) if val==1: self.rd.update_by_criteria( self.rd.ingredients_table, {'unit':unit}, {'unit':text}, ) elif val==2: self.rd.update_by_criteria( self.rd.ingredients_table, curdic, {'unit':text} ) elif field=='amount': amount = curdic['amount']; unit = curdic['unit']; key = curdic['ingkey']; item = curdic['item'] try: new_amount = convert.frac_to_float(text) except: de.show_amount_error(text) return val = de.getRadio(label='Change amount', options=[ [_('Change _all instances of "%(amount)s" %(unit)s to %(text)s %(unit)s')%locals(),1], [_('Change "%(amount)s" %(unit)s to "%(text)s" %(unit)s only _where the ingredient key is %(key)s')%locals(),2], [_('Change "%(amount)s" %(unit)s to "%(text)s" %(unit)s only where the ingredient key is %(key)s _and where the item is %(item)s')%locals(),3], ], default=3, ) if val == 1: cond = {'unit':unit,'amount':amount} elif val == 2: cond = {'unit':unit,'amount':amount,'ingkey':key} elif val == 3: cond = curdic self.rd.update_by_criteria( self.rd.ingredients_table, {'unit':unit,'amount':convert.frac_to_float(amount)}, {'unit':unit,'amount':new_amount} ) else: return self.treeModel.set_value(itr, n, text) return
def tree_edited(self, renderer, path_string, text, n, head): indices = path_string.split(':') path = tuple(map(int, indices)) itr = self.treeModel.get_iter(path) curdic, field = self.get_dic_describing_iter(itr) value = curdic[field] if value == text: return if field == 'ingkey': key = curdic['ingkey'] if de.getBoolean( label=_('Change all keys "%s" to "%s"?') % (key, text), sublabel=_( "You won't be able to undo this action. If there are already ingredients with the key \"%s\", you won't be able to distinguish between those items and the items you are changing now." % text)): self.rd.update_by_criteria(self.rd.ingredients_table, curdic, {'ingkey': text}) self.rd.delete_by_criteria(self.rd.keylookup_table, {'ingkey': key}) elif field == 'item': if de.getBoolean( label=_('Change all items "%s" to "%s"?') % (curdic['item'], text), sublabel= _("You won't be able to undo this action. If there are already ingredients with the item \"%s\", you won't be able to distinguish between those items and the items you are changing now." ) % text): self.rd.update_by_criteria(self.rd.ingredients_table, curdic, {'item': text}) elif field == 'unit': unit = curdic['unit'] key = curdic['ingkey'] item = curdic['item'] val = de.getRadio( label='Change unit', options=[ [ _('Change _all instances of "%(unit)s" to "%(text)s"') % locals(), 1 ], [ _('Change "%(unit)s" to "%(text)s" only for _ingredients "%(item)s" with key "%(key)s"' ) % locals(), 2 ], ], default=2, ) if val == 1: self.rd.update_by_criteria( self.rd.ingredients_table, {'unit': unit}, {'unit': text}, ) elif val == 2: self.rd.update_by_criteria(self.rd.ingredients_table, curdic, {'unit': text}) elif field == 'amount': amount = curdic['amount'] unit = curdic['unit'] key = curdic['ingkey'] item = curdic['item'] try: new_amount = convert.frac_to_float(text) except: de.show_amount_error(text) return val = de.getRadio( label='Change amount', options=[ [ _('Change _all instances of "%(amount)s" %(unit)s to %(text)s %(unit)s' ) % locals(), 1 ], [ _('Change "%(amount)s" %(unit)s to "%(text)s" %(unit)s only _where the ingredient key is %(key)s' ) % locals(), 2 ], [ _('Change "%(amount)s" %(unit)s to "%(text)s" %(unit)s only where the ingredient key is %(key)s _and where the item is %(item)s' ) % locals(), 3 ], ], default=3, ) if val == 1: cond = {'unit': unit, 'amount': amount} elif val == 2: cond = {'unit': unit, 'amount': amount, 'ingkey': key} elif val == 3: cond = curdic self.rd.update_by_criteria(self.rd.ingredients_table, { 'unit': unit, 'amount': convert.frac_to_float(amount) }, { 'unit': unit, 'amount': new_amount }) else: return self.treeModel.set_value(itr, n, text) return