# -*- coding: utf-8 -*-
import codecs, filecmp, os, subprocess, sys

def main():
    startApplication("sasview")
    mouseClick(waitForObject(":MainWindow.Data Explorer_QWorkspace"), 676, 556, 0, Qt.LeftButton)
    mouseClick(waitForObject(":groupBox_6.cbCategory_QComboBox_2"), 83, 13, 0, Qt.LeftButton)
    mouseClick(waitForObjectItem(":groupBox_6.cbCategory_QComboBox_2", "Cylinder"), 79, 6, 0, Qt.LeftButton)
    clickButton(waitForObject(":FittingWidgetUI.cmdPlot_QPushButton_2"))
    snooze(1)
    clickButton(waitForObject(":FittingWidgetUI.cmdPlot_QPushButton_2"))
    snooze(2)
    openContextMenu(waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), 287, 212, 0)
    activateItem(waitForObjectItem(":MainWindow_QMenu", "Save Image"))
    test.compare(waitForObjectExists(":QFileDialog.fileTypeCombo_QComboBox").currentIndex, 4)
    test.compare(waitForObjectExists(":QFileDialog.fileTypeCombo_QComboBox").count, 9)
    test.compare(str(waitForObjectExists(":QFileDialog.fileTypeCombo_QComboBox").currentText), "Portable Network Graphics (*.png)")
    test.compare(waitForObjectExists(":qt_workspacechild.Save_QPushButton").visible, True)
    test.compare(waitForObjectExists(":qt_workspacechild.Save_QPushButton").default, True)
    test.compare(str(waitForObjectExists(":qt_workspacechild.Save_QPushButton").text), "&Save")
    test.compare(str(waitForObjectExists(":fileNameEdit_QLineEdit").displayText), "image.png")
    test.compare(str(waitForObjectExists(":fileNameEdit_QLineEdit").text), "image.png")
    clickButton(waitForObject(":qt_workspacechild.Save_QPushButton"))
    snooze(1)
    pngfile = os.path.join(os.path.expanduser('~'), 'image.png')
    test.verify(os.path.exists(pngfile), "PNG file saved correctly")
    os.remove(pngfile)
    
    sendEvent("QMouseEvent", waitForObject(":qt_workspacechild_FigureCanvasQTAgg_2"), QEvent.MouseButtonPress, 153, 294, Qt.RightButton, 2, 0)
    sendEvent("QMouseEvent", waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), QEvent.MouseButtonRelease, 153, 294, Qt.RightButton, 0, 0)
    openContextMenu(waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), 153, 294, 0)
    activateItem(waitForObjectItem(":MainWindow_QMenu", "Save Image"))
    mouseClick(waitForObject(":QFileDialog.fileTypeCombo_QComboBox"), 304, 7, 0, Qt.LeftButton)
    mouseClick(waitForObjectItem(":QFileDialog.fileTypeCombo_QComboBox", "Encapsulated Postscript (*\\.eps)"), 299, 7, 0, Qt.LeftButton)
    clickButton(waitForObject(":qt_workspacechild.Save_QPushButton"))
    snooze(1)
    epsfile = os.path.join(os.path.expanduser('~'), 'image.eps')
    test.verify(os.path.exists(epsfile), "EPS file saved correctly")
    os.remove(epsfile)
    
    sendEvent("QMouseEvent", waitForObject(":qt_workspacechild_FigureCanvasQTAgg_2"), QEvent.MouseButtonPress, 138, 298, Qt.RightButton, 2, 0)
    sendEvent("QMouseEvent", waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), QEvent.MouseButtonRelease, 138, 298, Qt.RightButton, 0, 0)
    openContextMenu(waitForObject(":qt_workspacechild_FigureCanvasQTAgg"), 138, 298, 0)
    activateItem(waitForObjectItem(":MainWindow_QMenu", "Save Image"))
    mouseClick(waitForObject(":QFileDialog.fileTypeCombo_QComboBox"), 333, 8, 0, Qt.LeftButton)
    mouseClick(waitForObjectItem(":QFileDialog.fileTypeCombo_QComboBox", "Joint Photographic Experts Group (*\\.jpeg *\\.jpg)"), 335, 10, 0, Qt.LeftButton)
    clickButton(waitForObject(":qt_workspacechild.Save_QPushButton"))
    snooze(1)
    jpgfile = os.path.join(os.path.expanduser('~'), 'image.jpeg')
    test.verify(os.path.exists(jpgfile), "JPEG file saved correctly")
    os.remove(jpgfile)