Add an option to qgpib_plotter to make it auto display any newly received plot (+ fix in plot list management)

Fri, 05 Sep 2008 00:25:19 +0200

author
David Douard <david.douard@logilab.fr>
date
Fri, 05 Sep 2008 00:25:19 +0200
changeset 78
b73a9d9e45ec
parent 77
1e539617d6ac
child 79
b8eec4f9db52

Add an option to qgpib_plotter to make it auto display any newly received plot (+ fix in plot list management)

pygpibtoolkit/plotter/qgpib_plotter.py file | annotate | diff | comparison | revisions
pygpibtoolkit/qt4/qpreferences.py file | annotate | diff | comparison | revisions
pygpibtoolkit/qt4/qpreferenceseditor.py file | annotate | diff | comparison | revisions
--- a/pygpibtoolkit/plotter/qgpib_plotter.py	Fri Sep 05 00:23:55 2008 +0200
+++ b/pygpibtoolkit/plotter/qgpib_plotter.py	Fri Sep 05 00:25:19 2008 +0200
@@ -36,6 +36,7 @@
     from qhpgl_plotter_ui import Ui_MainWindow as form_class
     
 from pygpibtoolkit.qt4.qpreferences import BaseItem, IntItem, UnicodeItem, ColorItem
+from pygpibtoolkit.qt4.qpreferences import BoolItem
 from pygpibtoolkit.qt4.qpreferences import PointItem, SizeItem, ByteArrayItem
 from pygpibtoolkit.qt4.qpreferences import AbstractPreferences
 from pygpibtoolkit.qt4.qpreferenceseditor import PreferencesEditor
@@ -85,12 +86,19 @@
     color7 = ColorItem(default=QtGui.QColor("darkred"),
                        name="Pen #7",
                        group="Colors")
+
+    autodisplay = BoolItem(default=True,
+                           name="Auto display",
+                           description="Automatically display a new plot as soon as it is received",
+                           group="Misc")
     
 class QtHPGLPlotter(QtGui.QMainWindow, form_class):
     def __init__(self, parent=None):
         QtGui.QMainWindow.__init__(self, parent)
         self._plots = {}        
         self._prefs = Preferences()
+        self._receiving = False
+        
         self.setupUi()
         self.initializeGPIB()
         if self._prefs._pos:
@@ -145,7 +153,8 @@
         self.connect(self.plotsView.selectionModel(),
                      SIGNAL('currentChanged(const QModelIndex&, const QModelIndex&)'),
                      self.currentPlotChanged)
-
+        self.setReceivingLed()
+        
     def currentPlotChanged(self, index, old_index=None):
         if index.isValid():
             value = unicode(self.plotsView.model().data(index, Qt.DisplayRole).toString())
@@ -202,11 +211,14 @@
         self.setReceivingLed()
         plot, timestamp = self.captureThread.getPlot(num)
         name = "plot_%s"%(num)
-        lst = self.plotsView.model().stringList()
-        lst.append(name)            
         self._plots[name] = plot
-        self.plotsView.model().setStringList(lst)
-
+        model = self.plotsView.model()
+        pos = model.rowCount()
+        model.insertRows(pos, 1)
+        model.setData(model.index(pos), QtCore.QVariant(name))
+        if self._prefs.autodisplay:
+            self.plotsView.setCurrentIndex(model.index(pos))
+        
     def plotStarted(self):
         self._receiving = True
         self.setReceivingLed()
@@ -214,10 +226,10 @@
     def printTriggered(self, checked=False):
         cindex = self.plotsView.currentIndex()
         if cindex and cindex.isValid():
+            # isn't there a simpler way of getting this?
             plotname = unicode(self.plotsView.model().data(cindex, Qt.DisplayRole).toString())
             printer = QtGui.QPrinter()
             printer.setOutputFormat(printer.PdfFormat)
-            # isn't there a simpler way of doing this?
             printer.setOutputFileName('%s.pdf'%plotname)
             prdialog = QtGui.QPrintDialog(printer)
             prdialog.setEnabledOptions(prdialog.PrintToFile)
@@ -241,13 +253,12 @@
             value = unicode(self.plotsView.model().data(index, Qt.DisplayRole).toString())
             open(filename, 'w').write(self._plots[value])
             
-        
     def initializeGPIB(self):
         self._online = False
         try:
             self.gpib_plotter = QGPIBplotter(device=self._prefs.device,
-                                            address=self._prefs.address,
-                                            )
+                                             address=self._prefs.address,
+                                             )
             self.captureThread = GPIBReceiver(self.gpib_plotter)
             self.connect(self.captureThread, SIGNAL('plotReceived(int)'),
                          self.plotReceived)
@@ -342,7 +353,6 @@
                 self._startstop.wait(self._startstopmutex)
                 self._capturing = True
             self._startstopmutex.unlock()
-        
             plot = self.gpibplotter.load_plot(wait_timeout=0.1)
             timestamp = time.time()
             if plot:
--- a/pygpibtoolkit/qt4/qpreferences.py	Fri Sep 05 00:23:55 2008 +0200
+++ b/pygpibtoolkit/qt4/qpreferences.py	Fri Sep 05 00:25:19 2008 +0200
@@ -61,7 +61,7 @@
         if obj is None: #when called from the class, return the Item itself
             return self
         try:
-            return obj.getPref(self._id)
+            return self._type(obj.getPref(self._id))
         except Exception, e:
             return None
 
@@ -113,6 +113,15 @@
             return True
         except:
             return False
+
+class BoolItem(BaseItem):
+    _type = bool
+    def validate(self, value):
+        try:
+            self._type(value)
+            return True
+        except:
+            return False
     
 class AbstractPreferences(QtCore.QObject):
     def __init__(self):
--- a/pygpibtoolkit/qt4/qpreferenceseditor.py	Fri Sep 05 00:23:55 2008 +0200
+++ b/pygpibtoolkit/qt4/qpreferenceseditor.py	Fri Sep 05 00:25:19 2008 +0200
@@ -96,6 +96,26 @@
     def getValue(self):
         return self._editor.value()
 
+
+class BoolEditor(BaseEditor):
+    _accepts = "BoolItem"
+    def setupUI(self):
+        self._editor = QtGui.QCheckBox(self)
+        l = QtGui.QHBoxLayout(self)
+        l.setContentsMargins(0,0,0,0)
+        l.addWidget(self._editor, 1)
+        self.setFocusProxy(self._editor)
+
+    def setValue(self, value):
+        if value:
+            value = Qt.Checked
+        else:
+            value = Qt.Unchecked            
+        self._editor.setCheckState(value)
+
+    def getValue(self):
+        return self._editor.checkState() == Qt.Checked
+    
 class ColorEditor(BaseEditor):
     _accepts = "ColorItem"
     def setupUI(self):

mercurial