<reponame>cryos/qmicroscope
import sys

from qtpy.QtCore import (
    QPoint,
    QSettings,
    QSize,
)

from qtpy.QtWidgets import (
    QCheckBox,
    QLineEdit,
    QPushButton,
    QApplication,
    QVBoxLayout,
    QHBoxLayout,
    QFormLayout,
    QSpinBox,
    QMainWindow,
    QWidget,
)

from microscope.microscope import Microscope
from microscope.container import Container
from microscope.settings import Settings

class Form(QMainWindow):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        # Create widgets
        self.setWindowTitle("NSLS-II Microscope Widget")
        self.container = Container(self)
        self.container.count = 3
        self.container.size = [2, 2]
        self.microscope = self.container.microscope(0)
        #self.microscope = Microscope(self)

        self.startButton = QPushButton('Start')
        self.settingsButton = QPushButton('Settings')

        # Create layout and add widgets
        layout = QVBoxLayout()
        layout.addWidget(self.container)
        hButtonBox = QHBoxLayout()
        hButtonBox.addStretch()
        hButtonBox.addWidget(self.startButton)
        hButtonBox.addWidget(self.settingsButton)
        hButtonBox.addStretch()
        layout.addLayout(hButtonBox)

        # Set main windows widget using our central layout
        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

        # Add button signal to slot to start/stop
        self.startButton.clicked.connect(self.startButtonPressed)
        self.settingsButton.clicked.connect(self.settingsButtonClicked)

        # Connect to the microscope ROI clicked signal
        self.microscope.roiClicked.connect(self.onRoiClicked)

        # Read the settings and persist them
        settings = QSettings()
        self.readSettings(settings)

        self.settingsDialog = Settings(self)
        self.settingsDialog.setContainer(self.container)

    # event : QCloseEvent
    def closeEvent(self, event):
        settings = QSettings()
        self.writeSettings(settings)
        event.accept()

    def startButtonPressed(self):
        # Currently being a little lame - only update state on start/stop.
        print('Button pressed!', self.startButton.text())
        if self.startButton.text() == 'Start':
            self.container.start(True)
            self.startButton.setText('Stop')
        else:
            self.container.start(False)
            self.startButton.setText('Start')

    def settingsButtonClicked(self):
        # Open the settings dialog.
        self.settingsDialog.show()

    def onRoiClicked(self, x, y):
        print(f'ROI: {x}, {y}')

    def readSettings(self, settings):
        """ Load the application's settings. """
        settings.beginGroup('MainWindow')
        self.resize(settings.value('size', QSize(400, 400)))
        self.move(settings.value('pos', QPoint(200, 200)))
        self.container.readSettings(settings)
        settings.endGroup()

    def writeSettings(self, settings):
        """ Save the applications's settings persistently. """
        settings.beginGroup('MainWindow')
        settings.setValue('size', self.size())
        settings.setValue('pos', self.pos())
        self.container.writeSettings(settings)
        settings.endGroup()


if __name__ == '__main__':
    # Set up some application basics for saving settings
    QApplication.setOrganizationName('BNL')
    QApplication.setOrganizationDomain('bnl.gov')
    QApplication.setApplicationName('QCamera')

    # Create the Qt Application
    app = QApplication(sys.argv)

    # Create and show the form
    form = Form()
    form.show()

    # Run the main Qt loop
    sys.exit(app.exec_())

import sys

from qtpy.QtCore import (
    QPoint,
    QSettings,
    QSize,
)

from qtpy.QtWidgets import (
    QCheckBox,
    QLineEdit,
    QPushButton,
    QApplication,
    QVBoxLayout,
    QHBoxLayout,
    QFormLayout,
    QSpinBox,
    QMainWindow,
    QWidget,
)

from microscope.microscope import Microscope
from microscope.container import Container
from microscope.settings import Settings

class Form(QMainWindow):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        # Create widgets
        self.setWindowTitle("NSLS-II Microscope Widget")
        self.container = Container(self)
        self.container.count = 3
        self.container.size = [2, 2]
        self.microscope = self.container.microscope(0)
        #self.microscope = Microscope(self)

        self.startButton = QPushButton('Start')
        self.settingsButton = QPushButton('Settings')

        # Create layout and add widgets
        layout = QVBoxLayout()
        layout.addWidget(self.container)
        hButtonBox = QHBoxLayout()
        hButtonBox.addStretch()
        hButtonBox.addWidget(self.startButton)
        hButtonBox.addWidget(self.settingsButton)
        hButtonBox.addStretch()
        layout.addLayout(hButtonBox)

        # Set main windows widget using our central layout
        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

        # Add button signal to slot to start/stop
        self.startButton.clicked.connect(self.startButtonPressed)
        self.settingsButton.clicked.connect(self.settingsButtonClicked)

        # Connect to the microscope ROI clicked signal
        self.microscope.roiClicked.connect(self.onRoiClicked)

        # Read the settings and persist them
        settings = QSettings()
        self.readSettings(settings)

        self.settingsDialog = Settings(self)
        self.settingsDialog.setContainer(self.container)

    # event : QCloseEvent
    def closeEvent(self, event):
        settings = QSettings()
        self.writeSettings(settings)
        event.accept()

    def startButtonPressed(self):
        # Currently being a little lame - only update state on start/stop.
        print('Button pressed!', self.startButton.text())
        if self.startButton.text() == 'Start':
            self.container.start(True)
            self.startButton.setText('Stop')
        else:
            self.container.start(False)
            self.startButton.setText('Start')

    def settingsButtonClicked(self):
        # Open the settings dialog.
        self.settingsDialog.show()

    def onRoiClicked(self, x, y):
        print(f'ROI: {x}, {y}')

    def readSettings(self, settings):
        """ Load the application's settings. """
        settings.beginGroup('MainWindow')
        self.resize(settings.value('size', QSize(400, 400)))
        self.move(settings.value('pos', QPoint(200, 200)))
        self.container.readSettings(settings)
        settings.endGroup()

    def writeSettings(self, settings):
        """ Save the applications's settings persistently. """
        settings.beginGroup('MainWindow')
        settings.setValue('size', self.size())
        settings.setValue('pos', self.pos())
        self.container.writeSettings(settings)
        settings.endGroup()


if __name__ == '__main__':
    # Set up some application basics for saving settings
    QApplication.setOrganizationName('BNL')
    QApplication.setOrganizationDomain('bnl.gov')
    QApplication.setApplicationName('QCamera')

    # Create the Qt Application
    app = QApplication(sys.argv)

    # Create and show the form
    form = Form()
    form.show()

    # Run the main Qt loop
    sys.exit(app.exec_())
# Create widgets #self.microscope = Microscope(self) # Create layout and add widgets # Set main windows widget using our central layout # Add button signal to slot to start/stop # Connect to the microscope ROI clicked signal # Read the settings and persist them # event : QCloseEvent # Currently being a little lame - only update state on start/stop. # Open the settings dialog. Load the application's settings. Save the applications's settings persistently. # Set up some application basics for saving settings # Create the Qt Application # Create and show the form # Run the main Qt loop