[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 | """ |
---|
| 21 | super(appearanceDialog, self).__init__(parent, title=title, |
---|
| 22 | 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) |
---|
| 47 | |
---|
| 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)) |
---|
| 63 | self.colorlistbox = wx.ComboBox(panel, style=wx.CB_READONLY, |
---|
| 64 | size=(185, -1)) |
---|
| 65 | self.sizecombobox = wx.ComboBox(panel, style=wx.CB_READONLY, |
---|
| 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) |
---|
| 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 |
---|
[f866fb5] | 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 | |
---|
[f866fb5] | 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 | |
---|
[f866fb5] | 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 | |
---|
[f866fb5] | 97 | hbox2.Add(okbutton, flag=wx.ALL| wx.ALIGN_RIGHT, border=10) |
---|
| 98 | hbox2.Add(cancelbutton, flag=wx.ALL|wx.ALIGN_RIGHT, border=10) |
---|
[f8be87d] | 99 | |
---|
[f866fb5] | 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 | |
---|
[f866fb5] | 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 | """ |
---|
| 121 | dlg = wx.TextEntryDialog(self, |
---|
| 122 | 'Enter custom size', |
---|
| 123 | 'Custom size', |
---|
[f8be87d] | 124 | str(self.final_size)) |
---|
| 125 | if(dlg.ShowModal() == wx.ID_OK): |
---|
| 126 | if(float(dlg.GetValue()) < 0): |
---|
[f866fb5] | 127 | msg = "Unfortunately imaginary icons are not yet supported." |
---|
| 128 | msg += "Please enter a positive value" |
---|
| 129 | dial = wx.MessageDialog(None, msg, 'Error', |
---|
| 130 | wx.OK|wx.ICON_ERROR) |
---|
[f8be87d] | 131 | dial.ShowModal() |
---|
| 132 | dlg.Destroy() |
---|
[f866fb5] | 133 | self.custom_size(event) |
---|
[f8be87d] | 134 | else: |
---|
| 135 | self.final_size = dlg.GetValue() |
---|
| 136 | dlg.Destroy() |
---|
| 137 | else: |
---|
| 138 | dlg.Destroy() |
---|
| 139 | |
---|
[f866fb5] | 140 | def set_defaults(self, size, color, symbol, label): |
---|
| 141 | """ |
---|
| 142 | Set Defaults |
---|
| 143 | """ |
---|
[f8be87d] | 144 | self.final_size = size |
---|
| 145 | # set up gui values |
---|
[f866fb5] | 146 | self.labeltextbox.SetValue(label) |
---|
[f8be87d] | 147 | if(size % 1 == 0 and size > 1 and size < 11): |
---|
[f866fb5] | 148 | self.sizecombobox.SetSelection(int(size) - 1) |
---|
[f8be87d] | 149 | else: |
---|
[f866fb5] | 150 | self.sizecombobox.SetSelection(4) |
---|
| 151 | self.symbollistbox.SetSelection(self.sorted_sym_dic[symbol]) |
---|
| 152 | colorname = appearanceDialog.find_key(self.parent.get_color_label(), |
---|
| 153 | color) |
---|
| 154 | self.colorlistbox.SetStringSelection(colorname) |
---|
[f8be87d] | 155 | |
---|
[f866fb5] | 156 | def populate_symbol(self): |
---|
| 157 | """ |
---|
| 158 | Populate Symbols |
---|
| 159 | """ |
---|
| 160 | self.sorted_symbo_labels = sorted(self.symbo_labels.iteritems(), |
---|
| 161 | key=operator.itemgetter(1)) |
---|
[f8be87d] | 162 | self.sorted_sym_dic = {} |
---|
| 163 | i = 0 |
---|
[f866fb5] | 164 | for label in self.sorted_symbo_labels: |
---|
| 165 | self.symbollistbox.Append(str(label[0])) |
---|
[f8be87d] | 166 | self.sorted_sym_dic[str(label[0])] = i |
---|
| 167 | i += 1 |
---|
| 168 | |
---|
[f866fb5] | 169 | def populate_color(self): |
---|
| 170 | """ |
---|
| 171 | Populate Colors |
---|
| 172 | """ |
---|
| 173 | sortedcolor_labels = sorted(self.color_labels.iteritems(), |
---|
| 174 | key=operator.itemgetter(1)) |
---|
| 175 | for color in sortedcolor_labels: |
---|
| 176 | self.colorlistbox.Append(str(color[0])) |
---|
[f8be87d] | 177 | |
---|
[f866fb5] | 178 | def populate_size(self): |
---|
| 179 | """ |
---|
| 180 | Populate Size |
---|
| 181 | """ |
---|
| 182 | for i in range(1, 11): |
---|
| 183 | self.sizecombobox.Append(str(i) + '.0') |
---|
[f8be87d] | 184 | |
---|
[f866fb5] | 185 | def combo_click(self, event): |
---|
[f8be87d] | 186 | """ |
---|
[f866fb5] | 187 | Combox on click |
---|
[f8be87d] | 188 | """ |
---|
[f866fb5] | 189 | event.Skip() |
---|
| 190 | self.final_size = self.sizecombobox.GetValue() |
---|
| 191 | |
---|
| 192 | def close_dlg(self, event): |
---|
[f8be87d] | 193 | """ |
---|
[f866fb5] | 194 | On Close Dlg |
---|
[f8be87d] | 195 | """ |
---|
[f866fb5] | 196 | event.Skip() |
---|
| 197 | self.Destroy() |
---|
[f8be87d] | 198 | |
---|
| 199 | @staticmethod |
---|
[f866fb5] | 200 | def find_key(dic, val): |
---|
| 201 | """ |
---|
| 202 | Find key |
---|
| 203 | """ |
---|
[f8be87d] | 204 | return [k for k, v in dic.iteritems() if v == val][0] |
---|
| 205 | |
---|
[f866fb5] | 206 | def get_current_values(self): |
---|
| 207 | """ |
---|
| 208 | Get Current Values |
---|
| 209 | :returns : (size, color, symbol, dataname) |
---|
| 210 | """ |
---|
[f8be87d] | 211 | size = float(self.final_size) |
---|
[f866fb5] | 212 | name = str(self.labeltextbox.GetValue()) |
---|
| 213 | seltuple = self.symbollistbox.GetSelections() |
---|
| 214 | symbol = appearanceDialog.find_key(self.sorted_sym_dic, |
---|
| 215 | int(seltuple[0])) |
---|
| 216 | color = str(self.colorlistbox.GetValue()) |
---|
| 217 | return(size, color, symbol, name) |
---|
| 218 | |
---|
| 219 | def on_ok(self, event): |
---|
| 220 | """ |
---|
| 221 | On OK button clicked |
---|
| 222 | """ |
---|
| 223 | event.Skip() |
---|
[9f51c2c] | 224 | self.okay_clicked = True |
---|
| 225 | self.Close() |
---|