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

import codecs, filecmp, os, subprocess, sys

def main():
    startApplication("sasview")
    mouseClick(waitForObject(":groupBox_6.cbCategory_QComboBox_2"), 144, 11, 0, Qt.LeftButton)
    mouseClick(waitForObjectItem(":groupBox_6.cbCategory_QComboBox_2", "Shape Independent"), 124, 2, 0, Qt.LeftButton)
    mouseClick(waitForObject(":groupBox_6.cbModel_QComboBox_2"), 119, 10, 0, Qt.LeftButton)
    mouseClick(waitForObjectItem(":groupBox_6.cbModel_QComboBox_2", "fractal\\_core\\_shell"), 96, 5, 0, Qt.LeftButton)
    clickButton(waitForObject(":FittingWidgetUI.cmdPlot_QPushButton_2"))
    snooze(2)
    clickButton(waitForObject(":FittingWidgetUI.cmdPlot_QPushButton_2"))
    snooze(3)

    openContextMenu(waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), 160, 186, 0)
            
    activateItem(waitForObjectItem(":MainWindow_QMenu", "M1 [fractal\\_core\\_shell]"))
    activateItem(waitForObjectItem(":M1 [fractal_core_shell]_QMenu", "DataInfo"))
    test.compare(str(waitForObjectExists(":Data Info: M1 [fractal_core_shell]_QTextEdit").windowTitle), "Data Info: M1 [fractal_core_shell]")
    test.compare(str(waitForObjectExists(":Data Info: M1 [fractal_core_shell]_QTextEdit").plainText), "File:            M1 [fractal_core_shell]\nTitle:           M1 [fractal_core_shell]\nRun:             []\nSESANS:          None\nInstrument:      \nSample:\n   ID:           \n   Transmission: None\n   Thickness:    None [mm]\n   Temperature:  None [None]\n   Position:     x = None	y = None	z = None [mm]\n   Orientation:  x = None	y = None	z = None [degree]\n   Details:\n\nSource:\n   Radiation:    None\n   Shape:        None\n   Wavelength:   None [A]\n   Waveln_min:   None [nm]\n   Waveln_max:   None [nm]\n   Waveln_spread:None [percent]\n   Beam_size:    x = None	y = None	z = None [mm]\n\n\nData:\n   Type:         Data1D\n   X-axis:       \\rm{Q}	[A^{-1}]\n   Y-axis:       \\rm{Intensity}	[cm^{-1}]\n   Length:       25\n\nData Min Max:\nX_min = 0.005:  X_max = 0.1\nY_min = 0.00577961925859:  Y_max = 1592.22664984\ndY_min = 0.0:  dY_max = 0.0\n\nData Points:\n<index> 	<X> 	<Y> 	<dY> 	<dX>\n0 	0.005 	1592.22664984 	0.0 	0.0\n1 	0.00895833333333 	1160.4338237 	0.0 	0.0\n2 	0.0129166666667 	832.332829973 	0.0 	0.0\n3 	0.016875 	609.492198923 	0.0 	0.0\n4 	0.0208333333333 	454.848550322 	0.0 	0.0\n5 	0.0247916666667 	341.76757376 	0.0 	0.0\n6 	0.02875 	255.268917083 	0.0 	0.0\n7 	0.0327083333333 	187.358716694 	0.0 	0.0\n8 	0.0366666666667 	133.708192402 	0.0 	0.0\n9 	0.040625 	91.7799634289 	0.0 	0.0\n10 	0.0445833333333 	59.8328101721 	0.0 	0.0\n11 	0.0485416666667 	36.4197740996 	0.0 	0.0\n12 	0.0525 	20.1646142404 	0.0 	0.0\n13 	0.0564583333333 	9.69587397243 	0.0 	0.0\n14 	0.0604166666667 	3.66605410824 	0.0 	0.0\n15 	0.064375 	0.80918605159 	0.0 	0.0\n16 	0.0683333333333 	0.00577961925859 	0.0 	0.0\n17 	0.0722916666667 	0.335328964715 	0.0 	0.0\n18 	0.07625 	1.10554185938 	0.0 	0.0\n19 	0.0802083333333 	1.85484725334 	0.0 	0.0\n20 	0.0841666666667 	2.3305018113 	0.0 	0.0\n21 	0.088125 	2.44856278674 	0.0 	0.0\n22 	0.0920833333333 	2.24402371743 	0.0 	0.0\n23 	0.0960416666667 	1.81962984573 	0.0 	0.0\n24 	0.1 	1.30062869275 	0.0 	0.0\n")
    test.compare(waitForObjectExists(":Data Info: M1 [fractal_core_shell]_QTextEdit").visible, True)
    sendEvent("QCloseEvent", waitForObject(":Data Info: M1 [fractal_core_shell]_QTextEdit"))
    snooze(2)
    #sendEvent("QMouseEvent", waitForObject(":qt_workspacechild_FigureCanvasQTAgg_2"), QEvent.MouseButtonPress, 186, 208, Qt.RightButton, 2, 0)
    sendEvent("QMouseEvent", waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), QEvent.MouseButtonRelease, 186, 208, Qt.RightButton, 0, 0)
    openContextMenu(waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), 186, 208, 0)
    activateItem(waitForObjectItem(":MainWindow_QMenu", "M1 [fractal\\_core\\_shell]"))
    activateItem(waitForObjectItem(":M1 [fractal_core_shell]_QMenu", "Save Points as a File"))
    test.compare(str(waitForObjectExists(":QFileDialog_QFileDialog").windowTitle), "Save As")
    test.compare(waitForObjectExists(":QFileDialog_QFileDialog").visible, True)
    test.compare(str(waitForObjectExists(":fileNameEdit_QLineEdit").text), "M1 [fractal_core_shell]_out.txt")
    test.compare(waitForObjectExists(":QFileDialog.fileTypeCombo_QComboBox").count, 2)
    test.compare(str(waitForObjectExists(":QFileDialog.fileTypeCombo_QComboBox").currentText), "Text files (*.txt)")
    test.compare(waitForObjectExists(":QFileDialog.fileTypeCombo_QComboBox").currentIndex, 0)
    #clickButton(waitForObject(":QFileDialog.Save_QPushButton"))
    #snooze(2)
    #txtfile = os.path.join(os.path.expanduser('~'), 'M1 [fractal_core_shell]_out.txt')
    #txtfile = 'M1 [fractal_core_shell]_out.txt'
    #test.verify(os.path.exists(txtfile), "TXT file saved correctly")
    #os.remove(txtfile)
    clickButton(waitForObject(":QFileDialog.Cancel_QPushButton"))
    
    sendEvent("QMouseEvent", waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), QEvent.MouseButtonRelease, 223, 280, Qt.RightButton, 0, 0)
    openContextMenu(waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), 223, 280, 0)
    activateItem(waitForObjectItem(":MainWindow_QMenu", "M1 [fractal\\_core\\_shell]"))
    activateItem(waitForObjectItem(":M1 [fractal_core_shell]_QMenu", "Save Points as a File"))
    mouseClick(waitForObject(":QFileDialog.fileTypeCombo_QComboBox"), 280, 8, 0, Qt.LeftButton)
    mouseClick(waitForObjectItem(":QFileDialog.fileTypeCombo_QComboBox", "CanSAS 1D files(*\\.xml)"), 265, 4, 0, Qt.LeftButton)
    test.compare(str(waitForObjectExists(":fileNameEdit_QLineEdit").text), "M1 [fractal_core_shell]_out.xml")
    test.compare(str(waitForObjectExists(":QFileDialog.fileTypeCombo_QComboBox").currentText), "CanSAS 1D files(*.xml)")
    #clickButton(waitForObject(":QFileDialog.Save_QPushButton"))
    clickButton(waitForObject(":QFileDialog.Cancel_QPushButton"))

    openContextMenu(waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), 270, 151, 0)
    activateItem(waitForObjectItem(":MainWindow_QMenu", "M1 [fractal\\_core\\_shell]"))
    activateItem(waitForObjectItem(":M1 [fractal_core_shell]_QMenu", "Linear Fit"))
    test.compare(str(waitForObjectExists(":LinearFitUI_LinearFit").windowTitle), "Linear Fit")
    test.compare(waitForObjectExists(":LinearFitUI_LinearFit").visible, True)
    test.compare(str(waitForObjectExists(":groupBox_2.txtRangeMin_QLineEdit").toolTip), "<html><head/><body><p>Minimum value on the x-axis for the plotted data.</p></body></html>")
    test.compare(str(waitForObjectExists(":groupBox_2.txtRangeMin_QLineEdit").text), "0.005")
    test.compare(str(waitForObjectExists(":groupBox_2.txtRangeMax_QLineEdit").text), "0.1")
    test.compare(str(waitForObjectExists(":groupBox_2.txtRangeMax_QLineEdit").toolTip), "<html><head/><body><p>Maximum value on the x-axis for the plotted data.</p></body></html>")
    test.compare(str(waitForObjectExists(":groupBox.txtA_QLineEdit").text), "1")
    test.compare(str(waitForObjectExists(":groupBox.txtA_QLineEdit").toolTip), "<html><head/><body><p>Fit value for the slope parameter.</p></body></html>")
    test.compare(str(waitForObjectExists(":groupBox.txtB_QLineEdit").toolTip), "<html><head/><body><p>Fit value for the constant parameter.</p></body></html>")
    test.compare(str(waitForObjectExists(":groupBox.txtB_QLineEdit").text), "1")
    test.compare(str(waitForObjectExists(":groupBox.txtChi2_QLineEdit").toolTip), "<html><head/><body><p>χ<span style=\" vertical-align:super;\">2</span> over degrees of freedom.</p></body></html>")
    test.compare(str(waitForObjectExists(":groupBox.txtChi2_QLineEdit").text), "0")
    clickButton(waitForObject(":LinearFitUI.cmdClose_QPushButton"))
    
    openContextMenu(waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), 352, 244, 0)
    activateItem(waitForObjectItem(":MainWindow_QMenu", "M1 [fractal\\_core\\_shell]"))
    activateItem(waitForObjectItem(":M1 [fractal_core_shell]_QMenu", "Modify Plot Property"))
    sendEvent("QMoveEvent", waitForObject(":PlotPropertiesUI_PlotProperties"), 745, 331, 937, 348)
    test.compare(waitForObjectExists(":PlotPropertiesUI_PlotProperties").visible, True)
    test.compare(str(waitForObjectExists(":PlotPropertiesUI_PlotProperties").windowTitle), "Modify Plot Properties")
    test.compare(str(waitForObjectExists(":groupBox.cbShape_QComboBox").currentText), "Line")
    test.compare(waitForObjectExists(":groupBox.cbShape_QComboBox").currentIndex, 17)
    test.compare(waitForObjectExists(":groupBox.cbShape_QComboBox").count, 21)
    test.compare(str(waitForObjectExists(":groupBox.qt_spinbox_lineedit_QLineEdit").displayText), "5")
    test.compare(waitForObjectExists(":groupBox.cbColor_QComboBox").count, 7)
    test.compare(str(waitForObjectExists(":groupBox.cbColor_QComboBox").currentText), "Blue")
    test.compare(waitForObjectExists(":groupBox.cbColor_QComboBox").currentIndex, 0)
    test.compare(str(waitForObjectExists(":groupBox.cmdCustom_QPushButton").text), "Custom..")
    test.compare(str(waitForObjectExists(":qt_workspacechild.txtLegend_QLineEdit").text), "M1 [fractal_core_shell]")
    test.compare(waitForObjectExists(":qt_workspacechild.txtLegend_QLineEdit").visible, True)
    mouseClick(waitForObject(":groupBox.cbShape_QComboBox"), 135, 13, 0, Qt.LeftButton)
    mouseClick(waitForObjectItem(":groupBox.cbShape_QComboBox", "Triangle Down"), 116, 5, 0, Qt.LeftButton)
    spinUp(waitForObject(":groupBox.sbSize_QSpinBox"))
    doubleClick(waitForObject(":groupBox.sbSize_QSpinBox"), 33, 6, 0, Qt.LeftButton)
    clickButton(waitForObject(":qt_workspacechild.OK_QPushButton_2"))
    #test.vp("VP1")
    #sendEvent("QMouseEvent", waitForObject(":qt_workspacechild_FigureCanvasQTAgg_2"), QEvent.MouseButtonPress, 230, 166, Qt.RightButton, 2, 0)
    sendEvent("QMouseEvent", waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), QEvent.MouseButtonRelease, 230, 166, Qt.RightButton, 0, 0)
    openContextMenu(waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), 230, 166, 0)
    activateItem(waitForObjectItem(":MainWindow_QMenu", "M1 [fractal\\_core\\_shell]"))
    activateItem(waitForObjectItem(":M1 [fractal_core_shell]_QMenu", "Modify Plot Property"))
    mouseClick(waitForObject(":groupBox.cbShape_QComboBox"), 70, 13, 0, Qt.LeftButton)
    mouseClick(waitForObject(":groupBox.cbShape_QComboBox"), 70, 13, 0, Qt.LeftButton)
    mouseClick(waitForObject(":groupBox.cbShape_QComboBox"), 58, 7, 0, Qt.LeftButton)
    mouseClick(waitForObjectItem(":groupBox.cbShape_QComboBox", "Vline"), 93, 7, 0, Qt.LeftButton)
    mouseClick(waitForObject(":groupBox.cbColor_QComboBox"), 54, 7, 0, Qt.LeftButton)
    mouseClick(waitForObjectItem(":groupBox.cbColor_QComboBox", "Magenta"), 31, 9, 0, Qt.LeftButton)
    mouseDrag(waitForObject(":qt_workspacechild.txtLegend_QLineEdit"), 20, 13, 116, -2, 1, Qt.LeftButton)
    type(waitForObject(":qt_workspacechild.txtLegend_QLineEdit"), "<Backspace>")
    type(waitForObject(":qt_workspacechild.txtLegend_QLineEdit"), "<Backspace>")
    clickButton(waitForObject(":qt_workspacechild.OK_QPushButton_2"))
    #test.vp("VP2")
    
    
    