#gui code for pi calculator
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QApplication, QMainWindow
import sys


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(662, 340)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.startButton = QtGui.QPushButton(self.centralwidget)
        self.startButton.setGeometry(QtCore.QRect(0, 0, 81, 21))
        self.startButton.setObjectName(_fromUtf8("startButton"))
        self.labelPi = QtGui.QLabel(self.centralwidget)
        self.labelPi.setGeometry(QtCore.QRect(10, 50, 66, 17))
        self.labelPi.setObjectName(_fromUtf8("labelPi"))
        self.labelPlaces = QtGui.QLabel(self.centralwidget)
        self.labelPlaces.setGeometry(QtCore.QRect(10, 260, 271, 17))
        self.labelPlaces.setObjectName(_fromUtf8("labelPlaces"))
        self.txtPi = QtGui.QTextBrowser(self.centralwidget)
        self.txtPi.setGeometry(QtCore.QRect(10, 70, 641, 192))
        self.txtPi.setObjectName(_fromUtf8("txtPi"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 662, 25))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.startButton.clicked.connect(self.calcpi)
        
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.startButton.setText(_translate("MainWindow", "Start", None))
        self.labelPi.setText(_translate("MainWindow", "Pi:", None))
        self.labelPlaces.setText(_translate("MainWindow", "Pi has been calculated to 0 places", None))
   

    def calcpi(self, MainWindow):
        
        n = 100000
        length = int(n) / 3
        pi = ""
        a = []
        k = 0L
        x = 0L
        txtDigits = 0
        txtDec = ""
        txtPi = ""
        nines = 0L
        

        for j in range(0, int(length)): 
            a.append(2)

        nines = 0L
        predigit = 0 
        j = 0

        for j in range(0, int(n)):
            txtDec = j
            q = 0L
            for i in range(int(length), 0, - 1):
                x = 10 * a[i - 1] + q * i
                a[i - 1] = x % (2 * i - 1)
                q = x / (2 * i - 1)
            a[1 - 1] = q % 10
            q = q / 10
            if q == 9:
                nines += 1
            elif q == 10:
                txtDigits = (predigit + 1) 
                pi = pi + str(txtDigits) 
                self.txtPi.setText(pi) 
                self.labelPlaces.setText(_translate("MainWindow", "Pi has been calculated to " +str(len(pi))+ " places", None))
                self.txtPi.verticalScrollBar().setValue(
                    self.txtPi.verticalScrollBar().maximum())
                QtGui.QApplication.processEvents()
                for k in range(1, nines):
                    txtDigits = 0 
                    pi = pi + str(txtDigits) 
                    self.txtPi.setText(pi)           
                    self.labelPlaces.setText(_translate("MainWindow", "Pi has been calculated to " +str(len(pi))+ " places", None))
                    self.txtPi.verticalScrollBar().setValue(
                        self.txtPi.verticalScrollBar().maximum())
                    QtGui.QApplication.processEvents()
                predigit = 0
                nines = 0 
            else: 
                txtDigits = predigit
                pi = pi + str(txtDigits)
                self.txtPi.setText(pi) 
                self.labelPlaces.setText(_translate("MainWindow", "Pi has been calculated to " +str(len(pi))+ " places", None))
                self.txtPi.verticalScrollBar().setValue(
                    self.txtPi.verticalScrollBar().maximum())
                QtGui.QApplication.processEvents()
                predigit = q
                if nines != 0:
                    for k in range(0, nines):
                        txtDigits = 9
                        pi = pi + str(txtDigits)
                        self.txtPi.setText(pi) 
                        self.labelPlaces.setText(_translate("MainWindow", "Pi has been calculated to " +str(len(pi))+ " places", None))
                        self.txtPi.verticalScrollBar().setValue(
                            self.txtPi.verticalScrollBar().maximum())
                        QtGui.QApplication.processEvents()
                    nines = 0

def main():
    app = QApplication(sys.argv)
    window = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(window)
    
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
	main()