[8f59e95] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | Dialog for general graph appearance |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | /** |
---|
[9f51c2c] | 9 | This software was developed by Institut Laue-Langevin as part of |
---|
| 10 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE). |
---|
[8f59e95] | 11 | |
---|
[9f51c2c] | 12 | Copyright 2012 Institut Laue-Langevin |
---|
[8f59e95] | 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 | |
---|
[9f51c2c] | 25 | class graphAppearance(wx.Frame): |
---|
[8f59e95] | 26 | |
---|
[67fb83b] | 27 | def __init__(self, parent, title, legend=True): |
---|
| 28 | super(graphAppearance, self).__init__(parent, title=title, size=(520, 435)) |
---|
[8f59e95] | 29 | |
---|
[8a687cfd] | 30 | self.legend = legend |
---|
| 31 | |
---|
[8f59e95] | 32 | self.InitUI() |
---|
| 33 | self.Centre() |
---|
| 34 | self.Show() |
---|
| 35 | |
---|
| 36 | self.xfont = None |
---|
| 37 | self.yfont = None |
---|
[657e52c] | 38 | self.is_xtick = False |
---|
| 39 | self.is_ytick = False |
---|
[8f59e95] | 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 | |
---|
[8a687cfd] | 57 | if self.legend: |
---|
| 58 | legendLocText = wx.StaticText(panel, label='Legend location: ') |
---|
[67fb83b] | 59 | self.legend_loc_combo = wx.ComboBox(panel,style = wx.CB_READONLY, size=(180,-1)) |
---|
[8a687cfd] | 60 | self.fillLegendLocs() |
---|
| 61 | else: |
---|
[67fb83b] | 62 | self.legend_loc_combo = None |
---|
[8a687cfd] | 63 | |
---|
| 64 | |
---|
| 65 | if self.legend: |
---|
[67fb83b] | 66 | self.toggle_legend = wx.CheckBox(panel, label='Toggle legend on/off') |
---|
[8a687cfd] | 67 | else: |
---|
[67fb83b] | 68 | self.toggle_legend = None |
---|
[8a687cfd] | 69 | |
---|
[67fb83b] | 70 | self.toggle_grid = wx.CheckBox(panel, label='Toggle grid on/off') |
---|
[8f59e95] | 71 | |
---|
| 72 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 77 | |
---|
| 78 | |
---|
[67fb83b] | 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: ') |
---|
[8f59e95] | 83 | |
---|
[67fb83b] | 84 | self.xaxis_text = wx.TextCtrl(panel, -1, "",size=(220, -1)) |
---|
| 85 | self.yaxis_text = wx.TextCtrl(panel, -1, "",size=(220, -1)) |
---|
[8f59e95] | 86 | |
---|
[67fb83b] | 87 | self.xaxis_unit_text = wx.TextCtrl(panel, -1,"",size=(100, -1)) |
---|
| 88 | self.yaxis_unit_text = wx.TextCtrl(panel, -1,"",size=(100, -1)) |
---|
[8f59e95] | 89 | |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | xcolorLabel = wx.StaticText(panel, label='Font color: ') |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 98 | |
---|
| 99 | ycolorLabel = wx.StaticText(panel, label='Font color: ') |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 105 | |
---|
| 106 | |
---|
| 107 | |
---|
[67fb83b] | 108 | self.cancel_button = wx.Button(panel, label='Cancel') |
---|
[9f51c2c] | 109 | self.ok_button = wx.Button(panel, label='OK') |
---|
[8f59e95] | 110 | |
---|
[67fb83b] | 111 | self.cancel_button.Bind(wx.EVT_BUTTON, self.onCancel) |
---|
[9f51c2c] | 112 | self.ok_button.Bind(wx.EVT_BUTTON, self.on_ok) |
---|
[8f59e95] | 113 | |
---|
| 114 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 119 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 124 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 128 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 132 | |
---|
[8a687cfd] | 133 | if self.legend: |
---|
| 134 | hbox1.Add(legendLocText, flag = wx.ALL | wx.EXPAND | wx.ALIGN_LEFT, border=5) |
---|
[67fb83b] | 135 | hbox1.Add(self.legend_loc_combo, flag = wx.ALL | wx.EXPAND | wx.ALIGN_LEFT, border=5) |
---|
[8f59e95] | 136 | |
---|
[8a687cfd] | 137 | if self.legend: |
---|
[67fb83b] | 138 | hbox1.Add((5, -1)) |
---|
| 139 | hbox1.Add(self.toggle_legend, flag = wx.ALL | wx.EXPAND | wx.ALIGN_LEFT, border=5) |
---|
[8f59e95] | 140 | |
---|
[67fb83b] | 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)) |
---|
[8f59e95] | 144 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 149 | |
---|
[67fb83b] | 150 | vbox.Add((-1, 20)) |
---|
[8f59e95] | 151 | vbox.Add(hbox1, flag = wx.EXPAND | wx.ALL, border=5) |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 154 | |
---|
[67fb83b] | 155 | vbox.Add(self.toggle_grid, flag = wx.ALIGN_RIGHT | wx.RIGHT, border=20) |
---|
[8f59e95] | 156 | vbox.Add(hbox2, flag = wx.ALIGN_RIGHT | wx.ALL , border=5) |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | panel.SetSizer(vbox) |
---|
| 160 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 165 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 170 | |
---|
[67fb83b] | 171 | def onxFont(self, e): |
---|
[8f59e95] | 172 | title = 'Modify x axis font' |
---|
| 173 | |
---|
[67fb83b] | 174 | fonty = SimpleFont(self, wx.NewId(), title) |
---|
[8f59e95] | 175 | fonty.set_default_font(self.xfont) |
---|
[657e52c] | 176 | fonty.set_ticklabel_check(self.is_xtick) |
---|
[8f59e95] | 177 | if(fonty.ShowModal() == wx.ID_OK): |
---|
| 178 | self.xfont = fonty.get_font() |
---|
[657e52c] | 179 | self.is_xtick = fonty.get_ticklabel_check() |
---|
[8f59e95] | 180 | |
---|
[67fb83b] | 181 | def onyFont(self, e): |
---|
[8f59e95] | 182 | title = 'Modify y axis font' |
---|
[67fb83b] | 183 | fonty = SimpleFont(self, wx.NewId(), title) |
---|
[8f59e95] | 184 | fonty.set_default_font(self.yfont) |
---|
[657e52c] | 185 | fonty.set_ticklabel_check(self.is_ytick) |
---|
[8f59e95] | 186 | if(fonty.ShowModal() == wx.ID_OK): |
---|
| 187 | self.yfont = fonty.get_font() |
---|
[657e52c] | 188 | self.is_ytick = fonty.get_ticklabel_check() |
---|
[8f59e95] | 189 | |
---|
[9f51c2c] | 190 | def on_ok(self, e): |
---|
| 191 | self.Close() |
---|
| 192 | |
---|
[8f59e95] | 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): |
---|
[67fb83b] | 234 | # self.legend_loc_combo.Append(label) |
---|
[8f59e95] | 235 | for label in self.get_loc_label(): |
---|
[67fb83b] | 236 | self.legend_loc_combo.Append(label) |
---|
[8f59e95] | 237 | |
---|
| 238 | |
---|
| 239 | def setDefaults(self,grid,legend,xlab,ylab,xunit,yunit, |
---|
| 240 | xaxis_font,yaxis_font,legend_loc, |
---|
[657e52c] | 241 | xcolor,ycolor, is_xtick, is_ytick): |
---|
[67fb83b] | 242 | self.toggle_grid.SetValue(grid) |
---|
[8a687cfd] | 243 | if self.legend: |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 249 | self.xfont = xaxis_font |
---|
| 250 | self.yfont = yaxis_font |
---|
[657e52c] | 251 | self.is_xtick = is_xtick |
---|
| 252 | self.is_ytick = is_ytick |
---|
[8f59e95] | 253 | |
---|
| 254 | if not xcolor: |
---|
[67fb83b] | 255 | self.xfont_color.SetSelection(0) |
---|
[8f59e95] | 256 | else: |
---|
[67fb83b] | 257 | self.xfont_color.SetStringSelection(xcolor) |
---|
[8f59e95] | 258 | |
---|
| 259 | if not ycolor: |
---|
[67fb83b] | 260 | self.yfont_color.SetSelection(0) |
---|
[8f59e95] | 261 | else: |
---|
[67fb83b] | 262 | self.yfont_color.SetStringSelection(ycolor) |
---|
[8f59e95] | 263 | |
---|
| 264 | |
---|
[8a687cfd] | 265 | if self.legend: |
---|
[67fb83b] | 266 | self.legend_loc_combo.SetStringSelection(legend_loc) |
---|
[8a687cfd] | 267 | |
---|
| 268 | |
---|
| 269 | # get whether grid is toggled on/off |
---|
| 270 | def get_togglegrid(self): |
---|
[67fb83b] | 271 | return self.toggle_grid.GetValue() |
---|
[8a687cfd] | 272 | |
---|
| 273 | # get whether legend is toggled on/off |
---|
| 274 | def get_togglelegend(self): |
---|
[67fb83b] | 275 | return self.toggle_legend.GetValue() |
---|
[8a687cfd] | 276 | |
---|
| 277 | # get x label |
---|
| 278 | def get_xlab(self): |
---|
[67fb83b] | 279 | return self.xaxis_text.GetValue() |
---|
[8a687cfd] | 280 | |
---|
| 281 | # get y label |
---|
| 282 | def get_ylab(self): |
---|
[67fb83b] | 283 | return self.yaxis_text.GetValue() |
---|
[8a687cfd] | 284 | |
---|
| 285 | # get x unit |
---|
| 286 | def get_xunit(self): |
---|
[67fb83b] | 287 | return self.xaxis_unit_text.GetValue() |
---|
[8f59e95] | 288 | |
---|
[8a687cfd] | 289 | # get y unit |
---|
| 290 | def get_yunit(self): |
---|
[67fb83b] | 291 | return self.yaxis_unit_text.GetValue() |
---|
[8f59e95] | 292 | |
---|
[8a687cfd] | 293 | # get legend location |
---|
| 294 | def get_legend_loc(self): |
---|
[67fb83b] | 295 | return self.get_loc_label()[self.legend_loc_combo.GetStringSelection()] |
---|
[8f59e95] | 296 | |
---|
[8a687cfd] | 297 | # get x axis label color |
---|
| 298 | def get_xcolor(self): |
---|
[67fb83b] | 299 | return self.xfont_color.GetValue() |
---|
[8f59e95] | 300 | |
---|
[8a687cfd] | 301 | # get y axis label color |
---|
| 302 | def get_ycolor(self): |
---|
[67fb83b] | 303 | return self.yfont_color.GetValue() |
---|
[8f59e95] | 304 | |
---|
[8a687cfd] | 305 | # get x axis font (type is FontProperties) |
---|
| 306 | def get_xfont(self): |
---|
| 307 | return self.xfont |
---|
[8f59e95] | 308 | |
---|
[8a687cfd] | 309 | # get y axis font |
---|
| 310 | def get_yfont(self): |
---|
| 311 | return self.yfont |
---|
| 312 | |
---|
[657e52c] | 313 | def get_xtick_check(self): |
---|
| 314 | return self.is_xtick |
---|
| 315 | |
---|
| 316 | def get_ytick_check(self): |
---|
| 317 | return self.is_ytick |
---|
| 318 | |
---|
[8f59e95] | 319 | |
---|
| 320 | if __name__ == '__main__': |
---|
| 321 | |
---|
| 322 | app = wx.App() |
---|
| 323 | graphD = graphAppearance(None,title='Modify graph appearance') |
---|
| 324 | app.MainLoop() |
---|
| 325 | |
---|
| 326 | |
---|
| 327 | |
---|