Esempio n. 1
0
File: gui.py Progetto: cbig/zupport
    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowIcon(get_icon(os.path.join(APP_RESOURCES, 'icons',
                                                  'python.png')))
        self.setupUi(self)
        
        # Redirect output to GUI's QTextEdit
        sys.stdout = OutLog(self.outputTextEdit, sys.stdout)
        sys.stderr = OutLog(self.outputTextEdit, sys.stderr, QColor(255,0,0) )
        
        settings = QSettings()
        size = settings.value("MainWindow/Size",
                              QVariant(QSize(600, 500))).toSize()
        self.resize(size)
        position = settings.value("MainWindow/Position",
                                  QVariant(QPoint(0, 0))).toPoint()
        self.move(position)
        self.restoreState(
        settings.value("MainWindow/State").toByteArray())
    
        self.logger = Logger('Zupport.GUIloader')
        self.logger.debugging = settings.value("Logging/debugging").toBool()
         
        # Set up a ZupportManager to deal with the plugins
        self.manager = Manager(self)
        self.plugins = {}

        # Set up the extents menu
        self.extent = ExtentContainer()
        self.menuExtent = QMenu("Extent")
        resolutions = self.extent.resolutions
        self.extenactiongroup = QActionGroup(self.menuExtent)  
        noneaction = QAction("None", self.menuExtent)
        self.extenactiongroup.addAction(noneaction)
        self.menuExtent.addAction(noneaction)
        for resolution in resolutions:
            resolution_text = str(resolution)
            submenu = self.menuExtent.addMenu(resolution_text) 
            self.menuExtent.addMenu(submenu)
            for area in self.extent.get_names(resolution):
                subaction = QAction(str(area) + ": %s" % resolution_text, 
                                    submenu)
                subaction.setCheckable(True)
                self.extenactiongroup.addAction(subaction)
                submenu.addAction(subaction)
        
        noneaction.isChecked()
        self.menuSettings.addMenu(self.menuExtent)
        
        self.actionDebugging_messages.setChecked(self.logger.debugging)

        self.setWindowTitle("Zupport GUI")
        
        self.load_tools()
        
        self.toolTreeWidget.itemSelectionChanged.connect(self.update_ui)
        self.actionDebugging_messages.toggled.connect(self._set_debugging)
        self.menuExtent.triggered.connect(self.update_ui)
        self.actionLoad_tool.triggered.connect(self.show_tool_gui)
        self.toolTreeWidget.doubleClicked.connect(self.show_tool_gui)
Esempio n. 2
0
 def closeEvent(self, event):
     settings = QSettings()
     settings.setValue("Logging/debugging", self.logger.debugging)
     settings.setValue("MainWindow/Size", QVariant(self.size()))
     settings.setValue("MainWindow/Position",
                       QVariant(self.pos()))
     settings.setValue("MainWindow/State",
                       QVariant(self.saveState()))
Esempio n. 3
0
 def __init__(self, reset=False, start_freq=None,
                     bandwidth=None, numpts=None, max_hold=None):
     QMainWindow.__init__(self)
     self.settings = QSettings("Darkstar007", "signal_generator")
     if reset:
         self.settings.clear()
         
     self.setup(start_freq, bandwidth, numpts, max_hold)
Esempio n. 4
0
File: gui.py Progetto: cbig/zupport
 def closeEvent(self, event):
     settings = QSettings()
     settings.setValue("Logging/debugging", self.logger.debugging)
     settings.setValue("MainWindow/Size", QVariant(self.size()))
     settings.setValue("MainWindow/Position",
                       QVariant(self.pos()))
     settings.setValue("MainWindow/State",
                       QVariant(self.saveState()))
Esempio n. 5
0
    def __init__(self, reset=False, start_freq=None,
                 bandwidth=None, numpts=None, max_hold=None,
                 dev='/dev/ttyUSB0'):
        QMainWindow.__init__(self)
        self.settings = QSettings("Darkstar007", "networkanalyser")
        if reset:
            self.settings.clear()

        self.file_dir = self.settings.value('spectrum/file_dir', os.getenv('HOME'))
        print 'File dir', self.file_dir
        self.dev = dev

        self.setup(start_freq, bandwidth, numpts, max_hold)
