class AlexLabelTest(AbstractComponentTest):
    def __init__(self):
        super().__init__()
        self.name = "Alex label test"

    def test_component(self, master, message_label):

        self.message_label = message_label
        self.master = master

        self.label1 = AlexLabel(master)
        self.label1.set('Label text:')
        self.label1.grid(row=0, column=0)

        self.entry1 = AlexEntry(master)
        self.entry1.set("Enter label text")
        self.entry1.grid(row=0, column=1)

        AlexButton(master, text='Set label text',
                   command=self._set_label_text).grid(row=1, column=0)
        AlexButton(master,
                   text='Set entry from label',
                   command=self._set_entry_from_label).grid(row=1, column=1)

    def _set_label_text(self):

        self.label1.set(self.entry1.get())
        self.entry1.set('')

    def _set_entry_from_label(self):

        self.entry1.set(self.label1.get())
class GenericInputEditDialog(AbstractInputDialog):
    def __init__(self, window_manager, presenter):
        super().__init__(window_manager, presenter)

    def create_dialog(self):
        super().create_dialog()
        self.set_default_buttons()

        self.label = AlexLabel(self.interior)
        self.label.pack()

        self.entry = AlexEntry(self.interior)
        self.entry.pack()

    def config_dialog(self, label=_('Please edit string:'), initvalue=''):

        self.label.set(label)
        self.entry.set(initvalue)

    def _get_entry(self):
        return self.entry.get()

    def _set_entry(self, value):
        self.entry.set(value)

    input = property(_get_entry, _set_entry)
class ReferenceView(Frame):  # @UndefinedVariable
    def __init__(self, parent, presenter, label):
        super().__init__(parent, borderwidth=1, relief=RIDGE)
        self.parent = parent
        self.presenter = presenter
        self.presenter.view = self

        self.labelframe = Frame(self)
        self.labelframe.pack(pady=5)

        self.listbox = AlexComboBox(self)
        self.listbox.pack(fill=X, padx=5)

        self._add_label(label)
        self.pack(side=LEFT, padx=5, pady=5)

        self.buttonframe = Frame(self)
        self.buttonframe.pack()
        self.buttons = []

    def _add_label(self, label):

        self.label = AlexLabel(self.labelframe)
        self.label.pack()
        self.label.set(label)

    def _set_items(self, items):
        self.deactivate()
        self.listbox.set_items(items)
        self.activate()

    def _get_selected_item(self):
        return self.listbox.get()

    def _get_items(self):
        return self.listbox.get_items()

    def add_button(self, action):
        self.buttons.append(
            AlexButton(self.buttonframe,
                       text=action.label,
                       command=action.callback))
        self.buttons[-1].pack(side=LEFT, padx=5, pady=5)

    def deactivate(self):
        for button in self.buttons:
            button.configure(state=DISABLED)

    def activate(self):
        for button in self.buttons:
            button.configure(state=NORMAL)

    def show_message(self, message_text):
        messagebox.showinfo(_("Hint"), message_text)

    items = property(_get_items, _set_items)
    selected_item = property(_get_selected_item)
class GenericTreeSelectionDialog(AbstractInputDialog):
    def __init__(self, window_manager, presenter):

        super().__init__(window_manager, presenter)

        self.tree_widget = None
        self.filter_is_set = False

    def create_dialog(self):

        super().create_dialog()

        self.label = AlexLabel(self.interior)
        self.label.pack(side=TOP, padx=5, pady=5)

        filter_frame = Frame(self.interior)
        AlexLabel(filter_frame, text=_('Search tree:')).pack(side=LEFT, pady=5)
        self.filter_entry = AlexEntry(filter_frame)
        self.filter_entry.bind("<KeyRelease>",
                               lambda event: self._apply_filter(event))
        self.filter_entry.pack(side=LEFT, fill=X, expand=YES)
        filter_frame.pack(side=TOP, expand=YES, fill=X)

        self.set_default_buttons()

    def config_dialog(self, label=_('Select a tree node')):
        self.label.set(label)

    def _apply_filter(self, event):
        filter_string = self.filter_entry.get()
        if len(filter_string) > 2:
            visible_nodes = self.tree_widget.apply_filter(filter_string)
            if visible_nodes < 20:
                self.tree_widget.expand_all()
            self.filter_is_set = True
        else:
            if self.filter_is_set:
                self.tree_widget.clear_filter()
                self.filter_is_set = False

    def set_tree(self, tree):
        if self.tree_widget is not None:
            self.tree_widget.destroy()
        self.tree_widget = AlexTree(self.interior, tree)
        self.tree_widget.pack()

    def activate(self, callback, label):
        if self.window is not None:
            self.label.set(label)
        super().activate(callback, label=label)

    input = property(lambda self: self.tree_widget.get())
    tree = property(None, set_tree)
    def create_dialog(self):

        super().create_dialog()

        label = AlexLabel(self.interior)
        label.set("%s:" % _('Please select year'))
        label.pack()

        self.year_combo_box = AlexComboBox(self.interior)
        self.year_combo_box.pack(padx=5, pady=5)

        self.set_default_buttons()
