Merge branch 'master' of git.ucc.asn.au:/matches/honours
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / build / lib / pgu / gui / dialog.py
1 """
2 """
3 import os
4
5 from .const import *
6 from . import table, area
7 from . import basic, input, button
8 from . import pguglobals
9
10 class Dialog(table.Table):
11     """A dialog window with a title bar and an "close" button on the bar.
12     
13     Example:
14         title = gui.Label("My Title")
15         main = gui.Container()
16         #add stuff to the container...
17         
18         d = gui.Dialog(title,main)
19         d.open()
20
21     """
22     def __init__(self,title,main,**params):
23         """Dialog constructor.
24
25         Arguments:
26             title -- title widget, usually a label
27             main -- main widget, usually a container
28
29         """        
30         params.setdefault('cls','dialog')
31         table.Table.__init__(self,**params)
32         
33         
34         self.tr()
35         self.td(title,align=-1,cls=self.cls+'.bar')
36         clos = button.Icon(self.cls+".bar.close")
37         clos.connect(CLICK,self.close,None) 
38         self.td(clos,align=1,cls=self.cls+'.bar')
39         
40         self.tr()
41         self.td(main,colspan=2,cls=self.cls+".main")
42         
43         
44 #         self.tr()
45 #         
46 #         
47 #         t = table.Table(cls=self.cls+".bar")
48 #         t.tr()
49 #         t.td(title)
50 #         clos = button.Icon(self.cls+".bar.close")
51 #         t.td(clos,align=1)
52 #         clos.connect(CLICK,self.close,None) 
53 #         self.add(t,0,0)
54 #         
55 #         main.rect.w,main.rect.h = main.resize()
56 #         clos.rect.w,clos.rect.h = clos.resize()
57 #         title.container.style.width = main.rect.w - clos.rect.w
58 #         
59 #         self.tr()
60 #         self.td(main,cls=self.cls+".main")
61
62         
63         
64 class FileDialog(Dialog):
65     """A file picker dialog window."""
66     
67     def __init__(self, title_txt="File Browser", button_txt="Okay", cls="dialog", path=None):
68         """FileDialog constructor.
69
70         Keyword arguments:
71             title_txt -- title text
72             button_txt -- button text
73             path -- initial path
74
75         """
76
77         cls1 = "filedialog"
78         if not path: self.curdir = os.getcwd()
79         else: self.curdir = path
80         self.dir_img = basic.Image(
81             pguglobals.app.theme.get(cls1+".folder", "", 'image'))
82         td_style = {'padding_left': 4,
83                     'padding_right': 4,
84                     'padding_top': 2,
85                     'padding_bottom': 2}
86         self.title = basic.Label(title_txt, cls=cls+".title.label")
87         self.body = table.Table()
88         self.list = area.List(width=350, height=150)
89         self.input_dir = input.Input()
90         self.input_file = input.Input()
91         self._list_dir_()
92         self.button_ok = button.Button(button_txt)
93         self.body.tr()
94         self.body.td(basic.Label("Folder"), style=td_style, align=-1)
95         self.body.td(self.input_dir, style=td_style)
96         self.body.tr()
97         self.body.td(self.list, colspan=3, style=td_style)
98         self.list.connect(CHANGE, self._item_select_changed_, None)
99         self.button_ok.connect(CLICK, self._button_okay_clicked_, None)
100         self.body.tr()
101         self.body.td(basic.Label("File"), style=td_style, align=-1)
102         self.body.td(self.input_file, style=td_style)
103         self.body.td(self.button_ok, style=td_style)
104         self.value = None
105         Dialog.__init__(self, self.title, self.body)
106         
107     def _list_dir_(self):
108         self.input_dir.value = self.curdir
109         self.input_dir.pos = len(self.curdir)
110         self.input_dir.vpos = 0
111         dirs = []
112         files = []
113         try:
114             for i in os.listdir(self.curdir):
115                 if os.path.isdir(os.path.join(self.curdir, i)): dirs.append(i)
116                 else: files.append(i)
117         except:
118             self.input_file.value = "Opps! no access"
119         #if '..' not in dirs: dirs.append('..')
120         dirs.sort()
121         dirs = ['..'] + dirs
122         
123         files.sort()
124         for i in dirs:
125             #item = ListItem(image=self.dir_img, text=i, value=i)
126             self.list.add(i,image=self.dir_img,value=i)
127         for i in files:
128             #item = ListItem(image=None, text=i, value=i)
129             self.list.add(i,value=i)
130         #self.list.resize()
131         self.list.set_vertical_scroll(0)
132         #self.list.repaintall()
133         
134         
135     def _item_select_changed_(self, arg):
136         self.input_file.value = self.list.value
137         fname = os.path.abspath(os.path.join(self.curdir, self.input_file.value))
138         if os.path.isdir(fname):
139             self.input_file.value = ""
140             self.curdir = fname
141             self.list.clear()
142             self._list_dir_()
143
144
145     def _button_okay_clicked_(self, arg):
146         if self.input_dir.value != self.curdir:
147             if os.path.isdir(self.input_dir.value):
148                 self.input_file.value = ""
149                 self.curdir = os.path.abspath(self.input_dir.value)
150                 self.list.clear()
151                 self._list_dir_()
152         else:
153             self.value = os.path.join(self.curdir, self.input_file.value)
154             self.send(CHANGE)
155             self.close()
156

UCC git Repository :: git.ucc.asn.au