source: sasview/src/sas/sasgui/guiframe/local_perspectives/plotting/appearanceDialog.py @ 952ea1f

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249
Last change on this file since 952ea1f was d85c194, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 8 years ago

Remaining modules refactored

  • Property mode set to 100644
File size: 7.4 KB
Line 
1#!/usr/bin/python
2"""
3Dialog for appearance of plot symbols, color, size etc.
4
5This software was developed by Institut Laue-Langevin as part of
6Distributed Data Analysis of Neutron Scattering Experiments (DANSE).
7
8Copyright 2012 Institut Laue-Langevin
9"""
10import wx
11import operator
12
13class appearanceDialog(wx.Frame):
14    """
15    Appearance dialog
16    """
17    def __init__(self, parent, title):
18        """
19        Initialization of the Panel
20        """
21        super(appearanceDialog,
22              self).__init__(parent, title=title, size=(570, 450),
23                             style=wx.DEFAULT_FRAME_STYLE | wx.FRAME_FLOAT_ON_PARENT)
24
25        self.okay_clicked = False
26        self.parent = parent
27        self.symbo_labels = self.parent.get_symbol_label()
28        self.color_labels = self.parent.get_color_label()
29        self.init_ui()
30        self.Centre()
31        self.Show()
32
33    def init_ui(self):
34        """
35        Create spacing needed
36        """
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
51        symbolstaticbox = wx.StaticBox(panel, -1, 'Symbol')
52        symbolstaticboxsizer = wx.StaticBoxSizer(symbolstaticbox, wx.VERTICAL)
53
54        # add widgets - reverse order!
55        # texts
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')
60
61        # selection widgets
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))
71
72        # buttons
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)
77
78        # now Add all the widgets to relevant spacer - tricky
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)
81
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)
87
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)
90
91        ivbox2.Add(ihbox1, flag=wx.ALIGN_LEFT, border=10)
92        ivbox2.Add(ihbox2, flag=wx.ALIGN_LEFT, border=10)
93
94        hbox1.Add(ivbox1, flag=wx.ALIGN_LEFT, border=10)
95        hbox1.Add(ivbox2, flag=wx.ALIGN_LEFT, border=10)
96
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)
102
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)
108
109        panel.SetSizer(vbox)
110
111        self.populate_symbol()
112        self.populate_color()
113        self.populate_size()
114
115        self.SetDefaultItem(self.symbollistbox)
116
117    def custom_size(self, event):
118        """
119        On custom size
120        """
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:
124                msg = "Unfortunately imaginary icons are not yet supported."
125                msg += "Please enter a positive value"
126                dial = wx.MessageDialog(None, msg, 'Error', wx.OK | wx.ICON_ERROR)
127                dial.ShowModal()
128                dlg.Destroy()
129                self.custom_size(event)
130            else:
131                self.final_size = dlg.GetValue()
132                dlg.Destroy()
133        else:
134            dlg.Destroy()
135
136    def set_defaults(self, size, color, symbol, label):
137        """
138        Set Defaults
139        """
140        self.final_size = size
141        # set up gui values
142        self.labeltextbox.SetValue(label)
143        if size % 1 == 0 and size > 1 and size < 11:
144            self.sizecombobox.SetSelection(int(size) - 1)
145        else:
146            self.sizecombobox.SetSelection(4)
147        self.symbollistbox.SetSelection(self.sorted_sym_dic[symbol])
148        colorname = appearanceDialog.find_key(self.parent.get_color_label(), color)
149        self.colorlistbox.SetStringSelection(colorname)
150
151    def populate_symbol(self):
152        """
153        Populate Symbols
154        """
155        self.sorted_symbo_labels = sorted(self.symbo_labels.iteritems(),
156                                          key=operator.itemgetter(1))
157        self.sorted_sym_dic = {}
158        i = 0
159        for label in self.sorted_symbo_labels:
160            self.symbollistbox.Append(str(label[0]))
161            self.sorted_sym_dic[str(label[0])] = i
162            i += 1
163
164    def populate_color(self):
165        """
166        Populate Colors
167        """
168        sortedcolor_labels = sorted(self.color_labels.iteritems(),
169                                    key=operator.itemgetter(1))
170        for color in sortedcolor_labels:
171            self.colorlistbox.Append(str(color[0]))
172
173    def populate_size(self):
174        """
175        Populate Size
176        """
177        for i in range(1, 11):
178            self.sizecombobox.Append(str(i) + '.0')
179
180    def combo_click(self, event):
181        """
182        Combox on click
183        """
184        event.Skip()
185        self.final_size = self.sizecombobox.GetValue()
186
187    def close_dlg(self, event):
188        """
189        On Close Dlg
190        """
191        event.Skip()
192        self.Destroy()
193
194    @staticmethod
195    def find_key(dic, val):
196        """
197        Find key
198        """
199        return [k for k, v in dic.iteritems() if v == val][0]
200
201    def get_current_values(self):
202        """
203        Get Current Values
204        :returns : (size, color, symbol, dataname)
205        """
206        size = float(self.final_size)
207        name = str(self.labeltextbox.GetValue())
208        seltuple = self.symbollistbox.GetSelections()
209        symbol = appearanceDialog.find_key(self.sorted_sym_dic,
210                                           int(seltuple[0]))
211        color = str(self.colorlistbox.GetValue())
212        return(size, color, symbol, name)
213
214    def on_ok(self, event):
215        """
216        On OK button clicked
217        """
218        event.Skip()
219        self.okay_clicked = True
220        self.Close()
Note: See TracBrowser for help on using the repository browser.