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

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

"""
Compatibility module to use KApplication instead of QApplication.
"""

import os
import sys

import Preferences

try:
    raise ImportError
except (ImportError, RuntimeError):
    os.environ["e4nokde"] = "1"
    
from PyQt4.QtCore import QEvent, Qt, qVersion
from PyQt4.QtGui import QApplication, QKeyEvent
class __qtKQApplication(QApplication):
    """
    Compatibility class to use QApplication.
    """
    def __init__(self, argv, opts):
        """
        Constructor
        
        @param argv command line arguments
        @param opts acceptable command line options (ignored)
        """
        QApplication.__init__(self, argv)
        
        self.__objectRegistry = {}
        
    def registerObject(self, name, object):
        """
        Public method to register an object in the object registry.
        
        @param name name of the object (string)
        @param object reference to the object
        @exception KeyError raised when the given name is already in use
        """
        if self.__objectRegistry.has_key(name):
            raise KeyError('Object "%s" already registered.' % name)
        else:
            self.__objectRegistry[name] = object
        
    def getObject(self, name):
        """
        Public method to get a reference to a registered object.
        
        @param name name of the object (string)
        @return reference to the registered object
        @exception KeyError raised when the given name is not known
        """
        if self.__objectRegistry.has_key(name):
            return self.__objectRegistry[name]
        else:
            raise KeyError('Object "%s" is not registered.' % name)
##        
##    if sys.platform != "win32" and qVersion() < "4.2.3":
##        def notify(self, receiver, event):
##            """
##            Public method to send an event to a receiver.
##            
##            This override implement a workround for Qt4 bug on Linux, which causes
##            a wrong handling of the keypad keys.
##            
##            @param receiver the receiving object for the event (QObject)
##            @param event the event sent to the object (QEvent)
##            @return value returned from the receiver's event handler (boolean)
##            """
##            if (event.type() == QEvent.KeyPress or \
##                event.type() == QEvent.KeyRelease) and \
##               event.modifiers() == Qt.KeyboardModifiers(Qt.KeypadModifier):
##                modifiers = event.modifiers() ^ Qt.KeyboardModifiers(Qt.KeypadModifier)
##                newEvent = QKeyEvent(event.type(), event.key(), modifiers,
##                                event.text(), event.isAutoRepeat(), event.count())
##                return QApplication.notify(self, receiver, newEvent)
##            return QApplication.notify(self, receiver, event)

################################################################################

def KQApplication(argv, opts):
    """
    Public function to instantiate an application object.
    
    @param argv command line arguments
    @param opts acceptable command line options
    @return reference to the application object
    """
    if Preferences.getUI("UseKDEDialogs") and not os.getenv("e4nokde"):
        return __kdeKQApplication(argv, opts)
    else:
        return __qtKQApplication(argv, opts)

from PyQt4.QtCore import QCoreApplication
e4App = QCoreApplication.instance
