source: sasview/src/sas/guiframe/local_perspectives/plotting/graphAppearance.py @ c22c5e3

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since c22c5e3 was b5de88e, checked in by Mathieu Doucet <doucetm@…>, 10 years ago

pylint fixes

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