class GenericBooleanSelectionDialog(AbstractInputDialog):
    @inject
    def __init__(self, window_manager: guiinjectorkeys.WINDOW_MANAGER_KEY,
                 presenter: guiinjectorkeys.GENERIC_INPUT_DIALOG_PRESENTER):
        super().__init__(window_manager, presenter)

    def create_dialog(self):
        super().create_dialog()
        self.add_button(_('Yes'), self.presenter.yes_action)
        self.add_button(_('No'), self.presenter.no_action)
        self.label = AlexLabel(self.interior)
        self.label.pack(padx=20, pady=20)

    def config_dialog(self, question=('Select yes or no')):

        self.label.set(question)
Exemplo n.º 7
0
    def __init__(self, test_classes):

        self.message_broker = MessageBroker()
        self.window_manager = WindowManager(self.message_broker)

        self.create_test_instances(test_classes)

        self.root = self.window_manager.create_new_window()
        geometry = '800x450'
        self.root.geometry(geometry)

        self.test_frame = None

        top_frame = Frame(self.root)
        top_frame.pack(side=TOP, fill=BOTH, expand=TRUE)

        message_frame = Frame(self.root)
        message_frame.pack(side=TOP)

        button_frame = Frame(self.root)
        button_frame.pack(side=TOP)

        self.message_label = AlexLabel(message_frame)
        self.message_label.pack(side=LEFT)

        self.menu_frame = Frame(top_frame)
        self.menu_frame.pack(side=LEFT, anchor=NW, padx=5)

        label = AlexLabel(self.menu_frame)
        label.set("Select a test")
        label.pack()

        self.tests = AlexListBox(
            self.menu_frame,  # @UndefinedVariable
            height=len(self.test_instances),
            selectioncommand=self.run_selected_test,
        )

        self.tests.set_items(self.test_instances)
        self.tests.pack(anchor=NW)

        self.widget_frame = Frame(top_frame, borderwidth=1, relief=SOLID)
        self.widget_frame.pack(side=LEFT, fill=BOTH, expand=TRUE)

        quit_button = Button(button_frame, command=self.root.quit, text="Quit")
        quit_button.pack(side=LEFT)
