PluginAssistantEric.py

Tue, 02 Apr 2013 19:32:26 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 02 Apr 2013 19:32:26 +0200
changeset 69
9082f14126d9
parent 68
44e1af4dc5ad
child 71
025683852a63
permissions
-rw-r--r--

Activated status messages for processing API files.

# -*- coding: utf-8 -*-

# Copyright (c) 2008 - 2013 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the Eric assistant plugin.
"""

import os

from PyQt4.QtCore import QObject, QTranslator
from PyQt4.QtGui import QApplication

from E5Gui.E5Application import e5App

import Preferences

from AssistantEric.Assistant import AcsAPIs, AcsProject

# Start-Of-Header
name = "Assistant Eric Plugin"
author = "Detlev Offenbach <detlev@die-offenbachs.de>"
autoactivate = True
deactivateable = True
version = "2.4.2"
className = "AssistantEricPlugin"
packageName = "AssistantEric"
shortDescription = "Alternative autocompletion and calltips provider."
longDescription = """This plugin implements an alternative autocompletion and""" \
    """ calltips provider."""
needsRestart = True
pyqtApi = 2
# End-Of-Header

error = ""

assistantEricPluginObject = None


def createAutoCompletionPage(configDlg):
    """
    Module function to create the autocompletion configuration page.
    
    @return reference to the configuration page
    """
    global assistantEricPluginObject
    from AssistantEric.ConfigurationPages.AutoCompletionEricPage \
        import AutoCompletionEricPage
    page = AutoCompletionEricPage(assistantEricPluginObject)
    return page
    

def createCallTipsPage(configDlg):
    """
    Module function to create the calltips configuration page.
    
    @return reference to the configuration page
    """
    global assistantEricPluginObject
    from AssistantEric.ConfigurationPages.CallTipsEricPage \
        import CallTipsEricPage
    page = CallTipsEricPage(assistantEricPluginObject)
    return page
    

def getConfigData():
    """
    Module function returning data as required by the configuration dialog.
    
    @return dictionary containing the relevant data
    """
    return {
        "ericAutoCompletionPage": \
            [QApplication.translate("AssistantEricPlugin", "Eric"),
             os.path.join("AssistantEric", "ConfigurationPages",
                          "eric.png"),
             createAutoCompletionPage, "editorAutocompletionPage", None],
        "ericCallTipsPage": \
            [QApplication.translate("AssistantEricPlugin", "Eric"),
             os.path.join("AssistantEric", "ConfigurationPages",
                          "eric.png"),
             createCallTipsPage, "editorCalltipsPage", None],
    }


def prepareUninstall():
    """
    Module function to prepare for an uninstallation.
    """
    Preferences.Prefs.settings.remove(AssistantEricPlugin.PreferencesKey)
    

class AssistantEricPlugin(QObject):
    """
    Class implementing the Eric assistant plugin.
    """
    PreferencesKey = "AssistantEric"
    
    def __init__(self, ui):
        """
        Constructor
        
        @param ui reference to the user interface object (UI.UserInterface)
        """
        QObject.__init__(self, ui)
        self.__ui = ui
        self.__initialize()
        
        self.__defaults = {
            "AutoCompletionEnabled": False,
            "AutoCompletionSource": AcsAPIs | AcsProject,
            "AutoCompletionFollowHierarchy": False,
            "CalltipsEnabled": False,
            "CallTipsContextShown": True,
            "CallTipsFollowHierarchy": False,
        }
        
        self.__translator = None
        self.__loadTranslator()
    
    def __initialize(self):
        """
        Private slot to (re)initialize the plugin.
        """
        self.__object = None
    
    def __checkUiVersion(self):
        """
        Private method to check, if the IDE has a suitable version.
        
        @return flag indicating a suitable version (boolean)
        """
        global error
        
        suitable = self.__ui.versionIsNewer("5.0.99", "20100811")
        if not suitable:
            error = self.trUtf8("Your version of eric5 is not supported.")
        return suitable
    
    def __checkQSql(self):
        """
        Private method to perform some checks on QSql.
        
        @return flag indicating QSql is ok (boolean)
        """
        global error
        
        try:
            from PyQt4.QtSql import QSqlDatabase
        except ImportError:
            error = self.trUtf8("PyQt4.QtSql is not available.")
            return False
        
        drivers = QSqlDatabase.drivers()
        if "QSQLITE" in drivers:
            return True
        else:
            error = self.trUtf8("The SQLite database driver is not available.")
            return False
    
    def activate(self):
        """
        Public method to activate this plugin.
        
        @return tuple of None and activation status (boolean)
        """
        global error
        error = ""     # clear previous error
        
        if not self.__checkUiVersion() or \
           not self.__checkQSql():
            return None, False
        
        global assistantEricPluginObject
        assistantEricPluginObject = self
        
        from AssistantEric.Assistant import Assistant
        
        self.__object = Assistant(self, self.__ui)
        e5App().registerPluginObject("AssistantEric", self.__object)
        
        self.__object.activate()
        
        return None, True
    
    def deactivate(self):
        """
        Public method to deactivate this plugin.
        """
        e5App().unregisterPluginObject("AssistantEric")
        
        self.__object.deactivate()
        
        self.__initialize()
    
    def __loadTranslator(self):
        """
        Private method to load the translation file.
        """
        if self.__ui is not None:
            loc = self.__ui.getLocale()
            if loc and loc != "C":
                locale_dir = \
                    os.path.join(os.path.dirname(__file__), "AssistantEric", "i18n")
                translation = "assistant_{0}".format(loc)
                translator = QTranslator(None)
                loaded = translator.load(translation, locale_dir)
                if loaded:
                    self.__translator = translator
                    e5App().installTranslator(self.__translator)
                else:
                    print("Warning: translation file '{0}' could not be loaded."\
                        .format(translation))
                    print("Using default.")
    
    def getPreferences(self, key):
        """
        Public method to retrieve the various refactoring settings.
        
        @param key the key of the value to get
        @param prefClass preferences class used as the storage area
        @return the requested refactoring setting
        """
        if key in ["AutoCompletionEnabled", "AutoCompletionFollowHierarchy",
                   "CalltipsEnabled", "CallTipsContextShown",
                   "CallTipsFollowHierarchy"]:
            return Preferences.toBool(Preferences.Prefs.settings.value(
                self.PreferencesKey + "/" + key, self.__defaults[key]))
        else:
            return int(Preferences.Prefs.settings.value(
                self.PreferencesKey + "/" + key, self.__defaults[key]))
    
    def setPreferences(self, key, value):
        """
        Public method to store the various refactoring settings.
        
        @param key the key of the setting to be set (string)
        @param value the value to be set
        @param prefClass preferences class used as the storage area
        """
        Preferences.Prefs.settings.setValue(self.PreferencesKey + "/" + key, value)
        
        if key in ["AutoCompletionEnabled", "CalltipsEnabled"]:
            self.__object.setEnabled(key, value)

eric ide

mercurial