qgpib_plotter.py

Fri, 18 Jan 2008 16:33:51 +0100

author
David Douard <david.douard@logilab.fr>
date
Fri, 18 Jan 2008 16:33:51 +0100
changeset 25
32d0d1cd44c3
parent 23
cb97962a1ae9
child 26
e8f3c9276f3f
permissions
-rw-r--r--

add main menu shotcuts and make first openened file displayed

#

import os, sys

from PyQt4 import QtGui, QtCore, uic
from PyQt4.QtCore import SIGNAL, Qt

from gpib_plotter import GPIBplotter
from hpgl_qt import QHPGLPlotterWidget

form_class, base_class = uic.loadUiType(os.path.join(os.path.dirname(__file__), "qhpgl_plotter.ui"))

from qpreferences import PreferenceItem, AbstractPreferences, PreferencesEditor

class Preferences(AbstractPreferences):
    ORGANISATION="Logilab"
    APPLICATION="qgpib_plotter"

    device = PreferenceItem('/dev/ttyUSB0', name=u'device', description=u'GPIB device')
    address = PreferenceItem(5, name=u'GPIB address')
    _pos = PreferenceItem(basetype=QtCore.QPoint)
    _size = PreferenceItem(basetype=QtCore.QSize)
    _appState = PreferenceItem(basetype=QtCore.QByteArray)
    
class QtHPGLPlotter(QtGui.QMainWindow, form_class):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self._plots = {}        
        self._prefs = Preferences()
        self.setupUi()
        self.initializeGPIB()
        if self._prefs._pos:
            self.move(self._prefs._pos)
        if self._prefs._size:
            self.resize(self._prefs._size)
        if self._prefs._appState:
            self.restoreState(self._prefs._appState)

    def setupUi(self):
        form_class.setupUi(self, self) # call qtdesigner generated form creation        
        # actions defined in designer
        self.connect(self.actionPreferences, SIGNAL('triggered(bool)'),
                     self.preferencesTriggered)
        self.connect(self.actionQuit, SIGNAL('triggered(bool)'),
                     self.quitTriggered)
        self.actionQuit.setShortcut(QtGui.QKeySequence(u'Ctrl+Q'))
        self.connect(self.actionOpen, SIGNAL('triggered(bool)'),
                     self.openTriggered)
        self.actionOpen.setShortcut(QtGui.QKeySequence(u'Ctrl+O'))
        self.connect(self.actionSave, SIGNAL('triggered(bool)'),
                     self.saveTriggered)
        self.actionSave.setShortcut(QtGui.QKeySequence(u'Ctrl+S'))
        self.connect(self.actionSaveAs, SIGNAL('triggered(bool)'),
                     self.saveAsTriggered)

        self.plotterWidget = QHPGLPlotterWidget(self)
        self.setCentralWidget(self.plotterWidget)

        self.connect(self.captureButton, SIGNAL("toggled(bool)"),
                     self.captureToggled)

        self._plots_list = QtGui.QStringListModel()
        self.plotsView.setModel(self._plots_list)
        self.connect(self.plotsView, SIGNAL('activated(const QModelIndex&)'),
                     self.currentPlotChanged)
        self.connect(self.plotsView.selectionModel(),
                     SIGNAL('currentChanged(const QModelIndex&, const QModelIndex&)'),
                     self.currentPlotChanged)

    def currentPlotChanged(self, index, old_index=None):
        if index.isValid():
            value = unicode(self.plotsView.model().data(index, Qt.DisplayRole).toString())
            
            #self.plotterWidget = QHPGLPlotterWidget(self)
            #self.setCentralWidget(self.plotterWidget)
            self.plotterWidget.clear()
            self.plotterWidget.parse(self._plots[value])
            
    def preferencesTriggered(self, checked=False):
        PreferencesEditor(self._prefs, self).exec_()

    def quitTriggered(self, checked=False):
        self.close()

    def closeEvent(self, event):
        if 1:
        #if self.promptForSave():
            self._prefs._pos = self.pos()
            self._prefs._size = self.size()
            self._prefs._appState = self.saveState()
            event.accept()
        else:
            event.ignore()
        
    def openTriggered(self, checked=False):
        filenames = QtGui.QFileDialog.getOpenFileNames(self, "Open a HPGL file to display", '.', 'HPGL files (*.plt)\nAll files (*)')
        for filename in filenames:
            filename = str(filename)
            if os.path.exists(filename):
                data = open(filename).read()
                name = os.path.basename(filename)
                name = os.path.splitext(name)[0]
                lst = self.plotsView.model().stringList()
                lst.append(name)            
                self._plots[name] = data
                self.plotsView.model().setStringList(lst)

        if not self.plotsView.currentIndex().isValid():
            self.plotsView.setCurrentIndex(self.plotsView.model().index(0, 0))
        
    def saveTriggered(self, checked=False):
        print "save"
    def saveAsTriggered(self, checked=False):
        print "saveAs"
        
    def initializeGPIB(self):
        try:
            self.gpib_plotter = GPIBplotter(device=self._prefs.device,
                                            address=self._prefs.address,
                                            )
        except:
            self.gpib_plotter = None
        self._online = False
        self.setCaptureLed()
        
    def captureToggled(self, state):
        if state:
            if self.gpib_plotter is None:
                self.initializeGPIB()
                if self.gpib_plotter is None:
                    QtGui.QMessageBox.critical(self, self.tr("GPIB error"),
                                               self.tr("<b>Unable to initialize GPIB connection</b>.<br>Please check your GPIB dongle and settings."))
                    self._online = False
                else:
                    # todo
                    self._online = True
        else:
            self._online = False
        self.setCaptureLed()
            
            
    def setCaptureLed(self):
        if self._online:
            icn = QtGui.QIcon(':/icons/led_green.png')
        else:
            icn = QtGui.QIcon(':/icons/led_green_off.png')
        self.captureButton.setIcon(icn)
        self.captureButton.setChecked(self._online)
        
if __name__ == '__main__':
    a = QtGui.QApplication([])
    w = QtHPGLPlotter()
    w.show()
    a.exec_()
        

mercurial