class EventConfirmationDialog(AbstractInputDialog):
    @inject
    def __init__(self, window_manager: guiinjectorkeys.WINDOW_MANAGER_KEY,
                 presenter: guiinjectorkeys.EVENT_CONFIRMATION_PRESENTER_KEY):
        super().__init__(window_manager, presenter)

    def create_dialog(self):

        super().create_dialog()

        self.add_button(_('Create new event'), self.presenter.cancel_action)
        self.label = AlexLabel(self.interior,
                               wraplength=550,
                               font=("Helvetica", 14, "bold"))
        self.label.pack()
        self.event_frame = None

    def config_dialog(self, event_list=[], date=None):

        self.label.set(
            _("Events exist on %s. Please select the event you want or create a new one"
              ) % date)

        if self.event_frame is not None:
            self.event_frame.destroy()

        self.event_frame = Frame(self.interior)
        self.event_frame.pack()
        row_counter = 0
        for event in event_list:
            description = AlexLabel(self.event_frame,
                                    wraplength=500,
                                    justify=LEFT,
                                    text=event.description)
            description.grid(row=row_counter, column=0, sticky=W)

            def closure(event):
                return lambda: self._set_return_value(event)

            button = AlexButton(self.event_frame,
                                text=_("Goto event"),
                                command=closure(event))
            button.grid(row=row_counter, column=1)
            row_counter += 1
    def test_component(self, master, message_label):
        self.message_label = message_label
        self.master = master

        label1 = AlexLabel(master)
        label1.set('Entry 1:')
        label1.grid(row=0, column=0)

        self.entry1 = AlexEntry(master)
        self.entry1.set("Entry 1 text")
        self.entry1.grid(row=0, column=1)

        label2 = AlexLabel(master)
        label2.set('Entry 2:')
        label2.grid(row=1, column=0)

        self.entry2 = AlexEntry(master)
        self.entry2.set("Entry 2 text")
        self.entry2.grid(row=1, column=1)

        AlexButton(master, text='Switch input',
                   command=self._switch_input).grid(row=2, column=1)
    def create_dialog(self):

        self.window = self.window_manager.create_new_window()
        self.window.withdraw()

        super().__init__(self.window)
        self.pack()

        label = AlexLabel(self)
        label.set(_("Please select user:"))
        label.pack(padx=5, pady=5)

        self.combobox = AlexComboBox(self)
        self.combobox.pack(padx=5, pady=5)

        buttonframe = Frame(self)
        buttonframe.pack(padx=5, pady=5)

        AlexButton(buttonframe, text=_('OK'),
                   command=self.presenter.ok_action).pack(side=LEFT)
        AlexButton(buttonframe,
                   text=_('Cancel'),
                   command=self.presenter.cancel_action).pack(side=LEFT)
class DocumentWindow(BaseWindow):
    '''
    The window for manipulating documents.
    '''
    @inject
    @singleton
    def __init__(self, window_manager: guiinjectorkeys.WINDOW_MANAGER_KEY,
                 message_broker: guiinjectorkeys.MESSAGE_BROKER_KEY,
                 presenter: guiinjectorkeys.DOCUMENT_WINDOW_PRESENTER_KEY,
                 dialogs: guiinjectorkeys.DOCUMENT_WINDOW_DIALOGS_KEY,
                 document_menu_additions: guiinjectorkeys.
                 DOCUMENT_MENU_ADDITIONS_KEY):
        self.notebook = None
        self._description_widget = None
        self._condition_widget = None
        self._keywords_widget = None
        super().__init__(window_manager, message_broker, presenter, dialogs,
                         document_menu_additions)
        self.window.title(_("Alexandria documents"))

    def _create_new(self):
        self.presenter.create_new()

    def _change_widget_state(self, state):
        self._description_widget.configure(state=state)
        self._condition_widget.configure(state=state)
        self._keywords_widget.configure(state=state)

    def _clear_widgets(self):
        self._document_label.set(_("No document available"))
        self._description_widget.set('')
        self._condition_widget.set('')
        self._keywords_widget.set('')

    def _disable_widgets(self):
        self._change_widget_state(DISABLED)

    def _enable_widgets(self):
        self._change_widget_state(NORMAL)

    def _populate_entity_frame(self):
        # pylint: disable=no-member
        self._document_label = AlexLabel(self.entity_frame,
                                         text=_("No document available"))
        self._document_label.pack(padx=7, pady=7)
        self.notebook = Notebook(self.entity_frame, width=600)
        self.notebook.pack(fill=X)
        description = Frame(self.notebook)
        self.notebook.add(description, text=_('Description'))
        self._description_widget = AlexText(description,
                                            font="Helvetica 12 bold",
                                            wrap=WORD,
                                            height=6)
        self._description_widget.pack(fill=X)
        condition = Frame(self.notebook)
        self.notebook.add(condition, text=_('Condition'))
        self._condition_widget = AlexText(condition,
                                          font="Helvetica 12 bold",
                                          wrap=WORD,
                                          height=6)
        self._condition_widget.pack(fill=X)
        keywords = Frame(self.notebook)
        self.notebook.add(keywords, text=_('Keywords'))
        self._keywords_widget = AlexText(keywords,
                                         font="Helvetica 12 bold",
                                         wrap=WORD,
                                         height=6)
        self._keywords_widget.pack(fill=X)

    def _view_to_entity(self):
        if self._entity == None:
            return None

        if self._description_widget.get() != self._entity.description:
            self._entity_has_changed = True
            self._entity.description = self._description_widget.get()
        if self._condition_widget.get() != self._entity.condition:
            self._entity_has_changed = True
            self._entity.condition = self._condition_widget.get()
        if self._keywords_widget.get() != self._entity.keywords:
            self._entity_has_changed = True
            self._entity.keywords = self._keywords_widget.get()

        return self._entity

    def _entity_to_view(self, entity):

        if self._entity != entity:
            self._entity_has_changed = False

        if self._entity == None:
            self._enable_widgets()

        self._entity = entity
        if entity == None:
            self._clear_widgets()
            self._disable_widgets()
            return

        if self._entity.id == None:
            self._document_label.set(_("New document"))
        else:
            self._document_label.set(
                _("Document no.{0:d} ({1!s})").format(
                    self._entity.id, self._entity.document_type.description))
        self._description_widget.set(self._entity.description)
        self._condition_widget.set(self._entity.condition)
        self._keywords_widget.set(self._entity.keywords)
