[f8be87d] | 1 | #!/usr/bin/python |
---|
| 2 | """ |
---|
[b618b7f] | 3 | Dialog for appearance of plot symbols, color, size etc. |
---|
[f8be87d] | 4 | |
---|
[51f14603] | 5 | This software was developed by Institut Laue-Langevin as part of |
---|
| 6 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE). |
---|
[f8be87d] | 7 | |
---|
[51f14603] | 8 | Copyright 2012 Institut Laue-Langevin |
---|
[f8be87d] | 9 | """ |
---|
| 10 | import wx |
---|
| 11 | import operator |
---|
| 12 | |
---|
[9f51c2c] | 13 | class appearanceDialog(wx.Frame): |
---|
[f866fb5] | 14 | """ |
---|
| 15 | Appearance dialog |
---|
| 16 | """ |
---|
| 17 | def __init__(self, parent, title): |
---|
| 18 | """ |
---|
| 19 | Initialization of the Panel |
---|
| 20 | """ |
---|
[824e488] | 21 | super(appearanceDialog, |
---|
| 22 | self).__init__(parent, title=title, size=(570, 450), |
---|
| 23 | style=wx.DEFAULT_FRAME_STYLE | wx.FRAME_FLOAT_ON_PARENT) |
---|
[f8be87d] | 24 | |
---|
[9f51c2c] | 25 | self.okay_clicked = False |
---|
| 26 | self.parent = parent |
---|
[f866fb5] | 27 | self.symbo_labels = self.parent.get_symbol_label() |
---|
| 28 | self.color_labels = self.parent.get_color_label() |
---|
| 29 | self.init_ui() |
---|
[f8be87d] | 30 | self.Centre() |
---|
| 31 | self.Show() |
---|
| 32 | |
---|
[f866fb5] | 33 | def init_ui(self): |
---|
| 34 | """ |
---|
| 35 | Create spacing needed |
---|
| 36 | """ |
---|
[f8be87d] | 37 | panel = wx.Panel(self) |
---|
| 38 | |
---|
| 39 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 40 | |
---|
| 41 | hbox1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 42 | hbox2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 43 | hbox3 = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 44 | |
---|
| 45 | ivbox1 = wx.BoxSizer(wx.VERTICAL) |
---|
| 46 | ivbox2 = wx.BoxSizer(wx.VERTICAL) |
---|
[824e488] | 47 | |
---|
[f8be87d] | 48 | ihbox1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 49 | ihbox2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 50 | |
---|
[f866fb5] | 51 | symbolstaticbox = wx.StaticBox(panel, -1, 'Symbol') |
---|
| 52 | symbolstaticboxsizer = wx.StaticBoxSizer(symbolstaticbox, wx.VERTICAL) |
---|
[f8be87d] | 53 | |
---|
| 54 | # add widgets - reverse order! |
---|
| 55 | # texts |
---|
[f866fb5] | 56 | symboltext = wx.StaticText(panel, label='Shape') |
---|
| 57 | colortext = wx.StaticText(panel, label='Color') |
---|
| 58 | sizetext = wx.StaticText(panel, label='Size ') |
---|
| 59 | labeltext = wx.StaticText(panel, label='Legend Label') |
---|
[f8be87d] | 60 | |
---|
| 61 | # selection widgets |
---|
[f866fb5] | 62 | self.symbollistbox = wx.ListBox(panel, -1, size=(200, 200)) |
---|
[824e488] | 63 | self.colorlistbox = wx.ComboBox(panel, style=wx.CB_READONLY, |
---|
[f866fb5] | 64 | size=(185, -1)) |
---|
[824e488] | 65 | self.sizecombobox = wx.ComboBox(panel, style=wx.CB_READONLY, |
---|
[f866fb5] | 66 | size=(90, -1)) |
---|
| 67 | self.sizecombobox.Bind(wx.EVT_COMBOBOX, self.combo_click) |
---|
| 68 | self.sizecustombutton = wx.Button(panel, label='Custom...') |
---|
| 69 | self.sizecustombutton.Bind(wx.EVT_BUTTON, self.custom_size) |
---|
[824e488] | 70 | self.labeltextbox = wx.TextCtrl(panel, -1, "", size=(440, -1)) |
---|
[f8be87d] | 71 | |
---|
| 72 | # buttons |
---|
[f866fb5] | 73 | okbutton = wx.Button(panel, label='OK') |
---|
| 74 | okbutton.Bind(wx.EVT_BUTTON, self.on_ok) |
---|
| 75 | cancelbutton = wx.Button(panel, label='Cancel') |
---|
| 76 | cancelbutton.Bind(wx.EVT_BUTTON, self.close_dlg) |
---|
[f8be87d] | 77 | |
---|
| 78 | # now Add all the widgets to relevant spacer - tricky |
---|
[824e488] | 79 | ivbox1.Add(symboltext, flag=wx.ALL | wx.ALIGN_LEFT, border=10) |
---|
| 80 | ivbox1.Add(self.symbollistbox, flag=wx.ALL | wx.ALIGN_LEFT, border=10) |
---|
[f8be87d] | 81 | |
---|
[824e488] | 82 | ihbox1.Add(sizetext, flag=wx.ALL | wx.ALIGN_LEFT, border=10) |
---|
| 83 | ihbox1.Add(self.sizecombobox, |
---|
| 84 | flag=wx.ALL | wx.RIGHT | wx.ALIGN_LEFT, border=10) |
---|
| 85 | ihbox1.Add(self.sizecustombutton, |
---|
| 86 | flag=wx.ALIGN_LEFT | wx.ALL, border=10) |
---|
[f8be87d] | 87 | |
---|
[824e488] | 88 | ihbox2.Add(colortext, flag=wx.ALL | wx.ALIGN_LEFT, border=10) |
---|
| 89 | ihbox2.Add(self.colorlistbox, flag=wx.ALL | wx.ALIGN_LEFT, border=10) |
---|
[f8be87d] | 90 | |
---|
[f866fb5] | 91 | ivbox2.Add(ihbox1, flag=wx.ALIGN_LEFT, border=10) |
---|
| 92 | ivbox2.Add(ihbox2, flag=wx.ALIGN_LEFT, border=10) |
---|
[f8be87d] | 93 | |
---|
[f866fb5] | 94 | hbox1.Add(ivbox1, flag=wx.ALIGN_LEFT, border=10) |
---|
| 95 | hbox1.Add(ivbox2, flag=wx.ALIGN_LEFT, border=10) |
---|
[f8be87d] | 96 | |
---|
[824e488] | 97 | hbox2.Add(okbutton, flag=wx.ALL | wx.ALIGN_RIGHT, border=10) |
---|
| 98 | hbox2.Add(cancelbutton, flag=wx.ALL | wx.ALIGN_RIGHT, border=10) |
---|
| 99 | |
---|
| 100 | hbox3.Add(labeltext, flag=wx.EXPAND | wx.ALL | wx.ALIGN_LEFT, border=10) |
---|
| 101 | hbox3.Add(self.labeltextbox, flag=wx.EXPAND | wx.ALL | wx.ALIGN_LEFT, border=10) |
---|
[f8be87d] | 102 | |
---|
[824e488] | 103 | symbolstaticboxsizer.Add(hbox1, flag=wx.ALL | wx.EXPAND, border=10) |
---|
| 104 | vbox.Add(symbolstaticboxsizer, flag=wx.ALL | wx.EXPAND, border=10) |
---|
| 105 | vbox.Add(hbox3, flag=wx.EXPAND | wx.RIGHT, border=10) |
---|
| 106 | vbox.Add(wx.StaticLine(panel), 0, wx.ALL | wx.EXPAND, 5) |
---|
| 107 | vbox.Add(hbox2, flag=wx.RIGHT | wx.ALIGN_RIGHT, border=10) |
---|
[f8be87d] | 108 | |
---|
| 109 | panel.SetSizer(vbox) |
---|
| 110 | |
---|
[f866fb5] | 111 | self.populate_symbol() |
---|
| 112 | self.populate_color() |
---|
| 113 | self.populate_size() |
---|
[f8be87d] | 114 | |
---|
[f866fb5] | 115 | self.SetDefaultItem(self.symbollistbox) |
---|
[f8be87d] | 116 | |
---|
[f866fb5] | 117 | def custom_size(self, event): |
---|
| 118 | """ |
---|
| 119 | On custom size |
---|
| 120 | """ |
---|
[824e488] | 121 | dlg = wx.TextEntryDialog(self, 'Enter custom size', 'Custom size', str(self.final_size)) |
---|
| 122 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 123 | if float(dlg.GetValue()) < 0: |
---|
[f866fb5] | 124 | msg = "Unfortunately imaginary icons are not yet supported." |
---|
| 125 | msg += "Please enter a positive value" |
---|
[824e488] | 126 | dial = wx.MessageDialog(None, msg, 'Error', wx.OK | wx.ICON_ERROR) |
---|
[f8be87d] | 127 | dial.ShowModal() |
---|
| 128 | dlg.Destroy() |
---|
[f866fb5] | 129 | self.custom_size(event) |
---|
[f8be87d] | 130 | else: |
---|
| 131 | self.final_size = dlg.GetValue() |
---|
| 132 | dlg.Destroy() |
---|
| 133 | else: |
---|
| 134 | dlg.Destroy() |
---|
| 135 | |
---|
[f866fb5] | 136 | def set_defaults(self, size, color, symbol, label): |
---|
| 137 | """ |
---|
| 138 | Set Defaults |
---|
| 139 | """ |
---|
[f8be87d] | 140 | self.final_size = size |
---|
| 141 | # set up gui values |
---|
[f866fb5] | 142 | self.labeltextbox.SetValue(label) |
---|
[824e488] | 143 | if size % 1 == 0 and size > 1 and size < 11: |
---|
[f866fb5] | 144 | self.sizecombobox.SetSelection(int(size) - 1) |
---|
[f8be87d] | 145 | else: |
---|
[f866fb5] | 146 | self.sizecombobox.SetSelection(4) |
---|
| 147 | self.symbollistbox.SetSelection(self.sorted_sym_dic[symbol]) |
---|
[824e488] | 148 | colorname = appearanceDialog.find_key(self.parent.get_color_label(), color) |
---|
[f866fb5] | 149 | self.colorlistbox.SetStringSelection(colorname) |
---|
[f8be87d] | 150 | |
---|
[f866fb5] | 151 | def populate_symbol(self): |
---|
| 152 | """ |
---|
| 153 | Populate Symbols |
---|
| 154 | """ |
---|
[824e488] | 155 | self.sorted_symbo_labels = sorted(self.symbo_labels.iteritems(), |
---|
[f866fb5] | 156 | key=operator.itemgetter(1)) |
---|
[f8be87d] | 157 | self.sorted_sym_dic = {} |
---|
| 158 | i = 0 |
---|
[f866fb5] | 159 | for label in self.sorted_symbo_labels: |
---|
| 160 | self.symbollistbox.Append(str(label[0])) |
---|
[f8be87d] | 161 | self.sorted_sym_dic[str(label[0])] = i |
---|
| 162 | i += 1 |
---|
| 163 | |
---|
[f866fb5] | 164 | def populate_color(self): |
---|
| 165 | """ |
---|
| 166 | Populate Colors |
---|
| 167 | """ |
---|
[824e488] | 168 | sortedcolor_labels = sorted(self.color_labels.iteritems(), |
---|
| 169 | key=operator.itemgetter(1)) |
---|
[f866fb5] | 170 | for color in sortedcolor_labels: |
---|
| 171 | self.colorlistbox.Append(str(color[0])) |
---|
[824e488] | 172 | |
---|
[f866fb5] | 173 | def populate_size(self): |
---|
| 174 | """ |
---|
| 175 | Populate Size |
---|
| 176 | """ |
---|
| 177 | for i in range(1, 11): |
---|
| 178 | self.sizecombobox.Append(str(i) + '.0') |
---|
[f8be87d] | 179 | |
---|
[f866fb5] | 180 | def combo_click(self, event): |
---|
[f8be87d] | 181 | """ |
---|
[f866fb5] | 182 | Combox on click |
---|
[f8be87d] | 183 | """ |
---|
[f866fb5] | 184 | event.Skip() |
---|
| 185 | self.final_size = self.sizecombobox.GetValue() |
---|
| 186 | |
---|
| 187 | def close_dlg(self, event): |
---|
[f8be87d] | 188 | """ |
---|
[f866fb5] | 189 | On Close Dlg |
---|
[f8be87d] | 190 | """ |
---|
[f866fb5] | 191 | event.Skip() |
---|
| 192 | self.Destroy() |
---|
[f8be87d] | 193 | |
---|
| 194 | @staticmethod |
---|
[f866fb5] | 195 | def find_key(dic, val): |
---|
| 196 | """ |
---|
| 197 | Find key |
---|
| 198 | """ |
---|
[f8be87d] | 199 | return [k for k, v in dic.iteritems() if v == val][0] |
---|
[824e488] | 200 | |
---|
| 201 | def get_current_values(self): |
---|
[f866fb5] | 202 | """ |
---|
| 203 | Get Current Values |
---|
| 204 | :returns : (size, color, symbol, dataname) |
---|
| 205 | """ |
---|
[f8be87d] | 206 | size = float(self.final_size) |
---|
[f866fb5] | 207 | name = str(self.labeltextbox.GetValue()) |
---|
| 208 | seltuple = self.symbollistbox.GetSelections() |
---|
[824e488] | 209 | symbol = appearanceDialog.find_key(self.sorted_sym_dic, |
---|
[f866fb5] | 210 | int(seltuple[0])) |
---|
[824e488] | 211 | color = str(self.colorlistbox.GetValue()) |
---|
[f866fb5] | 212 | return(size, color, symbol, name) |
---|
| 213 | |
---|
| 214 | def on_ok(self, event): |
---|
| 215 | """ |
---|
| 216 | On OK button clicked |
---|
| 217 | """ |
---|
| 218 | event.Skip() |
---|
[9f51c2c] | 219 | self.okay_clicked = True |
---|
| 220 | self.Close() |
---|