class ComboSearchFilter(SearchFilter): """ - a label - a combo with a set of predefined item to select from """ __gtype_name__ = 'ComboSearchFilter' def __init__(self, label='', values=None): """ Create a new ComboSearchFilter object. @param name: name of the search filter @param values: items to put in the combo, see L{kiwi.ui.widgets.combo.ProxyComboBox.prefill} """ SearchFilter.__init__(self, label=label) label = gtk.Label(label) self.pack_start(label, False, False) label.show() self.title_label = label self.combo = ProxyComboBox() if values: self.combo.prefill(values) self.combo.connect('content-changed', self._on_combo__content_changed) self.pack_start(self.combo, False, False, 6) self.combo.show() # # SearchFilter # def get_state(self): return NumberQueryState(filter=self, value=self.combo.get_selected_data()) def get_title_label(self): return self.title_label def get_mode_combo(self): return self.combo # # Public API # def select(self, data): """ selects an item in the combo @param data: what to select """ self.combo.select(data) # # Callbacks # def _on_combo__content_changed(self, mode): self.emit('changed')
class Editor(Gtk.HBox): """Base class for field editors Subclasses must define a list of operations and a datatype """ operations = [] data_type = None def __init__(self, store, field, other_fields): """ :param store: a storm store if its needed :param field: the field that is being edited :param other_fields: other fields available for math operations """ assert len(self.operations) self._store = store self._other_fields = other_fields self._oper = None self._field = field super(Editor, self).__init__(spacing=6) self.operations_combo = ProxyComboBox() self.pack_start(self.operations_combo, True, True, 0) self.operations_combo.connect('changed', self._on_operation_changed) for oper in self.operations: self.operations_combo.append_item(oper.label, oper) self.operations_combo.select(self.operations[0]) self.show_all() def set_field(self, field): assert field.data_type == self.data_type self._field = field self._oper.set_field(field) def _on_operation_changed(self, combo): if self._oper is not None: # Remove previous operation self.remove(self._oper) self._oper = combo.get_selected()(self._store, self._field, self._other_fields) self.pack_start(self._oper, True, True, 0) def apply_operation(self, item): return self._oper.apply_operation(item)
class ComboSearchFilter(SearchFilter): """ - a label - a combo with a set of predefined item to select from """ __gtype_name__ = 'ComboSearchFilter' def __init__(self, label='', values=None): """ Create a new ComboSearchFilter object. :param label: label of the search filter :param values: items to put in the combo, see :class:`kiwi.ui.widgets.combo.ProxyComboBox.prefill` """ self._block_updates = False SearchFilter.__init__(self, label=label) label = Gtk.Label(label=label) self.pack_start(label, False, False, 0) label.show() self.title_label = label # We create the mode, but it will only be added to this box when # enable_advanced is called self.mode = ProxyComboBox() self.mode.connect('content-changed', self._on_mode__content_changed) for option in (ComboEquals, ComboDifferent): self.add_option(option) self.mode.select_item_by_position(0) self.combo = ProxyComboBox() if values: self.update_values(values) self.combo.connect('content-changed', self._on_combo__content_changed) self.pack_start(self.combo, False, False, 6) self.combo.show() # # SearchFilter # def get_state(self): value = self.combo.get_selected_data() mode = self.mode.get_selected_data() state = NumberQueryState(filter=self, value=value, mode=mode.mode) if hasattr(value, 'id'): state.value_id = value.id return state def set_state(self, value, value_id=None, mode=None): if mode is None: mode = NumberQueryState.EQUALS if value_id is not None: for item in self.combo.get_model_items().values(): if item is None: continue # Filter can come as a string or as a FilterItem object item_id = item if isinstance(item, str) else item.id if item_id == value_id: value = item break self.select(value) def update_values(self, values): self._block_updates = True self.combo.prefill(values) self._block_updates = False def get_title_label(self): return self.title_label def get_mode_combo(self): return self.combo def get_description(self): desc = '' data = self.combo.get_selected_data() if data is not None: desc += self.combo.get_selected_label() return '%s %s' % ( self.title_label.get_text(), desc, ) # # Public API # def add_option(self, option_type, position=0): """ Adds an option :param option_type: option to add :type option_type: a :class:`ComboSearchOption` subclass """ option = option_type() num = len(self.mode) + position self.mode.insert_item(num, option.name, option_type) def select(self, data): """ selects an item in the combo :param data: what to select """ self.combo.select(data) def enable_advanced(self): self.pack_start(self.mode, False, False, 6) self.reorder_child(self.mode, 1) self.mode.show() # # Callbacks # def _on_mode__content_changed(self, combo): if not self._block_updates: self.emit('changed') def _on_combo__content_changed(self, mode): if not self._block_updates: self.emit('changed')
class DateSearchFilter(SearchFilter): """ A filter which helps you to search by a date interval. Can be customized through add_option. """ __gtype_name__ = 'DateSearchFilter' class Type(enum): (USER_DAY, USER_INTERVAL) = range(100, 102) def __init__(self, label=''): """ Create a new DateSearchFilter object. :param label: name of the search filter """ self._options = {} SearchFilter.__init__(self, label=label) self.title_label = Gtk.Label(label=label) self.pack_start(self.title_label, False, False, 0) self.title_label.show() self.mode = ProxyComboBox() self.mode.connect('content-changed', self._on_mode__content_changed) self.pack_start(self.mode, False, False, 6) self.mode.show() self.from_label = Gtk.Label(label=_("From:")) self.pack_start(self.from_label, False, False, 0) self.from_label.show() self.start_date = ProxyDateEntry() self._start_changed_id = self.start_date.connect( 'content-changed', self._on_start_date__changed) self.pack_start(self.start_date, False, False, 6) self.start_date.show() self.to_label = Gtk.Label(label=_("To:")) self.pack_start(self.to_label, False, False, 0) self.to_label.show() self.end_date = ProxyDateEntry() self._end_changed_id = self.end_date.connect( 'content-changed', self._on_end_date__changed) self.pack_start(self.end_date, False, False, 6) self.end_date.show() self.add_custom_options() for option in (Any, Today, Yesterday, LastWeek, LastMonth): self.add_option(option) self.mode.select_item_by_position(0) # # SearchFilter # def get_state(self): start = self.start_date.get_date() end = self.end_date.get_date() if start == end: return DateQueryState(filter=self, date=start) return DateIntervalQueryState(filter=self, start=start, end=end) def set_state(self, start, end=None): self.start_date.set_date(start) if end is not None: self.end_date.set_date(end) def get_title_label(self): return self.title_label def get_mode_combo(self): return self.mode def get_description(self): desc = '' start_date = self.start_date.get_date() end_date = self.end_date.get_date() if start_date: if end_date and start_date != end_date: desc += ' %s %s %s %s' % ( _(u'from'), start_date.strftime('%x'), _(u'to'), end_date.strftime('%x'), ) else: desc += start_date.strftime('%x') if desc: return '%s %s' % ( self.get_title_label().get_text(), desc, ) # # Public API # def clear_options(self): """ Removes all previously added options """ self._options = {} self.mode.clear() def add_option(self, option_type, position=-2): """ Adds a date option :param option_type: option to add :type option_type: a :class:`DateSearchOption` subclass """ option = option_type() num = len(self.mode) + position self.mode.insert_item(num, option.name, option_type) self._options[option_type] = option def add_option_fixed(self, name, date, position=-2): """ Adds a fixed option, eg one for which date is not possible to modify. :param name: name of the option :param date: fixed data :param position: position to add the option at """ option_type = type('', (FixedDateSearchOption, ), dict(name=name, date=date)) self.add_option(option_type, position=position) def add_option_fixed_interval(self, name, start, end, position=-2): """ Adds a fixed option interval, eg one for which the dates are not possible to modify. :param name: name of the option :param start: start of the fixed interval :param end: end of the fixed interval :param position: position to add the option at """ option_type = type('', (FixedIntervalSearchOption, ), dict(name=name, start=start, end=end)) self.add_option(option_type, position=position) def add_custom_options(self): """Adds the custom options 'Custom day' and 'Custom interval' which let the user define its own interval dates. """ pos = len(self.mode) + 1 for name, option_type in [ (_('Custom day'), DateSearchFilter.Type.USER_DAY), (_('Custom interval'), DateSearchFilter.Type.USER_INTERVAL) ]: self.mode.insert_item(pos, name, option_type) pos += 1 def get_start_date(self): """ Get the start date. :returns: start date :rtype: datetime.date or None """ return self.start_date.get_date() def get_end_date(self): """ Get the end date. :returns: end date :rtype: datetime.date or None """ return self.end_date.get_date() def set_use_date_entries(self, use_date_entries): """ Toggles the visibility of the user selectable date entries :param use_date_entries: """ self.from_label.props.visible = use_date_entries self.to_label.props.visible = use_date_entries self.start_date.props.visible = use_date_entries self.end_date.props.visible = use_date_entries def select(self, data=None, position=None): """ selects an item in the combo Data or position can be sent in. If nothing is sent in the first item will be selected, if any :param data: data to select :param position: position of data to select """ if data is not None and position is not None: raise TypeError("You can't send in both data and position") if data is None and position is None: position = 0 if position is not None: if len(self.mode): self.mode.select_item_by_position(position) elif data: self.mode.select(data) # # Private # def _update_dates(self): # This is called when we change mode date_type = self.mode.get_selected_data() if date_type is None: return # If we switch to a user selectable day, make sure that # both dates are set to today if date_type == DateSearchFilter.Type.USER_DAY: today = datetime.date.today() self.start_date.set_date(today) self.end_date.set_date(today) # And for user interval, set start to today and to tomorrow elif date_type == DateSearchFilter.Type.USER_INTERVAL: today = datetime.date.today() self.start_date.set_date(today) self.end_date.set_date(today + datetime.timedelta(days=1)) # Finally for pre-defined ones let the DateSearchOption decide what the # values are going to be, these dates are not user editable so # we don't need to do any checking. else: option = self._options.get(date_type) assert option, (date_type, self._options) start_date, end_date = option.get_interval() self.start_date.set_date(start_date) self.end_date.set_date(end_date) def _update_sensitivity(self): date_type = self.mode.get_selected_data() enabled = date_type == DateSearchFilter.Type.USER_INTERVAL self.to_label.set_sensitive(enabled) self.end_date.set_sensitive(enabled) enabled = (date_type == DateSearchFilter.Type.USER_INTERVAL or date_type == DateSearchFilter.Type.USER_DAY) self.from_label.set_sensitive(enabled) self.start_date.set_sensitive(enabled) def _internal_set_start_date(self, date): self.start_date.handler_block(self._start_changed_id) self.start_date.set_date(date) self.start_date.handler_unblock(self._start_changed_id) def _internal_set_end_date(self, date): self.end_date.handler_block(self._end_changed_id) self.end_date.set_date(date) self.end_date.handler_unblock(self._end_changed_id) def _restore_date_validation(self): self.start_date.set_valid() self.end_date.set_valid() # # Callbacks # def _on_mode__content_changed(self, mode): self._update_dates() self._update_sensitivity() self._restore_date_validation() self.emit('changed') def _on_start_date__changed(self, start_date): date_type = self.mode.get_selected_data() start = start_date.get_date() # For user days, just make sure that the date entries # always are in sync if date_type == DateSearchFilter.Type.USER_DAY: if start is None: self.start_date.set_invalid(_(u'Invalid date')) else: self.start_date.set_valid() self._internal_set_end_date(start) # Make sure that we cannot select a start date after # the end date, be nice and increase the end date if # the start date happen to be the same elif date_type == DateSearchFilter.Type.USER_INTERVAL: end = self.end_date.get_date() if start is None: self.start_date.set_invalid(_(u'Invalid date')) return if end and start >= end: self._internal_set_end_date(start + datetime.timedelta(days=1)) self.start_date.set_valid() def _on_end_date__changed(self, end_date): date_type = self.mode.get_selected_data() # We don't need to do anything for user day, since # this the end date widget is disabled if date_type == DateSearchFilter.Type.USER_DAY: pass # Make sure that we cannot select an end date before # the start date, be nice and decrease the start date if # the end date happen to be the same elif date_type == DateSearchFilter.Type.USER_INTERVAL: start = self.start_date.get_date() end = end_date.get_date() if end is None: self.end_date.set_invalid(_(u'Invalid date')) else: self.end_date.set_valid() if start and end and end <= start: self._internal_set_start_date(end - datetime.timedelta(days=1))
class AccountEditor(BaseEditor): """ Account Editor """ gladefile = "AccountEditor" proxy_widgets = ['description', 'code'] model_type = Account model_name = _('Account') def __init__(self, store, model=None, parent_account=None): self._last_account_type = None self._bank_number = -1 self._bank_widgets = [] self._bank_option_widgets = [] self._option_fields = {} self._test_button = None self.existing = model is not None self.parent_account = parent_account self.bank_model = _TemporaryBankAccount() BaseEditor.__init__(self, store, model) action_area = self.main_dialog.action_area action_area.set_layout(Gtk.ButtonBoxStyle.END) action_area.pack_start(self._test_button, False, False, 0) action_area.set_child_secondary(self._test_button, True) self._test_button.show() # # BaseEditor hooks # def create_model(self, store): return Account(description=u"", account_type=Account.TYPE_CASH, store=store) def _setup_widgets(self): self._test_button = Gtk.Button(_("Print a test bill")) self._test_button.connect('clicked', self._on_test_button__clicked) self.parent_accounts = AccountTree(with_code=False, create_mode=True) self.parent_accounts.connect( 'selection-changed', self._on_parent_accounts__selection_changed) self.tree_box.pack_start(self.parent_accounts, True, True, 0) self.tree_box.reorder_child(self.parent_accounts, 0) if sysparam.compare_object('IMBALANCE_ACCOUNT', self.model): self.account_type.set_sensitive(False) self.account_type.prefill(Account.account_type_descriptions) account_type = self.model.account_type self.parent_accounts.insert_initial(self.store, edited_account=self.model) if self.parent_account: account = self.parent_accounts.get_account_by_id( self.parent_account.id) self.parent_accounts.select(account) if not self.existing: account_type = account.account_type self.account_type.select(account_type) self.parent_accounts.show() def setup_proxies(self): self._setup_widgets() self.add_proxy(self.model, AccountEditor.proxy_widgets) def validate_confirm(self): if not self.model.description: return False account = self.parent_accounts.get_selected() if not account: return True return account.selectable def on_confirm(self): new_parent = self.parent_accounts.get_selected() if new_parent: new_parent = new_parent.account if new_parent != self.model: self.model.parent = new_parent self.model.account_type = self.account_type.get_selected() self._save_bank() def refresh_ok(self, value): BaseEditor.refresh_ok(self, value) account_type = self.account_type.get_selected() if account_type != Account.TYPE_BANK: value = False self._test_button.set_sensitive(value) # Private def _save_bank(self): bank_account = self.model.bank if not bank_account: bank_account = BankAccount(account=self.model, store=self.store) # FIXME: Who sets this to a str? bank_account.bank_account = unicode(self.bank_model.bank_account) bank_account.bank_branch = unicode(self.bank_model.bank_branch) if self._bank_number is not None: bank_account.bank_number = self.bank_model.bank_number self._save_bank_bill_options(bank_account) def _save_bank_bill_options(self, bank_account): for option, entry in self._option_fields.items(): value = unicode(entry.get_text()) bill_option = self.store.find(BillOption, bank_account=bank_account, option=option).one() if bill_option is None: bill_option = BillOption(store=self.store, bank_account=bank_account, option=option, value=value) bill_option.value = value def _add_widget(self, label, widget, options=False): n_rows = self.table.props.n_rows l = Gtk.Label() l.set_markup(label) l.props.xalign = 1.0 self.table.resize(n_rows + 1, 2) self.table.attach(l, 0, 1, n_rows, n_rows + 1, Gtk.AttachOptions.FILL, 0, 0, 0) self.table.attach(widget, 1, 2, n_rows, n_rows + 1, Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL, 0, 0, 0) if options: self._bank_option_widgets.extend([l, widget]) else: self._bank_widgets.extend([l, widget]) l.show() return l def _update_bank_type(self): self._remove_bank_option_widgets() bank_number = self.bank_type.get_selected() bank_info = None if bank_number: bank_info = get_bank_info_by_number(bank_number) self.bank_number = ProxyEntry() self.bank_number.props.data_type = int self.bank_number.set_sensitive(False) bank_number_lbl = self._add_widget(api.escape(_("Number:")), self.bank_number, options=True) self.bank_branch = ProxyEntry() self.bank_branch.connect('validate', self._on_bank_branch__validate, bank_info) self.bank_branch.props.data_type = 'str' self.bank_branch.props.mandatory = True self.bank_branch.model_attribute = "bank_branch" bank_branch_lbl = self._add_widget(api.escape(_("Agency:")), self.bank_branch, options=True) if bank_number is not None: bank_branch_lbl.show() self.bank_branch.show() else: bank_branch_lbl.hide() self.bank_account = ProxyEntry() self.bank_account.connect('validate', self._on_bank_account__validate, bank_info) self._add_widget(api.escape(_("Account:")), self.bank_account, options=True) self.bank_account.model_attribute = "bank_account" self.bank_account.props.data_type = 'str' if bank_number is not None: self.bank_account.props.mandatory = True self.bank_account.show() attributes = ['bank_account', 'bank_branch', 'bank_number'] if bank_number is not None: bank_number_lbl.show() self.bank_number.show() self.bank_model.bank_number = bank_number for i, option in enumerate(bank_info.get_extra_options()): name = 'option' + str(i) entry = ProxyEntry() entry.model_attribute = name setattr(self, name, entry) # Set the model attr too so it can be validated setattr(self.bank_model, name, u'') entry.props.data_type = 'str' entry.connect('validate', self._on_bank_option__validate, bank_info, option) name = option.replace('_', ' ').capitalize() self._add_widget("<i>%s</i>:" % api.escape(name), entry, options=True) entry.show() self._option_fields[option] = entry attributes.append(entry.model_attribute) else: bank_number_lbl.hide() self.bank_proxy = self.add_proxy(self.bank_model, attributes) self._fill_bank_account() def _fill_bank_account(self): if not self.model.bank: return self.bank_model.bank_branch = self.model.bank.bank_branch.encode( 'utf-8') self.bank_model.bank_account = self.model.bank.bank_account.encode( 'utf-8') self.bank_proxy.update('bank_branch') self.bank_proxy.update('bank_account') bill_options = list( self.store.find(BillOption, bank_account=self.model.bank)) for bill_option in bill_options: if bill_option.option is None: continue field_entry = self._option_fields.get(bill_option.option) if field_entry: field_entry.set_text(bill_option.value) def _update_account_type(self, account_type): if not self.account_type.get_sensitive(): return if account_type != Account.TYPE_BANK: self._remove_bank_widgets() self._remove_bank_option_widgets() self.code.set_sensitive(True) return self.code.set_sensitive(False) self.bank_type = ProxyComboBox() self._add_widget(api.escape(_("Bank:")), self.bank_type) self.bank_type.connect('content-changed', self._on_bank_type__content_changed) self.bank_type.show() banks = [(_('Generic'), None)] banks.extend([(b.description, b.bank_number) for b in get_all_banks()]) self.bank_type.prefill(banks) if self.model.bank: try: self.bank_type.select(self.model.bank.bank_number) except KeyError: self.bank_type.select(None) self._update_bank_type() def _remove_bank_widgets(self): for widget in self._bank_widgets: widget.get_parent().remove(widget) widget.destroy() self.table.resize(5, 2) self._bank_widgets = [] def _remove_bank_option_widgets(self): for widget in self._bank_option_widgets: widget.get_parent().remove(widget) widget.destroy() self.table.resize(5 + len(self._bank_widgets) / 2, 2) self._bank_option_widgets = [] self._option_fields = {} def _print_test_bill(self): try: bank_info = get_bank_info_by_number(self.bank_model.bank_number) except NotImplementedError: info(_("This bank does not support printing of bills")) return kwargs = dict( valor_documento=12345.67, data_vencimento=datetime.date.today(), data_documento=datetime.date.today(), data_processamento=datetime.date.today(), nosso_numero=u'24533', numero_documento=u'1138', sacado=[_(u"Drawee"), _(u"Address"), _(u"Details")], cedente=[_(u"Supplier"), _(u"Address"), _(u"Details"), ("CNPJ")], demonstrativo=[_(u"Demonstration")], instrucoes=[_(u"Instructions")], agencia=self.bank_model.bank_branch, conta=self.bank_model.bank_account, ) for opt in self.bank_model.options: kwargs[opt.option] = opt.value data = bank_info(**kwargs) print_report(BillTestReport, data) # Callbacks def _on_parent_accounts__selection_changed(self, objectlist, account): self.force_validation() def on_description__activate(self, entry): if self.validate_confirm(): self.confirm() def on_description__validate(self, entry, text): if not text: return ValidationError(_("Account description cannot be empty")) def on_account_type__content_changed(self, account_type): account_type = account_type.get_selected() if self._last_account_type == account_type: return self._update_account_type(account_type) self._last_account_type = account_type def _on_bank_type__content_changed(self, bank_type): bank_number = bank_type.get_selected() if self._bank_number == bank_number: return self._update_bank_type() self._bank_number = bank_number def _on_bank_branch__validate(self, entry, value, bank_info): if bank_info: try: bank_info.validate_field(value) except BoletoException as e: return ValidationError(str(e)) def _on_bank_account__validate(self, entry, value, bank_info): if bank_info: try: bank_info.validate_field(value) except BoletoException as e: return ValidationError(str(e)) def _on_bank_option__validate(self, entry, value, bank_info, option): try: bank_info.validate_option(option, value) except BoletoException as e: return ValidationError(str(e)) self.bank_model.set_option(option, value) def _on_test_button__clicked(self, button): self._print_test_bill()
class ComboSearchFilter(SearchFilter): """ - a label - a combo with a set of predefined item to select from """ __gtype_name__ = 'ComboSearchFilter' def __init__(self, label='', values=None): """ Create a new ComboSearchFilter object. :param label: label of the search filter :param values: items to put in the combo, see :class:`kiwi.ui.widgets.combo.ProxyComboBox.prefill` """ self._block_updates = False SearchFilter.__init__(self, label=label) label = gtk.Label(label) self.pack_start(label, False, False) label.show() self.title_label = label self.combo = ProxyComboBox() if values: self.update_values(values) self.combo.connect('content-changed', self._on_combo__content_changed) self.pack_start(self.combo, False, False, 6) self.combo.show() # # SearchFilter # def get_state(self): value = self.combo.get_selected_data() state = NumberQueryState(filter=self, value=value) if hasattr(value, 'id'): state.value_id = value.id return state def set_state(self, value, value_id=None): if value_id is not None: for item in self.combo.get_model_items().values(): if item is None: continue if item.id == value_id: value = item break self.select(value) def update_values(self, values): self._block_updates = True self.combo.prefill(values) self._block_updates = False def get_title_label(self): return self.title_label def get_mode_combo(self): return self.combo def get_description(self): desc = '' data = self.combo.get_selected_data() if data is not None: desc += self.combo.get_selected_label() return '%s %s' % ( self.title_label.get_text(), desc, ) # # Public API # def select(self, data): """ selects an item in the combo :param data: what to select """ self.combo.select(data) # # Callbacks # def _on_combo__content_changed(self, mode): if not self._block_updates: self.emit('changed')
class ComboSearchFilter(SearchFilter): """ - a label - a combo with a set of predefined item to select from """ __gtype_name__ = 'ComboSearchFilter' def __init__(self, label='', values=None): """ Create a new ComboSearchFilter object. @param name: name of the search filter @param values: items to put in the combo, see L{kiwi.ui.widgets.combo.ProxyComboBox.prefill} """ SearchFilter.__init__(self, label=label) label = gtk.Label(label) self.pack_start(label, False, False) label.show() self.title_label = label self.combo = ProxyComboBox() if values: self.combo.prefill(values) self.combo.connect('content-changed', self._on_combo__content_changed) self.pack_start(self.combo, False, False, 6) self.combo.show() # # SearchFilter # def get_state(self): return NumberQueryState(filter=self, value=self.combo.get_selected_data()) def get_title_label(self): return self.title_label def get_mode_combo(self): return self.combo def get_description(self): desc = '' data = self.combo.get_selected_data() if data is not None: desc += self.combo.get_selected_label() return '%s %s' % (self.title_label.get_text(), desc,) # # Public API # def select(self, data): """ selects an item in the combo @param data: what to select """ self.combo.select(data) # # Callbacks # def _on_combo__content_changed(self, mode): self.emit('changed')
class DateSearchFilter(SearchFilter): """ A filter which helps you to search by a date interval. Can be customized through add_option. """ __gtype_name__ = 'DateSearchFilter' class Type(enum): (USER_DAY, USER_INTERVAL) = range(100, 102) def __init__(self, label=''): """ Create a new DateSearchFilter object. @param label: name of the search filter """ self._options = {} SearchFilter.__init__(self, label=label) self.title_label = gtk.Label(label) self.pack_start(self.title_label, False, False) self.title_label.show() self.mode = ProxyComboBox() self.mode.connect( 'content-changed', self._on_mode__content_changed) self.pack_start(self.mode, False, False, 6) self.mode.show() self.from_label = gtk.Label(_("From:")) self.pack_start(self.from_label, False, False) self.from_label.show() self.start_date = ProxyDateEntry() self._start_changed_id = self.start_date.connect( 'content-changed', self._on_start_date__changed) self.pack_start(self.start_date, False, False, 6) self.start_date.show() self.to_label = gtk.Label(_("To:")) self.pack_start(self.to_label, False, False) self.to_label.show() self.end_date = ProxyDateEntry() self._end_changed_id = self.end_date.connect( 'content-changed', self._on_end_date__changed) self.pack_start(self.end_date, False, False, 6) self.end_date.show() self.add_custom_options() for option in (Any, Today, Yesterday, LastWeek, LastMonth): self.add_option(option) self.mode.select_item_by_position(0) # # SearchFilter # def get_state(self): start = self.start_date.get_date() end = self.end_date.get_date() if start == end: return DateQueryState(filter=self, date=start) return DateIntervalQueryState(filter=self, start=start, end=end) def get_title_label(self): return self.title_label def get_mode_combo(self): return self.mode def get_description(self): desc = '' start_date = self.start_date.get_date() end_date = self.end_date.get_date() if start_date: if end_date and start_date != end_date: desc += ' %s %s %s %s' % (_(u'from'), start_date.strftime('%x'), _(u'to'), end_date.strftime('%x'),) else: desc += start_date.strftime('%x') if desc: return '%s %s' % (self.get_title_label().get_text(), desc,) # # Public API # def clear_options(self): """ Removes all previously added options """ self._options = {} self.mode.clear() def add_option(self, option_type, position=-2): """ Adds a date option @param option_type: option to add @type option_type: a L{DateSearchOption} subclass """ option = option_type() num = len(self.mode) + position self.mode.insert_item(num, option.name, option_type) self._options[option_type] = option def add_option_fixed(self, name, date, position=-2): """ Adds a fixed option, eg one for which date is not possible to modify. @param name: name of the option @param date: fixed data @param position: position to add the option at """ option_type = type('', (FixedDateSearchOption,), dict(name=name, date=date)) self.add_option(option_type, position=position) def add_option_fixed_interval(self, name, start, end, position=-2): """ Adds a fixed option interval, eg one for which the dates are not possible to modify. @param name: name of the option @param start: start of the fixed interval @param end: end of the fixed interval @param position: position to add the option at """ option_type = type('', (FixedIntervalSearchOption,), dict(name=name, start=start, end=end)) self.add_option(option_type, position=position) def add_custom_options(self): """Adds the custom options 'Custom day' and 'Custom interval' which let the user define its own interval dates. """ pos = len(self.mode) + 1 for name, option_type in [ (_('Custom day'), DateSearchFilter.Type.USER_DAY), (_('Custom interval'), DateSearchFilter.Type.USER_INTERVAL)]: self.mode.insert_item(pos, name, option_type) pos += 1 def get_start_date(self): """ Get the start date. @returns: start date @rtype: datetime.date or None """ return self.start_date.get_date() def get_end_date(self): """ Get the end date. @returns: end date @rtype: datetime.date or None """ return self.end_date.get_date() def set_use_date_entries(self, use_date_entries): """ Toggles the visibility of the user selectable date entries @param use_date_entries: """ self.from_label.props.visible = use_date_entries self.to_label.props.visible = use_date_entries self.start_date.props.visible = use_date_entries self.end_date.props.visible = use_date_entries def select(self, data=None, position=None): """ selects an item in the combo Data or position can be sent in. If nothing is sent in the first item will be selected, if any @param data: data to select @param position: position of data to select """ if data is not None and position is not None: raise TypeError("You can't send in both data and position") if data is None and position is None: position = 0 if position is not None: if len(self.mode): self.mode.select_item_by_position(position) elif data: self.mode.select(data) # # Private # def _update_dates(self): # This is called when we change mode date_type = self.mode.get_selected_data() if date_type is None: return # If we switch to a user selectable day, make sure that # both dates are set to today if date_type == DateSearchFilter.Type.USER_DAY: today = datetime.date.today() self.start_date.set_date(today) self.end_date.set_date(today) # And for user interval, set start to today and to tomorrow elif date_type == DateSearchFilter.Type.USER_INTERVAL: today = datetime.date.today() self.start_date.set_date(today) self.end_date.set_date(today + datetime.timedelta(days=1)) # Finally for pre-defined ones let the DateSearchOption decide what the # values are going to be, these dates are not user editable so # we don't need to do any checking. else: option = self._options.get(date_type) assert option, (date_type, self._options) start_date, end_date = option.get_interval() self.start_date.set_date(start_date) self.end_date.set_date(end_date) def _update_sensitivity(self): date_type = self.mode.get_selected_data() enabled = date_type == DateSearchFilter.Type.USER_INTERVAL self.to_label.set_sensitive(enabled) self.end_date.set_sensitive(enabled) enabled = (date_type == DateSearchFilter.Type.USER_INTERVAL or date_type == DateSearchFilter.Type.USER_DAY) self.from_label.set_sensitive(enabled) self.start_date.set_sensitive(enabled) def _internal_set_start_date(self, date): self.start_date.handler_block(self._start_changed_id) self.start_date.set_date(date) self.start_date.handler_unblock(self._start_changed_id) def _internal_set_end_date(self, date): self.end_date.handler_block(self._end_changed_id) self.end_date.set_date(date) self.end_date.handler_unblock(self._end_changed_id) def _restore_date_validation(self): self.start_date.set_valid() self.end_date.set_valid() # # Callbacks # def _on_mode__content_changed(self, mode): self._update_dates() self._update_sensitivity() self._restore_date_validation() self.emit('changed') def _on_start_date__changed(self, start_date): date_type = self.mode.get_selected_data() start = start_date.get_date() # For user days, just make sure that the date entries # always are in sync if date_type == DateSearchFilter.Type.USER_DAY: if start is None: self.start_date.set_invalid(_(u'Invalid date')) else: self.start_date.set_valid() self._internal_set_end_date(start) # Make sure that we cannot select a start date after # the end date, be nice and increase the end date if # the start date happen to be the same elif date_type == DateSearchFilter.Type.USER_INTERVAL: end = self.end_date.get_date() if start is None: self.start_date.set_invalid(_(u'Invalid date')) return if end and start >= end: self._internal_set_end_date(start + datetime.timedelta(days=1)) self.start_date.set_valid() def _on_end_date__changed(self, end_date): date_type = self.mode.get_selected_data() # We don't need to do anything for user day, since # this the end date widget is disabled if date_type == DateSearchFilter.Type.USER_DAY: pass # Make sure that we cannot select an end date before # the start date, be nice and decrease the start date if # the end date happen to be the same elif date_type == DateSearchFilter.Type.USER_INTERVAL: start = self.start_date.get_date() end = end_date.get_date() if end is None: self.end_date.set_invalid(_(u'Invalid date')) else: self.end_date.set_valid() if start and end and end <= start: self._internal_set_start_date(end - datetime.timedelta(days=1))
class AccountEditor(BaseEditor): """ Account Editor """ gladefile = "AccountEditor" proxy_widgets = ['description', 'code'] model_type = Account model_name = _('Account') def __init__(self, store, model=None, parent_account=None): self._last_account_type = None self._bank_number = -1 self._bank_widgets = [] self._bank_option_widgets = [] self._option_fields = {} self._test_button = None self.existing = model is not None self.parent_account = parent_account self.bank_model = _TemporaryBankAccount() BaseEditor.__init__(self, store, model) action_area = self.main_dialog.action_area action_area.set_layout(gtk.BUTTONBOX_END) action_area.pack_start(self._test_button, expand=False, fill=False) action_area.set_child_secondary(self._test_button, True) self._test_button.show() # # BaseEditor hooks # def create_model(self, store): return Account(description=u"", account_type=Account.TYPE_CASH, store=store) def _setup_widgets(self): self._test_button = gtk.Button(_("Print a test bill")) self._test_button.connect('clicked', self._on_test_button__clicked) self.parent_accounts = AccountTree(with_code=False, create_mode=True) self.parent_accounts.connect('selection-changed', self._on_parent_accounts__selection_changed) self.tree_box.pack_start(self.parent_accounts) self.tree_box.reorder_child(self.parent_accounts, 0) if self.model == sysparam(self.store).IMBALANCE_ACCOUNT: self.account_type.set_sensitive(False) self.account_type.prefill(Account.account_type_descriptions) account_type = self.model.account_type self.parent_accounts.insert_initial(self.store, edited_account=self.model) if self.parent_account: account = self.parent_accounts.get_account_by_id( self.parent_account.id) self.parent_accounts.select(account) if not self.existing: account_type = account.account_type self.account_type.select(account_type) self.parent_accounts.show() def setup_proxies(self): self._setup_widgets() self.add_proxy(self.model, AccountEditor.proxy_widgets) def validate_confirm(self): if not self.model.description: return False account = self.parent_accounts.get_selected() if not account: return True return account.selectable def on_confirm(self): new_parent = self.parent_accounts.get_selected() if new_parent: new_parent = new_parent.account if new_parent != self.model: self.model.parent = new_parent self.model.account_type = self.account_type.get_selected() self._save_bank() def refresh_ok(self, value): BaseEditor.refresh_ok(self, value) account_type = self.account_type.get_selected() if account_type != Account.TYPE_BANK: value = False self._test_button.set_sensitive(value) # Private def _save_bank(self): bank_account = self.model.bank if not bank_account: bank_account = BankAccount(account=self.model, store=self.store) # FIXME: Who sets this to a str? bank_account.bank_account = unicode(self.bank_model.bank_account) bank_account.bank_branch = unicode(self.bank_model.bank_branch) if self._bank_number is not None: bank_account.bank_number = self.bank_model.bank_number self._save_bank_bill_options(bank_account) def _save_bank_bill_options(self, bank_account): for option, entry in self._option_fields.items(): value = unicode(entry.get_text()) bill_option = self.store.find(BillOption, bank_account=bank_account, option=option).one() if bill_option is None: bill_option = BillOption(store=self.store, bank_account=bank_account, option=option, value=value) bill_option.value = value def _add_widget(self, label, widget, options=False): n_rows = self.table.props.n_rows l = gtk.Label() l.set_markup(label) l.props.xalign = 1.0 self.table.resize(n_rows + 1, 2) self.table.attach( l, 0, 1, n_rows, n_rows + 1, gtk.FILL, 0, 0, 0) self.table.attach( widget, 1, 2, n_rows, n_rows + 1, gtk.EXPAND | gtk.FILL, 0, 0, 0) if options: self._bank_option_widgets.extend([l, widget]) else: self._bank_widgets.extend([l, widget]) l.show() return l def _update_bank_type(self): self._remove_bank_option_widgets() bank_number = self.bank_type.get_selected() bank_info = None if bank_number: bank_info = get_bank_info_by_number(bank_number) self.bank_number = ProxyEntry() self.bank_number.props.data_type = int self.bank_number.set_sensitive(False) bank_number_lbl = self._add_widget(api.escape(_("Number:")), self.bank_number, options=True) self.bank_branch = ProxyEntry() self.bank_branch.connect('validate', self._on_bank_branch__validate, bank_info) self.bank_branch.props.data_type = 'str' self.bank_branch.props.mandatory = True self.bank_branch.model_attribute = "bank_branch" bank_branch_lbl = self._add_widget(api.escape(_("Agency:")), self.bank_branch, options=True) if bank_number is not None: bank_branch_lbl.show() self.bank_branch.show() else: bank_branch_lbl.hide() self.bank_account = ProxyEntry() self.bank_account.connect('validate', self._on_bank_account__validate, bank_info) self._add_widget(api.escape(_("Account:")), self.bank_account, options=True) self.bank_account.model_attribute = "bank_account" self.bank_account.props.data_type = 'str' if bank_number is not None: self.bank_account.props.mandatory = True self.bank_account.show() attributes = ['bank_account', 'bank_branch', 'bank_number'] if bank_number is not None: bank_number_lbl.show() self.bank_number.show() self.bank_model.bank_number = bank_number for i, option in enumerate(bank_info.get_extra_options()): name = 'option' + str(i) entry = ProxyEntry() entry.model_attribute = name setattr(self, name, entry) # Set the model attr too so it can be validated setattr(self.bank_model, name, u'') entry.props.data_type = 'str' entry.connect('validate', self._on_bank_option__validate, bank_info, option) self._add_widget("<i>%s</i>:" % (api.escape(option.capitalize()), ), entry, options=True) entry.show() self._option_fields[option] = entry attributes.append(entry.model_attribute) else: bank_number_lbl.hide() self.bank_proxy = self.add_proxy( self.bank_model, attributes) self._fill_bank_account() def _fill_bank_account(self): if not self.model.bank: return self.bank_model.bank_branch = self.model.bank.bank_branch.encode('utf-8') self.bank_model.bank_account = self.model.bank.bank_account.encode('utf-8') self.bank_proxy.update('bank_branch') self.bank_proxy.update('bank_account') bill_options = list(self.store.find(BillOption, bank_account=self.model.bank)) for bill_option in bill_options: if bill_option.option is None: continue field_entry = self._option_fields.get(bill_option.option) if field_entry: field_entry.set_text(bill_option.value) def _update_account_type(self, account_type): if not self.account_type.get_sensitive(): return if account_type != Account.TYPE_BANK: self._remove_bank_widgets() self._remove_bank_option_widgets() self.code.set_sensitive(True) return self.code.set_sensitive(False) self.bank_type = ProxyComboBox() self._add_widget(api.escape(_("Bank:")), self.bank_type) self.bank_type.connect('content-changed', self._on_bank_type__content_changed) self.bank_type.show() banks = [(_('Generic'), None)] banks.extend([(b.description, b.bank_number) for b in get_all_banks()]) self.bank_type.prefill(banks) if self.model.bank: try: self.bank_type.select(self.model.bank.bank_number) except KeyError: self.bank_type.select(None) self._update_bank_type() def _remove_bank_widgets(self): for widget in self._bank_widgets: widget.get_parent().remove(widget) widget.destroy() self.table.resize(5, 2) self._bank_widgets = [] def _remove_bank_option_widgets(self): for widget in self._bank_option_widgets: widget.get_parent().remove(widget) widget.destroy() self.table.resize(5 + len(self._bank_widgets) / 2, 2) self._bank_option_widgets = [] self._option_fields = {} def _print_test_bill(self): try: bank_info = get_bank_info_by_number(self.bank_model.bank_number) except NotImplementedError: info(_("This bank does not support printing of bills")) return kwargs = dict( valor_documento=12345.67, data_vencimento=datetime.date.today(), data_documento=datetime.date.today(), data_processamento=datetime.date.today(), nosso_numero=u'624533', numero_documento=u'1138', sacado=[_(u"Drawee"), _(u"Address"), _(u"Details")], cedente=_(u"Supplier"), demonstrativo=[_(u"Demonstration")], instrucoes=[_(u"Instructions")], agencia=self.bank_model.bank_branch, conta=self.bank_model.bank_account, ) for opt in self.bank_model.options: kwargs[opt.option] = opt.value data = bank_info(**kwargs) print_report(BillTestReport, data) # Callbacks def _on_parent_accounts__selection_changed(self, objectlist, account): self.force_validation() def on_description__activate(self, entry): if self.validate_confirm(): self.confirm() def on_description__validate(self, entry, text): if not text: return ValidationError(_("Account description cannot be empty")) def on_account_type__content_changed(self, account_type): account_type = account_type.get_selected() if self._last_account_type == account_type: return self._update_account_type(account_type) self._last_account_type = account_type def _on_bank_type__content_changed(self, bank_type): bank_number = bank_type.get_selected() if self._bank_number == bank_number: return self._update_bank_type() self._bank_number = bank_number def _on_bank_branch__validate(self, entry, value, bank_info): if bank_info: try: bank_info.validate_field(value) except BoletoException, e: return ValidationError(str(e))
class ComboSearchFilter(SearchFilter): """ - a label - a combo with a set of predefined item to select from """ __gtype_name__ = 'ComboSearchFilter' def __init__(self, label='', values=None): """ Create a new ComboSearchFilter object. :param label: label of the search filter :param values: items to put in the combo, see :class:`kiwi.ui.widgets.combo.ProxyComboBox.prefill` """ self._block_updates = False SearchFilter.__init__(self, label=label) label = gtk.Label(label) self.pack_start(label, False, False) label.show() self.title_label = label # We create the mode, but it will only be added to this box when # enable_advanced is called self.mode = ProxyComboBox() self.mode.connect('content-changed', self._on_mode__content_changed) for option in (ComboEquals, ComboDifferent): self.add_option(option) self.mode.select_item_by_position(0) self.combo = ProxyComboBox() if values: self.update_values(values) self.combo.connect('content-changed', self._on_combo__content_changed) self.pack_start(self.combo, False, False, 6) self.combo.show() # # SearchFilter # def get_state(self): value = self.combo.get_selected_data() mode = self.mode.get_selected_data() state = NumberQueryState(filter=self, value=value, mode=mode.mode) if hasattr(value, 'id'): state.value_id = value.id return state def set_state(self, value, value_id=None, mode=None): if mode is None: mode = NumberQueryState.EQUALS if value_id is not None: for item in self.combo.get_model_items().values(): if item is None: continue if item.id == value_id: value = item break self.select(value) def update_values(self, values): self._block_updates = True self.combo.prefill(values) self._block_updates = False def get_title_label(self): return self.title_label def get_mode_combo(self): return self.combo def get_description(self): desc = '' data = self.combo.get_selected_data() if data is not None: desc += self.combo.get_selected_label() return '%s %s' % (self.title_label.get_text(), desc,) # # Public API # def add_option(self, option_type, position=0): """ Adds an option :param option_type: option to add :type option_type: a :class:`ComboSearchOption` subclass """ option = option_type() num = len(self.mode) + position self.mode.insert_item(num, option.name, option_type) def select(self, data): """ selects an item in the combo :param data: what to select """ self.combo.select(data) def enable_advanced(self): self.pack_start(self.mode, False, False, 6) self.reorder_child(self.mode, 1) self.mode.show() # # Callbacks # def _on_mode__content_changed(self, combo): if not self._block_updates: self.emit('changed') def _on_combo__content_changed(self, mode): if not self._block_updates: self.emit('changed')
class ComboSearchFilter(SearchFilter): """ - a label - a combo with a set of predefined item to select from """ __gtype_name__ = 'ComboSearchFilter' def __init__(self, label='', values=None): """ Create a new ComboSearchFilter object. :param label: label of the search filter :param values: items to put in the combo, see :class:`kiwi.ui.widgets.combo.ProxyComboBox.prefill` """ self._block_updates = False SearchFilter.__init__(self, label=label) label = gtk.Label(label) self.pack_start(label, False, False) label.show() self.title_label = label self.combo = ProxyComboBox() if values: self.update_values(values) self.combo.connect('content-changed', self._on_combo__content_changed) self.pack_start(self.combo, False, False, 6) self.combo.show() # # SearchFilter # def get_state(self): value = self.combo.get_selected_data() state = NumberQueryState(filter=self, value=value) if hasattr(value, 'id'): state.value_id = value.id return state def set_state(self, value, value_id=None): if value_id is not None: for item in self.combo.get_model_items().values(): if item is None: continue if item.id == value_id: value = item break self.select(value) def update_values(self, values): self._block_updates = True self.combo.prefill(values) self._block_updates = False def get_title_label(self): return self.title_label def get_mode_combo(self): return self.combo def get_description(self): desc = '' data = self.combo.get_selected_data() if data is not None: desc += self.combo.get_selected_label() return '%s %s' % (self.title_label.get_text(), desc,) # # Public API # def select(self, data): """ selects an item in the combo :param data: what to select """ self.combo.select(data) # # Callbacks # def _on_combo__content_changed(self, mode): if not self._block_updates: self.emit('changed')