# HG changeset patch # User David Douard # Date 1200442802 -3600 # Node ID cb97962a1ae98e3475682b24649636cb64368fbf # Parent b2f4646161bea2f5adad9d9cc90a69478b110f69 make qplotter a working app: can new load several hpgl files à display them; have a preference system diff -r b2f4646161be -r cb97962a1ae9 hpgl_plotter.qrc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hpgl_plotter.qrc Wed Jan 16 01:20:02 2008 +0100 @@ -0,0 +1,8 @@ + + + icons/led_green.png + icons/led_green.svg + icons/led_green_off.png + icons/led_green_off.svg + + diff -r b2f4646161be -r cb97962a1ae9 hpgl_qt.py --- a/hpgl_qt.py Fri Jan 11 18:50:46 2008 +0100 +++ b/hpgl_qt.py Wed Jan 16 01:20:02 2008 +0100 @@ -21,22 +21,31 @@ def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) + l = QtGui.QVBoxLayout(self) + l.setMargin(1) + self.qview = QtGui.QGraphicsView(self) + self.qview.scale(0.5,-0.5) + l = self.layout() + l.addWidget(self.qview) + self.setBackgroundRole(QtGui.QPalette.Base) + self.setSizePolicy(QtGui.QSizePolicy.Expanding, + QtGui.QSizePolicy.Expanding) + self.clear() + HPGLParser.__init__(self) + def parse(self, data): + HPGLParser.parse(self, data) + self.resize(self.size()) + + def clear(self): self.qpen = QtGui.QPen(QtCore.Qt.blue) self.qbrush = QtGui.QBrush(QtCore.Qt.blue) self.qfont = QtGui.QFont('Courier') self.qantialiased = False self.qtransformed = False self.qscene = QtGui.QGraphicsScene() - self.qview = QtGui.QGraphicsView(self.qscene, self) - self.qview.scale(0.5,-0.5) - l = QtGui.QVBoxLayout(self) - l.addWidget(self.qview) - self.setBackgroundRole(QtGui.QPalette.Base) - self.setSizePolicy(QtGui.QSizePolicy.Expanding, - QtGui.QSizePolicy.Expanding) - HPGLParser.__init__(self) - + self.qview.setScene(self.qscene) + def _get_PW(self): return self._pen_width def _set_PW(self, value): diff -r b2f4646161be -r cb97962a1ae9 icons/led_green.png Binary file icons/led_green.png has changed diff -r b2f4646161be -r cb97962a1ae9 icons/led_green.svg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icons/led_green.svg Wed Jan 16 01:20:02 2008 +0100 @@ -0,0 +1,142 @@ + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + diff -r b2f4646161be -r cb97962a1ae9 icons/led_green_off.png Binary file icons/led_green_off.png has changed diff -r b2f4646161be -r cb97962a1ae9 icons/led_green_off.svg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icons/led_green_off.svg Wed Jan 16 01:20:02 2008 +0100 @@ -0,0 +1,114 @@ + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + diff -r b2f4646161be -r cb97962a1ae9 qgpib_plotter.py --- a/qgpib_plotter.py Fri Jan 11 18:50:46 2008 +0100 +++ b/qgpib_plotter.py Wed Jan 16 01:20:02 2008 +0100 @@ -1,34 +1,152 @@ # -from PyQt4 import QtGui, QtCore +import os, sys + +from PyQt4 import QtGui, QtCore, uic from PyQt4.QtCore import SIGNAL, Qt -from gppib_plotter import GPIBplotter +from gpib_plotter import GPIBplotter from hpgl_qt import QHPGLPlotterWidget -ORGANISATION="Logilab" -APPLICATION="qgpib_plotter" -def variant(v): - _cvrts = {1: lambda x:x.toInt(), - 10: lambda x:unicode(x.toString()), - } - t = v.userType() +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" -class Preferences(object): - _defaults = {'device': '/dev/ttyUSB0', - 'address': 0, - } - def __init__(self): - self._settings = qc.QSettings(qc.QSettings.UserScope, - ORGANISATION, APPLICATION) - - def getPref(self, key): - val = self._settings.value(key, QtCore.QVariant(self._defaults[key])) - - -class QtHPGLPlotter(QtGui.QMainWindow): + 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.gpib_plotter = GPIBplotter( + 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.connect(self.actionOpen, SIGNAL('triggered(bool)'), + self.openTriggered) + self.connect(self.actionSave, SIGNAL('triggered(bool)'), + self.saveTriggered) + 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) + #self.plotsView.setCurrentIndex(self.plotsView.model().index(self.plotsView.model().stringList().count(), 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("Unable to initialize GPIB connection.
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_() + diff -r b2f4646161be -r cb97962a1ae9 qhpgl_plotter.ui --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/qhpgl_plotter.ui Wed Jan 16 01:20:02 2008 +0100 @@ -0,0 +1,130 @@ + + MainWindow + + + + 0 + 0 + 507 + 492 + + + + MainWindow + + + + + + 0 + 0 + 507 + 25 + + + + + &File + + + + + + + + + + Edit + + + + + + + + + + 1 + + + + + 4 + + + 2 + + + 2 + + + 2 + + + 2 + + + + + + 0 + 29 + + + + on line + + + :/icons/led_green.png + + + + 16 + 16 + + + + true + + + Qt::ToolButtonTextBesideIcon + + + + + + + + + + + + &Open + + + + + &Save + + + + + Save as... + + + + + &Quit + + + + + Preferences + + + + + + + + diff -r b2f4646161be -r cb97962a1ae9 qpreferences.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/qpreferences.py Wed Jan 16 01:20:02 2008 +0100 @@ -0,0 +1,141 @@ +import os +from PyQt4 import QtCore, QtGui, uic + +def fromVariant(v): + _cvrts = {0: lambda x:None, + 1: lambda x:x.toBool(), + 2: lambda x:x.toInt()[0], + 6: lambda x:x.toDouble()[0], + 10: lambda x:unicode(x.toString()), + 12: lambda x:x.toByteArray(), + 21: lambda x:x.toSize(), + 22: lambda x:x.toSizeF(), + 25: lambda x:x.toPoint(), + 26: lambda x:x.toPointF(), + + } + t = v.userType() + return _cvrts[t](v) + +class PreferenceItem(object): + _id = 0 + def __init__(self, default=None, basetype=None, name=None, description=None): + self._default = default + self._basetype = basetype + if self._basetype is None: + self._basetype = self._default.__class__ + self._id = "_pref%X"%self.__class__._id + self._name = name + self._description = description + self.__class__._id += 1 + + def __get__(self, obj, cls): + if obj is None: + return self + try: + return obj.getPref(self._id) + except Exception, e: + #print "humm", e + return None + + def __set__(self, obj, value): + obj.setPref(self._id, value) + +class AbstractPreferences(QtCore.QObject): + def __init__(self): + QtCore.QObject.__init__(self) + self._settings = QtCore.QSettings(QtCore.QSettings.UserScope, + self.ORGANISATION, self.APPLICATION) + self._prefs = {} + for k in dir(self.__class__): + item = getattr(self.__class__, k) + if isinstance(item, PreferenceItem): + self._prefs[item._id] = k + + def getPref(self, key): + key = self._prefs.get(key, key) + default = getattr(self.__class__, key)._default + if default is not None: + default = QtCore.QVariant(default) + else: + default = QtCore.QVariant() + val = self._settings.value(key, default) + return fromVariant(val) + + def setPref(self, key, value): + key = self._prefs.get(key, key) + self._settings.setValue(key, QtCore.QVariant(value)) + + def keys(self): + return [k for k in self._prefs.values() if not k.startswith('_')] + + def getName(self, key): + item = getattr(self.__class__, key) + return item._name + + def getDescription(self, key): + item = getattr(self.__class__, key) + return item._description + +form_class, base_class = uic.loadUiType(os.path.join(os.path.dirname(__file__), "qpreferences_dialog.ui")) + +class PreferencesEditor(QtGui.QDialog, form_class): + def __init__(self, preferences, parent=None): + QtGui.QDialog.__init__(self, parent) + self.setupUi(self) + self._prefs = preferences + self.buildUI() + + def buildUI(self): + w = self.centralFrame + g = QtGui.QGridLayout(w) + p = self._prefs + eds = {} + self._editors = eds + for i, k in enumerate(p.keys()): + name = p.getName(k) + if not name: + name = k + l = QtGui.QLabel(name, w) + g.addWidget(l, i, 0) + if p.getDescription(k): + l.setToolTip(p.getDescription(k)) + + e = QtGui.QLineEdit(w) + eds[k] = e + g.addWidget(e, i, 1) + val = p.getPref(k) + if val is None: + val = '' + if not isinstance(val, basestring): + val = unicode(val) + e.setText(val) + + g.addWidget(QtGui.QWidget(w), i+1, 0) + g.setRowStretch(i+1,1) + g.setColumnStretch(1,1) + + def accept(self): + p=self._prefs + for k in p.keys(): + newval = unicode(self._editors[k].text()) + p.setPref(k, newval) + return QtGui.QDialog.accept(self) + +if __name__ == '__main__': + class TestPreferences(AbstractPreferences): + ORGANISATION="Logilab" + APPLICATION="test_qpref_editor" + + device = PreferenceItem('/dev/ttyUSB0', name="the device") + address = PreferenceItem(5, description="GPIB address of the plotter") + _pos = PreferenceItem(None) + _size = PreferenceItem(None) + _appState = PreferenceItem(None) + + a = QtGui.QApplication([]) + + prefs = TestPreferences() + w = PreferencesEditor(prefs) + w.show() + a.exec_() diff -r b2f4646161be -r cb97962a1ae9 qpreferences_dialog.ui --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/qpreferences_dialog.ui Wed Jan 16 01:20:02 2008 +0100 @@ -0,0 +1,73 @@ + + PreferencesDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + PreferencesDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + PreferencesDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +