eric7/EricWidgets/EricApplication.py

Thu, 28 Oct 2021 18:15:55 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 28 Oct 2021 18:15:55 +0200
branch
eric7
changeset 8729
226da2e26a84
parent 8358
144a6b854f70
child 8837
6b4b2acc5324
permissions
-rw-r--r--

Changed the 'small-screen' related code.

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

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

"""
Class implementing a specialized application class.
"""

from PyQt6.QtCore import Qt, QCoreApplication
from PyQt6.QtGui import QPalette
from PyQt6.QtWidgets import QApplication

QCoreApplication.setAttribute(
    Qt.ApplicationAttribute.AA_ShareOpenGLContexts, True)


class EricApplication(QApplication):
    """
    Eric application class with an object registry.
    """
    def __init__(self, argv):
        """
        Constructor
        
        @param argv command line arguments
        @type list
        """
        super().__init__(argv)
        
        QCoreApplication.setAttribute(
            Qt.ApplicationAttribute.AA_DontCreateNativeWidgetSiblings, True)
        
        self.__objectRegistry = {}
        self.__pluginObjectRegistry = {}
        
        self.__smallScreen = False
        if "--small-screen" in argv:
            self.__smallScreen = True
            argv.remove("--small-screen")
        if not self.__smallScreen:
            primaryScreenSize = self.primaryScreen().size()
            self.__smallScreen = (
                primaryScreenSize.width() < 1920 or
                primaryScreenSize.height() < 1080
            )
    
    def usesSmallScreen(self):
        """
        Public method to determine, if the application is used on a small
        screen.
        
        @return flag indicating the use of a small screen
        @rtype bool
        """
        return self.__smallScreen
    
    def registerObject(self, name, objectRef):
        """
        Public method to register an object in the object registry.
        
        @param name name of the object
        @type str
        @param objectRef reference to the object
        @type any
        @exception KeyError raised when the given name is already in use
        """
        if name in self.__objectRegistry:
            raise KeyError('Object "{0}" already registered.'.format(name))
        else:
            self.__objectRegistry[name] = objectRef
    
    def getObject(self, name):
        """
        Public method to get a reference to a registered object.
        
        @param name name of the object
        @type str
        @return reference to the registered object
        @rtype any
        @exception KeyError raised when the given name is not known
        """
        if name not in self.__objectRegistry:
            raise KeyError('Object "{0}" is not registered.'.format(name))
        
        return self.__objectRegistry[name]
    
    def registerPluginObject(self, name, objectRef, pluginType=None):
        """
        Public method to register a plugin object in the object registry.
        
        @param name name of the plugin object
        @type str
        @param objectRef reference to the plugin object
        @type any
        @param pluginType type of the plugin object
        @type str
        @exception KeyError raised when the given name is already in use
        """
        if name in self.__pluginObjectRegistry:
            raise KeyError(
                'Pluginobject "{0}" already registered.'.format(name))
        else:
            self.__pluginObjectRegistry[name] = (objectRef, pluginType)
    
    def unregisterPluginObject(self, name):
        """
        Public method to unregister a plugin object in the object registry.
        
        @param name name of the plugin object
        @type str
        """
        if name in self.__pluginObjectRegistry:
            del self.__pluginObjectRegistry[name]
    
    def getPluginObject(self, name):
        """
        Public method to get a reference to a registered plugin object.
        
        @param name name of the plugin object
        @type str
        @return reference to the registered plugin object
        @rtype any
        @exception KeyError raised when the given name is not known
        """
        if name not in self.__pluginObjectRegistry:
            raise KeyError(
                'Pluginobject "{0}" is not registered.'.format(name))
        
        return self.__pluginObjectRegistry[name][0]
    
    def getPluginObjects(self):
        """
        Public method to get a list of (name, reference) pairs of all
        registered plugin objects.
        
        @return list of (name, reference) pairs
        @rtype list of (str, any)
        """
        objects = []
        for name in self.__pluginObjectRegistry:
            objects.append((name, self.__pluginObjectRegistry[name][0]))
        return objects
    
    def getPluginObjectType(self, name):
        """
        Public method to get the type of a registered plugin object.
        
        @param name name of the plugin object
        @type str
        @return type of the plugin object
        @rtype str
        @exception KeyError raised when the given name is not known
        """
        if name not in self.__pluginObjectRegistry:
            raise KeyError(
                'Pluginobject "{0}" is not registered.'.format(name))
        
        return self.__pluginObjectRegistry[name][1]
    
    def usesDarkPalette(self):
        """
        Public method to check, if the application uses a palette with a dark
        background.
        
        @return flag indicating the use of a palette with a dark background
        @rtype bool
        """
        palette = self.palette()
        lightness = palette.color(QPalette.ColorRole.Window).lightness()
        return lightness <= 128

ericApp = QCoreApplication.instance

eric ide

mercurial