Esempio n. 6
0
    def _load_settings(self):
        self.appsettings = DataSetShowGroupBox("Settings",
                                             AppSettings, comment='',
                                             title=_("Application settings"))

        self.settings_storage = QSettings('settings.ini', QSettings.IniFormat)

        self.restoreGeometry( self.settings_storage.value('docksGeometry').toByteArray() )
        self.restoreState( self.settings_storage.value('docksState').toByteArray() )

        # load settings:
        self.settings_storage.beginGroup('BioHarnessPackets')
        rrdata = self.settings_storage.value('rrdata', True).toBool()
        breathing = self.settings_storage.value('breathing', True).toBool()
        ecg = self.settings_storage.value('ecg', False).toBool()
        summary = self.settings_storage.value('summary', False).toBool()
        accelerometer = self.settings_storage.value('accelerometer', False).toBool()
        self.settings_storage.endGroup()
        self.appsettings.dataset.bh_packets = []
        if rrdata: self.appsettings.dataset.bh_packets.append(0)
        if breathing: self.appsettings.dataset.bh_packets.append(1)
        if ecg: self.appsettings.dataset.bh_packets.append(2)
        if summary: self.appsettings.dataset.bh_packets.append(3)
        if accelerometer: self.appsettings.dataset.bh_packets.append(4)

        self.settings_storage.beginGroup('Misc')
        self.appsettings.dataset.timedsession = self.settings_storage.value('TimedDuration', 5).toInt()[0]
        # handle windows and linux serial port name
        portname = self.settings_storage.value('Serial_Port').toString()
        if str(portname).isdigit() is True:
            self.appsettings.dataset.serialport = int(portname)
        else:
             self.appsettings.dataset.serialport = str(portname)
        self.appsettings.dataset.use_virtual_serial = self.settings_storage.value('Use_Virtual_Serial_Port', False).toBool()
        self.settings_storage.endGroup()

        self.settings_storage.beginGroup('Storage')
        self.appsettings.dataset.enable_database = self.settings_storage.value('db_enable', False).toBool()
        self.appsettings.dataset.db_url = self.settings_storage.value('db_url').toString()
        self.appsettings.dataset.db_port = self.settings_storage.value('db_port').toString()
        self.appsettings.dataset.db_user = self.settings_storage.value('db_user').toString()
        self.appsettings.dataset.db_pwd = self.settings_storage.value('db_pwd').toString()
        self.appsettings.dataset.db_dbname = self.settings_storage.value('db_dbname').toString()
        self.appsettings.dataset.enable_files = self.settings_storage.value('files_enable', False).toBool()
        self.appsettings.dataset.directory_storage = self.settings_storage.value('directory').toString()
        self.settings_storage.endGroup()
Esempio n. 7
0
    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowIcon(get_icon(os.path.join(APP_RESOURCES, 'icons',
                                                  'python.png')))
        self.setupUi(self)
        
        # Redirect output to GUI's QTextEdit
        sys.stdout = OutLog(self.outputTextEdit, sys.stdout)
        sys.stderr = OutLog(self.outputTextEdit, sys.stderr, QColor(255,0,0) )
        
        settings = QSettings()
        size = settings.value("MainWindow/Size",
                              QVariant(QSize(600, 500))).toSize()
        self.resize(size)
        position = settings.value("MainWindow/Position",
                                  QVariant(QPoint(0, 0))).toPoint()
        self.move(position)
        self.restoreState(
        settings.value("MainWindow/State").toByteArray())
    
        self.logger = Logger('Zupport.GUIloader')
        self.logger.debugging = settings.value("Logging/debugging").toBool()
         
        # Set up a ZupportManager to deal with the plugins
        self.manager = Manager(self)
        self.plugins = {}

        # Set up the extents menu
        self.extent = ExtentContainer()
        self.menuExtent = QMenu("Extent")
        resolutions = self.extent.resolutions
        self.extenactiongroup = QActionGroup(self.menuExtent)  
        noneaction = QAction("None", self.menuExtent)
        self.extenactiongroup.addAction(noneaction)
        self.menuExtent.addAction(noneaction)
        for resolution in resolutions:
            resolution_text = str(resolution)
            submenu = self.menuExtent.addMenu(resolution_text) 
            self.menuExtent.addMenu(submenu)
            for area in self.extent.get_names(resolution):
                subaction = QAction(str(area) + ": %s" % resolution_text, 
                                    submenu)
                subaction.setCheckable(True)
                self.extenactiongroup.addAction(subaction)
                submenu.addAction(subaction)
        
        noneaction.isChecked()
        self.menuSettings.addMenu(self.menuExtent)
        
        self.actionDebugging_messages.setChecked(self.logger.debugging)

        self.setWindowTitle("Zupport GUI")
        
        self.load_tools()
        
        self.toolTreeWidget.itemSelectionChanged.connect(self.update_ui)
        self.actionDebugging_messages.toggled.connect(self._set_debugging)
        self.menuExtent.triggered.connect(self.update_ui)
        self.actionLoad_tool.triggered.connect(self.show_tool_gui)
        self.toolTreeWidget.doubleClicked.connect(self.show_tool_gui)
