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

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 d7bb526 was d7bb526, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 8 years ago

Refactored plottools into sasgui

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