#!/usr/bin/env python
# by klaatu
# and rowinggolfer and Timrit and cobra2
# basically openmolar in freenode
# good thing it's gplv3 huh.

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

class Form(QDialog):

    def __init__(qalc, parent=None):
        super(Form, qalc).__init__(parent)
        qalc.currenttime = QLineEdit("00:00:00:00")
        qalc.currenttime.selectAll()
        qalc.funcCombo = QComboBox()
        qalc.funcCombo.addItem("+")
        qalc.funcCombo.addItem("-")
        qalc.newtime = QLineEdit("00:00:00:00")
        qalc.totaltime = QLabel()
        qalc.fpsCombo = QComboBox()
        qalc.fpsCombo.addItem("29.97")
        qalc.fpsCombo.addItem("29.97.DF")
        qalc.fpsCombo.addItem("25")
        qalc.fpsCombo.addItem("24")
        qalc.fpsCombo.addItem("23.98")
        qalc.fpsCombo.addItem("18")
        qalc.fpsCombo.addItem("16")
        button1 = QPushButton("Calculate")
        layout = QVBoxLayout()
        layout.addWidget(qalc.currenttime)
        layout.addWidget(qalc.funcCombo)
        layout.addWidget(qalc.newtime)
        layout.addWidget(qalc.fpsCombo)
        layout.addWidget(qalc.totaltime)
        layout.addWidget(button1)
        qalc.setLayout(layout)
        qalc.currenttime.setFocus()
        qalc.connect(button1, SIGNAL("clicked()"), qalc.updateUi)
        qalc.setWindowTitle("SMPTE Calq")

    def updateUi(qalc):
        CUR = unicode(qalc.currenttime.text())
        DIF = unicode(qalc.newtime.text())
        FPS = qalc.fpsCombo.currentText()
        FUNC = qalc.funcCombo.currentText()
# correct for drop frame or non drop frame
        DROP=str(FPS).partition('.')
        if DROP[2] == '97.DF':
            FPS=DROP[0]
        else:
            FPS=int(DROP[0])+1
        if DROP[2] == '':
            FPS=DROP[0]
# are we adding or minussssing?
        if FUNC == '+':
            OP=1
        else:
            OP=0
        splitcur = CUR.split(':')
        splitdif = DIF.split(':')
# and now do the maths
        if OP == 1:
            FRM=int(splitcur[3])+int(splitdif[3])
            if int(FRM)>int(FPS):
                SECP=int(FRM)/int(FPS)
                FRAMES=int(FRM)-(int(SECP)*int(FPS))
            else:
                SECP=0
                FRAMES=int(FRM)
        else:
            FRM=int(splitcur[3])-int(splitdif[3])
            FRAMES=int(FRM)

# FRAMES contains final frame count
# SECP adds the seconds we need to add to seconds field

# SEC == new amount of seconds PLUS the SECP carry-over
        if OP == 1:
            SEC=int(splitcur[2])+int(splitdif[2])+int(SECP)
            if int(SEC)>60:
                MINP=int(SEC)/60
                SECONDS=int(SEC)-(int(MINP)*60)
            else:
                MINP=0
                SECONDS=int(SEC)

        else:
            SEC=int(splitcur[2])-int(splitdif[2])
            SECONDS=int(SEC)

# MIN == new amount of minues PLUS the MINP carry-over

        if OP == 1:
            MIN=int(splitcur[1])+int(splitdif[1])+int(MINP)
            if int(MIN)>60:
                HRP=int(MIN)/60
                MINUTES=int(MIN)-(int(HRP)*60)
            else:
                HRP=0
                MINUTES=int(MIN)

        else:
            MIN=int(splitcur[1])-int(splitdif[1])
            MINUTES=int(MIN)

        if OP == 1:
            HOURS=int(splitcur[0])+int(splitdif[0])+int(HRP)
        else:
            HOURS=int(splitcur[0])-int(splitdif[0])

# spit out the results for the user
        HH=('%02d' % int(HOURS))
        MM=('%02d' % int(MINUTES))
        SS=('%02d' % int(SECONDS))
        FF=('%02d' % int(FRAMES))
        SPIT=str('%02d:%02d:%02d:%02d' % (int(HH), int(MM), int(SS), int(FF)))
        qalc.totaltime.setText(str(SPIT))

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
