Exemplo n.º 1
0
class App(object):

    ui = """
    <ui>
        <toolbar name="upperToolbar">
            <toolitem action="Undo"/>
            <toolitem action="Redo"/>
            <separator />
            <toolitem action="NewEvent"/>
            <toolitem action="DelEvent"/>
            <separator />
            <toolitem action="Back"/>
            <toolitem action="Forward"/>
            <toolitem action="GoToToday"/>
            <toolitem action="GoToSelected"/>
            <separator />
            <toolitem action="SwitchViews"/>
        </toolbar>
        <toolbar name="lowerToolbar">
           <toolitem action="ZoomIn"/>
           <toolitem action="ZoomOut"/>
        </toolbar>
    </ui>"""


    def __init__(self):
        self.undo = UndoStack()
        self.history = UndoStack(
            gtk.Action("Back", None, None, gtk.STOCK_GO_BACK),
            gtk.Action("Forward", None, None, gtk.STOCK_GO_FORWARD))
        w = gtk.Window()
        w.connect("destroy", gtk.main_quit)
        vbox = gtk.VBox()
        self.model = Schedule("schedule.csv")
        self.info = CalendarInfo(self.model)
        self.weekview = WeekView(self.info, self.undo, self.history)
        self.monthview = MonthView(self.info, self.undo, self.history)

        uiman = gtk.UIManager ()
        actiongroup = MenuCommand.create_action_group(self)
        actiongroup.add_action(self.undo.undo_action)
        actiongroup.add_action(self.undo.redo_action)
        actiongroup.add_action(self.history.undo_action)
        actiongroup.add_action(self.history.redo_action)

        self.undo.undo_action.connect("activate", self.update_actions)
        self.undo.redo_action.connect("activate", self.update_actions)
        uiman.insert_action_group(actiongroup)
        uiman.add_ui_from_string(self.ui)
        toolbar = uiman.get_widget("/upperToolbar")
        vbox.pack_start (toolbar, False, False)

        vbox.pack_start(self.weekview, True, True)
        vbox.pack_start(self.monthview, True, True)

        toolbar = uiman.get_widget("/lowerToolbar")

        self.selection_buffer = gtk.TextBuffer()
        self.selection_buffer.create_tag("error",
                                         underline=pango.UNDERLINE_SINGLE)
        self.selection_entry = gtk.TextView(self.selection_buffer)
        
        self.pack_toolbar_widget(toolbar, self.selection_entry)
        vbox.pack_end (toolbar, False, False)

        w.add(vbox)
        w.show()
        vbox.show()
        self.monthview.hide()
        self.window = w
        self.weekview.connect("notify::selected", self.update_actions)
        self.info.connect("selection-recurrence-changed", self.update_actions)
        self.selection_entry.connect("key-press-event", self.selection_entry_key_press_cb)

    def pack_toolbar_widget(self, toolbar, widget):
        toolitem = gtk.ToolItem()
        toolitem.add(widget)
        toolitem.set_expand(True)
        widget.show()
        toolitem.show()
        toolbar.add(toolitem)

    dont_update_entry = False

    def selection_entry_key_press_cb(self, entry, event):
        if event.keyval == gtk.keysyms.Return:
            self._parse_text()
            return True
        return False

    def _parse_text(self):
        self.dont_update_entry = True
        self.dirty = False
        b = self.selection_buffer
        text = b.get_text(b.get_start_iter(), b.get_end_iter())

        try:
            if not (self.info.selected is None):
                self.undo.commit(SetEventRecurrence(self, text))
            else:
                self.undo.commit(SelectRecurrence(self, text))
            b.remove_all_tags(b.get_start_iter(), b.get_end_iter())
        except parser.ParseError, e:
            self.selection_entry_error(e.position)
        self.dont_update_entry = False
        return False