46759a5951f03715ad94a36349280d6bc2a95d4e
[public/library.git] / bookForm.py
1 import curses
2
3
4
5
6
7 class bookForm:
8     mx = my = 0
9     hl = 0
10     topline = 0
11     cursor = 0
12     left = 0
13     top = 2
14     caption = "Add a Book"
15     blabel = "Add"
16     labels = ["ISBN", "LCCN", "Title", "Subtitle", "Authors", "Edition",
17               "Publisher", "Publish Date", "Publish Year", "Publish Month", "Publish location",
18               "Pages", "Pagination", "Weight"]
19     entries = []
20
21     def __init__(self,window):
22         self.w = window
23         self.w.resize(len(self.labels)+6,50)
24         self.updateEntries({})
25         self.updateGeometry()
26
27     def updateGeometry(self):
28         (self.my, self.mx) = self.w.getmaxyx()
29         for l in self.labels:
30             self.left = max(len(l),self.left)
31         self.left += 4
32         self.width = self.mx-self.left-2
33         self.top = 2
34         # next, the buttons
35         self.bcol = [self.mx-len(self.blabel)-14, self.mx-len(self.blabel)-4]
36         self.bwidth = [8,len(self.blabel)+2]
37
38     def updateEntries(self,book)
39         self.entries=[]
40         for l in self.labels:
41             if l.lower() in book:
42                 self.entries.append(book[l.lower()])
43             else:
44                 self.entries.append("")
45
46
47     def highlight(self):
48         if bt = -1:
49             self.w.chgat(self.row, self.left, self.width, curses.A_UNDERLINE)
50         else:
51             self.w.chgat(self.row, self.bcol[self.bt], self.bwidth[self.bt], curses.A_REVERSE)
52
53         
54         cursor = len(entries[highlight])
55         w.move(r,m+cursor)
56         curses.curs_set(1)
57         row = self.hl-self.topline+self.top
58         if row > 1 and row < self.my:
59             self.w.chgat(row,0,self.mx,curses.A_REVERSE)
60
61     def unHighlight(self):
62         row = self.hl-self.topline+self.top
63         if row > 1 and row < self.my:
64             self.w.chgat(row,0,self.mx,curses.A_NORMAL)
65
66     def mvHighlight(self,delta):
67         new = self.hl+delta
68         if self.hl = len(self.labels)+1:    # skip cancel button when going up
69             new -= 1
70         new = max(new,0)
71         new = min(new,len(self.labels)+1)   # there are two buttons, so add 2 to length
72         self.unHighlight()
73         self.hl = new
74         self.row = self.hl + self.top
75         self.highlight()
76     
77
78 # items is a list of (label, value) pairs
79 def redrawForm(w, caption, items, buttonlabel, m):
80     (y,x)=w.getmaxyx()
81     w.border()
82     curses.curs_set(1)
83     w.addstr(1,1,caption)
84     r=3
85     for l,v in items:
86         c = m-len(l)-2
87         w.addstr(r,c,l+":")
88         w.addstr(r,m,v)
89         r+=2
90     w.addstr(r,x-len(buttonlabel)-len("<cancel>")-6, "<cancel>  <"+buttonlabel+">")
91     w.refresh()
92
93 def lists_to_dict(l1, l2):
94     book = {}
95     for k,v in zip(l1,l2):
96         if v!="" and k.lower()!="publish date":
97             book[k.lower()]=v
98     return book
99
100
101 #the final form for book data entry - takes caption and book info.
102 def bookForm(caption, book, buttonlabel):
103     labels = ["ISBN", "LCCN", "Title", "Subtitle", "Authors", "Edition",
104               "Publisher", "Publish Date", "Publish Year", "Publish Month", "Publish location",
105               "Pages", "Pagination", "Weight"]
106     entries = []
107     m = 0
108
109     w=curses.newwin(34,70,1,10)
110     (y,x)=w.getmaxyx()
111     w.keypad(1)
112     redrawForm(w,caption,zip(labels,entries),buttonlabel,m)
113     bcol = [x-len(buttonlabel)-len("<cancel>")-6, x-len(buttonlabel)-4]
114     bwidth = [8,len(buttonlabel)+2]
115     
116     highlight=0
117     b = -1
118     r=3
119     w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
120     w.move(r,m+len(entries[highlight]))
121     cursor = len(entries[highlight])
122     ch = w.getch()
123     while (1==1):
124         if ch==27: #escape key
125             curses.curs_set(0)
126             w.clear()
127             w.refresh()
128             return {}
129         if ch==curses.KEY_UP:
130             if highlight == len(labels):
131                 w.chgat(r,bcol[b],bwidth[b],curses.A_NORMAL)
132                 highlight = len(labels)-1
133                 b = -1
134                 r=3+2*highlight
135                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
136                 cursor = len(entries[highlight])
137                 w.move(r,m+cursor)
138                 curses.curs_set(1)
139             elif highlight!=0:
140                 w.chgat(r,m,x-m-2,curses.A_NORMAL)
141                 highlight -= 1
142                 r=3+2*highlight
143                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
144                 cursor = len(entries[highlight])
145                 w.move(r,m+cursor)
146         elif ch==curses.KEY_PPAGE:
147             w.chgat(r,m,x-m-2,curses.A_NORMAL)
148             highlight=0
149             b=-1
150             r=3+2*highlight
151             w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
152             cursor = len(entries[highlight])
153             w.move(r,m+cursor)
154             curses.curs_set(1)
155         elif ch==curses.KEY_DOWN:
156             if highlight >= len(labels) -1:
157                 highlight = len(labels)
158                 b += 1
159                 b = min(b,1)
160                 curses.curs_set(0)
161                 w.chgat(r,m,x-m-2,curses.A_NORMAL)
162                 r = y-3
163                 w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
164             else:
165                 w.chgat(r,m,x-m-2,curses.A_NORMAL)
166                 highlight += 1
167                 r=3+2*highlight
168                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
169                 cursor = len(entries[highlight])
170                 w.move(r,m+cursor)
171         elif ch==curses.KEY_NPAGE:
172             if highlight!=len(labels):
173                 highlight = len(labels)
174                 b += 1
175                 b = min(b,1)
176                 curses.curs_set(0)
177                 w.chgat(r,m,x-m-2,curses.A_NORMAL)
178                 r = y-3
179                 w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
180         elif ch==curses.KEY_LEFT:
181             if highlight == len(labels):
182                 w.chgat(r,bcol[b],bwidth[b],curses.A_NORMAL)
183                 b=0
184                 w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
185             else:
186                 if cursor>0:
187                     cursor-=1
188                     w.move(r,m+cursor)
189         elif ch==curses.KEY_RIGHT:
190             if highlight == len(labels):
191                 w.chgat(r,bcol[b],bwidth[b],curses.A_NORMAL)
192                 b=1
193                 w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
194             else:
195                 if cursor < len(entries[highlight]):
196                     cursor+=1
197                     w.move(r,m+cursor)
198         elif ch>31 and ch<127:
199             if highlight != len(labels):
200                 entries[highlight]=entries[highlight][:cursor] + curses.keyname(ch) + entries[highlight][cursor:]
201                 cursor+=1
202                 w.addnstr(r,m, entries[highlight]+(" "*40), x-m-2)
203                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
204                 w.move(r,m+cursor)
205         elif ch==curses.KEY_BACKSPACE:
206             if highlight != len(labels) and cursor!=0:
207                 cursor-=1
208                 entries[highlight]=entries[highlight][:cursor] + entries[highlight][cursor+1:]
209                 w.addnstr(r,m, entries[highlight]+(" "*40), x-m-2)
210                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
211                 w.move(r,m+cursor)
212         elif ch==curses.KEY_DC:
213             if highlight != len(labels):
214                 entries[highlight]=entries[highlight][:cursor] + entries[highlight][cursor+1:]
215                 w.addnstr(r,m, entries[highlight]+(" "*40), x-m-2)
216                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
217                 w.move(r,m+cursor)
218         elif ch==curses.KEY_HOME:
219             if highlight != len(labels):
220                 cursor=0
221                 w.move(r,m+cursor)
222         elif ch==curses.KEY_END:
223             if highlight != len(labels):
224                 cursor=len(entries[highlight])
225                 w.move(r,m+cursor)
226         elif ch==10:
227             if b != -1:
228                 if b == 0:
229                     w.clear()
230                     w.refresh()
231                     return {}
232                 elif b == 1:
233                     w.clear()
234                     w.refresh()
235                     return lists_to_dict(labels,entries)
236             elif highlight == len(labels)-1:
237                 highlight = len(labels)
238                 b=0
239                 curses.curs_set(0)
240                 w.chgat(r,m,x-m-2,curses.A_NORMAL)
241                 r = y-3
242                 w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
243             else:
244                 w.chgat(r,m,x-m-2,curses.A_NORMAL)
245                 highlight += 1
246                 r=3+2*highlight
247                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
248                 cursor = len(entries[highlight])
249                 w.move(r,m+cursor)
250
251
252         w.refresh()
253         ch = w.getch()
254
255     curses.curs_set(0)
256     w.clear()
257     w.refresh()
258     return {}