[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 |
---|
[79492222] | 20 | from sas.plottools.SimpleFont import SimpleFont |
---|
[8f59e95] | 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 | 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 | |
---|
[8a687cfd] | 54 | if self.legend: |
---|
| 55 | legendLocText = wx.StaticText(panel, label='Legend location: ') |
---|
[b5de88e] | 56 | self.legend_loc_combo = wx.ComboBox(panel, style=wx.CB_READONLY, size=(180, -1)) |
---|
[8a687cfd] | 57 | self.fillLegendLocs() |
---|
| 58 | else: |
---|
[67fb83b] | 59 | self.legend_loc_combo = None |
---|
[8a687cfd] | 60 | |
---|
| 61 | if self.legend: |
---|
[67fb83b] | 62 | self.toggle_legend = wx.CheckBox(panel, label='Toggle legend on/off') |
---|
[8a687cfd] | 63 | else: |
---|
[67fb83b] | 64 | self.toggle_legend = None |
---|
[8a687cfd] | 65 | |
---|
[67fb83b] | 66 | self.toggle_grid = wx.CheckBox(panel, label='Toggle grid on/off') |
---|
[8f59e95] | 67 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 72 | |
---|
[67fb83b] | 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: ') |
---|
[8f59e95] | 77 | |
---|
[b5de88e] | 78 | self.xaxis_text = wx.TextCtrl(panel, -1, "", size=(220, -1)) |
---|
| 79 | self.yaxis_text = wx.TextCtrl(panel, -1, "", size=(220, -1)) |
---|
[8f59e95] | 80 | |
---|
[b5de88e] | 81 | self.xaxis_unit_text = wx.TextCtrl(panel, -1, "", size=(100, -1)) |
---|
| 82 | self.yaxis_unit_text = wx.TextCtrl(panel, -1, "", size=(100, -1)) |
---|
[8f59e95] | 83 | |
---|
| 84 | xcolorLabel = wx.StaticText(panel, label='Font color: ') |
---|
[67fb83b] | 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') |
---|
[b5de88e] | 89 | xfont_button.Bind(wx.EVT_BUTTON, self.on_x_font) |
---|
[8f59e95] | 90 | |
---|
| 91 | ycolorLabel = wx.StaticText(panel, label='Font color: ') |
---|
[b5de88e] | 92 | self.yfont_color = wx.ComboBox(panel, size=(100, -1), style=wx.CB_READONLY) |
---|
[67fb83b] | 93 | self.yfill_colors() |
---|
| 94 | self.yfont_color.SetSelection(0) |
---|
| 95 | yfont_button = wx.Button(panel, label='Font') |
---|
[b5de88e] | 96 | yfont_button.Bind(wx.EVT_BUTTON, self.on_y_font) |
---|
[8f59e95] | 97 | |
---|
[67fb83b] | 98 | self.cancel_button = wx.Button(panel, label='Cancel') |
---|
[9f51c2c] | 99 | self.ok_button = wx.Button(panel, label='OK') |
---|
[8f59e95] | 100 | |
---|
[b5de88e] | 101 | self.cancel_button.Bind(wx.EVT_BUTTON, self.on_cancel) |
---|
[9f51c2c] | 102 | self.ok_button.Bind(wx.EVT_BUTTON, self.on_ok) |
---|
[8f59e95] | 103 | |
---|
[b5de88e] | 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) |
---|
[8f59e95] | 108 | |
---|
[b5de88e] | 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) |
---|
[8f59e95] | 113 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 117 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 121 | |
---|
[8a687cfd] | 122 | if self.legend: |
---|
[b5de88e] | 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) |
---|
[8f59e95] | 125 | |
---|
[8a687cfd] | 126 | if self.legend: |
---|
[67fb83b] | 127 | hbox1.Add((5, -1)) |
---|
[b5de88e] | 128 | hbox1.Add(self.toggle_legend, flag=wx.ALL | wx.EXPAND | wx.ALIGN_LEFT, border=5) |
---|
[8f59e95] | 129 | |
---|
[b5de88e] | 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) |
---|
[67fb83b] | 132 | hbox2.Add((15, -1)) |
---|
[8f59e95] | 133 | |
---|
[b5de88e] | 134 | xstatic_box_sizer.Add(xhbox1, flag=wx.EXPAND, border=5) |
---|
[67fb83b] | 135 | xstatic_box_sizer.Add(xhbox2, flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
[b5de88e] | 136 | ystatic_box_sizer.Add(yhbox1, flag=wx.EXPAND, border=5) |
---|
[67fb83b] | 137 | ystatic_box_sizer.Add(yhbox2, flag=wx.ALL | wx.ALIGN_RIGHT, border=5) |
---|
[8f59e95] | 138 | |
---|
[67fb83b] | 139 | vbox.Add((-1, 20)) |
---|
[b5de88e] | 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) |
---|
[8f59e95] | 143 | |
---|
[b5de88e] | 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) |
---|
[8f59e95] | 146 | |
---|
| 147 | panel.SetSizer(vbox) |
---|
| 148 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 153 | |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 158 | |
---|
[b5de88e] | 159 | def on_x_font(self, e): |
---|
[8f59e95] | 160 | title = 'Modify x axis font' |
---|
| 161 | |
---|
[67fb83b] | 162 | fonty = SimpleFont(self, wx.NewId(), title) |
---|
[8f59e95] | 163 | fonty.set_default_font(self.xfont) |
---|
[657e52c] | 164 | fonty.set_ticklabel_check(self.is_xtick) |
---|
[b5de88e] | 165 | if fonty.ShowModal() == wx.ID_OK: |
---|
[8f59e95] | 166 | self.xfont = fonty.get_font() |
---|
[657e52c] | 167 | self.is_xtick = fonty.get_ticklabel_check() |
---|
[8f59e95] | 168 | |
---|
[b5de88e] | 169 | def on_y_font(self, e): |
---|
[8f59e95] | 170 | title = 'Modify y axis font' |
---|
[67fb83b] | 171 | fonty = SimpleFont(self, wx.NewId(), title) |
---|
[8f59e95] | 172 | fonty.set_default_font(self.yfont) |
---|
[657e52c] | 173 | fonty.set_ticklabel_check(self.is_ytick) |
---|
[b5de88e] | 174 | if fonty.ShowModal() == wx.ID_OK: |
---|
[8f59e95] | 175 | self.yfont = fonty.get_font() |
---|
[657e52c] | 176 | self.is_ytick = fonty.get_ticklabel_check() |
---|
[8f59e95] | 177 | |
---|
[9f51c2c] | 178 | def on_ok(self, e): |
---|
| 179 | self.Close() |
---|
| 180 | |
---|
[b5de88e] | 181 | def on_cancel(self, e): |
---|
[8f59e95] | 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): |
---|
[67fb83b] | 222 | # self.legend_loc_combo.Append(label) |
---|
[8f59e95] | 223 | for label in self.get_loc_label(): |
---|
[67fb83b] | 224 | self.legend_loc_combo.Append(label) |
---|
[8f59e95] | 225 | |
---|
| 226 | |
---|
[b5de88e] | 227 | def setDefaults(self, grid, legend, xlab, ylab, xunit, yunit, |
---|
| 228 | xaxis_font, yaxis_font, legend_loc, |
---|
| 229 | xcolor, ycolor, is_xtick, is_ytick): |
---|
[67fb83b] | 230 | self.toggle_grid.SetValue(grid) |
---|
[8a687cfd] | 231 | if self.legend: |
---|
[67fb83b] | 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) |
---|
[8f59e95] | 237 | self.xfont = xaxis_font |
---|
| 238 | self.yfont = yaxis_font |
---|
[657e52c] | 239 | self.is_xtick = is_xtick |
---|
| 240 | self.is_ytick = is_ytick |
---|
[8f59e95] | 241 | |
---|
| 242 | if not xcolor: |
---|
[67fb83b] | 243 | self.xfont_color.SetSelection(0) |
---|
[8f59e95] | 244 | else: |
---|
[67fb83b] | 245 | self.xfont_color.SetStringSelection(xcolor) |
---|
[8f59e95] | 246 | |
---|
| 247 | if not ycolor: |
---|
[67fb83b] | 248 | self.yfont_color.SetSelection(0) |
---|
[8f59e95] | 249 | else: |
---|
[67fb83b] | 250 | self.yfont_color.SetStringSelection(ycolor) |
---|
[b5de88e] | 251 | |
---|
[8f59e95] | 252 | |
---|
[8a687cfd] | 253 | if self.legend: |
---|
[67fb83b] | 254 | self.legend_loc_combo.SetStringSelection(legend_loc) |
---|
[8a687cfd] | 255 | |
---|
| 256 | |
---|
| 257 | # get whether grid is toggled on/off |
---|
| 258 | def get_togglegrid(self): |
---|
[67fb83b] | 259 | return self.toggle_grid.GetValue() |
---|
[8a687cfd] | 260 | |
---|
| 261 | # get whether legend is toggled on/off |
---|
| 262 | def get_togglelegend(self): |
---|
[67fb83b] | 263 | return self.toggle_legend.GetValue() |
---|
[8a687cfd] | 264 | |
---|
| 265 | # get x label |
---|
| 266 | def get_xlab(self): |
---|
[67fb83b] | 267 | return self.xaxis_text.GetValue() |
---|
[8a687cfd] | 268 | |
---|
| 269 | # get y label |
---|
| 270 | def get_ylab(self): |
---|
[67fb83b] | 271 | return self.yaxis_text.GetValue() |
---|
[8a687cfd] | 272 | |
---|
| 273 | # get x unit |
---|
| 274 | def get_xunit(self): |
---|
[67fb83b] | 275 | return self.xaxis_unit_text.GetValue() |
---|
[8f59e95] | 276 | |
---|
[8a687cfd] | 277 | # get y unit |
---|
| 278 | def get_yunit(self): |
---|
[67fb83b] | 279 | return self.yaxis_unit_text.GetValue() |
---|
[8f59e95] | 280 | |
---|
[8a687cfd] | 281 | # get legend location |
---|
| 282 | def get_legend_loc(self): |
---|
[67fb83b] | 283 | return self.get_loc_label()[self.legend_loc_combo.GetStringSelection()] |
---|
[8f59e95] | 284 | |
---|
[8a687cfd] | 285 | # get x axis label color |
---|
| 286 | def get_xcolor(self): |
---|
[67fb83b] | 287 | return self.xfont_color.GetValue() |
---|
[8f59e95] | 288 | |
---|
[8a687cfd] | 289 | # get y axis label color |
---|
| 290 | def get_ycolor(self): |
---|
[67fb83b] | 291 | return self.yfont_color.GetValue() |
---|
[8f59e95] | 292 | |
---|
[8a687cfd] | 293 | # get x axis font (type is FontProperties) |
---|
| 294 | def get_xfont(self): |
---|
| 295 | return self.xfont |
---|
[8f59e95] | 296 | |
---|
[8a687cfd] | 297 | # get y axis font |
---|
| 298 | def get_yfont(self): |
---|
| 299 | return self.yfont |
---|
[b5de88e] | 300 | |
---|
[657e52c] | 301 | def get_xtick_check(self): |
---|
| 302 | return self.is_xtick |
---|
| 303 | |
---|
| 304 | def get_ytick_check(self): |
---|
| 305 | return self.is_ytick |
---|
[b5de88e] | 306 | |
---|
[8f59e95] | 307 | |
---|
| 308 | if __name__ == '__main__': |
---|
| 309 | |
---|
| 310 | app = wx.App() |
---|
[b5de88e] | 311 | graphD = graphAppearance(None, title='Modify graph appearance') |
---|
[8f59e95] | 312 | app.MainLoop() |
---|
| 313 | |
---|
| 314 | |
---|
| 315 | |
---|