-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprsam-tk
More file actions
executable file
·336 lines (286 loc) · 14.8 KB
/
Copy pathprsam-tk
File metadata and controls
executable file
·336 lines (286 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python
#
# Copyright 2012-2013 Robert Schroll
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.
import os
import sys
import pyPdf
import tempfile
from Tkinter import *
import tkMessageBox
import tkFileDialog
from prsannots.manager import Manager, NotMountedError
from prsannots.pdfdice import UNITS
from prsannots.openfile import open_file
from prsannots.misc import u_argv
class EntryValue(Entry):
def __init__(self, master, hint='', **kw):
self.var = StringVar()
Entry.__init__(self, master, textvariable=self.var, **kw)
def set(self, value):
self.var.set(value)
class SpinboxValue(Spinbox):
def __init__(self, master, **kw):
self.var = StringVar()
Spinbox.__init__(self, master, textvariable=self.var, **kw)
def set(self, value):
self.var.set(value)
class Main(Frame):
def __init__(self, master, manager, needs_save):
Frame.__init__(self, master)
self.manager = manager
self.needs_save = needs_save
n_sync = len(self.manager.needing_sync)
self.master.protocol('WM_DELETE_WINDOW', self.close)
self.file_entry = EntryValue(self, width=30, state='readonly')
self.file_entry.grid(row=0, column=0, sticky=E+W)
self.file_button = Button(self, text='...', command=self.load_file)
self.file_button.grid(row=0, column=1, sticky=W)
self.add_frame = Frame(self)
self.add_frame.grid(row=1, column=0, columnspan=2, sticky=E+W)
Label(self.add_frame, text="Add file to library", font='TkHeadingFont').grid(row=0, column=0, columnspan=6, pady=(0,2))
Label(self.add_frame, text="Dice PDF into").grid(row=1, column=0, columnspan=2, sticky=W)
self.ncols_entry = SpinboxValue(self.add_frame, from_=1, to=10, width=2, justify=RIGHT)
self.ncols_entry.grid(row=1, column=2, sticky=E)
Label(self.add_frame, text="columns").grid(row=1, column=3, sticky=W)
self.nrows_entry = SpinboxValue(self.add_frame, from_=1, to=10, width=2, justify=RIGHT)
self.nrows_entry.grid(row=1, column=4, sticky=E)
Label(self.add_frame, text="rows").grid(row=1, column=5, sticky=W)
Label(self.add_frame, text="Overlap fraction").grid(row=2, column=0, columnspan=2, sticky=W)
self.olh_entry = SpinboxValue(self.add_frame, from_=0, to=0.2, increment=0.01, width=4, justify=RIGHT)
self.olh_entry.grid(row=2, column=2, sticky=E)
Label(self.add_frame, text="horizontal").grid(row=2, column=3, sticky=W)
self.olv_entry = SpinboxValue(self.add_frame, from_=0, to=0.2, increment=0.01, width=4, justify=RIGHT)
self.olv_entry.grid(row=2, column=4, sticky=E)
Label(self.add_frame, text="vertical").grid(row=2, column=5, sticky=W)
Label(self.add_frame, text="Crop").grid(row=3, column=0, columnspan=2, sticky=W)
self.cropl = SpinboxValue(self.add_frame, from_=0, to=1000, increment=0.1, width=4, justify=RIGHT)
self.cropl.grid(row=3, column=2, sticky=E)
Label(self.add_frame, text="left").grid(row=3, column=3, sticky=W)
self.cropb = SpinboxValue(self.add_frame, from_=0, to=1000, increment=0.1, width=4, justify=RIGHT)
self.cropb.grid(row=3, column=4, sticky=E)
Label(self.add_frame, text="bottom").grid(row=3, column=5, sticky=W)
Label(self.add_frame, text="units:").grid(row=4, column=0, columnspan=1, sticky=E)
self.cropu = SpinboxValue(self.add_frame, values=UNITS.keys(), width=3)
self.cropu.grid(row=4, column=1, sticky=W)
self.cropr = SpinboxValue(self.add_frame, from_=0, to=1000, increment=0.1, width=4, justify=RIGHT)
self.cropr.grid(row=4, column=2, sticky=E)
Label(self.add_frame, text="right").grid(row=4, column=3, sticky=W)
self.cropt = SpinboxValue(self.add_frame, from_=0, to=1000, increment=0.1, width=4, justify=RIGHT)
self.cropt.grid(row=4, column=4, sticky=E)
Label(self.add_frame, text="top").grid(row=4, column=5, sticky=W)
Label(self.add_frame, text="Title").grid(row=5, column=0, sticky=E)
self.title_entry = EntryValue(self.add_frame)
self.title_entry.grid(row=5, column=1, columnspan=5, sticky=E+W)
Label(self.add_frame, text="Author").grid(row=6, column=0, sticky=E)
self.author_entry = EntryValue(self.add_frame)
self.author_entry.grid(row=6, column=1, columnspan=5, sticky=E+W)
self.preview_button = Button(self.add_frame, text='Preview', command=self.preview)
self.preview_button.grid(row=7, column=0, columnspan=2, sticky=W)
self.add_button = Button(self.add_frame, text='Add', command=self.add)
self.add_button.grid(row=7, column=5, sticky=E)
self.import_frame = Frame(self)
self.import_frame.grid(row=1, column=0, columnspan=2, sticky=N+S+E+W)
self.import_frame.columnconfigure(0, weight=1) # Expand first column to fill space
self.import_frame.columnconfigure(1, weight=0) # Don't expand second column
Label(self.import_frame, text="Import PDF already on reader", font='TkHeadingFont').grid(row=0, column=0, columnspan=2, pady=(0,2))
Label(self.import_frame, text="Select directory for syncing annotations").grid(row=1, column=0, sticky=W)
self.import_file_entry = EntryValue(self.import_frame, width=35, state='readonly')
self.import_file_entry.grid(row=2, column=0, sticky=E+W)
self.import_file_button = Button(self.import_frame, text='...', command=self.load_import_dir)
self.import_file_button.grid(row=2, column=1, sticky=W)
self.import_button = Button(self.import_frame, text="Import", command=self.do_import)
self.import_button.grid(row=3, column=1, sticky=E)
self.message_frame = Label(self, justify=LEFT)
self.message_frame.grid(row=1, column=0, columnspan=2, sticky=N+S+E+W)
self.solo_message_frame = Label(self, justify=LEFT)
self.solo_message_frame.grid(row=1, column=0, columnspan=2, sticky=N+S+E+W)
self.solo_message_frame.grid_remove()
Frame(self, height=2, bd=1, relief=SUNKEN).grid(row=2, column=0, columnspan=2, sticky=E+W, pady=2)
self.sync_button = Button(self, text='Sync %i files' % n_sync, command=self.sync)
self.sync_button.grid(row=3, column=0, sticky=W)
if not n_sync:
self.sync_button.grid_remove() # Bye Bye Bye
self.close_button = Button(self, text='Close', command=self.close)
self.close_button.grid(row=3, column=1, sticky=E)
self.status = Label(self, text="", bd=1, relief=SUNKEN, anchor=W)
self.status.grid(row=4, column=0, columnspan=2, sticky=E+W, pady=(2,0))
self.reset()
self.message()
def reset(self):
self.file_entry.set('')
self.title_entry.set('')
self.author_entry.set('')
self.ncols_entry.set(1)
self.nrows_entry.set(1)
self.olh_entry.set(0.05)
self.olv_entry.set(0.05)
self.import_file_entry.set('')
self.import_button.config(state='disabled')
self.master.focus()
def message(self, text=''):
self.import_frame.grid_remove()
self.message_frame.grid()
self.message_frame.config(text="Select a file above to be added to your library.\n\n"+text)
def solo_message(self, text=None):
if text is not None:
self.solo_message_frame.config(text=text)
self.solo_message_frame.grid()
else:
self.solo_message_frame.grid_remove()
def load_file(self):
filename = tkFileDialog.askopenfilename(parent=root, title="Choose PDF file to load",
filetypes=[('PDF files', '.pdf'), ('All files', '.*')])
self.prep_file(filename)
def prep_file(self, filename):
if filename is None:
filename = ''
filename = filename.replace('/', os.path.sep) # For some reason, the Tk file dialog returns
self.file_entry.set(filename) # a path using '/' instead of os.path.sep
if not filename:
self.message()
return
try:
pdf = pyPdf.PdfFileReader(open(filename, 'rb'))
except (IOError, pyPdf.utils.PdfReadError):
self.message("Selected file is not a PDF file.")
return
if self.manager.in_library(filename):
self.message("Selected file is already in library.")
return
if filename.startswith(self.manager.mount):
self.message_frame.grid_remove()
self.import_frame.grid()
else:
self.message_frame.grid_remove()
self.import_frame.grid_remove()
self.title_entry.set(pdf.documentInfo.get('/Title', ''))
self.author_entry.set(pdf.documentInfo.get('/Author', ''))
def get_dice_args(self):
return (int(self.ncols_entry.get()), int(self.nrows_entry.get()),
[float(getattr(self, a).get()) * UNITS[self.cropu.get()]
for a in ('cropl', 'cropb', 'cropr', 'cropt')],
(float(self.olh_entry.get()), float(self.olv_entry.get())))
def add(self):
try:
self.manager.add_diced_pdf(self.file_entry.get(),
self.get_dice_args(),
title=self.title_entry.get(), author=self.author_entry.get())
except (ValueError, IOError, pyPdf.utils.PdfReadError), e:
tkMessageBox.showerror(title="Add file",
message="Could not add file to library.\n\n" + str(e))
else:
self.reset()
self.needs_save = True
self.message("File added to library.")
def preview(self):
fh, preview = tempfile.mkstemp()
os.close(fh)
try:
self.manager.add_diced_pdf(self.file_entry.get(),
self.get_dice_args(),
title=self.title_entry.get(), author=self.author_entry.get(),
preview=preview)
except (ValueError, IOError, pyPdf.utils.PdfReadError), e:
tkMessageBox.showerror(title="Preview file",
message="Could not create preview file.\n\n" + str(e))
else:
if not open_file(preview):
tkMessageBox.showerror(title="Preview file",
message="Could not open default PDF viewer.\n\n"
"Preview file saved as %s" % preview);
def load_import_dir(self):
syncdir = tkFileDialog.askdirectory(title="Select mount point of reader", mustexist=True)
if syncdir.startswith(self.manager.mount):
syncdir = ''
self.import_file_entry.set(syncdir)
self.import_button.config(state=(syncdir and 'normal' or 'disabled'))
def do_import(self):
try:
self.manager.import_pdf(self.file_entry.get(), self.import_file_entry.get())
except IOError, e:
tkMessageBox.showerror(title="Import file",
message="Could not import file to library.\n\n" + str(e))
else:
self.reset()
self.needs_save = True
self.message("File added to library.")
def close(self):
if self.needs_save:
self.manager.save()
self.master.destroy()
def sync(self):
need_sync = self.manager.needing_sync
for i, filename in enumerate(need_sync):
self.sync_button.config(text="Syncing %i/%i..." % (i+1, len(need_sync)), relief=SUNKEN)
self.solo_message("Syncing %s" % os.path.basename(filename))
self.master.update_idletasks() # Update the sync_button and status text
try:
self.manager.sync_pdf(filename)
except Exception, e:
tkMessageBox.showerror(title="Syncing file",
message="Error syncing %s: %s" % (filename, e))
self.sync_button.grid_remove()
self.solo_message()
self.needs_save = True
if __name__ == '__main__':
m = Manager()
root = Tk()
root.resizable(width=False, height=False)
root.title("PRS Annotation Manager")
needs_save = False
quit_ = False
if not m.load_mounted_reader():
root.withdraw() # Don't show root window yet.
mount = tkFileDialog.askdirectory(title="Select mount point of reader", mustexist=True)
if not mount:
quit_ = True
else:
if m.load_mount(mount):
if tkMessageBox.askyesno(title="Adjust settings?",
message="The settings indicate that this reader is" +
"usually mounted at %s.\n\n" % m.settings['mount'] +
"Now it is mounted at %s.\n\n" % mount +
"Adjust the settings?"):
m.update_mount_setting()
needs_save = True
else:
if tkMessageBox.askyesno(title="Create library?",
message="There is no library on this computer for the " +
"reader at %s.\n\n" % mount +
"Create a new library?"):
try:
m.new(mount)
except (NotMountedError, IOError), e:
tkMessageBox.showerror(title="Create library",
message="Could not create library.\n\n" + str(e))
quit_ = True
else:
needs_save = True
else: # No to create library
quit_ = True
if quit_:
root.destroy()
sys.exit(1)
else:
root.deiconify() # Now show root
main = Main(root, m, needs_save)
main.pack(padx=2, pady=2)
if len(u_argv) > 1:
filename = os.path.abspath(u_argv[1])
if os.path.exists(filename):
main.prep_file(filename)
root.mainloop()