]> sigrok.org Git - sigrok-meter.git/commitdiff
Allow changing of the recording time.
authorJens Steinhauser <redacted>
Tue, 13 Oct 2015 19:00:46 +0000 (21:00 +0200)
committerJens Steinhauser <redacted>
Tue, 13 Oct 2015 19:22:47 +0000 (21:22 +0200)
mainwindow.py
settings.py

index 680dd97b5eb5cded0d7cb7ba4b2ad304298975d7..64caa2a7e9b72b03afef01d0ba4978b06aba2cc8 100644 (file)
@@ -54,9 +54,6 @@ class EmptyMessageListView(QtGui.QListView):
 class MainWindow(QtGui.QMainWindow):
     '''The main window of the application.'''
 
-    # Number of seconds that the plots display.
-    BACKLOG = 30
-
     # Update interval of the plots in milliseconds.
     UPDATEINTERVAL = 100
 
@@ -84,6 +81,8 @@ class MainWindow(QtGui.QMainWindow):
         self._plot_update_timer.setInterval(MainWindow.UPDATEINTERVAL)
         self._plot_update_timer.timeout.connect(self._updatePlots)
 
+        settings.graph.backlog.changed.connect(self.on_setting_graph_backlog_changed)
+
         QtCore.QTimer.singleShot(0, self._start_acquisition)
 
     def _start_acquisition(self):
@@ -158,17 +157,17 @@ class MainWindow(QtGui.QMainWindow):
         #actionLog.setIcon(icons.log)
         #actionLog.triggered.connect(self.showLogPage)
 
-        #actionPreferences = self.sideBar.addAction('Preferences')
-        #actionPreferences.setCheckable(True)
-        #actionPreferences.setIcon(icons.preferences)
-        #actionPreferences.triggered.connect(self.showPreferencesPage)
+        actionPreferences = self.sideBar.addAction('Preferences')
+        actionPreferences.setCheckable(True)
+        actionPreferences.setIcon(icons.preferences)
+        actionPreferences.triggered.connect(self.showPreferencesPage)
 
         # make the buttons at the top exclusive
         self.actionGroup = QtGui.QActionGroup(self)
         self.actionGroup.addAction(actionGraph)
         #self.actionGroup.addAction(actionAdd)
         #self.actionGroup.addAction(actionLog)
-        #self.actionGroup.addAction(actionPreferences)
+        self.actionGroup.addAction(actionPreferences)
 
         # show graph at startup
         actionGraph.setChecked(True)
@@ -250,9 +249,20 @@ class MainWindow(QtGui.QMainWindow):
 
     def _setup_preferencesPage(self):
         self.preferencesPage = QtGui.QWidget(self)
-        layout = QtGui.QVBoxLayout(self.preferencesPage)
-        label = QtGui.QLabel('preferences page')
-        layout.addWidget(label)
+        layout = QtGui.QGridLayout(self.preferencesPage)
+
+        layout.addWidget(QtGui.QLabel('<b>Graph</b>'), 0, 0)
+        layout.addWidget(QtGui.QLabel('Recording time (seconds):'), 1, 0)
+
+        spin = QtGui.QSpinBox(self)
+        spin.setMinimum(10)
+        spin.setMaximum(3600)
+        spin.setSingleStep(10)
+        spin.setValue(settings.graph.backlog.value())
+        spin.valueChanged[int].connect(settings.graph.backlog.setValue)
+        layout.addWidget(spin, 1, 1)
+
+        layout.setRowStretch(layout.rowCount(), 100)
 
     def showPage(self, page):
         self.stackedWidget.setCurrentIndex(self._pages.index(page))
@@ -273,6 +283,21 @@ class MainWindow(QtGui.QMainWindow):
     def showPreferencesPage(self):
         self.showPage(self.preferencesPage)
 
+    @QtCore.Slot(int)
+    def on_setting_graph_backlog_changed(self, bl):
+        for unit in self._plots:
+            plot = self._plots[unit]
+
+            # Remove the limits first, otherwise the range update would
+            # be ignored.
+            plot.view.setLimits(xMin=None, xMax=None)
+
+            # Now change the range, and then use the calculated limits
+            # (also see the comment in '_getPlot()').
+            plot.view.setXRange(-bl, 0, update=True)
+            r = plot.view.viewRange()
+            plot.view.setLimits(xMin=r[0][0], xMax=r[0][1])
+
     def _getPlot(self, unit):
         '''Looks up or creates a new plot for 'unit'.'''
 
@@ -282,7 +307,7 @@ class MainWindow(QtGui.QMainWindow):
         # create a new plot for the unit
         plot = self.plotwidget.addPlot()
         plot.yaxis.setLabel(util.quantity_from_unit(unit), units=util.format_unit(unit))
-        plot.view.setXRange(-MainWindow.BACKLOG, 0, update=False)
+        plot.view.setXRange(-settings.graph.backlog.value(), 0, update=False)
         plot.view.setYRange(-1, 1)
         plot.view.enableAutoRange(axis=pyqtgraph.ViewBox.YAxis)
         # lock to the range calculated by the view using additional padding,
@@ -328,7 +353,7 @@ class MainWindow(QtGui.QMainWindow):
                 now = time.time()
 
                 # remove old samples
-                l = now - MainWindow.BACKLOG
+                l = now - settings.graph.backlog.value()
                 while trace.samples and trace.samples[0][0] < l:
                     trace.samples.pop(0)
 
index 79484e6de2b8d0e7cb03e4673a90bf19447c2ffc..1c5523df4575a319e06e8cf9baa3c3e1ae837b71 100644 (file)
@@ -30,16 +30,25 @@ class Setting(QtCore.QObject):
     '''Signal emitted when the setting has changed.'''
     changed = QtCore.Signal(object)
 
-    def __init__(self, key, default=None):
+    def __init__(self, key, default=None, conv=None):
+        '''Initializes the Settings object.
+
+        :param key: The key of the used 'QSettings' object.
+        :param default: Value returned if the setting doesn't already exist.
+        :param conv: Function used to convert the setting to the correct type.
+        '''
+
         super(self.__class__, self).__init__()
 
         self._key = key
         self._default = default
+        self._conv = conv
         self._value = None
 
     def value(self):
         s = QtCore.QSettings()
-        self._value = s.value(self._key, self._default)
+        v = s.value(self._key, self._default)
+        self._value = self._conv(v) if self._conv else v
         return self._value
 
     @QtCore.Slot(object)
@@ -71,3 +80,7 @@ def init():
     mainwindow.size = Setting('mainwindow/size', QtCore.QSize(900, 550))
     mainwindow.pos  = Setting('mainwindow/pos')
     globals()['mainwindow'] = mainwindow
+
+    graph = _SettingsGroup()
+    graph.backlog = Setting('graph/backlog', 30, conv=int)
+    globals()['graph'] = graph