Esempio n. 8
0
class MainWindow(QMainWindow):
    def __init__(self,
                 reset=False,
                 start_freq=None,
                 bandwidth=None,
                 numpts=None,
                 max_hold=None,
                 dev='/dev/ttyUSB0'):
        QMainWindow.__init__(self)
        self.settings = QSettings("Darkstar007", "networkanalyser")
        if reset:
            self.settings.clear()

        self.file_dir = self.settings.value('spectrum/file_dir',
                                            os.getenv('HOME'))
        print 'File dir', self.file_dir
        self.dev = dev

        self.setup(start_freq, bandwidth, numpts, max_hold)

    def setup(self, start_freq, bandwidth, numpts, max_hold):
        """Setup window parameters"""
        self.setWindowIcon(get_icon('python.png'))
        self.setWindowTitle(APP_NAME + ' ' + VERS + ' Running on ' + self.dev)
        dt = QDesktopWidget()
        print dt.numScreens(), dt.screenGeometry()
        sz = dt.screenGeometry()

        self.resize(QSize(sz.width() * 9 / 10, sz.height() * 9 / 10))

        # Welcome message in statusbar:
        status = self.statusBar()
        status.showMessage(_("Welcome to the NetworkAnalyser application!"),
                           5000)

        # File menu
        file_menu = self.menuBar().addMenu(_("File"))

        open_action = create_action(self,
                                    _("Save"),
                                    shortcut="Ctrl+S",
                                    icon=get_std_icon("DialogSaveButton"),
                                    tip=_("Save a Cal File"),
                                    triggered=self.saveFileDialog)

        load_action = create_action(self,
                                    _("Load"),
                                    shortcut="Ctrl+L",
                                    icon=get_std_icon("FileIcon"),
                                    tip=_("Load a cal File"),
                                    triggered=self.loadFileDialog)

        quit_action = create_action(self,
                                    _("Quit"),
                                    shortcut="Ctrl+Q",
                                    icon=get_std_icon("DialogCloseButton"),
                                    tip=_("Quit application"),
                                    triggered=self.close)
        add_actions(file_menu, (open_action, load_action, None, quit_action))

        # Help menu - prolly should just say "you're on your own..."!!
        help_menu = self.menuBar().addMenu("Help")
        about_action = create_action(
            self,
            _("About..."),
            icon=get_std_icon('MessageBoxInformation'),
            triggered=self.about)
        add_actions(help_menu, (about_action, ))

        main_toolbar = self.addToolBar("Main")
        # add_actions(main_toolbar, (new_action, open_action, ))

        rescan_action = create_action(
            self,
            _("Rescan"),
            shortcut="Ctrl+R",
            icon=get_std_icon("BrowserReload"),
            tip=_("Rescan the current frequency selection"),
            checkable=False,
            triggered=self.do_scan)

        max_hold_action = create_action(
            self,
            _("Max Hold"),
            shortcut="Ctrl+M",
            icon=get_std_icon("ArrowUp"),
            tip=_("Display the maximum value encountered"),
            checkable=True,
            triggered=self.do_max_hold)

        log_lin_action = create_action(self,
                                       _("Log/Lin"),
                                       shortcut="Ctrl+L",
                                       icon=get_std_icon("ArrowRight"),
                                       tip=_("Use linear power receive mode"),
                                       checkable=True,
                                       triggered=self.do_log_lin)

        new_plot_action = create_action(self,
                                        _("New Plot"),
                                        shortcut="Ctrl+N",
                                        icon=get_std_icon("ArrowLeft"),
                                        tip=_("Creates a new labeled plot"),
                                        checkable=False,
                                        triggered=self.do_new_plot)

        if max_hold is None:
            max_hold = self.settings.value('gui/max_hold', False)
            print 'Got max_hold', max_hold
            if type(max_hold) != bool:
                if max_hold in ['y', 'Y', 'T', 'True', 'true', '1']:
                    max_hold = True
                else:
                    max_hold = False
        max_hold_action.setChecked(max_hold)

        # Calibration action?
        add_actions(main_toolbar,
                    (open_action, load_action, rescan_action, max_hold_action,
                     log_lin_action, new_plot_action))

        # Set central widget:

        toolbar = self.addToolBar("Image")
        self.mainwidget = CentralWidget(self, self.settings, toolbar,
                                        start_freq, bandwidth, numpts,
                                        self.dev)
        self.setCentralWidget(self.mainwidget)

        if max_hold:
            self.do_max_hold()

    def do_scan(self):
        self.mainwidget.rescan()

    def do_new_plot(self):
        self.mainwidget.do_new_plot()

    def do_max_hold(self):
        self.mainwidget.do_max_hold()

    def do_log_lin(self):
        self.mainwidget.do_log_lin()

    def saveFileDialog(self):
        print 'Save f dialog'
        fileName = QFileDialog.getSaveFileName(self, _("Save Cal Data"),
                                               self.file_dir)
        print fileName
        self.mainwidget.save_cal_data(fileName)

    def loadFileDialog(self):
        print 'load f dialog'
        fileName = QFileDialog.getOpenFileName(self, _("Open Cal Data"),
                                               self.file_dir)
        print fileName
        self.mainwidget.load_cal_data(fileName)

    def about(self):
        QMessageBox.about(
            self,
            _("About ") + APP_NAME, """<b>%s</b> v%s<p>%s Matt Nottingham
                          <br>Copyright &copy; 2015-2017 Matt Nottingham
                          <p>Python %s, Qt %s, PyQt %s %s %s""" %
            (APP_NAME, VERS, _("Developped by"), platform.python_version(),
             QT_VERSION_STR, PYQT_VERSION_STR, _("on"), platform.system()))
