1 | #!/usr/bin/python |
---|
2 | |
---|
3 | """ |
---|
4 | |
---|
5 | Dialog for general graph appearance |
---|
6 | |
---|
7 | |
---|
8 | /** |
---|
9 | This software was developed by Institut Laue-Langevin as part of |
---|
10 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE). |
---|
11 | |
---|
12 | Copyright 2012 Institut Laue-Langevin |
---|
13 | |
---|
14 | **/ |
---|
15 | |
---|
16 | |
---|
17 | """ |
---|
18 | |
---|
19 | import wx |
---|
20 | from matplotlib.font_manager import FontProperties |
---|
21 | from danse.common.plottools.SimpleFont import SimpleFont |
---|
22 | |
---|
23 | COLOR = ['black', 'blue', 'green', 'red', 'cyan', 'magenta', 'yellow'] |
---|
24 | |
---|
25 | |
---|
26 | class graphAppearance(wx.Frame): |
---|
27 | |
---|
28 | def __init__(self,parent,title,legend=True): |
---|
29 | super(graphAppearance,self).__init__(parent, title=title,size=(520,435)) |
---|
30 | |
---|
31 | self.legend = legend |
---|
32 | |
---|
33 | self.InitUI() |
---|
34 | self.Centre() |
---|
35 | self.Show() |
---|
36 | |
---|
37 | self.xfont = None |
---|
38 | self.yfont = None |
---|
39 | |
---|
40 | |
---|
41 | def InitUI(self): |
---|
42 | |
---|
43 | panel = wx.Panel(self) |
---|
44 | |
---|
45 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
46 | hbox1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
47 | hbox2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
48 | |
---|
49 | xhbox1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
50 | xhbox2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
51 | yhbox1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
52 | yhbox2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | if self.legend: |
---|
57 | legendLocText = wx.StaticText(panel, label='Legend location: ') |
---|
58 | self.legendLocCombo = wx.ComboBox(panel,style = wx.CB_READONLY, size=(180,-1)) |
---|
59 | self.fillLegendLocs() |
---|
60 | else: |
---|
61 | self.legendLocCombo = None |
---|
62 | |
---|
63 | |
---|
64 | if self.legend: |
---|
65 | self.toggleLegend = wx.CheckBox(panel, label='Toggle legend on/off') |
---|
66 | else: |
---|
67 | self.toggleLegend = None |
---|
68 | |
---|
69 | self.toggleGrid = wx.CheckBox(panel, label='Toggle grid on/off') |
---|
70 | |
---|
71 | |
---|
72 | xStaticBox = wx.StaticBox(panel,-1, 'x-axis label') |
---|
73 | xStaticBoxSizer = wx.StaticBoxSizer(xStaticBox, wx.VERTICAL) |
---|
74 | yStaticBox = wx.StaticBox(panel,-1, 'y-axis label') |
---|
75 | yStaticBoxSizer = wx.StaticBoxSizer(yStaticBox, wx.VERTICAL) |
---|
76 | |
---|
77 | |
---|
78 | xaxisLabel = wx.StaticText(panel, label='X-axis: ') |
---|
79 | yaxisLabel = wx.StaticText(panel, label='Y-axis: ') |
---|
80 | unitLabel1 = wx.StaticText(panel, label='Units: ') |
---|
81 | unitLabel2 = wx.StaticText(panel, label='Units: ') |
---|
82 | |
---|
83 | self.xaxisText = wx.TextCtrl(panel,-1,"",size=(220,-1)) |
---|
84 | self.yaxisText = wx.TextCtrl(panel,-1,"",size=(220,-1)) |
---|
85 | |
---|
86 | self.xaxisUnitText = wx.TextCtrl(panel,-1,"",size=(100,-1)) |
---|
87 | self.yaxisUnitText = wx.TextCtrl(panel,-1,"",size=(100,-1)) |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | xcolorLabel = wx.StaticText(panel, label='Font color: ') |
---|
92 | self.xfontColor = wx.ComboBox(panel,size=(100,-1),style=wx.CB_READONLY) |
---|
93 | self.xfillColors() |
---|
94 | self.xfontColor.SetSelection(0) |
---|
95 | xfontButton = wx.Button(panel, label='Font') |
---|
96 | xfontButton.Bind(wx.EVT_BUTTON,self.onxFont) |
---|
97 | |
---|
98 | ycolorLabel = wx.StaticText(panel, label='Font color: ') |
---|
99 | self.yfontColor = wx.ComboBox(panel,size=(100,-1),style=wx.CB_READONLY) |
---|
100 | self.yfillColors() |
---|
101 | self.yfontColor.SetSelection(0) |
---|
102 | yfontButton = wx.Button(panel, label='Font') |
---|
103 | yfontButton.Bind(wx.EVT_BUTTON,self.onyFont) |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | self.cancelButton = wx.Button(panel, label='Cancel') |
---|
108 | self.okButton = wx.Button(panel, label='OK') |
---|
109 | |
---|
110 | self.cancelButton.Bind(wx.EVT_BUTTON,self.onCancel) |
---|
111 | self.okButton.Bind(wx.EVT_BUTTON,self.onOK) |
---|
112 | |
---|
113 | |
---|
114 | xhbox1.Add(xaxisLabel,flag= wx.ALL | wx.EXPAND | wx.ALIGN_LEFT,border=10) |
---|
115 | xhbox1.Add(self.xaxisText,flag=wx.ALL | wx.EXPAND | wx.ALIGN_LEFT,border=10) |
---|
116 | xhbox1.Add(unitLabel1,flag=wx.ALL | wx.EXPAND | wx.ALIGN_RIGHT,border=10) |
---|
117 | xhbox1.Add(self.xaxisUnitText, flag=wx.ALL | wx.EXPAND | wx.ALIGN_RIGHT,border=10) |
---|
118 | |
---|
119 | yhbox1.Add(yaxisLabel,flag= wx.ALL | wx.EXPAND | wx.ALIGN_LEFT,border=10) |
---|
120 | yhbox1.Add(self.yaxisText,flag=wx.ALL | wx.EXPAND | wx.ALIGN_LEFT,border=10) |
---|
121 | yhbox1.Add(unitLabel2,flag=wx.ALL | wx.EXPAND | wx.ALIGN_RIGHT,border=10) |
---|
122 | yhbox1.Add(self.yaxisUnitText, flag=wx.ALL | wx.EXPAND | wx.ALIGN_RIGHT,border=10) |
---|
123 | |
---|
124 | xhbox2.Add(xcolorLabel,flag=wx.ALL | wx.ALIGN_RIGHT, border=10) |
---|
125 | xhbox2.Add(self.xfontColor,flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
126 | xhbox2.Add(xfontButton,flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
127 | |
---|
128 | yhbox2.Add(ycolorLabel,flag=wx.ALL | wx.ALIGN_RIGHT, border=10) |
---|
129 | yhbox2.Add(self.yfontColor,flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
130 | yhbox2.Add(yfontButton,flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
131 | |
---|
132 | if self.legend: |
---|
133 | hbox1.Add(legendLocText, flag = wx.ALL | wx.EXPAND | wx.ALIGN_LEFT, border=5) |
---|
134 | hbox1.Add(self.legendLocCombo, flag = wx.ALL | wx.EXPAND | wx.ALIGN_LEFT, border=5) |
---|
135 | |
---|
136 | if self.legend: |
---|
137 | hbox1.Add((5,-1)) |
---|
138 | hbox1.Add(self.toggleLegend, flag = wx.ALL | wx.EXPAND | wx.ALIGN_LEFT, border=5) |
---|
139 | |
---|
140 | hbox2.Add(self.okButton, flag = wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
141 | hbox2.Add(self.cancelButton, flag = wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
142 | hbox2.Add((15,-1)) |
---|
143 | |
---|
144 | xStaticBoxSizer.Add(xhbox1,flag= wx.EXPAND ,border=5) |
---|
145 | xStaticBoxSizer.Add(xhbox2, flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
146 | yStaticBoxSizer.Add(yhbox1,flag= wx.EXPAND ,border=5) |
---|
147 | yStaticBoxSizer.Add(yhbox2, flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
148 | |
---|
149 | vbox.Add((-1,20)) |
---|
150 | vbox.Add(hbox1, flag = wx.EXPAND | wx.ALL, border=5) |
---|
151 | vbox.Add(xStaticBoxSizer, flag = wx.ALL | wx.EXPAND, border=10) |
---|
152 | vbox.Add(yStaticBoxSizer, flag = wx.ALL | wx.EXPAND, border=10) |
---|
153 | |
---|
154 | vbox.Add(self.toggleGrid, flag = wx.ALIGN_RIGHT | wx.RIGHT, border=20) |
---|
155 | vbox.Add(hbox2, flag = wx.ALIGN_RIGHT | wx.ALL , border=5) |
---|
156 | |
---|
157 | |
---|
158 | panel.SetSizer(vbox) |
---|
159 | |
---|
160 | def xfillColors(self): |
---|
161 | list = COLOR |
---|
162 | for idx in range(len(list)): |
---|
163 | self.xfontColor.Append(list[idx],idx) |
---|
164 | |
---|
165 | def yfillColors(self): |
---|
166 | list = COLOR |
---|
167 | for idx in range(len(list)): |
---|
168 | self.yfontColor.Append(list[idx],idx) |
---|
169 | |
---|
170 | def onxFont(self,e): |
---|
171 | title = 'Modify x axis font' |
---|
172 | |
---|
173 | fonty = SimpleFont(self,wx.NewId(),title) |
---|
174 | fonty.set_default_font(self.xfont) |
---|
175 | if(fonty.ShowModal() == wx.ID_OK): |
---|
176 | self.xfont = fonty.get_font() |
---|
177 | |
---|
178 | def onyFont(self,e): |
---|
179 | title = 'Modify y axis font' |
---|
180 | fonty = SimpleFont(self,wx.NewId(),title) |
---|
181 | fonty.set_default_font(self.yfont) |
---|
182 | if(fonty.ShowModal() == wx.ID_OK): |
---|
183 | self.yfont = fonty.get_font() |
---|
184 | |
---|
185 | def onOK(self,e): |
---|
186 | self.Close() |
---|
187 | |
---|
188 | def onCancel(self, e): |
---|
189 | self.Destroy() |
---|
190 | |
---|
191 | |
---|
192 | def get_loc_label(self): |
---|
193 | """ |
---|
194 | Associates label to a specific legend location |
---|
195 | """ |
---|
196 | _labels = {} |
---|
197 | i = 0 |
---|
198 | _labels['best'] = i |
---|
199 | i += 1 |
---|
200 | _labels['upper right'] = i |
---|
201 | i += 1 |
---|
202 | _labels['upper left'] = i |
---|
203 | i += 1 |
---|
204 | _labels['lower left'] = i |
---|
205 | i += 1 |
---|
206 | _labels['lower right'] = i |
---|
207 | i += 1 |
---|
208 | _labels['right'] = i |
---|
209 | i += 1 |
---|
210 | _labels['center left'] = i |
---|
211 | i += 1 |
---|
212 | _labels['center right'] = i |
---|
213 | i += 1 |
---|
214 | _labels['lower center'] = i |
---|
215 | i += 1 |
---|
216 | _labels['upper center'] = i |
---|
217 | i += 1 |
---|
218 | _labels['center'] = i |
---|
219 | return _labels |
---|
220 | |
---|
221 | |
---|
222 | def fillLegendLocs(self): |
---|
223 | |
---|
224 | # labels = [] |
---|
225 | # for label in self.get_loc_label(): |
---|
226 | # labels.append(str(label)) |
---|
227 | |
---|
228 | # for label in reversed(labels): |
---|
229 | # self.legendLocCombo.Append(label) |
---|
230 | for label in self.get_loc_label(): |
---|
231 | self.legendLocCombo.Append(label) |
---|
232 | |
---|
233 | |
---|
234 | def setDefaults(self,grid,legend,xlab,ylab,xunit,yunit, |
---|
235 | xaxis_font,yaxis_font,legend_loc, |
---|
236 | xcolor,ycolor): |
---|
237 | self.toggleGrid.SetValue(grid) |
---|
238 | if self.legend: |
---|
239 | self.toggleLegend.SetValue(legend) |
---|
240 | self.xaxisText.SetValue(xlab) |
---|
241 | self.yaxisText.SetValue(ylab) |
---|
242 | self.xaxisUnitText.SetValue(xunit) |
---|
243 | self.yaxisUnitText.SetValue(yunit) |
---|
244 | self.xfont = xaxis_font |
---|
245 | self.yfont = yaxis_font |
---|
246 | |
---|
247 | if not xcolor: |
---|
248 | self.xfontColor.SetSelection(0) |
---|
249 | else: |
---|
250 | self.xfontColor.SetStringSelection(xcolor) |
---|
251 | |
---|
252 | if not ycolor: |
---|
253 | self.yfontColor.SetSelection(0) |
---|
254 | else: |
---|
255 | self.yfontColor.SetStringSelection(ycolor) |
---|
256 | |
---|
257 | |
---|
258 | if self.legend: |
---|
259 | self.legendLocCombo.SetStringSelection(legend_loc) |
---|
260 | |
---|
261 | |
---|
262 | # get whether grid is toggled on/off |
---|
263 | def get_togglegrid(self): |
---|
264 | return self.toggleGrid.GetValue() |
---|
265 | |
---|
266 | # get whether legend is toggled on/off |
---|
267 | def get_togglelegend(self): |
---|
268 | return self.toggleLegend.GetValue() |
---|
269 | |
---|
270 | # get x label |
---|
271 | def get_xlab(self): |
---|
272 | return self.xaxisText.GetValue() |
---|
273 | |
---|
274 | # get y label |
---|
275 | def get_ylab(self): |
---|
276 | return self.yaxisText.GetValue() |
---|
277 | |
---|
278 | # get x unit |
---|
279 | def get_xunit(self): |
---|
280 | return self.xaxisUnitText.GetValue() |
---|
281 | |
---|
282 | # get y unit |
---|
283 | def get_yunit(self): |
---|
284 | return self.yaxisUnitText.GetValue() |
---|
285 | |
---|
286 | # get legend location |
---|
287 | def get_legend_loc(self): |
---|
288 | return self.get_loc_label()[self.legendLocCombo.GetStringSelection()] |
---|
289 | |
---|
290 | # get x axis label color |
---|
291 | def get_xcolor(self): |
---|
292 | return self.xfontColor.GetValue() |
---|
293 | |
---|
294 | # get y axis label color |
---|
295 | def get_ycolor(self): |
---|
296 | return self.yfontColor.GetValue() |
---|
297 | |
---|
298 | # get x axis font (type is FontProperties) |
---|
299 | def get_xfont(self): |
---|
300 | return self.xfont |
---|
301 | |
---|
302 | # get y axis font |
---|
303 | def get_yfont(self): |
---|
304 | return self.yfont |
---|
305 | |
---|
306 | |
---|
307 | if __name__ == '__main__': |
---|
308 | |
---|
309 | app = wx.App() |
---|
310 | graphD = graphAppearance(None,title='Modify graph appearance') |
---|
311 | app.MainLoop() |
---|
312 | |
---|
313 | |
---|
314 | |
---|