#!/usr/bin/env python
"""Pops a huge button the size of the screen.

This is usually used as part of a shell command to indicate the user the
completion of a task, while he is working on something else.
"""
__author__ = "Martin Blais <blais@furius.ca>"


# stdlib imports
import os
try:
    import PyQt4.Qt
except ImportError:
    raise ImportError('fin: cannot run fin, PyQt is not installed.')

from PyQt4.QtCore import QTimer, SIGNAL, Qt
from PyQt4.QtGui import QApplication, QFont, QPushButton, QDesktopWidget


mintime = 600 # msecs
killtime = 30000 # msecs

class BigButton(QPushButton):

    def __init__(self):
        QPushButton.__init__(self)
        self.setWindowFlags(Qt.SplashScreen)

        # Wait for 2 seconds before enabling quit on move events.
        self.timer = QTimer(self)
        self.connect(self.timer, SIGNAL('timeout()'),
                     self.enableMouseTracking)
        self.timer.start(mintime)

        # Use a long timer to kill itself in order to avoid blocking something
        # if I'm not at the console and some commands are queue after it.
        self.killtimer = QTimer(self)
        self.connect(self.killtimer, SIGNAL('timeout()'),
                     app.quit)
        self.killtimer.start(killtime)

    ##def paintEvent(self, e):
    ##    pass

    def enableMouseTracking(self):
        self.setMouseTracking(1)

    def mouseMoveEvent(self, ev):
        app.quit()

    def keyPressEvent(self, ev):
        ev.ignore()
        app.quit()

def main():
    import optparse
    parser = optparse.OptionParser(__doc__.strip())
    opts, args = parser.parse_args()

    if args:
        parser.error("'fin' takes no arguments.")

    # Show a screen-sized button
    global app; app = QApplication(args)
    app.setStyle("motif") # Select a style that is fast at drawing large windows!
    font = QFont("Helvetica", 48, QFont.Bold)
    app.setFont(font)

    fintaskname = os.environ.get('PWD', None)
    msg = 'Task DONE in \n%s' % fintaskname
    button = BigButton()
    button.setText(msg)
    button.connect(button, SIGNAL("clicked()"), app.quit)

    # allow the app to be interrupted once we enter the event loop
    import signal; signal.signal(signal.SIGINT, signal.SIG_DFL)

    isfull = False
    if isfull:
        button.showFullScreen()
    else:
        r = QDesktopWidget().screenGeometry()
        s = 200
        r.adjust(s, s, -s, -s)
        button.setGeometry(r)
        button.show()

    app.exec_()


if __name__ == '__main__':
    main()
