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): |
---|
29 | super(graphAppearance,self).__init__(parent, title=title,size=(520,435)) |
---|
30 | |
---|
31 | self.InitUI() |
---|
32 | self.Centre() |
---|
33 | self.Show() |
---|
34 | |
---|
35 | self.xfont = None |
---|
36 | self.yfont = None |
---|
37 | |
---|
38 | |
---|
39 | def InitUI(self): |
---|
40 | |
---|
41 | panel = wx.Panel(self) |
---|
42 | |
---|
43 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
44 | hbox1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
45 | hbox2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
46 | |
---|
47 | xhbox1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
48 | xhbox2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
49 | yhbox1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
50 | yhbox2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
51 | |
---|
52 | |
---|
53 | legendLocText = wx.StaticText(panel, label='Legend location: ') |
---|
54 | self.legendLocCombo = wx.ComboBox(panel,style = wx.CB_READONLY, size=(180,-1)) |
---|
55 | self.fillLegendLocs() |
---|
56 | |
---|
57 | self.toggleLegend = wx.CheckBox(panel, label='Toggle legend on/off') |
---|
58 | self.toggleGrid = wx.CheckBox(panel, label='Toggle grid on/off') |
---|
59 | |
---|
60 | |
---|
61 | xStaticBox = wx.StaticBox(panel,-1, 'x-axis label') |
---|
62 | xStaticBoxSizer = wx.StaticBoxSizer(xStaticBox, wx.VERTICAL) |
---|
63 | yStaticBox = wx.StaticBox(panel,-1, 'y-axis label') |
---|
64 | yStaticBoxSizer = wx.StaticBoxSizer(yStaticBox, wx.VERTICAL) |
---|
65 | |
---|
66 | |
---|
67 | xaxisLabel = wx.StaticText(panel, label='X-axis: ') |
---|
68 | yaxisLabel = wx.StaticText(panel, label='Y-axis: ') |
---|
69 | unitLabel1 = wx.StaticText(panel, label='Units: ') |
---|
70 | unitLabel2 = wx.StaticText(panel, label='Units: ') |
---|
71 | |
---|
72 | self.xaxisText = wx.TextCtrl(panel,-1,"",size=(220,-1)) |
---|
73 | self.yaxisText = wx.TextCtrl(panel,-1,"",size=(220,-1)) |
---|
74 | |
---|
75 | self.xaxisUnitText = wx.TextCtrl(panel,-1,"",size=(100,-1)) |
---|
76 | self.yaxisUnitText = wx.TextCtrl(panel,-1,"",size=(100,-1)) |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | xcolorLabel = wx.StaticText(panel, label='Font color: ') |
---|
81 | self.xfontColor = wx.ComboBox(panel,size=(100,-1),style=wx.CB_READONLY) |
---|
82 | self.xfillColors() |
---|
83 | self.xfontColor.SetSelection(0) |
---|
84 | xfontButton = wx.Button(panel, label='Font') |
---|
85 | xfontButton.Bind(wx.EVT_BUTTON,self.onxFont) |
---|
86 | |
---|
87 | ycolorLabel = wx.StaticText(panel, label='Font color: ') |
---|
88 | self.yfontColor = wx.ComboBox(panel,size=(100,-1),style=wx.CB_READONLY) |
---|
89 | self.yfillColors() |
---|
90 | self.yfontColor.SetSelection(0) |
---|
91 | yfontButton = wx.Button(panel, label='Font') |
---|
92 | yfontButton.Bind(wx.EVT_BUTTON,self.onyFont) |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | self.cancelButton = wx.Button(panel, label='Cancel') |
---|
97 | self.okButton = wx.Button(panel, label='OK') |
---|
98 | |
---|
99 | self.cancelButton.Bind(wx.EVT_BUTTON,self.onCancel) |
---|
100 | self.okButton.Bind(wx.EVT_BUTTON,self.onOK) |
---|
101 | |
---|
102 | |
---|
103 | xhbox1.Add(xaxisLabel,flag= wx.ALL | wx.EXPAND | wx.ALIGN_LEFT,border=10) |
---|
104 | xhbox1.Add(self.xaxisText,flag=wx.ALL | wx.EXPAND | wx.ALIGN_LEFT,border=10) |
---|
105 | xhbox1.Add(unitLabel1,flag=wx.ALL | wx.EXPAND | wx.ALIGN_RIGHT,border=10) |
---|
106 | xhbox1.Add(self.xaxisUnitText, flag=wx.ALL | wx.EXPAND | wx.ALIGN_RIGHT,border=10) |
---|
107 | |
---|
108 | yhbox1.Add(yaxisLabel,flag= wx.ALL | wx.EXPAND | wx.ALIGN_LEFT,border=10) |
---|
109 | yhbox1.Add(self.yaxisText,flag=wx.ALL | wx.EXPAND | wx.ALIGN_LEFT,border=10) |
---|
110 | yhbox1.Add(unitLabel2,flag=wx.ALL | wx.EXPAND | wx.ALIGN_RIGHT,border=10) |
---|
111 | yhbox1.Add(self.yaxisUnitText, flag=wx.ALL | wx.EXPAND | wx.ALIGN_RIGHT,border=10) |
---|
112 | |
---|
113 | xhbox2.Add(xcolorLabel,flag=wx.ALL | wx.ALIGN_RIGHT, border=10) |
---|
114 | xhbox2.Add(self.xfontColor,flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
115 | xhbox2.Add(xfontButton,flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
116 | |
---|
117 | yhbox2.Add(ycolorLabel,flag=wx.ALL | wx.ALIGN_RIGHT, border=10) |
---|
118 | yhbox2.Add(self.yfontColor,flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
119 | yhbox2.Add(yfontButton,flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
120 | |
---|
121 | |
---|
122 | hbox1.Add(legendLocText, flag = wx.ALL | wx.EXPAND | wx.ALIGN_LEFT, border=5) |
---|
123 | hbox1.Add(self.legendLocCombo, flag = wx.ALL | wx.EXPAND | wx.ALIGN_LEFT, border=5) |
---|
124 | hbox1.Add((5,-1)) |
---|
125 | hbox1.Add(self.toggleLegend, flag = wx.ALL | wx.EXPAND | wx.ALIGN_LEFT, border=5) |
---|
126 | |
---|
127 | hbox2.Add(self.okButton, flag = wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
128 | hbox2.Add(self.cancelButton, flag = wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
129 | hbox2.Add((15,-1)) |
---|
130 | |
---|
131 | xStaticBoxSizer.Add(xhbox1,flag= wx.EXPAND ,border=5) |
---|
132 | xStaticBoxSizer.Add(xhbox2, flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
133 | yStaticBoxSizer.Add(yhbox1,flag= wx.EXPAND ,border=5) |
---|
134 | yStaticBoxSizer.Add(yhbox2, flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
135 | |
---|
136 | vbox.Add((-1,20)) |
---|
137 | vbox.Add(hbox1, flag = wx.EXPAND | wx.ALL, border=5) |
---|
138 | vbox.Add(xStaticBoxSizer, flag = wx.ALL | wx.EXPAND, border=10) |
---|
139 | vbox.Add(yStaticBoxSizer, flag = wx.ALL | wx.EXPAND, border=10) |
---|
140 | |
---|
141 | vbox.Add(self.toggleGrid, flag = wx.ALIGN_RIGHT | wx.RIGHT, border=20) |
---|
142 | vbox.Add(hbox2, flag = wx.ALIGN_RIGHT | wx.ALL , border=5) |
---|
143 | |
---|
144 | |
---|
145 | panel.SetSizer(vbox) |
---|
146 | |
---|
147 | def xfillColors(self): |
---|
148 | list = COLOR |
---|
149 | for idx in range(len(list)): |
---|
150 | self.xfontColor.Append(list[idx],idx) |
---|
151 | |
---|
152 | def yfillColors(self): |
---|
153 | list = COLOR |
---|
154 | for idx in range(len(list)): |
---|
155 | self.yfontColor.Append(list[idx],idx) |
---|
156 | |
---|
157 | def onxFont(self,e): |
---|
158 | title = 'Modify x axis font' |
---|
159 | |
---|
160 | fonty = SimpleFont(self,wx.NewId(),title) |
---|
161 | fonty.set_default_font(self.xfont) |
---|
162 | if(fonty.ShowModal() == wx.ID_OK): |
---|
163 | self.xfont = fonty.get_font() |
---|
164 | |
---|
165 | def onyFont(self,e): |
---|
166 | title = 'Modify y axis font' |
---|
167 | fonty = SimpleFont(self,wx.NewId(),title) |
---|
168 | fonty.set_default_font(self.yfont) |
---|
169 | if(fonty.ShowModal() == wx.ID_OK): |
---|
170 | self.yfont = fonty.get_font() |
---|
171 | |
---|
172 | def onOK(self,e): |
---|
173 | self.Close() |
---|
174 | |
---|
175 | def onCancel(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.legendLocCombo.Append(label) |
---|
217 | for label in self.get_loc_label(): |
---|
218 | self.legendLocCombo.Append(label) |
---|
219 | |
---|
220 | |
---|
221 | def setDefaults(self,grid,legend,xlab,ylab,xunit,yunit, |
---|
222 | xaxis_font,yaxis_font,legend_loc, |
---|
223 | xcolor,ycolor): |
---|
224 | self.toggleGrid.SetValue(grid) |
---|
225 | self.toggleLegend.SetValue(legend) |
---|
226 | self.xaxisText.SetValue(xlab) |
---|
227 | self.yaxisText.SetValue(ylab) |
---|
228 | self.xaxisUnitText.SetValue(xunit) |
---|
229 | self.yaxisUnitText.SetValue(yunit) |
---|
230 | self.xfont = xaxis_font |
---|
231 | self.yfont = yaxis_font |
---|
232 | |
---|
233 | if not xcolor: |
---|
234 | self.xfontColor.SetSelection(0) |
---|
235 | else: |
---|
236 | self.xfontColor.SetStringSelection(xcolor) |
---|
237 | |
---|
238 | if not ycolor: |
---|
239 | self.yfontColor.SetSelection(0) |
---|
240 | else: |
---|
241 | self.yfontColor.SetStringSelection(ycolor) |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | self.legendLocCombo.SetStringSelection(legend_loc) |
---|
246 | |
---|
247 | |
---|
248 | def getAppInfo(self): |
---|
249 | grid_on = self.toggleGrid.GetValue() |
---|
250 | legend_on = self.toggleLegend.GetValue() |
---|
251 | xlab = self.xaxisText.GetValue() |
---|
252 | ylab = self.yaxisText.GetValue() |
---|
253 | xunit = self.xaxisUnitText.GetValue() |
---|
254 | yunit = self.yaxisUnitText.GetValue() |
---|
255 | legend_loc = self.get_loc_label()[self.legendLocCombo.GetStringSelection()] |
---|
256 | xcolor = self.xfontColor.GetValue() |
---|
257 | ycolor = self.yfontColor.GetValue() |
---|
258 | |
---|
259 | |
---|
260 | return [grid_on,legend_on,xlab,ylab,xunit,yunit, |
---|
261 | self.xfont,self.yfont, |
---|
262 | xcolor,ycolor,legend_loc] |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | if __name__ == '__main__': |
---|
267 | |
---|
268 | app = wx.App() |
---|
269 | graphD = graphAppearance(None,title='Modify graph appearance') |
---|
270 | app.MainLoop() |
---|
271 | |
---|
272 | |
---|
273 | |
---|