hpgl_qt.py

Sun, 20 Jan 2008 17:09:31 +0100

author
David Douard <david.douard@logilab.fr>
date
Sun, 20 Jan 2008 17:09:31 +0100
changeset 26
e8f3c9276f3f
parent 23
cb97962a1ae9
permissions
-rw-r--r--

add a 'receiving a plot' led

21
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
2 import numpy
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
3 import pylab
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
4 import math
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
5 import new # monkey patching...
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
6
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
7 from PyQt4 import QtGui, QtCore
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
8 from PyQt4.QtCore import Qt, SIGNAL
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
9
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
10 from hpgl_parser import HPGLParser
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
11
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
12 class QHPGLPlotterWidget(HPGLParser, QtGui.QWidget):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
13 pen_styles = {2: [0.4, 0.6],
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
14 3: [0.6, 0.4],
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
15 4: [0.7, 0.1, 0.1, 0.1],
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
16 5: [0.6, 0.1, 0.2, 0.1],
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
17 6: [0.45, 0.1, 0.1, 0.2, 0.1, 0.05],
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
18 }
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
19 pen_colors = ["black", "green", "red", "blue", "yellow", "cyan", "magenta",
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
20 "darkred", "darkblue"]
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
21
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
22 def __init__(self, parent=None):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
23 QtGui.QWidget.__init__(self, parent)
23
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
24 l = QtGui.QVBoxLayout(self)
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
25 l.setMargin(1)
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
26 self.qview = QtGui.QGraphicsView(self)
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
27 self.qview.scale(0.5,-0.5)
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
28 l = self.layout()
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
29 l.addWidget(self.qview)
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
30 self.setBackgroundRole(QtGui.QPalette.Base)
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
31 self.setSizePolicy(QtGui.QSizePolicy.Expanding,
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
32 QtGui.QSizePolicy.Expanding)
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
33 self.clear()
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
34 HPGLParser.__init__(self)
21
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
35
23
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
36 def parse(self, data):
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
37 HPGLParser.parse(self, data)
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
38 self.resize(self.size())
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
39
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
40 def clear(self):
21
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
41 self.qpen = QtGui.QPen(QtCore.Qt.blue)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
42 self.qbrush = QtGui.QBrush(QtCore.Qt.blue)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
43 self.qfont = QtGui.QFont('Courier')
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
44 self.qantialiased = False
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
45 self.qtransformed = False
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
46 self.qscene = QtGui.QGraphicsScene()
23
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
47 self.qview.setScene(self.qscene)
cb97962a1ae9 make qplotter a working app: can new load several hpgl files à display them; have a preference system
David Douard <david.douard@logilab.fr>
parents: 21
diff changeset
48
21
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
49 def _get_PW(self):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
50 return self._pen_width
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
51 def _set_PW(self, value):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
52 value = float(value)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
53 #print "set pen width", value, value*300.0/75.0
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
54 #self.qpen.setWidthF(value*300.0/75.0)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
55 self._pen_width = value
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
56 pen_width = property(_get_PW, _set_PW)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
57
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
58 def LT(self):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
59 HPGLParser.LT(self)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
60 if self.line_type == 0:
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
61 self.qpen.setStyle(Qt.SolidLine)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
62 elif self.line_type == 1:
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
63 self.qpen.setStyle(Qt.DotLine)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
64 else:
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
65 pattern = numpy.array(self.pen_styles[self.line_type])
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
66 x0, x1, y0, y1 = self.scale
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
67 dist = math.sqrt((x1-x0)**2 + (y1-y0)**2)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
68 pattern = self.pattern_len*dist/100.0 * pattern
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
69 if self.qpen.widthF()>0:
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
70 pattern = pattern/self.qpen.widthF()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
71 self.qpen.setDashPattern(pattern.tolist())
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
72
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
73 def SP(self):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
74 HPGLParser.SP(self)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
75 self.qpen.setColor(QtGui.QColor(self.pen_colors[self.pen]))
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
76
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
77 def parse(self, data):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
78 HPGLParser.parse(self, data)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
79 self.update()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
80 self.qview.fitInView(self.qscene.sceneRect(), Qt.KeepAspectRatio)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
81
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
82 def minimumSizeHint(self):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
83 return QtCore.QSize(100, 100)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
84
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
85 def sizeHint(self):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
86 return QtCore.QSize(400, 400)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
87
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
88 def resizeEvent(self, event):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
89 self.qview.fitInView(self.qscene.sceneRect(), Qt.KeepAspectRatio)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
90 QtGui.QWidget.resizeEvent(self, event)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
91
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
92 def plot_lines(self, points):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
93 if len(points) == 1:
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
94 return
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
95 pa = QtGui.QPainterPath()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
96 pa.addPolygon(QtGui.QPolygonF([QtCore.QPointF(*p) for p in points]))
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
97 self.qscene.addPath(pa, self.qpen)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
98
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
99 def plot_symbols(self, points):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
100 pass
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
101
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
102 def plot_string(self, s):
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
103 cw, ch = self.get_char_size()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
104 # this is very ugly and so, but I don't understand haw is
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
105 # computed string positionning in HPGL...
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
106 t0 = self.qscene.addSimpleText(" ", self.qfont)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
107 br = t0.boundingRect()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
108 mw = br.width()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
109 mh = br.height()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
110 self.qscene.removeItem(t0)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
111 # don't ask me why theses constants are here, they seem OK, that's all
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
112 t = self.qscene.addSimpleText(s, self.qfont)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
113 t.scale(1.5 * cw/mw, -2.0 * ch/mh)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
114 x, y = self.pos
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
115 t.moveBy(x,y)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
116 br = t.boundingRect()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
117 t.moveBy(-0.5*cw, math.sqrt(2)*ch)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
118
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
119
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
120 if __name__=='__main__':
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
121 import sys
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
122 a = QtGui.QApplication(sys.argv)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
123 mw = QtGui.QMainWindow()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
124 w = QtGui.QWidget(mw)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
125 mw.setCentralWidget(w)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
126
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
127 l = QtGui.QVBoxLayout(w)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
128 p = QHPGLPlotterWidget(w)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
129 l.addWidget(p, 1)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
130 b = QtGui.QPushButton("Replot", w)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
131 l.addWidget(b)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
132 def plot():
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
133 p.parse(open(sys.argv[1]).read())
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
134 b.connect(b, SIGNAL('pressed()'), plot)
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
135 mw.show()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
136 plot()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
137 a.exec_()
66b7cd43bac1 Add HPGL plotter backend for Qt4
David Douard <david.douard@logilab.fr>
parents:
diff changeset
138

mercurial