# main.py
# ================================================================================
# BOOST SOFTWARE LICENSE
#
# Copyright 2020 BitWise Laboratories Inc.
# Original Author.......<NAME>
# Contact...............<EMAIL>
#
# Permission is hereby granted, free of charge, to any person or organization
# obtaining a copy of the software and accompanying documentation covered by
# this license (the "Software") to use, reproduce, display, distribute,
# execute, and transmit the Software, and to prepare derivative works of the
# Software, and to permit third-parties to whom the Software is furnished to
# do so, all subject to the following:
#
# The copyright notices in the Software and this entire statement, including
# the above license grant, this restriction and the following disclaimer,
# must be included in all copies of the Software, in whole or in part, and
# all derivative works of the Software, unless such copies or derivative
# works are solely in the form of machine-executable object code generated by
# a source language processor.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# ================================================================================
import os

from PyQt5.QtWidgets import QMainWindow

from popup import Popup
from ui_SS import Ui_SS
from connect import Connect

class Main(Ui_SS):
    def __init__(self, mainWindow: QMainWindow):
        super().__init__()
        # print("Main::__init__()")

        self.ParentWindow = mainWindow
        self.setupUi(mainWindow)
        self.statusbar.hide()

        # initialize gui variables
        self.editSerialNumber.setText("Not connected")
        self.editSerialNumber.setReadOnly(True)

        # Establish event handlers
        self.buttonConnect.clicked.connect(self.buttonConnect_clicked)

        # Initialize widgets as disabled since not connected
        self.setConnectionDependentEnables(False)

    def buttonConnect_clicked(self):
        # print("Main::buttonConnect_clicked")

        try:
            if Connect.getConnected():
                Connect.Disconnect()
            else:
                Connect.Connect()

                self.editSerialNumber.setText("S/N " + Connect.getDevice().Const.getSN())
                self.refreshResults(0xff)

        except Exception as e:
            Popup.error(e)

        finally:
            self.editIPAddress.setEnabled(not Connect.getConnected())
            self.editSerialNumber.setEnabled(True if Connect.getConnected() else False)
            self.buttonConnect.setText("Disconnect" if Connect.getConnected() else "Connect")
            self.setConnectionDependentEnables(True if Connect.getConnected() else False)

            if not Connect.getConnected():
                self.editSerialNumber.setText("Not connected")
            pass
        pass

    def setConnectionDependentEnables(self, newValue: bool):
        # print("Main::setConnectionDependentEnables, newValue=", newValue)

        self.editTDRShortCalib.setEnabled(newValue)
        self.editTDRTermCalib.setEnabled(newValue)

    def ensureDirectoriesExist(self):
        # print("Main::ensureDirectoriesExist")

        try:
            if len(self.editDataDirectory.text().strip()) == 0:
                raise Exception("Missing a Data Director")

            if not os.path.exists(os.path.normpath(self.editDataDirectory.text().strip())):
                os.mkdir(os.path.normpath(self.editDataDirectory.text().strip()))

            configurationSubdir = os.path.normpath(self.editDataDirectory.text().strip() + "/_Configurations")
            if not os.path.exists(configurationSubdir):
                os.mkdir(configurationSubdir)

        except Exception as e:
            print("Problem ensuring directories exist: ", e)
            raise e

    def refreshResults(self, flag: int):
        # print("Main::refreshResults()")
        pass

# EOF

# main.py
# ================================================================================
# BOOST SOFTWARE LICENSE
#
# Copyright 2020 BitWise Laboratories Inc.
# Original Author.......<NAME>
# Contact...............<EMAIL>
#
# Permission is hereby granted, free of charge, to any person or organization
# obtaining a copy of the software and accompanying documentation covered by
# this license (the "Software") to use, reproduce, display, distribute,
# execute, and transmit the Software, and to prepare derivative works of the
# Software, and to permit third-parties to whom the Software is furnished to
# do so, all subject to the following:
#
# The copyright notices in the Software and this entire statement, including
# the above license grant, this restriction and the following disclaimer,
# must be included in all copies of the Software, in whole or in part, and
# all derivative works of the Software, unless such copies or derivative
# works are solely in the form of machine-executable object code generated by
# a source language processor.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# ================================================================================
import os

from PyQt5.QtWidgets import QMainWindow

from popup import Popup
from ui_SS import Ui_SS
from connect import Connect

class Main(Ui_SS):
    def __init__(self, mainWindow: QMainWindow):
        super().__init__()
        # print("Main::__init__()")

        self.ParentWindow = mainWindow
        self.setupUi(mainWindow)
        self.statusbar.hide()

        # initialize gui variables
        self.editSerialNumber.setText("Not connected")
        self.editSerialNumber.setReadOnly(True)

        # Establish event handlers
        self.buttonConnect.clicked.connect(self.buttonConnect_clicked)

        # Initialize widgets as disabled since not connected
        self.setConnectionDependentEnables(False)

    def buttonConnect_clicked(self):
        # print("Main::buttonConnect_clicked")

        try:
            if Connect.getConnected():
                Connect.Disconnect()
            else:
                Connect.Connect()

                self.editSerialNumber.setText("S/N " + Connect.getDevice().Const.getSN())
                self.refreshResults(0xff)

        except Exception as e:
            Popup.error(e)

        finally:
            self.editIPAddress.setEnabled(not Connect.getConnected())
            self.editSerialNumber.setEnabled(True if Connect.getConnected() else False)
            self.buttonConnect.setText("Disconnect" if Connect.getConnected() else "Connect")
            self.setConnectionDependentEnables(True if Connect.getConnected() else False)

            if not Connect.getConnected():
                self.editSerialNumber.setText("Not connected")
            pass
        pass

    def setConnectionDependentEnables(self, newValue: bool):
        # print("Main::setConnectionDependentEnables, newValue=", newValue)

        self.editTDRShortCalib.setEnabled(newValue)
        self.editTDRTermCalib.setEnabled(newValue)

    def ensureDirectoriesExist(self):
        # print("Main::ensureDirectoriesExist")

        try:
            if len(self.editDataDirectory.text().strip()) == 0:
                raise Exception("Missing a Data Director")

            if not os.path.exists(os.path.normpath(self.editDataDirectory.text().strip())):
                os.mkdir(os.path.normpath(self.editDataDirectory.text().strip()))

            configurationSubdir = os.path.normpath(self.editDataDirectory.text().strip() + "/_Configurations")
            if not os.path.exists(configurationSubdir):
                os.mkdir(configurationSubdir)

        except Exception as e:
            print("Problem ensuring directories exist: ", e)
            raise e

    def refreshResults(self, flag: int):
        # print("Main::refreshResults()")
        pass

# EOF

# main.py # ================================================================================ # BOOST SOFTWARE LICENSE # # Copyright 2020 BitWise Laboratories Inc. # Original Author.......<NAME> # Contact...............<EMAIL> # # Permission is hereby granted, free of charge, to any person or organization # obtaining a copy of the software and accompanying documentation covered by # this license (the "Software") to use, reproduce, display, distribute, # execute, and transmit the Software, and to prepare derivative works of the # Software, and to permit third-parties to whom the Software is furnished to # do so, all subject to the following: # # The copyright notices in the Software and this entire statement, including # the above license grant, this restriction and the following disclaimer, # must be included in all copies of the Software, in whole or in part, and # all derivative works of the Software, unless such copies or derivative # works are solely in the form of machine-executable object code generated by # a source language processor. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. # ================================================================================ # print("Main::__init__()") # initialize gui variables # Establish event handlers # Initialize widgets as disabled since not connected # print("Main::buttonConnect_clicked") # print("Main::setConnectionDependentEnables, newValue=", newValue) # print("Main::ensureDirectoriesExist") # print("Main::refreshResults()") # EOF