"""
:platform: Unix, Windows
:synopsis: This module is mostly generated code from QtDesigner. It describes the format of the settings
    dialog for editing the limits of the plot axes.
:moduleauthor: <NAME> <<EMAIL>>
"""

from PyQt5 import QtCore, QtGui, QtWidgets
from settings_interface import set_x_axis_size, get_x_axis_size, set_y_axis_min, get_y_axis_min, set_y_axis_max, get_y_axis_max


class Ui_Dialog(object):
    """
    The graphical structure of this *Ui_Dialog* was generated by QtDesigner.
    The window allows the user to set the limits of the plot axes.
    """

    def setupUi(self, Dialog):
        """
        This function initializes the window by altering the *Dialog* object passed (by reference) to it.

        :param Dialog: This must be of type PyQt5.QtWidgets.QDialog.
        """

        # set window name, icon
        Dialog.setObjectName("Settings - Scale Axes")
        Dialog.setWindowIcon(QtGui.QIcon('resources/gear.ico'))

        # make a grid layout
        # grid layouts allow the window to be resized easily
        self.gridLayout = QtWidgets.QGridLayout(Dialog)
        self.gridLayout.setObjectName("gridLayout")

        # create y-axis maximum input box
        self.yaxis_max_input = QtWidgets.QDoubleSpinBox(Dialog)
        self.yaxis_max_input.setMaximum(70)
        self.yaxis_max_input.setMinimum(1)
        self.yaxis_max_input.setSingleStep(1)
        ymax = float(get_y_axis_max())
        self.yaxis_max_input.setValue(ymax)
        self.yaxis_max_input.setObjectName("yaxis_max_input")
        self.gridLayout.addWidget(self.yaxis_max_input, 8, 0, 1, 1)

        # create y-axis minimum input box
        self.yaxis_min_input = QtWidgets.QDoubleSpinBox(Dialog)
        self.yaxis_min_input.setMaximum(69.9)
        self.yaxis_min_input.setMinimum(-60)
        self.yaxis_min_input.setSingleStep(1)
        ymin = float(get_y_axis_min())
        self.yaxis_min_input.setValue(ymin)
        self.yaxis_min_input.setObjectName("yaxis_min_input")
        self.gridLayout.addWidget(self.yaxis_min_input, 6, 0, 1, 1)

        # the following spacer items organize the objects in the window nicely
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem, 3, 3, 1, 2)
        spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem1, 9, 0, 1, 1)
        spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem2, 4, 0, 1, 1)
        spacerItem3 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem3, 0, 0, 1, 1)
        spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem4, 8, 3, 1, 2)
        spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem5, 12, 0, 1, 1)
        spacerItem6 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem6, 6, 3, 1, 2)

        # create a labels for the input boxes
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 2, 0, 1, 1)
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 5, 0, 1, 1)
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 7, 0, 1, 1)

        # create the box for the x-axis size input
        self.xaxis_scale_input = QtWidgets.QSpinBox(Dialog)
        self.xaxis_scale_input.setMinimum(5)
        self.xaxis_scale_input.setMaximum(3600)
        self.xaxis_scale_input.setSingleStep(1)
        xsize = int(get_x_axis_size())
        self.xaxis_scale_input.setValue(xsize)
        self.xaxis_scale_input.setObjectName("xaxis_scale_input")
        self.gridLayout.addWidget(self.xaxis_scale_input, 3, 0, 1, 1)

        # create a save and cancel button, these are built in buttons that emit specific signals
        self.settings_axis_scale_buttons = QtWidgets.QDialogButtonBox(Dialog)
        self.settings_axis_scale_buttons.setOrientation(QtCore.Qt.Horizontal)
        self.settings_axis_scale_buttons.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Save)
        self.settings_axis_scale_buttons.setObjectName("settings_axis_scale_buttons")
        self.gridLayout.addWidget(self.settings_axis_scale_buttons, 12, 4, 1, 1)

        # move the objects over to Dialog and set the text
        self.retranslateUi(Dialog)

        # set what to do when the save button is clicked
        self.settings_axis_scale_buttons.accepted.connect(lambda: self.save(Dialog))

        # set what to do when the cancel button is hit
        self.settings_axis_scale_buttons.rejected.connect(Dialog.reject)

        # connect the signals and slots by name
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        """
        This function translates the GUI objects of this class to the passed *Dialog* objcect,
        then sets the text of the various objects in the window.

        :param Dialog: This must be of type PyQt5.QtWidgets.QDialog.
        """

        # rename the function for readability
        _translate = QtCore.QCoreApplication.translate

        # set the window title and label text
        Dialog.setWindowTitle(_translate("Dialog", "Settings - Scale Axes"))
        self.label_2.setText(_translate("Dialog", "Y-Axis, Min."))
        self.label.setText(_translate("Dialog", "X-Axis, total time in seconds that the window will show."))
        self.label_3.setText(_translate("Dialog", "Y-Axis, Max."))

    def save(self, Dialog):
        """
        This function saves the inputs and closes the window.

        :param Dialog: This must be of type PyQt5.QtWidgets.QDialog.
        """

        # grab the values from each input box
        xsize = self.xaxis_scale_input.value()
        ymin = self.yaxis_min_input.value()
        ymax = self.yaxis_max_input.value()

        # save them
        set_x_axis_size(xsize)
        set_y_axis_min(ymin)
        set_y_axis_max(ymax)

        # close the window
        Dialog.accept()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