class AbstractInputDialog:
    '''
    A basic dialog skeleton for input dialogs. For simple
    message dialogs use the native dialogs of tkinter.

    This provides a rather simple framework for fast creating
    dialogs that return user input to the calling component.
    
    The framework works like this:
    
    - Make a subclass of this class
    - Overwrite the create_dialog method. You need to
      call the method in this superclass to provide you
      with the basic frames in the dialog window.
      Add your input stuff to the interior frame and
      your buttons to the button_frame (there are methods
      to help you for this)
    - Make a subclass of the AbstractInputDialogPresenter
    - Create action methods in the presenter that
      are bound to the buttons in the dialog window
    - The actions that are considered to close the
      dialog must set the return_value property of
      the view. This will close the dialog window and
      return the return_value to the caller of activate
    - Inject the dialog presenter into the dialog
    - Inject the dialog into your component that wants to
      use it
    - Call the activate method in the dialog in your
      component. You need to set a callback for your
      value. This callback will be executed with a value
      when the presenter closes the dialog window
    '''
    def __init__(self, window_manager, presenter):
        self.window_manager = window_manager
        self.presenter = presenter
        self.window = None
        self.callback = None

    def create_dialog(self):
        '''
        Extend this method in your child class. It already
        provides three frames: interior, buttons_frame and
        errormessage. The errormessage frame already has
        a label that may be read and set by the errormessage
        property.
        To set default buttons in the button_frame, use the
        set default buttons.
        Other buttons may be set through the add_button method.
        '''

        self.window = self.window_manager.create_new_window()
        self.window.protocol("WM_DELETE_WINDOW",
                             lambda: self._set_return_value(None))
        self.window.transient()
        self.window.attributes('-topmost', True)
        self.window.withdraw()

        self.interior = Frame(self.window)
        self.interior.pack()

        self.buttons_frame = Frame(self.window)
        self.buttons_frame.pack()

        self.message_frame = Frame(self.window)
        self.message_frame.pack()

        self._errormessage = AlexLabel(self.message_frame)
        self._errormessage.pack(side=TOP)

    def add_button(self, label, callback):
        '''
        Fast setup method for buttons. Just provide
        a label and a callback and a button will be
        appended to the button_frame
        '''

        button = AlexButton(self.buttons_frame, command=callback)
        button.set(label)
        button.pack(side=LEFT, padx=5, pady=5)
        return button

    def set_default_buttons(self):
        '''
        This method may be used in child classes
        to set the default buttons OK and Cancel
        '''
        self.add_button(_('OK'), self.presenter.ok_action)
        self.add_button(_('Cancel'), self.presenter.cancel_action)

    def _get_errormessage(self):
        message = self._errormessage.get()
        if message == '':
            return None
        else:
            return message

    def _set_errormessage(self, message):
        if message == None:
            self._errormessage.set('')
        else:
            self._errormessage.set(message)

    def activate(self, callback, **kw):
        '''
        '''
        if self.window is None:
            self.create_dialog()

        self.config_dialog(**kw)

        if self.window is None:
            callback(None)
            return
        self.callback = callback

        self.presenter.view = self
        self.window.deiconify()
        self.window.grab_set()

    def config_dialog(self, **kw):

        pass

    def _set_return_value(self, value):
        self.window.grab_release()
        self.window.withdraw()
        self.callback(value)

    return_value = property(None, _set_return_value)
    errormessage = property(_get_errormessage, _set_errormessage)