예제 #1
0
    def __init__(self, window, persistenceHandler=None):
        """
        Default class constructor.

        :param `window`: an instance of :class:`Window`;
        :param `persistenceHandler`: if not ``None``, this should a custom handler derived
         from :class:`~lib.agw.persist.persist_handlers.AbstractHandler`.
        """

        self._name = window.GetName()

        if not self._name.strip():
            raise Exception("Persistent windows should be named! (class=%s)" %
                            window.__class__)

        klass = window.__class__
        if issubclass(klass, wx.GenericDirCtrl):
            self._window = window.GetTreeCtrl()
        elif issubclass(klass, wx.gizmos.EditableListBox):
            self._window = window.GetListCtrl()
        else:
            self._window = window

        if persistenceHandler is not None:
            self._persistentHandler = persistenceHandler(self)
        else:
            self._persistentHandler = FindHandler(self)

        if self._name in BAD_DEFAULT_NAMES:
            warnings.warn(
                "Window names should not be defaulted! (class=%s, name=%s)" %
                (window.__class__, window.GetName()))
예제 #2
0
class PersistentObject(object):
    """
    :class:`PersistentObject`: ABC for anything persistent.

    This is the base class for persistent object adapters.
    wxPython persistence framework is non-intrusive, i.e. can work with the
    classes which have no relationship to nor knowledge of it. To allow this,
    an intermediate persistence adapter is used: this is just a simple object
    which provides the methods used by :class:`PersistenceManager` to save and restore
    the object properties and implements them using the concrete class methods.

    You may derive your own classes from :class:`PersistentObject` to implement persistence
    support for your common classes, see :ref:`persistent-windows` in the
    `__init__.py` file.
    """
    def __init__(self, window, persistenceHandler=None):
        """
        Default class constructor.

        :param `window`: an instance of :class:`Window`;
        :param `persistenceHandler`: if not ``None``, this should a custom handler derived
         from :class:`~lib.agw.persist.persist_handlers.AbstractHandler`.
        """

        self._name = window.GetName()

        if not self._name.strip():
            raise Exception("Persistent windows should be named! (class=%s)" %
                            window.__class__)

        klass = window.__class__
        if issubclass(klass, wx.GenericDirCtrl):
            self._window = window.GetTreeCtrl()
        elif issubclass(klass, wx.gizmos.EditableListBox):
            self._window = window.GetListCtrl()
        else:
            self._window = window

        if persistenceHandler is not None:
            self._persistentHandler = persistenceHandler(self)
        else:
            self._persistentHandler = FindHandler(self)

        if self._name in BAD_DEFAULT_NAMES:
            warnings.warn(
                "Window names should not be defaulted! (class=%s, name=%s)" %
                (window.__class__, window.GetName()))

    def GetName(self):
        """
        Returns the string uniquely identifying the window we're associated with
        among all the other objects of the same type.
        
        :note: This method is used together with :meth:`~PersistentObject.GetKind` to construct the unique
         full name of the object in e.g. a configuration file.
        """

        return self._name

    def GetWindow(self):
        """ Returns the actual associated window. """

        return self._window

    def GetKind(self):
        """
        Returns the string uniquely identifying the objects supported by this adapter.

        :note: This method is called from :meth:`~PersistentObject.SaveValue` and :meth:`~PersistentObject.RestoreValue` and normally
         returns some short (but not too cryptic) strings, e.g. "Checkbox".
        """

        return self._persistentHandler.GetKind()

    def Save(self):
        """
        Saves the corresponding window settings. 

        .. note::

           This method shouldn't be used directly as it doesn't respect the
           global :meth:`PersistenceManager.DisableSaving() <PersistenceManager.DisableSaving>` settings, use :class:`PersistenceManager`
           methods with the same name instead.
           
        """

        self._persistentHandler.Save()

    def Restore(self):
        """
        Restores the corresponding window settings. 

        :note: This method shouldn't be used directly as it doesn't respect the
         global :meth:`PersistenceManager.DisableRestoring() <PersistenceManager.DisableRestoring>` settings, use :class:`PersistenceManager`
         methods with the same name instead.
        """

        self._persistentHandler.Restore()

    def SaveValue(self, name, value):
        """
        Save the specified value using the given name.
        
        :param `name`: the name of the value in the configuration file;
        :param `value`: the value to save.

        :returns: ``True`` if the value was saved or ``False`` if an error occurred.
        """

        return PersistenceManager.Get().SaveValue(self, name, value)

    def SaveCtrlValue(self, name, value):
        """
        Save the specified value using the given name, should be used only for
        controls data value.
        
        :param `name`: the name of the value in the configuration file;
        :param `value`: the value to save.

        :returns: ``True`` if the value was saved or ``False`` if an error occurred.
        """

        return PersistenceManager.Get().SaveCtrlValue(self, name, value)

    def RestoreValue(self, name):
        """
        Restore the value saved by :meth:`~PersistentObject.SaveValue`.
        
        :param `name`: the same name as was used by :meth:`~PersistentObject.SaveValue`.

        :returns: ``True`` if the value was successfully read or ``False`` if
         it was not found or an error occurred.
        """

        return PersistenceManager.Get().RestoreValue(self, name)

    def RestoreCtrlValue(self, name):
        """
        Restore the value saved by :meth:`~PersistentObject.SaveCtrlValue`, should be used only for
        controls data value.
        
        :param `name`: the same name as was used by :meth:`~PersistentObject.SaveCtrlValue`.

        :returns: ``True`` if the value was successfully read or ``False`` if
         it was not found or an error occurred.
        """

        return PersistenceManager.Get().RestoreCtrlValue(self, name)