#!/usr/bin/env python

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import default_config as config
import numpy as np

#only modifies the values of default_config for the imported session, maybe add a possibility to save/load mostly used configs
class AskWidget(QWidget):  
    
    def __init__(self):
        super(AskWidget, self).__init__()
        
        self.initUI()
        
    def initUI(self):
        
        # Buttons/elements
        self.inputPrecision = QSpinBox(self)
        self.inputPrecision.setWrapping(True)
        self.inputPrecision.setValue(config.Tracking_inputPrecision)
        self.ipLabel = QLabel("User input precision", self)

        self.integrationWindowRadius = QSpinBox(self)
        self.integrationWindowRadius.setWrapping(True)
        self.integrationWindowRadius.setValue(config.Tracking_minWindowSize)
        self.iwrLabel = QLabel("Minimun radius of the integration window", self)

        self.validationRegionSize = QSpinBox(self)
        self.validationRegionSize.setWrapping(True)
        self.validationRegionSize.setValue(config.Tracking_gamma)
        self.vrsLabel = QLabel("Size of the validation region", self)

        self.determinationCoefficient = QDoubleSpinBox(self)
        self.determinationCoefficient.setWrapping(True)
        self.determinationCoefficient.setSingleStep(0.01)
        self.determinationCoefficient.setValue(config.Tracking_minRsq)
        self.dcLabel = QLabel("Minimal coefficient of determinating R^2 for fit", self)

        self.integrationWindowScale = QCheckBox("Scale integration window with changing energy")
        self.integrationWindowScale.setChecked(config.Tracking_windowScalingOn)

        self.backgroundSubstraction = QCheckBox("Background substraction")
        self.backgroundSubstraction.setChecked(config.Processing_backgroundSubstractionOn)

        self.spotIdentification = QComboBox(self)
        self.spotIdentification.addItem("guess_from_Gaussian")
        self.siLabel = QLabel("Spot indentification algorithm", self)


        self.fnLabel = QLabel("Kalman tracker process noise", self)
        self.text = QLabel("Set the diagonal values for 4x4 matrix:", self)
        self.value1 = QLineEdit(self)
        self.value1.setText(str(config.Tracking_processNoise.diagonal()[0]))
        self.value2 = QLineEdit(self)
        self.value2.setText(str(config.Tracking_processNoise.diagonal()[1]))
        self.value3 = QLineEdit(self)
        self.value3.setText(str(config.Tracking_processNoise.diagonal()[2]))
        self.value4 = QLineEdit(self)
        self.value4.setText(str(config.Tracking_processNoise.diagonal()[3]))



        self.wrongLabel = QLabel("Set values correctly", self)
        self.acceptButton = QPushButton('&Accept', self)
        self.cancelButton = QPushButton('&Cancel', self)

        #Layouts
        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle('Set tracking parameters')
     
        #base grid
        self.gridLayout = QGridLayout()
        self.setLayout(self.gridLayout)

        #1st (left) vertical layout
        #adding items
        self.lvLayout = QVBoxLayout()
        self.lvLayout.addWidget(self.ipLabel)
        self.lvLayout.addWidget(self.inputPrecision)
        self.lvLayout.addWidget(self.iwrLabel)
        self.lvLayout.addWidget(self.integrationWindowRadius)
        self.lvLayout.addWidget(self.vrsLabel)
        self.lvLayout.addWidget(self.validationRegionSize)
        self.lvLayout.addWidget(self.dcLabel)
        self.lvLayout.addWidget(self.determinationCoefficient)

        #2nd (right) vertical layout
        #adding items
        self.rvLayout = QVBoxLayout()
        self.rvLayout.addWidget(self.integrationWindowScale)
        self.rvLayout.addWidget(self.backgroundSubstraction)
        self.rvLayout.addWidget(self.siLabel)
        self.rvLayout.addWidget(self.spotIdentification)

        #3rd (process noise) vertical layout
        self.vpLayout = QVBoxLayout()
        self.hpLayout = QHBoxLayout()
        self.vpLayout.addWidget(self.fnLabel)
        self.vpLayout.addWidget(self.text)
        self.hpLayout.addWidget(self.value1)
        self.hpLayout.addWidget(self.value2)
        self.hpLayout.addWidget(self.value3)
        self.hpLayout.addWidget(self.value4)

        #horizontal layout
        #adding items
        self.hLayout = QHBoxLayout()
        self.hLayout.addWidget(self.wrongLabel)
        self.hLayout.addWidget(self.acceptButton)
        self.hLayout.addWidget(self.cancelButton)

        #adding layouts to the grid
        self.gridLayout.addLayout(self.lvLayout, 0, 0)
        self.gridLayout.addLayout(self.rvLayout, 0, 1)
        self.gridLayout.addLayout(self.vpLayout, 4, 0)
        self.gridLayout.addLayout(self.hpLayout, 4, 1)
        self.gridLayout.addLayout(self.hLayout, 5, 1)

        QObject.connect(self.acceptButton, SIGNAL("clicked()"), self.acceptParameters)
        QObject.connect(self.cancelButton, SIGNAL("clicked()"), self.close)

    def acceptParameters(self):
        config.Tracking_inputPrecision = self.inputPrecision.value()
        config.Tracking_windowScalingOn = self.integrationWindowScale.isChecked()
        config.Tracking_minWindowSize = self.integrationWindowRadius.value()
        config.Tracking_guessFunc = self.spotIdentification.currentText()
        config.Tracking_gamma = self.validationRegionSize.value()
        config.Tracking_minRsq = self.determinationCoefficient.value()
        config.Processing_backgroundSubstractionOn = self.backgroundSubstraction.isChecked()
        try:
            self.noiseList = [float(self.value1.text()), float(self.value2.text()), float(self.value3.text()), float(self.value4.text())]
            config.Tracking_processNoise = np.diag(self.noiseList)
            self.close()
        except ValueError:
            self.wrongLabel.setText("Invalid process noise value")




        
def main():
    
    app = QApplication(sys.argv)
    ex = AskWidget()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()    
