def get_date(self): date = DateEntry.get_date(self) data_type = self.get_data_type() # The widget has asked for a datetime, but the entry gives only a date. if date and data_type == 'datetime': return datetime.datetime(date.year, date.month, date.day) return date
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 = DateEntry() self._start_changed_id = self.start_date.connect( '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 = DateEntry() self._end_changed_id = self.end_date.connect( '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)
def __init__(self): DateEntry.__init__(self) ValidatableProxyWidgetMixin.__init__(self) # Add some space to the entry, so it has rom for the icon, in case # of a validation error. # # Since we set the widget's width based on the number of characters, # get the width of a single char, so we can calculate how many # 'chars' the icon takes. layout = self.entry.get_layout() context = layout.get_context() metrics = context.get_metrics(context.get_font_description()) char_width = metrics.get_approximate_char_width() / pango.SCALE current_width = self.entry.get_width_chars() # We add 4 pixels to the width, because of the icon borders icon_width = VALIDATION_ICON_WIDTH + 4 self.entry.set_width_chars(current_width + int(icon_width / char_width))
class DateTimeEditUI(GladeSlaveDelegate): def __init__(self, dt = None): if not dt: self.dt = datetime.datetime.now() else: self.dt = dt # Set up the user interface GladeSlaveDelegate.__init__(self, gladefile="mo_datetime_edit", toplevel_name="window_main") self.entry_date = DateEntry() self.entry_time = ProxyEntry(data_type=datetime.time) self.set_datetime(self.dt) self.entry_date.connect('changed', self.entry_date__changed) self.entry_time.connect('changed', self.entry_time__changed) self.hbox.pack_start(self.entry_date, expand=False, fill=False) self.hbox.pack_start(self.entry_time, expand=False, fill=False) self.show() def entry_date__changed(self, *args): dt = self.entry_date.get_date() if dt != None: self.dt = self.dt.replace(dt.year, dt.month, dt.day) def entry_time__changed(self, *args): dt = None try: dt = self.entry_time.read() except ValidationError, e: # Ignore invalid times because the user might still be filling it # out. pass if dt != None: self.dt = self.dt.replace(self.dt.year, self.dt.month, self.dt.day, dt.hour, dt.minute)
def __init__(self, dt = None): if not dt: self.dt = datetime.datetime.now() else: self.dt = dt # Set up the user interface GladeSlaveDelegate.__init__(self, gladefile="mo_datetime_edit", toplevel_name="window_main") self.entry_date = DateEntry() self.entry_time = ProxyEntry(data_type=datetime.time) self.set_datetime(self.dt) self.entry_date.connect('changed', self.entry_date__changed) self.entry_time.connect('changed', self.entry_time__changed) self.hbox.pack_start(self.entry_date, expand=False, fill=False) self.hbox.pack_start(self.entry_time, expand=False, fill=False) self.show()
def check_widgets(self): for i in self.glade.get_widget_prefix("kdt"): widget = DateEntry() i.pack_start(widget, False, False) name = i.name.replace("kdt", "ent") c = compile("self.%s = widget" % name, '<string>', 'exec') eval(c) for i in self.glade.get_widget_prefix("krt"): widget = KiwiEntry() widget.set_mask("00.000.000-A") #p = widget.render_icon(gtk.STOCK_OK, gtk.ICON_SIZE_MENU) #widget.set_pixbuf(p) i.pack_start(widget, True, True) name = i.name.replace("krt", "ent") c = compile("self.%s = widget" % name, '<string>', 'exec') eval(c) for i in self.glade.get_widget_prefix("ktm"): widget = KiwiEntry() widget.set_mask("00:00") i.pack_start(widget, False, False) name = i.name.replace("ktm", "ent") c = compile("self.%s = widget" % name, '<string>', 'exec') eval(c)
def testGetSetDate(self): entry = DateEntry() entry.set_date(self.date) self.assertEqual(entry.get_date(), self.date)
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 = DateEntry() self._start_changed_id = self.start_date.connect( '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 = DateEntry() self._end_changed_id = self.end_date.connect( '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 # # 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) # # Callbacks # def _on_mode__content_changed(self, mode): self._update_dates() self._update_sensitivity() 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: 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 not start or not end: return if start >= end: self._internal_set_end_date(start + datetime.timedelta(days=1)) 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 not start or not end: return if end <= start: self._internal_set_start_date(end - datetime.timedelta(days=1))