1 | #!/usr/bin/python |
---|
2 | """ |
---|
3 | Dialog for appearance of plot symbols, color, size etc. |
---|
4 | /** |
---|
5 | This software was developed by Institut Laue-Langevin as part of |
---|
6 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE). |
---|
7 | |
---|
8 | Copyright 2012 Institut Laue-Langevin |
---|
9 | |
---|
10 | **/ |
---|
11 | """ |
---|
12 | import wx |
---|
13 | import operator |
---|
14 | |
---|
15 | class appearanceDialog(wx.Frame): |
---|
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) |
---|
26 | |
---|
27 | self.okay_clicked = False |
---|
28 | self.parent = parent |
---|
29 | self.symbo_labels = self.parent.get_symbol_label() |
---|
30 | self.color_labels = self.parent.get_color_label() |
---|
31 | self.init_ui() |
---|
32 | self.Centre() |
---|
33 | self.Show() |
---|
34 | |
---|
35 | def init_ui(self): |
---|
36 | """ |
---|
37 | Create spacing needed |
---|
38 | """ |
---|
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 | |
---|
53 | symbolstaticbox = wx.StaticBox(panel, -1, 'Symbol') |
---|
54 | symbolstaticboxsizer = wx.StaticBoxSizer(symbolstaticbox, wx.VERTICAL) |
---|
55 | |
---|
56 | # add widgets - reverse order! |
---|
57 | # texts |
---|
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') |
---|
62 | |
---|
63 | # selection widgets |
---|
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)) |
---|
73 | |
---|
74 | # buttons |
---|
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) |
---|
79 | |
---|
80 | # now Add all the widgets to relevant spacer - tricky |
---|
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) |
---|
83 | |
---|
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) |
---|
89 | |
---|
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) |
---|
92 | |
---|
93 | ivbox2.Add(ihbox1, flag=wx.ALIGN_LEFT, border=10) |
---|
94 | ivbox2.Add(ihbox2, flag=wx.ALIGN_LEFT, border=10) |
---|
95 | |
---|
96 | hbox1.Add(ivbox1, flag=wx.ALIGN_LEFT, border=10) |
---|
97 | hbox1.Add(ivbox2, flag=wx.ALIGN_LEFT, border=10) |
---|
98 | |
---|
99 | hbox2.Add(okbutton, flag=wx.ALL| wx.ALIGN_RIGHT, border=10) |
---|
100 | hbox2.Add(cancelbutton, flag=wx.ALL|wx.ALIGN_RIGHT, border=10) |
---|
101 | |
---|
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) |
---|
104 | |
---|
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) |
---|
110 | |
---|
111 | panel.SetSizer(vbox) |
---|
112 | |
---|
113 | self.populate_symbol() |
---|
114 | self.populate_color() |
---|
115 | self.populate_size() |
---|
116 | |
---|
117 | self.SetDefaultItem(self.symbollistbox) |
---|
118 | |
---|
119 | def custom_size(self, event): |
---|
120 | """ |
---|
121 | On custom size |
---|
122 | """ |
---|
123 | dlg = wx.TextEntryDialog(self, |
---|
124 | 'Enter custom size', |
---|
125 | 'Custom size', |
---|
126 | str(self.final_size)) |
---|
127 | if(dlg.ShowModal() == wx.ID_OK): |
---|
128 | if(float(dlg.GetValue()) < 0): |
---|
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) |
---|
133 | dial.ShowModal() |
---|
134 | dlg.Destroy() |
---|
135 | self.custom_size(event) |
---|
136 | else: |
---|
137 | self.final_size = dlg.GetValue() |
---|
138 | dlg.Destroy() |
---|
139 | else: |
---|
140 | dlg.Destroy() |
---|
141 | |
---|
142 | def set_defaults(self, size, color, symbol, label): |
---|
143 | """ |
---|
144 | Set Defaults |
---|
145 | """ |
---|
146 | self.final_size = size |
---|
147 | # set up gui values |
---|
148 | self.labeltextbox.SetValue(label) |
---|
149 | if(size % 1 == 0 and size > 1 and size < 11): |
---|
150 | self.sizecombobox.SetSelection(int(size) - 1) |
---|
151 | else: |
---|
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) |
---|
157 | |
---|
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)) |
---|
164 | self.sorted_sym_dic = {} |
---|
165 | i = 0 |
---|
166 | for label in self.sorted_symbo_labels: |
---|
167 | self.symbollistbox.Append(str(label[0])) |
---|
168 | self.sorted_sym_dic[str(label[0])] = i |
---|
169 | i += 1 |
---|
170 | |
---|
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])) |
---|
179 | |
---|
180 | def populate_size(self): |
---|
181 | """ |
---|
182 | Populate Size |
---|
183 | """ |
---|
184 | for i in range(1, 11): |
---|
185 | self.sizecombobox.Append(str(i) + '.0') |
---|
186 | |
---|
187 | def combo_click(self, event): |
---|
188 | """ |
---|
189 | Combox on click |
---|
190 | """ |
---|
191 | event.Skip() |
---|
192 | self.final_size = self.sizecombobox.GetValue() |
---|
193 | |
---|
194 | def close_dlg(self, event): |
---|
195 | """ |
---|
196 | On Close Dlg |
---|
197 | """ |
---|
198 | event.Skip() |
---|
199 | self.Destroy() |
---|
200 | |
---|
201 | @staticmethod |
---|
202 | def find_key(dic, val): |
---|
203 | """ |
---|
204 | Find key |
---|
205 | """ |
---|
206 | return [k for k, v in dic.iteritems() if v == val][0] |
---|
207 | |
---|
208 | def get_current_values(self): |
---|
209 | """ |
---|
210 | Get Current Values |
---|
211 | :returns : (size, color, symbol, dataname) |
---|
212 | """ |
---|
213 | size = float(self.final_size) |
---|
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() |
---|
226 | self.okay_clicked = True |
---|
227 | self.Close() |
---|