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