Esempio n. 9
0
class MainWindow( QMainWindow ):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowTitle(APP_NAME)
        self.setWindowIcon(get_icon('python.png'))
        self.timeout = None

        # used to recognise the checkbox in settings
        self.bhpacketname = {
            'RR':0,
            'BREATHING':1,
            'ECG':2,
            'SUMMARY':3,
            'ACC':4,
        }

        # Welcome message in statusbar:
        status = self.statusBar()
        status.showMessage(_("Zephyr BioHarness 3.0"), 5000)

        self._setup_layout()
        self._setup_menu()
        self._load_settings()
        self._init_objects()
        self.show()

    def _setup_menu(self):
        # File menu
        file_menu = self.menuBar().addMenu(_("File"))
        settings_action = create_action(self, _("Settings"),
                                   icon=get_icon('settings.png'),
                                   tip=_("Settings"),
                                   triggered=self.edit_settings)
        quit_action = create_action(self, _("Quit"),
                                    shortcut="Ctrl+Q",
                                    icon=get_std_icon("DialogCloseButton"),
                                    tip=_("Quit application"),
                                    triggered=self.close)
        add_actions(file_menu, (settings_action, None, quit_action))

        # View menu
        view_menu = self.createPopupMenu()
        view_menu.setTitle(_(u"&View"))
        self.menuBar().addMenu(view_menu)

        # Help menu
        help_menu = self.menuBar().addMenu("?")
        about_action = create_action(self, _("About..."),
                                     icon=get_std_icon('MessageBoxInformation'),
                                     triggered=self.about)
        add_actions(help_menu, (about_action,))

        # Base toolbar
        self.connectAction = QAction(QIcon('common/disconnected.png'), 'Connect', self)
        self.connectAction.triggered.connect( self.connect_button )
        self.playAction = QAction(QIcon('common/play.png'), 'Play free', self)
        self.playAction.triggered.connect( self.start_free_session_button )
        self.stopAction = QAction(QIcon('common/stop.png'), 'Stop', self)
        self.stopAction.triggered.connect( self.stop_button )
        self.timedAction = QAction(QIcon('common/timed.png'), 'Start', self)
        self.timedAction.triggered.connect( self.start_timed_session_button )

        self.toolbar = self.addToolBar('Controls')
        self.toolbar.addAction( self.connectAction )
        self.toolbar.addAction( self.playAction )
        self.toolbar.addAction( self.stopAction )
        self.toolbar.addAction( self.timedAction )
        self.toolbar.setObjectName('Controls')

        # Time toolbar
        self.timer = Timer( self )
        self.connect( self.timer, SIGNAL( 'SessionStop' ), self.session_stop )

    def _setup_layout(self):
        # Allow dockable widgets to be side by side
        self.setDockOptions(QMainWindow.AnimatedDocks | QMainWindow.AllowNestedDocks)
        self.setGeometry(300,100,1500,900)

        self.rrplot         = RealTimePlot( self, 'RR-Interval', 'ms', QColor( 255, 0, 0 ) )
        self.bwplot         = RealTimePlot( self, 'Breathing','', QColor( 0, 0, 255 ) )
        self.ecgplot        = RealTimePlot( self, 'ECG Waveform','', QColor( 0, 0, 255 ) )
        self.logarea        = myDockableWidget(self, QTextEdit)
        self.bwpsd          = RealTimePSD( self, 'Breathing PSD', inity=25000 )
        self.rrpsd          = RealTimePSD( self, 'RR-Interval PSD')

        self.logarea.widget.setReadOnly( True )
        self.logarea.widget.setFont( QFont("Courier", 8) )
        self.logarea.widget.setMinimumHeight(150)

        # Add the DockWidget to the main window
        self.ecgplot_dock = self.add_dockwidget( self.ecgplot.dockwidget, _("ECG Waveform") )
        self.rrcurve_dock = self.add_dockwidget( self.rrplot.dockwidget, _("RR-Intervals Plot") )
        self.rrpsd_dock = self.add_dockwidget( self.rrpsd.dockwidget, _("RRI PSD"))
        self.bwcurve_dock = self.add_dockwidget( self.bwplot.dockwidget, _("Breathing Plot") )
        self.bwpsd_dock = self.add_dockwidget( self.bwpsd.dockwidget, _("Breathing PSD (not implemented)") )
        self.splitDockWidget(self.rrcurve_dock, self.rrpsd_dock, Qt.Horizontal)
        self.splitDockWidget(self.bwcurve_dock, self.bwpsd_dock, Qt.Horizontal)
        self.log_dock = self.add_dockwidget( self.logarea, _("Messages"),  position=Qt.BottomDockWidgetArea)

        #self.splitDockWidget(self.rrcurve_dock, self.ecgplot_dock, Qt.Horizontal)

        # setting the name of the dock widget is required to save correclty
        # the postion of the widget when the application close
        self.rrcurve_dock.setObjectName('rrcurve_dock')
        self.rrpsd_dock.setObjectName('rrpsd_dock')
        self.bwcurve_dock.setObjectName('bwcurve_dock')
        self.bwpsd_dock.setObjectName('bwpsd_dock')
        self.log_dock.setObjectName('log_dock')
        self.ecgplot_dock.setObjectName('ecgplot_dock')

        #self.log_dock.setMinimumHeight( 20 )


        self.rrcurve_dock.setMinimumSize( 400, 200 )
        self.bwcurve_dock.setMinimumSize( 400, 200 )
        self.rrpsd_dock.setMinimumSize( 400, 200 )
        self.bwpsd_dock.setMinimumSize( 400, 200 )
        self.log_dock.setMinimumSize( 400, 100 )
        self.log_dock.setMaximumHeight( 250 )

    def _load_settings(self):
        self.appsettings = DataSetShowGroupBox("Settings",
                                             AppSettings, comment='',
                                             title=_("Application settings"))

        self.settings_storage = QSettings('settings.ini', QSettings.IniFormat)

        self.restoreGeometry( self.settings_storage.value('docksGeometry').toByteArray() )
        self.restoreState( self.settings_storage.value('docksState').toByteArray() )

        # load settings:
        self.settings_storage.beginGroup('BioHarnessPackets')
        rrdata = self.settings_storage.value('rrdata', True).toBool()
        breathing = self.settings_storage.value('breathing', True).toBool()
        ecg = self.settings_storage.value('ecg', False).toBool()
        summary = self.settings_storage.value('summary', False).toBool()
        accelerometer = self.settings_storage.value('accelerometer', False).toBool()
        self.settings_storage.endGroup()
        self.appsettings.dataset.bh_packets = []
        if rrdata: self.appsettings.dataset.bh_packets.append(0)
        if breathing: self.appsettings.dataset.bh_packets.append(1)
        if ecg: self.appsettings.dataset.bh_packets.append(2)
        if summary: self.appsettings.dataset.bh_packets.append(3)
        if accelerometer: self.appsettings.dataset.bh_packets.append(4)

        self.settings_storage.beginGroup('Misc')
        self.appsettings.dataset.timedsession = self.settings_storage.value('TimedDuration', 5).toInt()[0]
        # handle windows and linux serial port name
        portname = self.settings_storage.value('Serial_Port').toString()
        if str(portname).isdigit() is True:
            self.appsettings.dataset.serialport = int(portname)
        else:
             self.appsettings.dataset.serialport = str(portname)
        self.appsettings.dataset.use_virtual_serial = self.settings_storage.value('Use_Virtual_Serial_Port', False).toBool()
        self.settings_storage.endGroup()

        self.settings_storage.beginGroup('Storage')
        self.appsettings.dataset.enable_database = self.settings_storage.value('db_enable', False).toBool()
        self.appsettings.dataset.db_url = self.settings_storage.value('db_url').toString()
        self.appsettings.dataset.db_port = self.settings_storage.value('db_port').toString()
        self.appsettings.dataset.db_user = self.settings_storage.value('db_user').toString()
        self.appsettings.dataset.db_pwd = self.settings_storage.value('db_pwd').toString()
        self.appsettings.dataset.db_dbname = self.settings_storage.value('db_dbname').toString()
        self.appsettings.dataset.enable_files = self.settings_storage.value('files_enable', False).toBool()
        self.appsettings.dataset.directory_storage = self.settings_storage.value('directory').toString()
        self.settings_storage.endGroup()

    def _init_objects(self):
        # The time series container hold the data of the heart beat and breathing signal
        self.timeseriescontainer = TimeSeriesContainer()

        self.sessiontype = 'free'   # either free or timed

        self.zephyr_connect = ZephyrDevice()
        self.connect( self.zephyr_connect, SIGNAL( 'Message' ), self.printmessage )
        self.connect( self.zephyr_connect, SIGNAL( 'rrinterval' ), self.update_RR_plot )
        self.connect( self.zephyr_connect, SIGNAL( 'breathing_wave' ), self.update_BW_plot )
        self.connect( self.zephyr_connect, SIGNAL( 'ecg' ), self.update_ECG_plot )
        self.connect( self.zephyr_connect, SIGNAL( 'heart_rate' ), self.add_heart_rate )
        self.connect( self.zephyr_connect, SIGNAL( 'respiration_rate' ), self.add_respiration_rate )
        self.connect( self.zephyr_connect, SIGNAL( 'breathing_wave_amplitude' ), self.add_breathing_wave_amplitude )
        self.connect( self.zephyr_connect, SIGNAL( 'activity' ), self.add_activity )
        self.connect( self.zephyr_connect, SIGNAL( 'posture' ), self.add_posture )
        self.zephyr_connect.virtual_serial = self.appsettings.dataset.use_virtual_serial

        # the button are disabled by default
        # they are enabled if the connection to the device is successfull
        self.stopAction.setEnabled( False )
        self.playAction.setEnabled( False )
        self.timedAction.setEnabled( False )

        # InfluxDB storage configuration
        # Data storage need the application settings for db credentials
        self.datastorage = DataStorage()
        if self.appsettings.dataset.enable_database is True:
            self.datastorage.db_init( self.appsettings.dataset )
            self._test_database_connection()

        # size for the ecg window is different that the default value
        # of 60' set at the creation of the real time plot
        self.ecgplot.window_length = 6

    def sendbhcmd( self ):
        cmd =  int(str(self.bhcmdinput.text()), 16)
        self.zephyr_connect.sendmessage(cmd, [])

    #------?
    def about( self ):
        QMessageBox.about( self, _("About ")+APP_NAME,
              """<b>%s</b> v%s<p>%s Darko Petrovic
              <br>(Lisence goes here)
              <p>Python %s, Qt %s, PyQt %s %s %s""" % \
              (APP_NAME, VERSION, _("Developped by"), platform.python_version(),
               QT_VERSION_STR, PYQT_VERSION_STR, _("on"), platform.system()) )

    def edit_settings( self ):
        ok = self.appsettings.dataset.edit()

        # save settings in the .ini file
        if ok == 1:
            # Application settings (window position, view, ...):
            # ...
            # User settings:
            rrdata = breathing = ecg = summary = accelerometer = False
            for a in self.appsettings.dataset.bh_packets:
                if a == 0: rrdata = True
                elif a == 1: breathing = True
                elif a == 2: ecg = True
                elif a == 3: summary = True
                elif a == 4: accelerometer = True

            self.settings_storage.beginGroup('BioHarnessPackets')
            self.settings_storage.setValue('rrdata', str(rrdata) )
            self.settings_storage.setValue('breathing', str(breathing) )
            self.settings_storage.setValue('ecg', str(ecg) )
            self.settings_storage.setValue('summary', str(summary) )
            self.settings_storage.setValue('accelerometer', str(accelerometer) )
            self.settings_storage.endGroup()

            self.settings_storage.beginGroup('Misc')
            self.settings_storage.setValue('TimedDuration', self.appsettings.dataset.timedsession )
            self.settings_storage.setValue('Serial_Port', self.appsettings.dataset.serialport )
            self.settings_storage.setValue('Use_Virtual_Serial_Port', self.appsettings.dataset.use_virtual_serial )
            self.settings_storage.endGroup()

            self.settings_storage.beginGroup('Storage')
            self.settings_storage.setValue('db_enable', str(self.appsettings.dataset.enable_database) )
            self.settings_storage.setValue('db_url', self.appsettings.dataset.db_url )
            self.settings_storage.setValue('db_port', self.appsettings.dataset.db_port )
            self.settings_storage.setValue('db_user', str(self.appsettings.dataset.db_user) )
            self.settings_storage.setValue('db_pwd', str(self.appsettings.dataset.db_pwd) )
            self.settings_storage.setValue('db_dbname', str(self.appsettings.dataset.db_dbname) )
            self.settings_storage.setValue('files_enable', str(self.appsettings.dataset.enable_files) )
            self.settings_storage.setValue('directory', str(self.appsettings.dataset.directory_storage) )
            self.settings_storage.endGroup()

        if ok==1 and self.appsettings.dataset.enable_database:
            self.datastorage.db_init( self.appsettings.dataset )
            self._test_database_connection()

        if ok==1:
            self.zephyr_connect.virtual_serial = self.appsettings.dataset.use_virtual_serial

    def _test_database_connection(self):
        result, message = self.datastorage.db_connection()
        if result:
            self.logmessage(message)
        else:
            self.logmessage("Connection to the database failed: %s" % message, 'error')

    #------GUI refresh/setup
    def add_dockwidget( self, child, title, orientation = Qt.Vertical, position=None ):
        """Add a QDockWidget to the main window."""
        dockwidget, location = child.create_dockwidget( title )
        if position is not None:
            location = position
        self.addDockWidget( location, dockwidget, orientation )
        return dockwidget

    def logmessage( self, text, msgtype='info' ):
        """ Print a message in the message window
        """
        if msgtype == 'error':
            self.logarea.widget.setTextColor( QColor( 255, 0, 0 ) )
        else:
            self.logarea.widget.setTextColor( QColor( 0, 0, 0 ) )

        self.logarea.widget.insertPlainText( text + "\n" )
        sb = self.logarea.widget.verticalScrollBar()
        sb.setValue( sb.maximum() )

    def update_RR_plot( self, value ):
        # Store value in the data-set. We store every value in the dataset
        # but we display only a certain duration specified by 'self.rrplot.window_length'
        self.timeseriescontainer.ts_rri.add_rrinterval( value )
        if self.appsettings.dataset.enable_database is True:
            self.datastorage.write_points('rrintervals', value, self.timeseriescontainer.ts_rri.realtime[-1]*1000, 'm')
        # Set the data to the curve with values from the time series and update the plot
        self.rrplot.startIdx = self.timeseriescontainer.ts_rri.getSampleIndex( self.rrplot.window_length )
        self.rrplot.update( self.timeseriescontainer.ts_rri.realtime, self.timeseriescontainer.ts_rri.series )

        # Wait minimum 10 samples
        if len(self.timeseriescontainer.ts_rri.series) > 10:
            self.timeseriescontainer.ts_rri.computeLombPeriodogram()
            self.rrpsd.update(self.timeseriescontainer.ts_rri.psd_freq, self.timeseriescontainer.ts_rri.psd_mag)

    def update_BW_plot( self, values ):
        # Store value in the data-set. We store every value in the dataset
        # but we display only a certain duration specified by 'self.rrplot.window_length'
        self.timeseriescontainer.ts_bw.add_breath( values )
        if self.appsettings.dataset.enable_database is True:
            self.datastorage.write_points('breathing_wave', values, self.timeseriescontainer.ts_bw.realtime[-len(values):]*1000, 'm')
        # Set the data to the curve with values from the data-set and update the plot
        self.bwplot.startIdx = self.timeseriescontainer.ts_bw.getSampleIndex( self.bwplot.window_length )
        self.bwplot.update( self.timeseriescontainer.ts_bw.realtime, self.timeseriescontainer.ts_bw.series )

        if len(self.timeseriescontainer.ts_bw.series) > 50:
            # ---- Compute and display the Power Spectral Density of breathing signal
            self.timeseriescontainer.ts_bw.computeWelchPeriodogram()
            self.bwpsd.update(self.timeseriescontainer.ts_bw.psd_freq, self.timeseriescontainer.ts_bw.psd_mag)

    def update_ECG_plot( self, values ):
        self.timeseriescontainer.ts_ecg.add_ecg( values )
        if self.appsettings.dataset.enable_database is True:
            self.datastorage.write_points('ecg', values, self.timeseriescontainer.ts_ecg.realtime[-len(values):]*1000, 'm')

        self.ecgplot.startIdx = self.timeseriescontainer.ts_ecg.getSampleIndex( self.ecgplot.window_length )
        self.ecgplot.update( self.timeseriescontainer.ts_ecg.realtime, self.timeseriescontainer.ts_ecg.series )

    def add_heart_rate(self, value):
        self.timeseriescontainer.heart_rate = np.append(self.timeseriescontainer.heart_rate, value)
        if self.appsettings.dataset.enable_database is True:
            self.datastorage.write_points('heart_rate', value)

    def add_respiration_rate(self, value):
        self.timeseriescontainer.respiration_rate = np.append(self.timeseriescontainer.respiration_rate, value)
        if self.appsettings.dataset.enable_database is True:
            self.datastorage.write_points('respiration_rate', value)

    def add_breathing_wave_amplitude(self, value):
        self.timeseriescontainer.breathwave_ampltitude = np.append(self.timeseriescontainer.breathwave_ampltitude,value)
        if self.appsettings.dataset.enable_database is True:
            self.datastorage.write_points('breathing_wave_amplitude', value)

    def add_activity(self, value):
        self.timeseriescontainer.activity = np.append(self.timeseriescontainer.activity, value)
        if self.appsettings.dataset.enable_database is True:
            self.datastorage.write_points('activity', value)

    def add_posture(self, value):
        self.timeseriescontainer.posture = np.append(self.timeseriescontainer.posture, value)
        if self.appsettings.dataset.enable_database is True:
            self.datastorage.write_points('posture', value)

    def printmessage( self, message ):
        if message == 'connected':
            self.logmessage( "Successfully connected to the device %s." % self.zephyr_connect.SerialNumber )
            self._toggle_connect_button()
            if self.timeout:
                self.timeout.stop()
                self.timeout = None

        if isinstance(message, zephyr.message.BatteryStatus):
            self.logmessage("Battery charge is %d%%" % message.Charge )

    def _toggle_connect_button(self):
        if self.zephyr_connect.connected is True:
            self.connectAction.setIcon(QIcon('common/connected.png'))
            self.connectAction.setToolTip("Disconnect")
            self.connectAction.triggered.disconnect( self.connect_button )
            self.connectAction.triggered.connect( self.disconnect_button )
            self.playAction.setEnabled( True )
            self.timedAction.setEnabled( True )
        else:
            self.connectAction.setIcon(QIcon('common/disconnected.png'))
            self.connectAction.setToolTip("Connect")
            self.connectAction.triggered.disconnect( self.disconnect_button )
            self.connectAction.triggered.connect( self.connect_button )
            self.playAction.setEnabled( False )
            self.timedAction.setEnabled( False )

    def connect_button(self):
        # The connect button is first trying to open the com port. If the port can be opened,
        # the zephyr protocol functions are instancied and a message is send to the device
        # (the thread is started for this purpose and to let the reception of the response).
        # The device has 3 seconds to respond (a timeout is started to close the serial
        # and terminate the thread in case of no response). When the device responds a signal 'Message'
        # is sent to the GUI (the message is the Serial Number of the device).
        if self.zephyr_connect.connectTo( self.appsettings.dataset.serialport,
                                          self.appsettings.dataset.use_virtual_serial):
            self.zephyr_connect.start()
            if self.appsettings.dataset.use_virtual_serial is False:
                self.timeout = QTimer( self )
                self.connect( self.timeout, SIGNAL( 'timeout()' ), self.connectionTimeout )
                self.timeout.setSingleShot(True)
                self.timeout.start(3000)
            else:
                self.logmessage("Serial virtualization in use.")
                self._toggle_connect_button()
        else:
            self.logmessage( "Fail to open port '%s' !" % self.appsettings.dataset.serialport, 'error' )

    def disconnect_button(self):
        self.zephyr_connect.terminate()
        if self.zephyr_connect.wait():
            self._toggle_connect_button()
            if self.appsettings.dataset.use_virtual_serial is False:
                self.logmessage( "Successfully disconnected from the device." )
            else:
                self.logmessage( "Virtual serial stopped." )

    def connectionTimeout(self):
        self.logmessage("Unable to connected to the device on %s." % self.appsettings.dataset.serialport, 'error' )
        self.zephyr_connect.terminate()
        if self.timeout:
            self.timeout = None

    def start_free_session_button( self ):
        self.sessiontype = 'free'
        self.timer.initialize( 0 )
        self.session_start()

    def start_timed_session_button(self):

        if not self.timeout:
            self.sessiontype = 'timed'
            self.timer.initialize( self.appsettings.dataset.timedsession * 60 )
            # the session will start after 'X' seconds
            X = 20
            self.timeout = QTimer( self )
            self.connect( self.timeout, SIGNAL( 'timeout()' ), self.session_start )
            self.timeout.setSingleShot(True)
            self.timeout.start(X*1000)
            self.logmessage('The session will start in %d seconds.' % X)
        else:
             # the button is pressed a second time
            self.timeout.stop()
            self.timeout = None
            self.session_start()

    def stop_button( self ):
        sel = 0
        if self.sessiontype == 'timed':
            sel = QMessageBox.warning( self, "Timed Session",
                                       "A Timed Session is currently active!\nIf you stop the session "
                                       "the session will be stored as a free session.", "OK", "Cancel")
        if sel == 0:
            self.session_stop()

    def session_start( self ):
        if self.timeout:
            self.timeout.stop()
            self.timeout = None

        # empty all arrays
        self.timeseriescontainer.clearContainer()

        if self.appsettings.dataset.use_virtual_serial is True:
            self.zephyr_connect.resume()

        for a in self.appsettings.dataset.bh_packets:
            if a == 0:
                self.zephyr_connect.enablePacket('RRDATA')
                self.timeseriescontainer.ts_rri.setStartTime()
            elif a == 1:
                self.zephyr_connect.enablePacket('BREATHING')
                self.timeseriescontainer.ts_bw.setStartTime()
            elif a == 2:
                self.zephyr_connect.enablePacket('ECG')
                self.timeseriescontainer.ts_ecg.setStartTime()
            elif a == 3:
                self.zephyr_connect.enablePacket('SUMMARY')

        self.timer.start()

        # handle graphical change:
        self.playAction.setEnabled( False )
        self.timedAction.setEnabled( False )
        self.stopAction.setEnabled( True )
        self.connectAction.setEnabled( False )

        # in any case, we create the new session in database because the
        # data are written in real time
        if self.appsettings.dataset.enable_database is True:
            self.datastorage.create_session()

    def session_stop(self):
        if self.appsettings.dataset.use_virtual_serial is True:
            self.zephyr_connect.pause()

        for a in self.appsettings.dataset.bh_packets:
            if a == 0: self.zephyr_connect.disablePacket('RRDATA')
            elif a == 1: self.zephyr_connect.disablePacket('BREATHING')
            elif a == 2: self.zephyr_connect.disablePacket('ECG')
            elif a == 3: self.zephyr_connect.disablePacket('SUMMARY')

        self.timer.stop()
        # handle graphical change:
        self.playAction.setEnabled( True )
        self.timedAction.setEnabled( True )
        self.stopAction.setEnabled( False )
        self.connectAction.setEnabled( True )

        # update session with the duration
        if self.appsettings.dataset.enable_database is True:
            self.datastorage.update_duration(self.timer.getRunningTime())

        # store more info for the current session?
        if self.appsettings.dataset.enable_database or self.appsettings.dataset.enable_files:
            self.infosdialog = SessionInfos( self, self.datastorage )
            self.connect(self.infosdialog, SIGNAL('accepted()'), self.add_more_infos )
            self.infosdialog.exec_()

    def add_more_infos(self):
        sessiontype = 0
        for i, r in enumerate(self.infosdialog.sessiontypes):
            if r.isChecked():
                sessiontype = i+1

        breathing_zone = self.infosdialog.breathzone.currentIndex()
        infos = {   'session_type': sessiontype,
                    'breathing_zone': '' if breathing_zone == 0 else breathing_zone,
                    'note': str(self.infosdialog.note.toPlainText()),
                }

        if self.appsettings.dataset.enable_database is True:
            self.datastorage.add_informations( infos )
        self.logmessage("The information was stored in the database for this session.")

    def closeEvent(self, event):
        self.settings_storage.setValue( 'docksGeometry', self.saveGeometry() )
        self.settings_storage.setValue( 'docksState', self.saveState() )
        QMainWindow.closeEvent(self, event)
Esempio n. 10
0
class MainWindow(QMainWindow):
    def __init__(self, reset=False, start_freq=None,
                        bandwidth=None, numpts=None, max_hold=None):
        QMainWindow.__init__(self)
        self.settings = QSettings("Darkstar007", "signal_generator")
        if reset:
            self.settings.clear()
            
        self.setup(start_freq, bandwidth, numpts, max_hold)
        
    def setup(self, start_freq, bandwidth, numpts, max_hold):
        """Setup window parameters"""
        self.setWindowIcon(get_icon('python.png'))
        self.setWindowTitle(APP_NAME)
        #dt = QDesktopWidget()
        #print dt.numScreens(), dt.screenGeometry()
        #sz = dt.screenGeometry()

        #self.resize(QSize(sz.width()*9/10, sz.height()*9/10))
        
        # Welcome message in statusbar:
        status = self.statusBar()
        status.showMessage(_("Welcome to the Signal Generator application!"), 5000)
        
        # File menu
        file_menu = self.menuBar().addMenu(_("File"))

        open_action = create_action(self, _("Save"),
                                    shortcut="Ctrl+S",
                                    icon=get_std_icon("FileIcon"),
                                    tip=_("Save a File"),
                                    triggered=self.saveFileDialog)

        quit_action = create_action(self, _("Quit"),
                                    shortcut="Ctrl+Q",
                                    icon=get_std_icon("DialogCloseButton"),
                                    tip=_("Quit application"),
                                    triggered=self.close)
        add_actions(file_menu, (open_action, None, quit_action))
        
        # Help menu - prolly should just say "you're on your own..."!!
        help_menu = self.menuBar().addMenu("Help")
        about_action = create_action(self, _("About..."),
                                     icon=get_std_icon('MessageBoxInformation'),
                                     triggered=self.about)
        add_actions(help_menu, (about_action,))
        
        main_toolbar = self.addToolBar("Main")
        
        # Calibration action?
        add_actions(main_toolbar, (open_action, ))
        
        # Set central widget:

        toolbar = self.addToolBar("Image")

        self.setCentralWidget(self.mainwidget)
        
        if max_hold:
            self.do_max_hold()


    def about(self):
        QMessageBox.about( self, _("About ")+APP_NAME,
              """<b>%s</b> v%s<p>%s Matt Nottingham
              <br>Copyright &copy; 2015 Matt Nottingham
              <p>Python %s, Qt %s, PyQt %s %s %s""" % \
              (APP_NAME, VERS, _("Developped by"), platform.python_version(),
               QT_VERSION_STR, PYQT_VERSION_STR, _("on"), platform.system()) )