]> sigrok.org Git - sigrok-meter.git/commitdiff
Create one central libsigrok context.
authorJens Steinhauser <redacted>
Wed, 19 Nov 2014 02:13:55 +0000 (03:13 +0100)
committerJens Steinhauser <redacted>
Wed, 19 Nov 2014 02:13:55 +0000 (03:13 +0100)
sigrok-meter

index ce7dbf36a7d6ab276a17487ac5725103e4b6f7b2..0c263a448ef3df4fce1dd5f0a8b34e8b5df66203 100755 (executable)
@@ -128,17 +128,13 @@ class SamplingThread(QtCore.QObject):
         '''Signal emmited in case of an error.'''
         error = QtCore.Signal(str)
 
-        def __init__(self, drivers, loglevel):
+        def __init__(self, context, drivers):
             super(self.__class__, self).__init__()
 
-            self.sampling = False
+            self.context = context
             self.drivers = drivers
 
-            self.context = sr.Context_create()
-            self.context.log_level = loglevel
-
-            self.sr_pkg_version = self.context.package_version
-            self.sr_lib_version = self.context.lib_version
+            self.sampling = False
 
         @QtCore.Slot()
         def start_sampling(self):
@@ -185,10 +181,10 @@ class SamplingThread(QtCore.QObject):
     # signal used to start the worker across threads
     _start_signal = QtCore.Signal()
 
-    def __init__(self, drivers, loglevel):
+    def __init__(self, context, drivers):
         super(self.__class__, self).__init__()
 
-        self.worker = self.Worker(drivers, loglevel)
+        self.worker = self.Worker(context, drivers)
         self.thread = QtCore.QThread()
         self.worker.moveToThread(self.thread)
 
@@ -210,14 +206,6 @@ class SamplingThread(QtCore.QObject):
         self.thread.quit()
         self.thread.wait()
 
-    def sr_pkg_version(self):
-        '''Returns the version number of the libsigrok package.'''
-        return self.worker.sr_pkg_version
-
-    def sr_lib_version(self):
-        '''Returns the version number fo the libsigrok library.'''
-        return self.worker.sr_lib_version
-
 class MeasurementDataModel(QtGui.QStandardItemModel):
     '''Model to hold the measured values.'''
 
@@ -404,16 +392,18 @@ class EmptyMessageListView(QtGui.QListView):
 class SigrokMeter(QtGui.QMainWindow):
     '''The main window of the application.'''
 
-    def __init__(self, thread):
+    def __init__(self, context, drivers):
         super(SigrokMeter, self).__init__()
 
+        self.context = context
+
         self.delegate = MultimeterDelegate(self, self.font())
         self.model = MeasurementDataModel(self)
         self.model.rowsInserted.connect(self.modelRowsInserted)
 
         self.setup_ui()
 
-        self.thread = thread
+        self.thread = SamplingThread(self.context, drivers)
         self.thread.measured.connect(self.model.update)
         self.thread.error.connect(self.error)
         self.thread.start()
@@ -460,6 +450,10 @@ class SigrokMeter(QtGui.QMainWindow):
         self.setCentralWidget(self.listView)
         self.centralWidget().setContentsMargins(0, 0, 0, 0)
 
+    def closeEvent(self, event):
+        self.thread.stop()
+        event.accept()
+
     @QtCore.Slot()
     def show_about(self):
         text = textwrap.dedent('''\
@@ -475,7 +469,7 @@ class SigrokMeter(QtGui.QMainWindow):
                 <a href='http://www.gnu.org/licenses/gpl.html'>
                          http://www.gnu.org/licenses/gpl.html</a>
             </div>
-        '''.format(self.thread.sr_pkg_version(), self.thread.sr_lib_version()))
+        '''.format(self.context.package_version, self.context.lib_version))
 
         QtGui.QMessageBox.about(self, 'About sigrok-meter', text)
 
@@ -494,12 +488,11 @@ class SigrokMeter(QtGui.QMainWindow):
         self.listView.setMinimumHeight(dh * rows)
 
 if __name__ == '__main__':
-    thread = SamplingThread(args['drivers'], args['loglevel'])
+    context = sr.Context_create()
+    context.log_level = args['loglevel']
 
     app = QtGui.QApplication([])
-    s = SigrokMeter(thread)
+    s = SigrokMeter(context, args['drivers'])
     s.show()
 
-    r = app.exec_()
-    thread.stop()
-    sys.exit(r)
+    sys.exit(app.exec_())