Package Camelot :: Package camelot :: Package admin :: Package application_action |
[frames] | no frames]

# Source Code for Package Camelot.camelot.admin.application_action

     1
     2  from camelot.core.utils import ugettext_lazy as _
     3
     4 -class ApplicationAction(object):
     5      """An action that can be triggered by the user at the application level"""
     6
     7 -    def run(self, parent):
     8          """Execute the action, called within the gui thread
     9
    10  :param parent: a QWidget that can be used as a parent for widgets during the
    11  execution of the action
    12      """
    13          raise NotImplemented
    14
    15 -    def get_verbose_name(self):
    16          """:return: the name of the action, as it can be shown to the user"""
    17          raise NotImplemented
    18
    19 -    def get_icon(self):
    20          """:return: a camelot.view.art.Icon object"""
    21          raise NotImplemented
    22
    23 -class ApplicationActionFromGuiFunction( ApplicationAction ):
    24      """Create an application action object from a function that is supposed to run
    25      in the GUI thread"""
    26
    27 -    def __init__(self, name, gui_function, icon=None, verbose_name=None):
    28          """
    29          :param name: a unicode string naming this action
    30          :param gui_function: the function that will be called when the action
    31          is triggered, this function takes a its single argument a parent QObject
    32          :param icon: a camelot.view.art.Icon object
    33          :param verbose_name: the name used to display the action, if not given,
    34          the capitalized name will be used
    35          """
    36          self._name = name
    37          self._verbose_name = verbose_name or _(name.capitalize())
    38          self._icon = icon
    39          self._gui_function = gui_function
    40
    41 -    def run(self, parent):
    42          self._gui_function(parent)
    43
    44 -    def get_icon(self):
    45          return self._icon
    46
    47 -    def get_verbose_name(self):
    48          return self._verbose_name
    49
    50 -class TableViewAction(ApplicationAction):
    51      """An application action that opens a TableView for an Entity"""
    52
    53 -    def __init__(self, entity, admin=None, verbose_name=None, parent_admin=None):
    54          from camelot.admin.application_admin import get_application_admin
    55          self.parent_admin = parent_admin or get_application_admin()
    56          if admin:
    57              self.admin = admin(self.parent_admin, entity)
    58          else:
    59              self.admin = self.parent_admin.get_entity_admin(entity)
    60          self.entity = entity
    61          self.verbose_name = verbose_name
    62
    63 -    def get_verbose_name(self):
    64          return unicode(self.verbose_name or self.admin.get_verbose_name_plural())
    65
    66 -    def run(self, parent):
    67          tableview = self.admin.create_table_view(parent)
    68          return tableview
    69
    70 -def structure_to_application_action(structure):
    71      """Convert a python structure to an ApplicationAction"""
    72      if isinstance(structure, (ApplicationAction,)):
    73          return structure
    74      return TableViewAction(structure)
    75