1
2 """Plots a collection of timelines"""
3
4 from PyFoam.Error import warning,error
5
6 from PyFoam.Basics.CustomPlotInfo import readCustomPlotInfo,CustomPlotInfo
7
8 from GeneralPlotTimelines import GeneralPlotTimelines
9
10 from os import uname
11
12 firstTimeImport=True
13 app=None
14
15
17 """This class opens a Qt-window and plots a timelines-collection in aQwt.Plot-widget"""
18
19 figureNr=1
20
21 - def __init__(self,
22 timelines,
23 custom,
24 showWindow=True,
25 registry=None):
26 """@param timelines: The timelines object
27 @type timelines: TimeLineCollection
28 @param custom: A CustomplotInfo-object. Values in this object usually override the
29 other options
30 """
31
32 try:
33 global Qt,Qwt,app
34
35 from PyQt4 import Qt
36 import PyQt4.Qwt5 as Qwt
37
38 if showWindow and app==None:
39 app = Qt.QApplication([])
40
41 except ImportError:
42 error("Could not import Qt4 or Qwt")
43
44 GeneralPlotTimelines.__init__(self,timelines,custom,showWindow=showWindow,registry=registry)
45
46 self.figNr=QwtPlotTimelines.figureNr
47 QwtPlotTimelines.figureNr+=1
48
49 self.figure=None
50 self.title="no title"
51
52 self.ylabel="no label"
53 self.ylabel2="no label"
54 try:
55 if self.spec.ylabel:
56 self.setYLabel(self.spec.ylabel)
57 except AttributeError:
58 pass
59 try:
60 if self.spec.y2label:
61 self.setYLabel2(self.spec.y2label)
62 except AttributeError:
63 pass
64
65 self.axis1=None
66 self.axis2=None
67
68 self.setTitle(self.spec.theTitle)
69
70 self.with_=self.spec.with_
71 if not self.with_ in ['lines']:
72 warning("'with'-style",self.with_,"not implemented, using 'lines'")
73 self.with_='lines'
74
75 self.curves={}
76
77 self.redo()
78
79 - def buildData(self,times,name,title,lastValid):
80 """Build the implementation specific data
81 @param times: The vector of times for which data exists
82 @param name: the name under which the data is stored in the timeline
83 @param title: the title under which this will be displayed"""
84
85 if self.figure==None:
86 return
87
88 axis=self.axis1
89 if name in self.alternate:
90 a=self.axis2
91 data=self.data.getValues(name)
92 tm=times
93 if len(tm)>0 and not lastValid:
94 tm=tm[:-1]
95 data=data[:-1]
96 plotIt=True
97 try:
98 if self.spec.logscale and min(data)<=0:
99 plotIt=False
100 except AttributeError:
101 pass
102
103 if not plotIt:
104 return
105
106 if name not in self.curves:
107 a=Qwt.QwtPlotCurve(title)
108 print "Plot",dir(a)
109 a.attach(self.figure)
110 a.setPen(Qt.QPen(Qt.Qt.red))
111 self.curves[name]=a
112 self.figure.update()
113
114 a=self.curves[name]
115 a.setData(tm,data)
116
117 self.figure.replot()
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
147 """Prepare the plotting window"""
148 if self.figure:
149 return
150 self.figure=Qwt.QwtPlot()
151 self.figure.setCanvasBackground(Qt.Qt.white)
152 self.figure.canvas().setFrameStyle(Qt.QFrame.Box | Qt.QFrame.Plain)
153 self.figure.canvas().setLineWidth(1)
154 for i in range(Qwt.QwtPlot.axisCnt):
155 scaleWidget = self.figure.axisWidget(i)
156 if scaleWidget:
157 scaleWidget.setMargin(0)
158 scaleDraw = self.figure.axisScaleDraw(i)
159 if scaleDraw:
160 scaleDraw.enableComponent(
161 Qwt.QwtAbstractScaleDraw.Backbone, False)
162 self.figure.setTitle("Figure: %d - %s" % (self.figNr,self.title))
163 self.figure.insertLegend(Qwt.QwtLegend(), Qwt.QwtPlot.BottomLegend)
164
165 self.figure.setAxisTitle(Qwt.QwtPlot.xBottom, "Time")
166 self.figure.setAxisTitle(Qwt.QwtPlot.yLeft, self.ylabel)
167 self.axis1=Qwt.QwtPlot.yLeft
168 if len(self.alternate)>0:
169 self.figure.enableAxis(Qwt.QwtPlot.yRight)
170 self.figure.setAxisTitle(Qwt.QwtPlot.yRight, self.ylabel2)
171 self.axis2=Qwt.QwtPlot.yRight
172
173 if self.spec.logscale:
174 self.figure.setAxisScaleEngine(Qwt.QwtPlot.yLeft,
175 Qwt.QwtLog10ScaleEngine())
176 if len(self.alternate)>0:
177 self.figure.setAxisScaleEngine(Qwt.QwtPlot.yRight,
178 Qwt.QwtLog10ScaleEngine())
179
180 mY = Qwt.QwtPlotMarker()
181 mY.setLabelAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignTop)
182 mY.setLineStyle(Qwt.QwtPlotMarker.HLine)
183 mY.setYValue(0.0)
184 mY.attach(self.figure)
185
186 self.figure.resize(500,300)
187 self.figure.show()
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
215 """Replot the whole data"""
216
217 self.figure.replot()
218
219
220
221
222
223
224
225
226
227
228
229
234
236 """Sets the label on the first Y-Axis"""
237
238 self.ylabel=title
239
241 """Sets the label on the second Y-Axis"""
242
243 self.ylabel2=title
244
246 """Write the contents of the plot to disk
247 @param filename: Name of the file without type extension
248 @param form: String describing the format"""
249
250 Qt.QPixmap.grabWidget(self.figure).save(filename+"."+form.lower(),form)
251