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

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class s60_Popup(QFrame):
    def __init__(self,  parent,  main):
        super(s60_Popup,  self).__init__(parent)

        self.parent = parent
        self.main = main

        self.log = main.log

        self.setWindowFlags(Qt.Tool| Qt.X11BypassWindowManagerHint | Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)
        self.setFrameStyle( QFrame.Box| QFrame.Plain )
        self.setLineWidth( 2 )

        self.BUTTON_1 = 0
        self.BUTTON_2 = 1

        self.__parent = parent
        self.__caption = ""
        self.__text = ""
        self.__icon = ""
        self.__target = QRect(0,  0,  0,  0)
        self.__pos = QPoint(0,  0)
        self.buttons = QDialogButtonBox(self)
        self.buttons.setCenterButtons(True)

        # TODO: Ein Timeout hinzufügen
        self.connect(self, SIGNAL("clicked()"), SLOT("hide()"))

    def __str__(self):
        return "\"s60 Popup\""

    def setCaption(self,  caption):
        self.__caption = unicode(caption,  "utf8")

    def setText(self,  text):
        self.__text = unicode(text,  "utf8")

    def setIcon(self,  icon):
        self.__icon = QPixmap(icon)

    def setTarget(self,  target):
        self.__target = target

    def addButton(self,  text):
        return self.buttons.addButton(text,  QDialogButtonBox.AcceptRole)

    def showPopup(self,  caption=None,  text=None,  icon=None,  target=None):

        if caption:
            self.setCaption(caption)
        caption = self.__caption

        if text:
            self.setText(text)
        text = self.__text

        if icon:
            self.setIcon(icon)
        icon = self.__icon

        if target:
            self.__target = target
        target = self.__target

        hb = QHBoxLayout(self)
        hb.setMargin(8)
        hb.setSpacing(6)

        vb = QVBoxLayout()
        vb.setMargin(8)
        vb.setSpacing(6)

        ttlIcon = QLabel()
        ttlIcon.setPixmap(icon)
        ttlIcon.setAlignment(Qt.AlignLeft)
        hb.addWidget(ttlIcon)

        ttl = QLabel("<b><fonz size=+1>" + caption + "</font></b>")
        fnt = ttl.font()
        fnt.setBold(True)
        ttl.setFont(fnt)
        ttl.setAlignment(Qt.AlignHCenter)
        vb.setStretchFactor(ttl,  10)  # erzwinge Zentrierung
        vb.addWidget(ttl)

        msg = QLabel(text)
        msg.setAlignment(Qt.AlignLeft)
        msg.setTextInteractionFlags(Qt.LinksAccessibleByMouse)
        msg.setOpenExternalLinks(True)
        vb.addWidget(msg)

        if self.buttons.buttons():
            vb.addWidget(self.buttons)
            self.connect(self.buttons, SIGNAL("clicked(QAbstractButton *)"), lambda btn: self.hide())

        hb.addLayout(vb)

        self.__moveNear(target)

        self.log.info("Zeige Popup mit Titel %s und Text %s" % (caption,  text))

        self.move(self.__pos)
        self.show()

    def __moveNear(self,  target):
        pos = target.topLeft()
        x = pos.x()
        y = pos.y()

        w = self.minimumSizeHint().width()
        h = self.minimumSizeHint().height()

        # FIXME: irgendwas sinnvolles..

        r = QApplication.desktop().geometry()
        #r = QDesktopWidget.screenGeometry()

#        if x < r.center().x():
#            x = x + target.width()
#        else:
#            x = x - w;

        # Es geht über den Bildschirmrand, also zeige es ganz am Rand an
        if (y + h) > r.bottom():
            y = r.bottom() - h;

        if (x + w) > r.right():
            x = r.right() - w;

        if y < r.top():
            y = r.top()

        if x < r.left():
            x = r.left()

        # FIXME: In KDE4 ist das Popup am unteren Ende, es sollte aber auf dem Pager sein
        y = y - 40

        self.__pos = QPoint(x,  y)

    def mouseReleaseEvent(self,  e):
        self.emit(SIGNAL("clicked()"))
        self.emit(SIGNAL("clicked(QPoint)"),  e.pos())
