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