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