Merge lp:~matttbe/ubuntu/precise/pdfshuffler/0.5.99-svn64 into lp:ubuntu/precise/pdfshuffler
- Precise (12.04)
- 0.5.99-svn64
- Merge into precise
Status: | Merged |
---|---|
Merge reported by: | Stefano Rivera |
Merged at revision: | not available |
Proposed branch: | lp:~matttbe/ubuntu/precise/pdfshuffler/0.5.99-svn64 |
Merge into: | lp:ubuntu/precise/pdfshuffler |
Diff against target: |
5610 lines (+3230/-2150) 20 files modified
.pc/applied-patches (+1/-1) .pc/fix-shebang/pdfshuffler (+0/-1031) .pc/install_and_look_for_UI_file_in_right_dirs.patch/pdfshuffler/pdfshuffler.py (+1057/-0) .pc/install_and_look_for_UI_file_in_right_dirs.patch/setup.py (+66/-0) TODO (+0/-2) bin/pdfshuffler (+33/-0) data/pdfshuffler.ui (+359/-0) debian/changelog (+11/-0) debian/patches/fix-shebang (+0/-12) debian/patches/install_and_look_for_UI_file_in_right_dirs.patch (+31/-0) debian/patches/series (+1/-1) doc/pdfshuffler.1 (+2/-2) pdfshuffler (+0/-1031) pdfshuffler/cairorendering.py (+168/-0) pdfshuffler/pdfshuffler.py (+1057/-0) po/genpot.sh (+2/-1) po/ja.po (+156/-0) po/pdfshuffler.pot (+118/-60) po/zh_CN.po (+156/-0) setup.py (+12/-9) |
To merge this branch: | bzr merge lp:~matttbe/ubuntu/precise/pdfshuffler/0.5.99-svn64 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Ubuntu Sponsors | Pending | ||
Review via email: mp+92648@code.launchpad.net |
Commit message
Description of the change
Hello,
Is it possible to upload this new version because it fixes this annoying bug: PDFShuffler displays only black squares
- http://
- https:/
This version has been tested and it is available on this ppa:matttbe/ppa
This branch should be ready to be pushed on lp:ubuntu/pdfshuffler (except the "UNRELEASED" version in debian/changelog)
Please upload this package before the FF :)
Thank you for your help!
Stefano Rivera (stefanor) wrote : | # |
Matthieu Baerts (matttbe) wrote : | # |
It's strange! I simply used 'bzr merge-upstream' with the tarball...
And if I want to give you a debdiff, I have this error: File /tmp/ThBQcppeQT
What can I do?
Matthieu Baerts (matttbe) wrote : | # |
This is strange... These two commands work fine:
bzr branch lp:~matttbe/ubuntu/precise/pdfshuffler/0.5.99-svn64
bzr bd lp:~matttbe/ubuntu/precise/pdfshuffler/0.5.99-svn64
But not this one:
bzr merge lp:~matttbe/ubuntu/precise/pdfshuffler/0.5.99-svn64
Stefano Rivera (stefanor) wrote : | # |
Yeah, it's LP: #923688. I worked around it...
Matthieu Baerts (matttbe) wrote : | # |
Oh ok, it's a known bug!
Thank you for your help :)
Stefano Rivera (stefanor) wrote : | # |
Slightly modified the changelog and uploaded.
Preview Diff
1 | === modified file '.pc/applied-patches' | |||
2 | --- .pc/applied-patches 2010-09-15 23:33:31 +0000 | |||
3 | +++ .pc/applied-patches 2012-02-12 00:35:23 +0000 | |||
4 | @@ -1,1 +1,1 @@ | |||
6 | 1 | fix-shebang | 1 | install_and_look_for_UI_file_in_right_dirs.patch |
7 | 2 | 2 | ||
8 | === removed directory '.pc/fix-shebang' | |||
9 | === removed file '.pc/fix-shebang/pdfshuffler' | |||
10 | --- .pc/fix-shebang/pdfshuffler 2011-02-15 00:43:28 +0000 | |||
11 | +++ .pc/fix-shebang/pdfshuffler 1970-01-01 00:00:00 +0000 | |||
12 | @@ -1,1031 +0,0 @@ | |||
13 | 1 | #!/usr/bin/env python | ||
14 | 2 | # -*- coding: utf-8 -*- | ||
15 | 3 | |||
16 | 4 | """ | ||
17 | 5 | -------------------------------------------------------------------------- | ||
18 | 6 | |||
19 | 7 | PDF-Shuffler 0.5.1 - pyGTK PDF Merging, Rearranging, and Splitting | ||
20 | 8 | Copyright (C) 2008-2010 Konstantinos Poulios | ||
21 | 9 | <https://sourceforge.net/projects/pdfshuffler> | ||
22 | 10 | |||
23 | 11 | -------------------------------------------------------------------------- | ||
24 | 12 | |||
25 | 13 | This program is free software; you can redistribute it and/or modify | ||
26 | 14 | it under the terms of the GNU General Public License as published by | ||
27 | 15 | the Free Software Foundation; either version 3 of the License, or | ||
28 | 16 | (at your option) any later version. | ||
29 | 17 | |||
30 | 18 | This program is distributed in the hope that it will be useful, | ||
31 | 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
32 | 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
33 | 21 | GNU General Public License for more details. | ||
34 | 22 | |||
35 | 23 | You should have received a copy of the GNU General Public License along | ||
36 | 24 | with this program; if not, write to the Free Software Foundation, Inc., | ||
37 | 25 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
38 | 26 | |||
39 | 27 | -------------------------------------------------------------------------- | ||
40 | 28 | """ | ||
41 | 29 | |||
42 | 30 | import os | ||
43 | 31 | import shutil #needed for file operations like whole directory deletion | ||
44 | 32 | import sys #needed for proccessing of command line args | ||
45 | 33 | import urllib #needed to parse filename information passed by DnD | ||
46 | 34 | import threading | ||
47 | 35 | import tempfile | ||
48 | 36 | from copy import copy | ||
49 | 37 | |||
50 | 38 | import locale #for multilanguage support | ||
51 | 39 | import gettext | ||
52 | 40 | gettext.install('pdfshuffler', unicode=1) | ||
53 | 41 | |||
54 | 42 | try: | ||
55 | 43 | import pygtk | ||
56 | 44 | pygtk.require('2.0') | ||
57 | 45 | import gtk | ||
58 | 46 | assert gtk.gtk_version >= (2, 10, 0) | ||
59 | 47 | assert gtk.pygtk_version >= (2, 10, 0) | ||
60 | 48 | except AssertionError: | ||
61 | 49 | print('You do not have the required versions of GTK+ and/or PyGTK ' + | ||
62 | 50 | 'installed.\n\n' + | ||
63 | 51 | 'Installed GTK+ version is ' + | ||
64 | 52 | '.'.join([str(n) for n in gtk.gtk_version]) + '\n' + | ||
65 | 53 | 'Required GTK+ version is 2.10.0 or higher\n\n' | ||
66 | 54 | 'Installed PyGTK version is ' + | ||
67 | 55 | '.'.join([str(n) for n in gtk.pygtk_version]) + '\n' + | ||
68 | 56 | 'Required PyGTK version is 2.10.0 or higher') | ||
69 | 57 | sys.exit(1) | ||
70 | 58 | except: | ||
71 | 59 | print('PyGTK version 2.10.0 or higher is required to run this program.') | ||
72 | 60 | print('No version of PyGTK was found on your system.') | ||
73 | 61 | sys.exit(1) | ||
74 | 62 | |||
75 | 63 | gtk.gdk.threads_init() | ||
76 | 64 | import gobject #to use custom signals | ||
77 | 65 | import pango #to adjust the text alignment in CellRendererText | ||
78 | 66 | import gio #to inquire mime types information | ||
79 | 67 | |||
80 | 68 | import poppler #for the rendering of pdf pages | ||
81 | 69 | from pyPdf import PdfFileWriter, PdfFileReader | ||
82 | 70 | |||
83 | 71 | class PDFshuffler: | ||
84 | 72 | # ======================================================= | ||
85 | 73 | # All the preferences are stored here. | ||
86 | 74 | # ======================================================= | ||
87 | 75 | prefs = { | ||
88 | 76 | 'window width': min (700, gtk.gdk.screen_get_default().get_width() / 2 ), | ||
89 | 77 | 'window height': min(600, gtk.gdk.screen_get_default().get_height() - 50 ), | ||
90 | 78 | 'window x': 0, | ||
91 | 79 | 'window y': 0, | ||
92 | 80 | 'initial thumbnail size': 530, | ||
93 | 81 | 'initial zoom scale': 0.25, | ||
94 | 82 | } | ||
95 | 83 | |||
96 | 84 | MODEL_ROW_INTERN = 1001 | ||
97 | 85 | MODEL_ROW_EXTERN = 1002 | ||
98 | 86 | TEXT_URI_LIST = 1003 | ||
99 | 87 | MODEL_ROW_MOTION = 1004 | ||
100 | 88 | TARGETS_IV = [('MODEL_ROW_INTERN', gtk.TARGET_SAME_WIDGET, MODEL_ROW_INTERN), | ||
101 | 89 | ('MODEL_ROW_EXTERN', gtk.TARGET_OTHER_APP, MODEL_ROW_EXTERN), | ||
102 | 90 | ('MODEL_ROW_MOTION', 0, MODEL_ROW_MOTION)] | ||
103 | 91 | TARGETS_SW = [('text/uri-list', 0, TEXT_URI_LIST), | ||
104 | 92 | ('MODEL_ROW_EXTERN', gtk.TARGET_OTHER_APP, MODEL_ROW_EXTERN)] | ||
105 | 93 | |||
106 | 94 | def __init__(self): | ||
107 | 95 | # Create the temporary directory | ||
108 | 96 | self.tmp_dir = tempfile.mkdtemp("pdfshuffler") | ||
109 | 97 | os.chmod(self.tmp_dir, 0700) | ||
110 | 98 | |||
111 | 99 | pixmap = os.path.join(sys.prefix,'share','pixmaps','pdfshuffler.png') | ||
112 | 100 | try: | ||
113 | 101 | gtk.window_set_default_icon_from_file(pixmap) | ||
114 | 102 | except: | ||
115 | 103 | print(_('File %s does not exist') % pixmap) | ||
116 | 104 | |||
117 | 105 | # Create the main window, and attach delete_event signal to terminating | ||
118 | 106 | # the application | ||
119 | 107 | self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) | ||
120 | 108 | self.window.set_title('PDF-Shuffler') | ||
121 | 109 | self.window.set_border_width(0) | ||
122 | 110 | self.window.move(self.prefs['window x'], self.prefs['window y']) | ||
123 | 111 | self.window.set_size_request(self.prefs['window width'], | ||
124 | 112 | self.prefs['window height']) | ||
125 | 113 | self.window.connect('delete_event', self.close_application) | ||
126 | 114 | self.window.show() | ||
127 | 115 | |||
128 | 116 | # Create a vbox to hold the thumnails-container | ||
129 | 117 | vbox = gtk.VBox() | ||
130 | 118 | self.window.add(vbox) | ||
131 | 119 | |||
132 | 120 | # Create a scrolled window to hold the thumbnails-container | ||
133 | 121 | self.sw = gtk.ScrolledWindow() | ||
134 | 122 | self.sw.set_border_width(0) | ||
135 | 123 | self.sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) | ||
136 | 124 | self.sw.drag_dest_set(gtk.DEST_DEFAULT_MOTION | | ||
137 | 125 | gtk.DEST_DEFAULT_HIGHLIGHT | | ||
138 | 126 | gtk.DEST_DEFAULT_DROP | | ||
139 | 127 | gtk.DEST_DEFAULT_MOTION, | ||
140 | 128 | self.TARGETS_SW, | ||
141 | 129 | gtk.gdk.ACTION_COPY | | ||
142 | 130 | gtk.gdk.ACTION_MOVE ) | ||
143 | 131 | self.sw.connect('drag_data_received', self.sw_dnd_received_data) | ||
144 | 132 | self.sw.connect('button_press_event', self.sw_button_press_event) | ||
145 | 133 | vbox.pack_start(self.sw) | ||
146 | 134 | |||
147 | 135 | # Create an alignment to keep the thumbnails center-aligned | ||
148 | 136 | align = gtk.Alignment(0.5, 0.5, 0, 0) | ||
149 | 137 | self.sw.add_with_viewport(align) | ||
150 | 138 | |||
151 | 139 | # Create ListStore model and IconView | ||
152 | 140 | self.model = gtk.ListStore(str, # 0.Text descriptor | ||
153 | 141 | gtk.gdk.Pixbuf, # 1.Thumbnail image | ||
154 | 142 | int, # 2.Document number | ||
155 | 143 | int, # 3.Page number | ||
156 | 144 | int, # 4.Thumbnail width | ||
157 | 145 | str, # 5.Document filename | ||
158 | 146 | bool, # 6.Rendered | ||
159 | 147 | int, # 7.Rotation angle | ||
160 | 148 | float, # 8.Crop left | ||
161 | 149 | float, # 9.Crop right | ||
162 | 150 | float, # 10.Crop top | ||
163 | 151 | float) # 11.Crop bottom | ||
164 | 152 | |||
165 | 153 | self.zoom_scale = self.prefs['initial zoom scale'] | ||
166 | 154 | self.iv_col_width = self.prefs['initial thumbnail size'] | ||
167 | 155 | |||
168 | 156 | self.iconview = gtk.IconView(self.model) | ||
169 | 157 | self.iconview.set_item_width(self.iv_col_width + 12) | ||
170 | 158 | |||
171 | 159 | self.iconview.set_pixbuf_column(1) | ||
172 | 160 | # self.cellpb = gtk.CellRendererPixbuf() | ||
173 | 161 | # self.cellpb.set_property('follow-state', True) | ||
174 | 162 | # self.iconview.pack_start(self.cellpb, False) | ||
175 | 163 | # self.iconview.set_attributes(self.cellpb, pixbuf=1) | ||
176 | 164 | |||
177 | 165 | # self.iconview.set_text_column(0) | ||
178 | 166 | self.celltxt = gtk.CellRendererText() | ||
179 | 167 | self.celltxt.set_property('width', self.iv_col_width) | ||
180 | 168 | self.celltxt.set_property('wrap-width', self.iv_col_width) | ||
181 | 169 | self.celltxt.set_property('alignment', pango.ALIGN_CENTER) | ||
182 | 170 | self.iconview.pack_start(self.celltxt, False) | ||
183 | 171 | self.iconview.set_attributes(self.celltxt, text=0) | ||
184 | 172 | |||
185 | 173 | self.iconview.set_selection_mode(gtk.SELECTION_MULTIPLE) | ||
186 | 174 | self.iconview.enable_model_drag_source(gtk.gdk.BUTTON1_MASK, | ||
187 | 175 | self.TARGETS_IV, | ||
188 | 176 | gtk.gdk.ACTION_COPY | | ||
189 | 177 | gtk.gdk.ACTION_MOVE ) | ||
190 | 178 | self.iconview.enable_model_drag_dest(self.TARGETS_IV, | ||
191 | 179 | gtk.gdk.ACTION_DEFAULT) | ||
192 | 180 | self.iconview.connect('drag_begin', self.iv_drag_begin) | ||
193 | 181 | self.iconview.connect('drag_data_get', self.iv_dnd_get_data) | ||
194 | 182 | self.iconview.connect('drag_data_received', self.iv_dnd_received_data) | ||
195 | 183 | self.iconview.connect('drag_data_delete', self.iv_dnd_data_delete) | ||
196 | 184 | self.iconview.connect('drag_motion', self.iv_dnd_motion) | ||
197 | 185 | self.iconview.connect('drag_leave', self.iv_dnd_leave_end) | ||
198 | 186 | self.iconview.connect('drag_end', self.iv_dnd_leave_end) | ||
199 | 187 | self.iconview.connect('button_press_event', self.iv_button_press_event) | ||
200 | 188 | self.iv_auto_scroll_direction = 0 | ||
201 | 189 | |||
202 | 190 | style = self.iconview.get_style().copy() | ||
203 | 191 | style_sw = self.sw.get_style() | ||
204 | 192 | for state in (gtk.STATE_NORMAL, gtk.STATE_PRELIGHT, gtk.STATE_ACTIVE): | ||
205 | 193 | style.base[state] = style_sw.bg[gtk.STATE_NORMAL] | ||
206 | 194 | self.iconview.set_style(style) | ||
207 | 195 | |||
208 | 196 | align.add(self.iconview) | ||
209 | 197 | |||
210 | 198 | # Create a horizontal box to hold the buttons | ||
211 | 199 | hbox = gtk.HBox() | ||
212 | 200 | vbox.pack_start(hbox, expand=False, fill=False) | ||
213 | 201 | |||
214 | 202 | # Create buttons | ||
215 | 203 | self.button_exit = gtk.Button(stock=gtk.STOCK_QUIT) | ||
216 | 204 | self.button_exit.connect('clicked', self.close_application) | ||
217 | 205 | hbox.pack_start(self.button_exit, expand=True, fill=True, padding=20) | ||
218 | 206 | |||
219 | 207 | self.button_del = gtk.Button(_('Delete Page(s)')) | ||
220 | 208 | image = gtk.Image() | ||
221 | 209 | image.set_from_stock(gtk.STOCK_DELETE, gtk.ICON_SIZE_BUTTON) | ||
222 | 210 | self.button_del.set_image(image) | ||
223 | 211 | self.button_del.connect('clicked', self.clear_selected) | ||
224 | 212 | hbox.pack_start(self.button_del, expand=True, fill=True, padding=20) | ||
225 | 213 | |||
226 | 214 | self.button_import = gtk.Button(_('Import pdf')) | ||
227 | 215 | image = gtk.Image() | ||
228 | 216 | image.set_from_stock(gtk.STOCK_OPEN, gtk.ICON_SIZE_BUTTON) | ||
229 | 217 | self.button_import.set_image(image) | ||
230 | 218 | self.button_import.connect('clicked', self.on_action_add_doc_activate) | ||
231 | 219 | hbox.pack_start(self.button_import, expand=True, fill=True, padding=20) | ||
232 | 220 | |||
233 | 221 | self.button_export = gtk.Button(_('Export pdf')) | ||
234 | 222 | image = gtk.Image() | ||
235 | 223 | image.set_from_stock(gtk.STOCK_SAVE_AS, gtk.ICON_SIZE_BUTTON) | ||
236 | 224 | self.button_export.set_image(image) | ||
237 | 225 | self.button_export.connect('clicked', self.choose_export_pdf_name) | ||
238 | 226 | hbox.pack_start(self.button_export, expand=True, fill=True, padding=20) | ||
239 | 227 | |||
240 | 228 | self.button_export = gtk.Button(_('About')) | ||
241 | 229 | image = gtk.Image() | ||
242 | 230 | image.set_from_stock(gtk.STOCK_ABOUT, gtk.ICON_SIZE_BUTTON) | ||
243 | 231 | self.button_export.set_image(image) | ||
244 | 232 | self.button_export.connect('clicked', self.about_dialog) | ||
245 | 233 | hbox.pack_start(self.button_export, expand=True, fill=True, padding=20) | ||
246 | 234 | |||
247 | 235 | # Define window callback function and show window | ||
248 | 236 | self.window.connect('size_allocate', self.on_window_size_request) # resize | ||
249 | 237 | self.window.connect('key_press_event', self.on_keypress_event ) # keypress | ||
250 | 238 | self.window.show_all() | ||
251 | 239 | |||
252 | 240 | #Creating the popup menu | ||
253 | 241 | self.popup = gtk.Menu() | ||
254 | 242 | popup_rotate_right = gtk.MenuItem(_('Rotate Page(s) Clockwise')) | ||
255 | 243 | popup_rotate_left = gtk.MenuItem(_('Rotate Page(s) Counterclockwise')) | ||
256 | 244 | popup_crop = gtk.MenuItem(_('Crop Page(s)')) | ||
257 | 245 | popup_delete = gtk.MenuItem(_('Delete Page(s)')) | ||
258 | 246 | popup_rotate_right.connect('activate', self.rotate_page_right) | ||
259 | 247 | popup_rotate_left.connect('activate', self.rotate_page_left) | ||
260 | 248 | popup_crop.connect('activate', self.crop_page_dialog) | ||
261 | 249 | popup_delete.connect('activate', self.clear_selected) | ||
262 | 250 | popup_rotate_right.show() | ||
263 | 251 | popup_rotate_left.show() | ||
264 | 252 | popup_crop.show() | ||
265 | 253 | popup_delete.show() | ||
266 | 254 | self.popup.append(popup_rotate_right) | ||
267 | 255 | self.popup.append(popup_rotate_left) | ||
268 | 256 | self.popup.append(popup_crop) | ||
269 | 257 | self.popup.append(popup_delete) | ||
270 | 258 | |||
271 | 259 | # Initializing variables | ||
272 | 260 | self.export_directory = os.getenv('HOME') | ||
273 | 261 | self.import_directory = os.getenv('HOME') | ||
274 | 262 | self.nfile = 0 | ||
275 | 263 | self.iv_auto_scroll_timer = None | ||
276 | 264 | self.pdfqueue = [] | ||
277 | 265 | |||
278 | 266 | gobject.type_register(PDF_Renderer) | ||
279 | 267 | gobject.signal_new('reset_iv_width', PDF_Renderer, | ||
280 | 268 | gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ()) | ||
281 | 269 | self.rendering_thread = PDF_Renderer(self.model, self.pdfqueue, | ||
282 | 270 | self.zoom_scale, self.iv_col_width) | ||
283 | 271 | self.rendering_thread.connect('reset_iv_width', self.reset_iv_width) | ||
284 | 272 | self.rendering_thread.start() | ||
285 | 273 | |||
286 | 274 | # Importing documents passed as command line arguments | ||
287 | 275 | for filename in sys.argv[1:]: | ||
288 | 276 | self.add_pdf_pages(filename) | ||
289 | 277 | |||
290 | 278 | # ======================================================= | ||
291 | 279 | def render(self): | ||
292 | 280 | if self.rendering_thread.paused: | ||
293 | 281 | self.rendering_thread.paused = False | ||
294 | 282 | self.rendering_thread.evnt.set() | ||
295 | 283 | self.rendering_thread.evnt.clear() | ||
296 | 284 | |||
297 | 285 | # ======================================================= | ||
298 | 286 | def on_window_size_request(self, window, event): | ||
299 | 287 | """Main Window resize - workaround for autosetting of | ||
300 | 288 | iconview cols no.""" | ||
301 | 289 | |||
302 | 290 | #add 12 because of: http://bugzilla.gnome.org/show_bug.cgi?id=570152 | ||
303 | 291 | col_num = 9 * window.get_size()[0] / (10 * (self.iv_col_width + 12)) | ||
304 | 292 | self.iconview.set_columns(col_num) | ||
305 | 293 | |||
306 | 294 | # ======================================================= | ||
307 | 295 | def reset_iv_width(self, renderer=None): | ||
308 | 296 | """Reconfigures the width of the iconview columns""" | ||
309 | 297 | |||
310 | 298 | max_w = max(row[4] for row in self.model) | ||
311 | 299 | if max_w != self.iv_col_width: | ||
312 | 300 | self.iv_col_width = max_w | ||
313 | 301 | self.celltxt.set_property('width', self.iv_col_width) | ||
314 | 302 | self.celltxt.set_property('wrap-width', self.iv_col_width) | ||
315 | 303 | self.iconview.set_item_width(self.iv_col_width + 12) #-1) | ||
316 | 304 | self.on_window_size_request(self.window, None) | ||
317 | 305 | |||
318 | 306 | # ======================================================= | ||
319 | 307 | def on_keypress_event(self, widget, event): | ||
320 | 308 | """Keypress events in Main Window""" | ||
321 | 309 | |||
322 | 310 | #keyname = gtk.gdk.keyval_name(event.keyval) | ||
323 | 311 | if event.keyval == 65535: # Delete keystroke | ||
324 | 312 | self.clear_selected() | ||
325 | 313 | |||
326 | 314 | # ======================================================= | ||
327 | 315 | def close_application(self, widget, event=None, data=None): | ||
328 | 316 | """Termination""" | ||
329 | 317 | |||
330 | 318 | #gtk.gdk.threads_leave() | ||
331 | 319 | self.rendering_thread.quit = True | ||
332 | 320 | #gtk.gdk.threads_enter() | ||
333 | 321 | if self.rendering_thread.paused == True: | ||
334 | 322 | self.rendering_thread.evnt.set() | ||
335 | 323 | self.rendering_thread.evnt.clear() | ||
336 | 324 | if os.path.isdir(self.tmp_dir): | ||
337 | 325 | shutil.rmtree(self.tmp_dir) | ||
338 | 326 | if gtk.main_level(): | ||
339 | 327 | gtk.main_quit() | ||
340 | 328 | else: | ||
341 | 329 | sys.exit(0) | ||
342 | 330 | return False | ||
343 | 331 | |||
344 | 332 | # ======================================================= | ||
345 | 333 | def add_pdf_pages(self, filename, | ||
346 | 334 | firstpage=None, lastpage=None, | ||
347 | 335 | angle=0, crop=[0.,0.,0.,0.] ): | ||
348 | 336 | """Add pages of a pdf document to the model""" | ||
349 | 337 | |||
350 | 338 | res = False | ||
351 | 339 | # Check if the document has already been loaded | ||
352 | 340 | pdfdoc = None | ||
353 | 341 | for it_pdfdoc in self.pdfqueue: | ||
354 | 342 | if os.path.isfile(it_pdfdoc.filename) and \ | ||
355 | 343 | os.path.samefile(filename, it_pdfdoc.filename) and \ | ||
356 | 344 | os.path.getmtime(filename) is it_pdfdoc.mtime: | ||
357 | 345 | pdfdoc = it_pdfdoc | ||
358 | 346 | break | ||
359 | 347 | |||
360 | 348 | if not pdfdoc: | ||
361 | 349 | pdfdoc = PDF_Doc(filename, self.nfile, self.tmp_dir) | ||
362 | 350 | self.import_directory = os.path.split(filename)[0] | ||
363 | 351 | self.export_directory = self.import_directory | ||
364 | 352 | if pdfdoc.nfile != 0 and pdfdoc != []: | ||
365 | 353 | self.nfile = pdfdoc.nfile | ||
366 | 354 | self.pdfqueue.append(pdfdoc) | ||
367 | 355 | else: | ||
368 | 356 | return res | ||
369 | 357 | |||
370 | 358 | n_start = 1 | ||
371 | 359 | n_end = pdfdoc.npage | ||
372 | 360 | if firstpage: | ||
373 | 361 | n_start = min(n_end, max(1, firstpage)) | ||
374 | 362 | if lastpage: | ||
375 | 363 | n_end = max(n_start, min(n_end, lastpage)) | ||
376 | 364 | |||
377 | 365 | for npage in range(n_start, n_end + 1): | ||
378 | 366 | descriptor = ''.join([pdfdoc.shortname, '\n', _('page'), ' ', str(npage)]) | ||
379 | 367 | width = self.iv_col_width | ||
380 | 368 | thumbnail = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, | ||
381 | 369 | 8, width, width) | ||
382 | 370 | self.model.append((descriptor, # 0 | ||
383 | 371 | thumbnail, # 1 | ||
384 | 372 | pdfdoc.nfile, # 2 | ||
385 | 373 | npage, # 3 | ||
386 | 374 | width, # 4 | ||
387 | 375 | pdfdoc.filename, # 5 | ||
388 | 376 | False, # 6 | ||
389 | 377 | angle, # 7 | ||
390 | 378 | crop[0],crop[1], # 8-9 | ||
391 | 379 | crop[2],crop[3] )) # 10-11 | ||
392 | 380 | res = True | ||
393 | 381 | |||
394 | 382 | if res: | ||
395 | 383 | self.render() | ||
396 | 384 | return res | ||
397 | 385 | |||
398 | 386 | # ======================================================= | ||
399 | 387 | def choose_export_pdf_name(self, widget=None): | ||
400 | 388 | """Handles choosing a name for exporting """ | ||
401 | 389 | |||
402 | 390 | chooser = gtk.FileChooserDialog(title=_('Export ...'), | ||
403 | 391 | action=gtk.FILE_CHOOSER_ACTION_SAVE, | ||
404 | 392 | buttons=(gtk.STOCK_CANCEL, | ||
405 | 393 | gtk.RESPONSE_CANCEL, | ||
406 | 394 | gtk.STOCK_SAVE, | ||
407 | 395 | gtk.RESPONSE_OK)) | ||
408 | 396 | chooser.set_do_overwrite_confirmation(True) | ||
409 | 397 | chooser.set_current_folder(self.export_directory) | ||
410 | 398 | filter_pdf = gtk.FileFilter() | ||
411 | 399 | filter_pdf.set_name(_('PDF files')) | ||
412 | 400 | filter_pdf.add_mime_type('application/pdf') | ||
413 | 401 | chooser.add_filter(filter_pdf) | ||
414 | 402 | |||
415 | 403 | filter_all = gtk.FileFilter() | ||
416 | 404 | filter_all.set_name(_('All files')) | ||
417 | 405 | filter_all.add_pattern('*') | ||
418 | 406 | chooser.add_filter(filter_all) | ||
419 | 407 | |||
420 | 408 | while True: | ||
421 | 409 | response = chooser.run() | ||
422 | 410 | if response == gtk.RESPONSE_OK: | ||
423 | 411 | file_out = chooser.get_filename() | ||
424 | 412 | (path, shortname) = os.path.split(file_out) | ||
425 | 413 | (shortname, ext) = os.path.splitext(shortname) | ||
426 | 414 | if ext.lower() != '.pdf': | ||
427 | 415 | file_out = file_out + '.pdf' | ||
428 | 416 | try: | ||
429 | 417 | self.export_to_file(file_out) | ||
430 | 418 | self.export_directory = path | ||
431 | 419 | except IOError: | ||
432 | 420 | error_msg_win = gtk.MessageDialog(flags=gtk.DIALOG_MODAL, | ||
433 | 421 | type=gtk.MESSAGE_ERROR, | ||
434 | 422 | message_format=_("Error writing file: %s") % file_out, | ||
435 | 423 | buttons=gtk.BUTTONS_OK) | ||
436 | 424 | response = error_msg_win.run() | ||
437 | 425 | if response == gtk.RESPONSE_OK: | ||
438 | 426 | error_msg_win.destroy() | ||
439 | 427 | continue | ||
440 | 428 | break | ||
441 | 429 | chooser.destroy() | ||
442 | 430 | |||
443 | 431 | # ======================================================= | ||
444 | 432 | def export_to_file(self, file_out): | ||
445 | 433 | """Export to file""" | ||
446 | 434 | |||
447 | 435 | pdf_output = PdfFileWriter() | ||
448 | 436 | pdf_input = [] | ||
449 | 437 | for pdfdoc in self.pdfqueue: | ||
450 | 438 | pdfdoc_inp = PdfFileReader(file(pdfdoc.copyname, 'rb')) | ||
451 | 439 | if pdfdoc_inp.getIsEncrypted(): | ||
452 | 440 | if (pdfdoc_inp.decrypt('')!=1): # Workaround for lp:#355479 | ||
453 | 441 | print(_('File %s is encrypted.') % pdfdoc.filename) | ||
454 | 442 | print(_('Support for such files has not been implemented yet.')) | ||
455 | 443 | print(_('File export failed.')) | ||
456 | 444 | return | ||
457 | 445 | #FIXME | ||
458 | 446 | #else | ||
459 | 447 | # ask for password and decrypt file | ||
460 | 448 | pdf_input.append(pdfdoc_inp) | ||
461 | 449 | |||
462 | 450 | for row in self.model: | ||
463 | 451 | # add pages from input to output document | ||
464 | 452 | nfile = row[2] | ||
465 | 453 | npage = row[3] | ||
466 | 454 | current_page = copy(pdf_input[nfile-1].getPage(npage-1)) | ||
467 | 455 | angle = row[7] | ||
468 | 456 | angle0 = current_page.get("/Rotate",0) | ||
469 | 457 | crop = [row[8],row[9],row[10],row[11]] | ||
470 | 458 | if angle is not 0: | ||
471 | 459 | current_page.rotateClockwise(angle) | ||
472 | 460 | if crop != [0.,0.,0.,0.]: | ||
473 | 461 | rotate_times = ( ( ( angle + angle0 ) % 360 + 45 ) / 90 ) % 4 | ||
474 | 462 | crop_init = crop | ||
475 | 463 | if rotate_times is not 0: | ||
476 | 464 | perm = [0,2,1,3] | ||
477 | 465 | for it in range(rotate_times): | ||
478 | 466 | perm.append(perm.pop(0)) | ||
479 | 467 | perm.insert(1,perm.pop(2)) | ||
480 | 468 | crop = [crop_init[perm[side]] for side in range(4)] | ||
481 | 469 | #(x1, y1) = current_page.cropBox.lowerLeft | ||
482 | 470 | #(x2, y2) = current_page.cropBox.upperRight | ||
483 | 471 | (x1, y1) = [float(xy) for xy in current_page.mediaBox.lowerLeft] | ||
484 | 472 | (x2, y2) = [float(xy) for xy in current_page.mediaBox.upperRight] | ||
485 | 473 | x1_new = int( x1 + (x2-x1) * crop[0] ) | ||
486 | 474 | x2_new = int( x2 - (x2-x1) * crop[1] ) | ||
487 | 475 | y1_new = int( y1 + (y2-y1) * crop[3] ) | ||
488 | 476 | y2_new = int( y2 - (y2-y1) * crop[2] ) | ||
489 | 477 | #current_page.cropBox.lowerLeft = (x1_new, y1_new) | ||
490 | 478 | #current_page.cropBox.upperRight = (x2_new, y2_new) | ||
491 | 479 | current_page.mediaBox.lowerLeft = (x1_new, y1_new) | ||
492 | 480 | current_page.mediaBox.upperRight = (x2_new, y2_new) | ||
493 | 481 | |||
494 | 482 | pdf_output.addPage(current_page) | ||
495 | 483 | |||
496 | 484 | # finally, write "output" to document-output.pdf | ||
497 | 485 | print(_('exporting to:'), file_out) | ||
498 | 486 | pdf_output.write(file(file_out, 'wb')) | ||
499 | 487 | |||
500 | 488 | # ======================================================= | ||
501 | 489 | def on_action_add_doc_activate(self, widget, data=None): | ||
502 | 490 | """Import doc""" | ||
503 | 491 | |||
504 | 492 | chooser = gtk.FileChooserDialog(title=_('Import...'), | ||
505 | 493 | action=gtk.FILE_CHOOSER_ACTION_OPEN, | ||
506 | 494 | buttons=(gtk.STOCK_CANCEL, | ||
507 | 495 | gtk.RESPONSE_CANCEL, | ||
508 | 496 | gtk.STOCK_OPEN, | ||
509 | 497 | gtk.RESPONSE_OK)) | ||
510 | 498 | chooser.set_current_folder(self.import_directory) | ||
511 | 499 | chooser.set_select_multiple(True) | ||
512 | 500 | |||
513 | 501 | filter_all = gtk.FileFilter() | ||
514 | 502 | filter_all.set_name(_('All files')) | ||
515 | 503 | filter_all.add_pattern('*') | ||
516 | 504 | chooser.add_filter(filter_all) | ||
517 | 505 | |||
518 | 506 | filter_pdf = gtk.FileFilter() | ||
519 | 507 | filter_pdf.set_name(_('PDF files')) | ||
520 | 508 | filter_pdf.add_mime_type('application/pdf') | ||
521 | 509 | chooser.add_filter(filter_pdf) | ||
522 | 510 | chooser.set_filter(filter_pdf) | ||
523 | 511 | |||
524 | 512 | response = chooser.run() | ||
525 | 513 | if response == gtk.RESPONSE_OK: | ||
526 | 514 | for filename in chooser.get_filenames(): | ||
527 | 515 | if os.path.isfile(filename): | ||
528 | 516 | # FIXME | ||
529 | 517 | f = gio.File(filename) | ||
530 | 518 | f_info = f.query_info('standard::content-type') | ||
531 | 519 | mime_type = f_info.get_content_type() | ||
532 | 520 | if mime_type == 'application/pdf': | ||
533 | 521 | self.add_pdf_pages(filename) | ||
534 | 522 | elif mime_type[:34] == 'application/vnd.oasis.opendocument': | ||
535 | 523 | print(_('OpenDocument not supported yet!')) | ||
536 | 524 | elif mime_type[:5] == 'image': | ||
537 | 525 | print(_('Image file not supported yet!')) | ||
538 | 526 | else: | ||
539 | 527 | print(_('File type not supported!')) | ||
540 | 528 | else: | ||
541 | 529 | print(_('File %s does not exist') % filename) | ||
542 | 530 | elif response == gtk.RESPONSE_CANCEL: | ||
543 | 531 | print(_('Closed, no files selected')) | ||
544 | 532 | chooser.destroy() | ||
545 | 533 | |||
546 | 534 | # ======================================================= | ||
547 | 535 | def clear_selected(self, button=None): | ||
548 | 536 | """Removes the selected Element in the IconView""" | ||
549 | 537 | |||
550 | 538 | model = self.iconview.get_model() | ||
551 | 539 | selection = self.iconview.get_selected_items() | ||
552 | 540 | if selection: | ||
553 | 541 | selection.sort(reverse=True) | ||
554 | 542 | for path in selection: | ||
555 | 543 | iter = model.get_iter(path) | ||
556 | 544 | model.remove(iter) | ||
557 | 545 | path = selection[-1] | ||
558 | 546 | self.iconview.select_path(path) | ||
559 | 547 | if not self.iconview.path_is_selected(path): | ||
560 | 548 | if len(model) > 0: # select the last row | ||
561 | 549 | row = model[-1] | ||
562 | 550 | path = row.path | ||
563 | 551 | self.iconview.select_path(path) | ||
564 | 552 | self.iconview.grab_focus() | ||
565 | 553 | |||
566 | 554 | # ======================================================= | ||
567 | 555 | def iv_drag_begin(self, iconview, context): | ||
568 | 556 | """Sets custom icon on drag begin for multiple items selected""" | ||
569 | 557 | |||
570 | 558 | if len(iconview.get_selected_items()) > 1: | ||
571 | 559 | iconview.stop_emission('drag_begin') | ||
572 | 560 | context.set_icon_stock(gtk.STOCK_DND_MULTIPLE, 0, 0) | ||
573 | 561 | |||
574 | 562 | # ======================================================= | ||
575 | 563 | def iv_dnd_get_data(self, iconview, context, | ||
576 | 564 | selection_data, target_id, etime): | ||
577 | 565 | """Handles requests for data by drag and drop in iconview""" | ||
578 | 566 | |||
579 | 567 | model = iconview.get_model() | ||
580 | 568 | selection = self.iconview.get_selected_items() | ||
581 | 569 | selection.sort(key=lambda x: x[0]) | ||
582 | 570 | data = [] | ||
583 | 571 | for path in selection: | ||
584 | 572 | if selection_data.target == 'MODEL_ROW_INTERN': | ||
585 | 573 | data.append( str(path[0]) ) | ||
586 | 574 | elif selection_data.target == 'MODEL_ROW_EXTERN': | ||
587 | 575 | iter = model.get_iter(path) | ||
588 | 576 | nfile, npage, angle = model.get(iter, 2, 3, 7) | ||
589 | 577 | crop = model.get(iter, 8, 9, 10, 11) | ||
590 | 578 | pdfdoc = self.pdfqueue[nfile - 1] | ||
591 | 579 | data.append('\n'.join([pdfdoc.filename, | ||
592 | 580 | str(npage), | ||
593 | 581 | str(angle) ] + | ||
594 | 582 | [str(side) for side in crop] ) ) | ||
595 | 583 | if data: | ||
596 | 584 | data = '\n;\n'.join(data) | ||
597 | 585 | selection_data.set(selection_data.target, 8, data) | ||
598 | 586 | |||
599 | 587 | # ======================================================= | ||
600 | 588 | def iv_dnd_received_data(self, iconview, context, x, y, | ||
601 | 589 | selection_data, target_id, etime): | ||
602 | 590 | """Handles received data by drag and drop in iconview""" | ||
603 | 591 | |||
604 | 592 | model = iconview.get_model() | ||
605 | 593 | data = selection_data.data | ||
606 | 594 | if data: | ||
607 | 595 | data = data.split('\n;\n') | ||
608 | 596 | drop_info = iconview.get_dest_item_at_pos(x, y) | ||
609 | 597 | iter_to = None | ||
610 | 598 | if drop_info: | ||
611 | 599 | path, position = drop_info | ||
612 | 600 | ref_to = gtk.TreeRowReference(model,path) | ||
613 | 601 | else: | ||
614 | 602 | position = gtk.ICON_VIEW_DROP_RIGHT | ||
615 | 603 | if len(model) > 0: #find the iterator of the last row | ||
616 | 604 | row = model[-1] | ||
617 | 605 | path = row.path | ||
618 | 606 | ref_to = gtk.TreeRowReference(model,path) | ||
619 | 607 | if ref_to: | ||
620 | 608 | before = ( position == gtk.ICON_VIEW_DROP_LEFT | ||
621 | 609 | or position == gtk.ICON_VIEW_DROP_ABOVE) | ||
622 | 610 | #if target_id == self.MODEL_ROW_INTERN: | ||
623 | 611 | if selection_data.target == 'MODEL_ROW_INTERN': | ||
624 | 612 | if before: | ||
625 | 613 | data.sort(key=int) | ||
626 | 614 | else: | ||
627 | 615 | data.sort(key=int,reverse=True) | ||
628 | 616 | ref_from_list = [gtk.TreeRowReference(model,path) | ||
629 | 617 | for path in data] | ||
630 | 618 | for ref_from in ref_from_list: | ||
631 | 619 | path = ref_to.get_path() | ||
632 | 620 | iter_to = model.get_iter(path) | ||
633 | 621 | path = ref_from.get_path() | ||
634 | 622 | iter_from = model.get_iter(path) | ||
635 | 623 | row = model[iter_from] | ||
636 | 624 | if before: | ||
637 | 625 | model.insert_before(iter_to, row) | ||
638 | 626 | else: | ||
639 | 627 | model.insert_after(iter_to, row) | ||
640 | 628 | if context.action == gtk.gdk.ACTION_MOVE: | ||
641 | 629 | for ref_from in ref_from_list: | ||
642 | 630 | path = ref_from.get_path() | ||
643 | 631 | iter_from = model.get_iter(path) | ||
644 | 632 | model.remove(iter_from) | ||
645 | 633 | |||
646 | 634 | #elif target_id == self.MODEL_ROW_EXTERN: | ||
647 | 635 | elif selection_data.target == 'MODEL_ROW_EXTERN': | ||
648 | 636 | if not before: | ||
649 | 637 | data.reverse() | ||
650 | 638 | while data: | ||
651 | 639 | tmp = data.pop(0).split('\n') | ||
652 | 640 | filename = tmp[0] | ||
653 | 641 | npage, angle = [int(k) for k in tmp[1:3]] | ||
654 | 642 | crop = [float(side) for side in tmp[3:7]] | ||
655 | 643 | if self.add_pdf_pages(filename, npage, npage, | ||
656 | 644 | angle, crop ): | ||
657 | 645 | if len(model) > 0: | ||
658 | 646 | path = ref_to.get_path() | ||
659 | 647 | iter_to = model.get_iter(path) | ||
660 | 648 | row = model[-1] #the last row | ||
661 | 649 | path = row.path | ||
662 | 650 | iter_from = model.get_iter(path) | ||
663 | 651 | if before: | ||
664 | 652 | model.move_before(iter_from, iter_to) | ||
665 | 653 | else: | ||
666 | 654 | model.move_after(iter_from, iter_to) | ||
667 | 655 | if context.action == gtk.gdk.ACTION_MOVE: | ||
668 | 656 | context.finish(True, True, etime) | ||
669 | 657 | |||
670 | 658 | # ======================================================= | ||
671 | 659 | def iv_dnd_data_delete(self, widget, context): | ||
672 | 660 | """Deletes dnd items after a successful move operation""" | ||
673 | 661 | |||
674 | 662 | model = self.iconview.get_model() | ||
675 | 663 | selection = self.iconview.get_selected_items() | ||
676 | 664 | ref_del_list = [gtk.TreeRowReference(model,path) for path in selection] | ||
677 | 665 | for ref_del in ref_del_list: | ||
678 | 666 | path = ref_del.get_path() | ||
679 | 667 | iter = model.get_iter(path) | ||
680 | 668 | model.remove(iter) | ||
681 | 669 | |||
682 | 670 | # ======================================================= | ||
683 | 671 | def iv_dnd_motion(self, iconview, context, x, y, etime): | ||
684 | 672 | """Handles the drag-motion signal in order to auto-scroll the view""" | ||
685 | 673 | |||
686 | 674 | autoscroll_area = 40 | ||
687 | 675 | sw_vadj = self.sw.get_vadjustment() | ||
688 | 676 | sw_height = self.sw.get_allocation().height | ||
689 | 677 | if y -sw_vadj.get_value() < autoscroll_area: | ||
690 | 678 | if not self.iv_auto_scroll_timer: | ||
691 | 679 | self.iv_auto_scroll_direction = gtk.DIR_UP | ||
692 | 680 | self.iv_auto_scroll_timer = gobject.timeout_add(150, | ||
693 | 681 | self.iv_auto_scroll) | ||
694 | 682 | elif y -sw_vadj.get_value() > sw_height - autoscroll_area: | ||
695 | 683 | if not self.iv_auto_scroll_timer: | ||
696 | 684 | self.iv_auto_scroll_direction = gtk.DIR_DOWN | ||
697 | 685 | self.iv_auto_scroll_timer = gobject.timeout_add(150, | ||
698 | 686 | self.iv_auto_scroll) | ||
699 | 687 | elif self.iv_auto_scroll_timer: | ||
700 | 688 | gobject.source_remove(self.iv_auto_scroll_timer) | ||
701 | 689 | self.iv_auto_scroll_timer = None | ||
702 | 690 | |||
703 | 691 | # ======================================================= | ||
704 | 692 | def iv_dnd_leave_end(self, widget, context, ignored=None): | ||
705 | 693 | """Ends the auto-scroll during DND""" | ||
706 | 694 | |||
707 | 695 | if self.iv_auto_scroll_timer: | ||
708 | 696 | gobject.source_remove(self.iv_auto_scroll_timer) | ||
709 | 697 | self.iv_auto_scroll_timer = None | ||
710 | 698 | |||
711 | 699 | # ======================================================= | ||
712 | 700 | def iv_auto_scroll(self): | ||
713 | 701 | """Timeout routine for auto-scroll""" | ||
714 | 702 | |||
715 | 703 | sw_vadj = self.sw.get_vadjustment() | ||
716 | 704 | sw_vpos = sw_vadj.get_value() | ||
717 | 705 | if self.iv_auto_scroll_direction == gtk.DIR_UP: | ||
718 | 706 | sw_vpos -= sw_vadj.step_increment | ||
719 | 707 | sw_vadj.set_value( max(sw_vpos, sw_vadj.lower) ) | ||
720 | 708 | elif self.iv_auto_scroll_direction == gtk.DIR_DOWN: | ||
721 | 709 | sw_vpos += sw_vadj.step_increment | ||
722 | 710 | sw_vadj.set_value( min(sw_vpos, sw_vadj.upper - sw_vadj.page_size) ) | ||
723 | 711 | return True #call me again | ||
724 | 712 | |||
725 | 713 | # ======================================================= | ||
726 | 714 | def iv_button_press_event(self, iconview, event): | ||
727 | 715 | """Manages mouse clicks on the iconview""" | ||
728 | 716 | |||
729 | 717 | if event.button == 3: | ||
730 | 718 | x = int(event.x) | ||
731 | 719 | y = int(event.y) | ||
732 | 720 | time = event.time | ||
733 | 721 | path = iconview.get_path_at_pos(x, y) | ||
734 | 722 | selection = iconview.get_selected_items() | ||
735 | 723 | if path: | ||
736 | 724 | if path not in selection: | ||
737 | 725 | iconview.unselect_all() | ||
738 | 726 | iconview.select_path(path) | ||
739 | 727 | iconview.grab_focus() | ||
740 | 728 | self.popup.popup( None, None, None, event.button, time) | ||
741 | 729 | return 1 | ||
742 | 730 | |||
743 | 731 | # ======================================================= | ||
744 | 732 | def sw_dnd_received_data(self, scrolledwindow, context, x, y, | ||
745 | 733 | selection_data, target_id, etime): | ||
746 | 734 | """Handles received data by drag and drop in scrolledwindow""" | ||
747 | 735 | |||
748 | 736 | data = selection_data.data | ||
749 | 737 | if target_id == self.MODEL_ROW_EXTERN: | ||
750 | 738 | self.model | ||
751 | 739 | if data: | ||
752 | 740 | data = data.split('\n;\n') | ||
753 | 741 | while data: | ||
754 | 742 | tmp = data.pop(0).split('\n') | ||
755 | 743 | filename = tmp[0] | ||
756 | 744 | npage, angle = [int(k) for k in tmp[1:3]] | ||
757 | 745 | crop = [float(side) for side in tmp[3:7]] | ||
758 | 746 | if self.add_pdf_pages(filename, npage, npage, angle, crop): | ||
759 | 747 | if context.action == gtk.gdk.ACTION_MOVE: | ||
760 | 748 | context.finish(True, True, etime) | ||
761 | 749 | elif target_id == self.TEXT_URI_LIST: | ||
762 | 750 | uri = data.strip() | ||
763 | 751 | uri_splitted = uri.split() # we may have more than one file dropped | ||
764 | 752 | for uri in uri_splitted: | ||
765 | 753 | filename = self.get_file_path_from_dnd_dropped_uri(uri) | ||
766 | 754 | if os.path.isfile(filename): # is it file? | ||
767 | 755 | self.add_pdf_pages(filename) | ||
768 | 756 | |||
769 | 757 | # ======================================================= | ||
770 | 758 | def sw_button_press_event(self, scrolledwindow, event): | ||
771 | 759 | """Unselects all items in iconview on mouse click in scrolledwindow""" | ||
772 | 760 | |||
773 | 761 | if event.button == 1: | ||
774 | 762 | self.iconview.unselect_all() | ||
775 | 763 | |||
776 | 764 | # ======================================================= | ||
777 | 765 | def get_file_path_from_dnd_dropped_uri(self, uri): | ||
778 | 766 | """Extracts the path from an uri""" | ||
779 | 767 | |||
780 | 768 | path = urllib.url2pathname(uri) # escape special chars | ||
781 | 769 | path = path.strip('\r\n\x00') # remove \r\n and NULL | ||
782 | 770 | |||
783 | 771 | # get the path to file | ||
784 | 772 | if path.startswith('file:\\\\\\'): # windows | ||
785 | 773 | path = path[8:] # 8 is len('file:///') | ||
786 | 774 | elif path.startswith('file://'): # nautilus, rox | ||
787 | 775 | path = path[7:] # 7 is len('file://') | ||
788 | 776 | elif path.startswith('file:'): # xffm | ||
789 | 777 | path = path[5:] # 5 is len('file:') | ||
790 | 778 | return path | ||
791 | 779 | |||
792 | 780 | # ======================================================= | ||
793 | 781 | def rotate_page_right(self, widget, data=None): | ||
794 | 782 | self.rotate_page(90) | ||
795 | 783 | |||
796 | 784 | def rotate_page_left(self, widget, data=None): | ||
797 | 785 | self.rotate_page(-90) | ||
798 | 786 | |||
799 | 787 | def rotate_page(self, angle): | ||
800 | 788 | """Rotates the selected page in the IconView""" | ||
801 | 789 | |||
802 | 790 | model = self.iconview.get_model() | ||
803 | 791 | selection = self.iconview.get_selected_items() | ||
804 | 792 | for path in selection: | ||
805 | 793 | iter = model.get_iter(path) | ||
806 | 794 | nfile = model.get_value(iter, 2) | ||
807 | 795 | npage = model.get_value(iter, 3) | ||
808 | 796 | |||
809 | 797 | rotate_times = ( ( (-angle) % 360 + 45 ) / 90 ) % 4 | ||
810 | 798 | crop = [0.,0.,0.,0.] | ||
811 | 799 | if rotate_times is not 0: | ||
812 | 800 | perm = [0,2,1,3] | ||
813 | 801 | for it in range(rotate_times): | ||
814 | 802 | perm.append(perm.pop(0)) | ||
815 | 803 | perm.insert(1,perm.pop(2)) | ||
816 | 804 | crop = [model.get_value(iter, 8 + perm[side]) for side in range(4)] | ||
817 | 805 | for side in range(4): | ||
818 | 806 | model.set_value(iter, 8 + side, crop[side]) | ||
819 | 807 | |||
820 | 808 | new_angle = model.get_value(iter, 7) + angle | ||
821 | 809 | model.set_value(iter, 7, new_angle) | ||
822 | 810 | model.set_value(iter, 6, False) #rendering request | ||
823 | 811 | |||
824 | 812 | self.render() | ||
825 | 813 | |||
826 | 814 | # ======================================================= | ||
827 | 815 | def crop_page_dialog(self, widget): | ||
828 | 816 | """Opens a dialog box to define margins for page cropping""" | ||
829 | 817 | |||
830 | 818 | sides = ('L', 'R', 'T', 'B') | ||
831 | 819 | side_names = {'L':_('Left'), 'R':_('Right'), | ||
832 | 820 | 'T':_('Top'), 'B':_('Bottom') } | ||
833 | 821 | opposite_sides = {'L':'R', 'R':'L', 'T':'B', 'B':'T' } | ||
834 | 822 | |||
835 | 823 | def set_crop_value(spinbutton, side): | ||
836 | 824 | opp_side = opposite_sides[side] | ||
837 | 825 | pos = sides.index(opp_side) | ||
838 | 826 | adj = spin_list[pos].get_adjustment() | ||
839 | 827 | adj.set_upper(99.0 - spinbutton.get_value()) | ||
840 | 828 | |||
841 | 829 | model = self.iconview.get_model() | ||
842 | 830 | selection = self.iconview.get_selected_items() | ||
843 | 831 | |||
844 | 832 | crop = [0.,0.,0.,0.] | ||
845 | 833 | if selection: | ||
846 | 834 | path = selection[0] | ||
847 | 835 | pos = model.get_iter(path) | ||
848 | 836 | crop = [model.get_value(pos, 8 + side) for side in range(4)] | ||
849 | 837 | |||
850 | 838 | dialog = gtk.Dialog(title=(_('Crop Selected Page(s)')), | ||
851 | 839 | parent=self.window, | ||
852 | 840 | flags=gtk.DIALOG_MODAL, | ||
853 | 841 | buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, | ||
854 | 842 | gtk.STOCK_OK, gtk.RESPONSE_OK)) | ||
855 | 843 | dialog.set_size_request(340, 250) | ||
856 | 844 | dialog.set_default_response(gtk.RESPONSE_OK) | ||
857 | 845 | |||
858 | 846 | frame = gtk.Frame(_('Crop Margins')) | ||
859 | 847 | dialog.vbox.pack_start(frame, False, False, 20) | ||
860 | 848 | |||
861 | 849 | vbox = gtk.VBox(False, 0) | ||
862 | 850 | frame.add(vbox) | ||
863 | 851 | |||
864 | 852 | spin_list = [] | ||
865 | 853 | units = 2 * [_('% of width')] + 2 * [_('% of height')] | ||
866 | 854 | for side in sides: | ||
867 | 855 | hbox = gtk.HBox(True, 0) | ||
868 | 856 | vbox.pack_start(hbox, False, False, 5) | ||
869 | 857 | |||
870 | 858 | label = gtk.Label(side_names[side]) | ||
871 | 859 | label.set_alignment(0, 0.0) | ||
872 | 860 | hbox.pack_start(label, True, True, 20) | ||
873 | 861 | |||
874 | 862 | adj = gtk.Adjustment(100.*crop.pop(0), 0.0, 99.0, 1.0, 5.0, 0.0) | ||
875 | 863 | spin = gtk.SpinButton(adj, 0, 1) | ||
876 | 864 | spin.set_activates_default(True) | ||
877 | 865 | spin.connect('value-changed', set_crop_value, side) | ||
878 | 866 | spin_list.append(spin) | ||
879 | 867 | hbox.pack_start(spin, False, False, 30) | ||
880 | 868 | |||
881 | 869 | label = gtk.Label(units.pop(0)) | ||
882 | 870 | label.set_alignment(0, 0.0) | ||
883 | 871 | hbox.pack_start(label, True, True, 0) | ||
884 | 872 | |||
885 | 873 | dialog.show_all() | ||
886 | 874 | result = dialog.run() | ||
887 | 875 | |||
888 | 876 | if result == gtk.RESPONSE_OK: | ||
889 | 877 | crop = [] | ||
890 | 878 | for spin in spin_list: | ||
891 | 879 | crop.append( spin.get_value()/100. ) | ||
892 | 880 | for path in selection: | ||
893 | 881 | pos = model.get_iter(path) | ||
894 | 882 | for it in range(4): | ||
895 | 883 | model.set_value(pos, 8 + it, crop[it]) | ||
896 | 884 | model.set_value(pos, 6, False) #rendering request | ||
897 | 885 | self.render() | ||
898 | 886 | elif result == gtk.RESPONSE_CANCEL: | ||
899 | 887 | print(_('Dialog closed')) | ||
900 | 888 | dialog.destroy() | ||
901 | 889 | |||
902 | 890 | # ======================================================= | ||
903 | 891 | def about_dialog(self, widget, data=None): | ||
904 | 892 | about_dialog = gtk.AboutDialog() | ||
905 | 893 | try: | ||
906 | 894 | about_dialog.set_transient_for(self.window) | ||
907 | 895 | about_dialog.set_modal(True) | ||
908 | 896 | except: | ||
909 | 897 | pass | ||
910 | 898 | # FIXME | ||
911 | 899 | about_dialog.set_name('PDF-Shuffler') | ||
912 | 900 | about_dialog.set_version('0.5.1') | ||
913 | 901 | about_dialog.set_comments(_( | ||
914 | 902 | 'PDF-Shuffler is a simple pyGTK utility which lets you merge, \ | ||
915 | 903 | split and rearrange PDF documents. You can also rotate and crop individual \ | ||
916 | 904 | pages of a pdf document.')) | ||
917 | 905 | about_dialog.set_authors(['Konstantinos Poulios',]) | ||
918 | 906 | about_dialog.set_website_label('http://pdfshuffler.sourceforge.net/') | ||
919 | 907 | about_dialog.set_logo_icon_name('pdfshuffler') | ||
920 | 908 | about_dialog.connect('response', lambda w, a: about_dialog.destroy()) | ||
921 | 909 | about_dialog.connect('delete_event', lambda w, a: about_dialog.destroy()) | ||
922 | 910 | about_dialog.show_all() | ||
923 | 911 | |||
924 | 912 | # ======================================================= | ||
925 | 913 | class PDF_Doc: | ||
926 | 914 | """Class handling pdf documents""" | ||
927 | 915 | |||
928 | 916 | def __init__(self, filename, nfile, tmp_dir): | ||
929 | 917 | |||
930 | 918 | self.filename = os.path.abspath(filename) | ||
931 | 919 | (self.path, self.shortname) = os.path.split(self.filename) | ||
932 | 920 | (self.shortname, self.ext) = os.path.splitext(self.shortname) | ||
933 | 921 | f = gio.File(filename) | ||
934 | 922 | mime_type = f.query_info('standard::content-type').get_content_type() | ||
935 | 923 | if mime_type == 'application/pdf': | ||
936 | 924 | self.nfile = nfile + 1 | ||
937 | 925 | self.mtime = os.path.getmtime(filename) | ||
938 | 926 | self.copyname = os.path.join(tmp_dir, '%02d_' % self.nfile + | ||
939 | 927 | self.shortname + '.pdf') | ||
940 | 928 | shutil.copy(self.filename, self.copyname) | ||
941 | 929 | self.document = poppler.document_new_from_file("file://" + self.copyname, None) | ||
942 | 930 | self.npage = self.document.get_n_pages() | ||
943 | 931 | else: | ||
944 | 932 | self.nfile = 0 | ||
945 | 933 | |||
946 | 934 | |||
947 | 935 | # ======================================================= | ||
948 | 936 | class PDF_Renderer(threading.Thread,gobject.GObject): | ||
949 | 937 | |||
950 | 938 | def __init__(self, model, pdfqueue, scale=1., width=100): | ||
951 | 939 | threading.Thread.__init__(self) | ||
952 | 940 | gobject.GObject.__init__(self) | ||
953 | 941 | self.model = model | ||
954 | 942 | self.scale = scale | ||
955 | 943 | self.default_width = width | ||
956 | 944 | self.pdfqueue = pdfqueue | ||
957 | 945 | self.quit = False | ||
958 | 946 | self.evnt = threading.Event() | ||
959 | 947 | self.paused = False | ||
960 | 948 | |||
961 | 949 | def run(self): | ||
962 | 950 | while not self.quit: | ||
963 | 951 | rendered_all = True | ||
964 | 952 | for row in self.model: | ||
965 | 953 | if self.quit: | ||
966 | 954 | break | ||
967 | 955 | if not row[6]: | ||
968 | 956 | rendered_all = False | ||
969 | 957 | gtk.gdk.threads_enter() | ||
970 | 958 | try: | ||
971 | 959 | nfile = row[2] | ||
972 | 960 | npage = row[3] | ||
973 | 961 | angle = row[7] | ||
974 | 962 | crop = [row[8],row[9],row[10],row[11]] | ||
975 | 963 | pdfdoc = self.pdfqueue[nfile - 1] | ||
976 | 964 | thumbnail = self.load_pdf_thumbnail(pdfdoc, npage, angle, crop) | ||
977 | 965 | row[6] = True | ||
978 | 966 | row[4] = thumbnail.get_width() | ||
979 | 967 | row[1] = thumbnail | ||
980 | 968 | finally: | ||
981 | 969 | gtk.gdk.threads_leave() | ||
982 | 970 | if rendered_all: | ||
983 | 971 | if self.model.get_iter_first(): #just checking if model isn't empty | ||
984 | 972 | self.emit('reset_iv_width') | ||
985 | 973 | self.paused = True | ||
986 | 974 | self.evnt.wait() | ||
987 | 975 | |||
988 | 976 | # ======================================================= | ||
989 | 977 | def load_pdf_thumbnail(self, pdfdoc, npage, rotation=0, crop=[0.,0.,0.,0.]): | ||
990 | 978 | """Create pdf pixbuf""" | ||
991 | 979 | |||
992 | 980 | page = pdfdoc.document.get_page(npage-1) | ||
993 | 981 | try: | ||
994 | 982 | pix_w, pix_h = page.get_size() | ||
995 | 983 | pix_w = int(pix_w * self.scale) | ||
996 | 984 | pix_h = int(pix_h * self.scale) | ||
997 | 985 | thumbnail = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, | ||
998 | 986 | 8, pix_w , pix_h) | ||
999 | 987 | page.render_to_pixbuf(0,0,pix_w,pix_h,self.scale,0,thumbnail) | ||
1000 | 988 | rotation = (-rotation) % 360 | ||
1001 | 989 | rotation = ((rotation + 45) / 90) * 90 | ||
1002 | 990 | thumbnail = thumbnail.rotate_simple(rotation) | ||
1003 | 991 | pix_w = thumbnail.get_width() | ||
1004 | 992 | pix_h = thumbnail.get_height() | ||
1005 | 993 | if crop != [0.,0.,0.,0.]: | ||
1006 | 994 | src_x = int( crop[0] * pix_w ) | ||
1007 | 995 | src_y = int( crop[2] * pix_h ) | ||
1008 | 996 | width = int( (1. - crop[0] - crop[1]) * pix_w ) | ||
1009 | 997 | height = int( (1. - crop[2] - crop[3]) * pix_h ) | ||
1010 | 998 | new_thumbnail = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, | ||
1011 | 999 | 8, width, height) | ||
1012 | 1000 | thumbnail.copy_area(src_x, src_y, width, height, | ||
1013 | 1001 | new_thumbnail, 0, 0) | ||
1014 | 1002 | thumbnail = new_thumbnail | ||
1015 | 1003 | pix_w = thumbnail.get_width() | ||
1016 | 1004 | pix_h = thumbnail.get_height() | ||
1017 | 1005 | except: | ||
1018 | 1006 | pix_w = self.default_width | ||
1019 | 1007 | pix_h = pix_w | ||
1020 | 1008 | thumbnail = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, | ||
1021 | 1009 | 8, pix_w, pix_h) | ||
1022 | 1010 | pixbuf.fill(0xffffffff) | ||
1023 | 1011 | |||
1024 | 1012 | #add border | ||
1025 | 1013 | thickness = 3 | ||
1026 | 1014 | color = 0x000000FF | ||
1027 | 1015 | canvas = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, | ||
1028 | 1016 | pix_w + thickness + 1, | ||
1029 | 1017 | pix_h + thickness + 1) | ||
1030 | 1018 | canvas.fill(color) | ||
1031 | 1019 | thumbnail.copy_area(0, 0, pix_w, pix_h, canvas, 1, 1) | ||
1032 | 1020 | thumbnail = canvas | ||
1033 | 1021 | |||
1034 | 1022 | return thumbnail | ||
1035 | 1023 | |||
1036 | 1024 | |||
1037 | 1025 | # ======================================================= | ||
1038 | 1026 | if __name__ == '__main__': | ||
1039 | 1027 | PDFshuffler() | ||
1040 | 1028 | gtk.gdk.threads_enter() | ||
1041 | 1029 | gtk.main() | ||
1042 | 1030 | gtk.gdk.threads_leave() | ||
1043 | 1031 | |||
1044 | 1032 | 0 | ||
1045 | === added directory '.pc/install_and_look_for_UI_file_in_right_dirs.patch' | |||
1046 | === added directory '.pc/install_and_look_for_UI_file_in_right_dirs.patch/pdfshuffler' | |||
1047 | === added file '.pc/install_and_look_for_UI_file_in_right_dirs.patch/pdfshuffler/pdfshuffler.py' | |||
1048 | --- .pc/install_and_look_for_UI_file_in_right_dirs.patch/pdfshuffler/pdfshuffler.py 1970-01-01 00:00:00 +0000 | |||
1049 | +++ .pc/install_and_look_for_UI_file_in_right_dirs.patch/pdfshuffler/pdfshuffler.py 2012-02-12 00:35:23 +0000 | |||
1050 | @@ -0,0 +1,1057 @@ | |||
1051 | 1 | #!/usr/bin/python | ||
1052 | 2 | # -*- coding: utf-8 -*- | ||
1053 | 3 | |||
1054 | 4 | """ | ||
1055 | 5 | |||
1056 | 6 | PdfShuffler 0.6.0 - GTK+ based utility for splitting, rearrangement and | ||
1057 | 7 | modification of PDF documents. | ||
1058 | 8 | Copyright (C) 2008-2011 Konstantinos Poulios | ||
1059 | 9 | <https://sourceforge.net/projects/pdfshuffler> | ||
1060 | 10 | |||
1061 | 11 | This file is part of PdfShuffler. | ||
1062 | 12 | |||
1063 | 13 | PdfShuffler is free software; you can redistribute it and/or modify | ||
1064 | 14 | it under the terms of the GNU General Public License as published by | ||
1065 | 15 | the Free Software Foundation; either version 3 of the License, or | ||
1066 | 16 | (at your option) any later version. | ||
1067 | 17 | |||
1068 | 18 | This program is distributed in the hope that it will be useful, | ||
1069 | 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1070 | 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1071 | 21 | GNU General Public License for more details. | ||
1072 | 22 | |||
1073 | 23 | You should have received a copy of the GNU General Public License along | ||
1074 | 24 | with this program; if not, write to the Free Software Foundation, Inc., | ||
1075 | 25 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
1076 | 26 | |||
1077 | 27 | """ | ||
1078 | 28 | |||
1079 | 29 | import os | ||
1080 | 30 | import shutil #needed for file operations like whole directory deletion | ||
1081 | 31 | import sys #needed for proccessing of command line args | ||
1082 | 32 | import urllib #needed to parse filename information passed by DnD | ||
1083 | 33 | import threading | ||
1084 | 34 | import tempfile | ||
1085 | 35 | from copy import copy | ||
1086 | 36 | |||
1087 | 37 | import locale #for multilanguage support | ||
1088 | 38 | import gettext | ||
1089 | 39 | gettext.install('pdfshuffler', unicode=1) | ||
1090 | 40 | |||
1091 | 41 | |||
1092 | 42 | APPNAME = 'PdfShuffler' # PDF-Shuffler, PDFShuffler, pdfshuffler | ||
1093 | 43 | VERSION = '0.6.0' | ||
1094 | 44 | WEBSITE = 'http://pdfshuffler.sourceforge.net/' | ||
1095 | 45 | LICENSE = 'GNU General Public License (GPL) Version 3.' | ||
1096 | 46 | |||
1097 | 47 | try: | ||
1098 | 48 | import pygtk | ||
1099 | 49 | pygtk.require('2.0') | ||
1100 | 50 | import gtk | ||
1101 | 51 | assert gtk.gtk_version >= (2, 10, 0) | ||
1102 | 52 | assert gtk.pygtk_version >= (2, 10, 0) | ||
1103 | 53 | except AssertionError: | ||
1104 | 54 | print('You do not have the required versions of GTK+ and PyGTK ' + | ||
1105 | 55 | 'installed.\n\n' + | ||
1106 | 56 | 'Installed GTK+ version is ' + | ||
1107 | 57 | '.'.join([str(n) for n in gtk.gtk_version]) + '\n' + | ||
1108 | 58 | 'Required GTK+ version is 2.10.0 or higher\n\n' | ||
1109 | 59 | 'Installed PyGTK version is ' + | ||
1110 | 60 | '.'.join([str(n) for n in gtk.pygtk_version]) + '\n' + | ||
1111 | 61 | 'Required PyGTK version is 2.10.0 or higher') | ||
1112 | 62 | sys.exit(1) | ||
1113 | 63 | except: | ||
1114 | 64 | print('PyGTK version 2.10.0 or higher is required to run this program.') | ||
1115 | 65 | print('No version of PyGTK was found on your system.') | ||
1116 | 66 | sys.exit(1) | ||
1117 | 67 | |||
1118 | 68 | import gobject #to use custom signals | ||
1119 | 69 | import pango #to adjust the text alignment in CellRendererText | ||
1120 | 70 | import gio #to inquire mime types information | ||
1121 | 71 | import cairo | ||
1122 | 72 | |||
1123 | 73 | import poppler #for the rendering of pdf pages | ||
1124 | 74 | from pyPdf import PdfFileWriter, PdfFileReader | ||
1125 | 75 | |||
1126 | 76 | from cairorendering import CellRendererImage, CairoImage | ||
1127 | 77 | gobject.type_register(CellRendererImage) | ||
1128 | 78 | |||
1129 | 79 | import time | ||
1130 | 80 | |||
1131 | 81 | class PdfShuffler: | ||
1132 | 82 | prefs = { | ||
1133 | 83 | 'window width': min(700, gtk.gdk.screen_get_default().get_width() / 2), | ||
1134 | 84 | 'window height': min(600, gtk.gdk.screen_get_default().get_height() - 50), | ||
1135 | 85 | 'window x': 0, | ||
1136 | 86 | 'window y': 0, | ||
1137 | 87 | 'initial thumbnail size': 300, | ||
1138 | 88 | 'initial zoom level': -14, | ||
1139 | 89 | } | ||
1140 | 90 | |||
1141 | 91 | MODEL_ROW_INTERN = 1001 | ||
1142 | 92 | MODEL_ROW_EXTERN = 1002 | ||
1143 | 93 | TEXT_URI_LIST = 1003 | ||
1144 | 94 | MODEL_ROW_MOTION = 1004 | ||
1145 | 95 | TARGETS_IV = [('MODEL_ROW_INTERN', gtk.TARGET_SAME_WIDGET, MODEL_ROW_INTERN), | ||
1146 | 96 | ('MODEL_ROW_EXTERN', gtk.TARGET_OTHER_APP, MODEL_ROW_EXTERN), | ||
1147 | 97 | ('MODEL_ROW_MOTION', 0, MODEL_ROW_MOTION)] | ||
1148 | 98 | TARGETS_SW = [('text/uri-list', 0, TEXT_URI_LIST), | ||
1149 | 99 | ('MODEL_ROW_EXTERN', gtk.TARGET_OTHER_APP, MODEL_ROW_EXTERN)] | ||
1150 | 100 | |||
1151 | 101 | def __init__(self): | ||
1152 | 102 | # Create the temporary directory | ||
1153 | 103 | self.tmp_dir = tempfile.mkdtemp("pdfshuffler") | ||
1154 | 104 | os.chmod(self.tmp_dir, 0700) | ||
1155 | 105 | |||
1156 | 106 | icon_theme = gtk.icon_theme_get_default() | ||
1157 | 107 | try: | ||
1158 | 108 | gtk.window_set_default_icon(icon_theme.load_icon("pdfshuffler", 64, 0)) | ||
1159 | 109 | except: | ||
1160 | 110 | print(_("Can't load icon. Application is not installed correctly.")) | ||
1161 | 111 | |||
1162 | 112 | # Import the user interface file, trying different possible locations | ||
1163 | 113 | ui_path = '/usr/share/pdfshuffler/pdfshuffler.ui' | ||
1164 | 114 | if not os.path.exists(ui_path): | ||
1165 | 115 | ui_path = '/usr/local/share/applications/pdfshuffler/pdfshuffler.ui' | ||
1166 | 116 | if not os.path.exists(ui_path): | ||
1167 | 117 | parent_dir = os.path.dirname( \ | ||
1168 | 118 | os.path.dirname(os.path.realpath(__file__))) | ||
1169 | 119 | ui_path = os.path.join(parent_dir, 'data', 'pdfshuffler.ui') | ||
1170 | 120 | self.uiXML = gtk.Builder() | ||
1171 | 121 | self.uiXML.add_from_file(ui_path) | ||
1172 | 122 | self.uiXML.connect_signals(self) | ||
1173 | 123 | |||
1174 | 124 | # Create the main window, and attach delete_event signal to terminating | ||
1175 | 125 | # the application | ||
1176 | 126 | self.window = self.uiXML.get_object('main_window') | ||
1177 | 127 | self.window.set_title(APPNAME) | ||
1178 | 128 | self.window.set_border_width(0) | ||
1179 | 129 | self.window.move(self.prefs['window x'], self.prefs['window y']) | ||
1180 | 130 | self.window.set_default_size(self.prefs['window width'], | ||
1181 | 131 | self.prefs['window height']) | ||
1182 | 132 | self.window.connect('delete_event', self.close_application) | ||
1183 | 133 | self.window.show_all() | ||
1184 | 134 | |||
1185 | 135 | # Create a scrolled window to hold the thumbnails-container | ||
1186 | 136 | self.sw = self.uiXML.get_object('scrolledwindow') | ||
1187 | 137 | self.sw.drag_dest_set(gtk.DEST_DEFAULT_MOTION | | ||
1188 | 138 | gtk.DEST_DEFAULT_HIGHLIGHT | | ||
1189 | 139 | gtk.DEST_DEFAULT_DROP | | ||
1190 | 140 | gtk.DEST_DEFAULT_MOTION, | ||
1191 | 141 | self.TARGETS_SW, | ||
1192 | 142 | gtk.gdk.ACTION_COPY | | ||
1193 | 143 | gtk.gdk.ACTION_MOVE) | ||
1194 | 144 | self.sw.connect('drag_data_received', self.sw_dnd_received_data) | ||
1195 | 145 | self.sw.connect('button_press_event', self.sw_button_press_event) | ||
1196 | 146 | self.sw.connect('scroll_event', self.sw_scroll_event) | ||
1197 | 147 | |||
1198 | 148 | # Create an alignment to keep the thumbnails center-aligned | ||
1199 | 149 | align = gtk.Alignment(0.5, 0.5, 0, 0) | ||
1200 | 150 | self.sw.add_with_viewport(align) | ||
1201 | 151 | |||
1202 | 152 | # Create ListStore model and IconView | ||
1203 | 153 | self.model = gtk.ListStore(str, # 0.Text descriptor | ||
1204 | 154 | CairoImage, # 1.Cached page image | ||
1205 | 155 | int, # 2.Document number | ||
1206 | 156 | int, # 3.Page number | ||
1207 | 157 | float, # 4.Scale | ||
1208 | 158 | str, # 5.Document filename | ||
1209 | 159 | int, # 6.Rotation angle | ||
1210 | 160 | float, # 7.Crop left | ||
1211 | 161 | float, # 8.Crop right | ||
1212 | 162 | float, # 9.Crop top | ||
1213 | 163 | float, # 10.Crop bottom | ||
1214 | 164 | int, # 11.Page width | ||
1215 | 165 | int) # 12.Page height | ||
1216 | 166 | |||
1217 | 167 | self.zoom_set(self.prefs['initial zoom level']) | ||
1218 | 168 | self.iv_col_width = self.prefs['initial thumbnail size'] | ||
1219 | 169 | |||
1220 | 170 | self.iconview = gtk.IconView(self.model) | ||
1221 | 171 | self.iconview.set_item_width(self.iv_col_width + 12) | ||
1222 | 172 | |||
1223 | 173 | self.cellthmb = CellRendererImage() | ||
1224 | 174 | self.iconview.pack_start(self.cellthmb, False) | ||
1225 | 175 | self.iconview.set_attributes(self.cellthmb, image=1, | ||
1226 | 176 | scale=4, rotation=6, cropL=7, cropR=8, cropT=9, cropB=10, | ||
1227 | 177 | width=11, height=12) | ||
1228 | 178 | |||
1229 | 179 | # self.iconview.set_text_column(0) | ||
1230 | 180 | self.celltxt = gtk.CellRendererText() | ||
1231 | 181 | self.celltxt.set_property('width', self.iv_col_width) | ||
1232 | 182 | self.celltxt.set_property('wrap-width', self.iv_col_width) | ||
1233 | 183 | self.celltxt.set_property('alignment', pango.ALIGN_CENTER) | ||
1234 | 184 | self.iconview.pack_start(self.celltxt, False) | ||
1235 | 185 | self.iconview.set_attributes(self.celltxt, text=0) | ||
1236 | 186 | |||
1237 | 187 | self.iconview.set_selection_mode(gtk.SELECTION_MULTIPLE) | ||
1238 | 188 | self.iconview.enable_model_drag_source(gtk.gdk.BUTTON1_MASK, | ||
1239 | 189 | self.TARGETS_IV, | ||
1240 | 190 | gtk.gdk.ACTION_COPY | | ||
1241 | 191 | gtk.gdk.ACTION_MOVE) | ||
1242 | 192 | self.iconview.enable_model_drag_dest(self.TARGETS_IV, | ||
1243 | 193 | gtk.gdk.ACTION_DEFAULT) | ||
1244 | 194 | self.iconview.connect('drag_begin', self.iv_drag_begin) | ||
1245 | 195 | self.iconview.connect('drag_data_get', self.iv_dnd_get_data) | ||
1246 | 196 | self.iconview.connect('drag_data_received', self.iv_dnd_received_data) | ||
1247 | 197 | self.iconview.connect('drag_data_delete', self.iv_dnd_data_delete) | ||
1248 | 198 | self.iconview.connect('drag_motion', self.iv_dnd_motion) | ||
1249 | 199 | self.iconview.connect('drag_leave', self.iv_dnd_leave_end) | ||
1250 | 200 | self.iconview.connect('drag_end', self.iv_dnd_leave_end) | ||
1251 | 201 | self.iconview.connect('button_press_event', self.iv_button_press_event) | ||
1252 | 202 | self.iv_auto_scroll_direction = 0 | ||
1253 | 203 | |||
1254 | 204 | style = self.iconview.get_style().copy() | ||
1255 | 205 | style_sw = self.sw.get_style() | ||
1256 | 206 | for state in (gtk.STATE_NORMAL, gtk.STATE_PRELIGHT, gtk.STATE_ACTIVE): | ||
1257 | 207 | style.base[state] = style_sw.bg[gtk.STATE_NORMAL] | ||
1258 | 208 | self.iconview.set_style(style) | ||
1259 | 209 | |||
1260 | 210 | align.add(self.iconview) | ||
1261 | 211 | |||
1262 | 212 | # Progress bar | ||
1263 | 213 | self.progress_bar = self.uiXML.get_object('progressbar') | ||
1264 | 214 | self.progress_bar_timeout_id = 0 | ||
1265 | 215 | |||
1266 | 216 | # Define window callback function and show window | ||
1267 | 217 | self.window.connect('size_allocate', self.on_window_size_request) # resize | ||
1268 | 218 | self.window.connect('key_press_event', self.on_keypress_event ) # keypress | ||
1269 | 219 | self.window.show_all() | ||
1270 | 220 | self.progress_bar.hide_all() | ||
1271 | 221 | |||
1272 | 222 | # Creating the popup menu | ||
1273 | 223 | self.popup = gtk.Menu() | ||
1274 | 224 | popup_rotate_right = gtk.ImageMenuItem(_('_Rotate Right')) | ||
1275 | 225 | popup_rotate_left = gtk.ImageMenuItem(_('Rotate _Left')) | ||
1276 | 226 | popup_crop = gtk.MenuItem(_('C_rop...')) | ||
1277 | 227 | popup_delete = gtk.ImageMenuItem(gtk.STOCK_DELETE) | ||
1278 | 228 | popup_rotate_right.connect('activate', self.rotate_page_right) | ||
1279 | 229 | popup_rotate_left.connect('activate', self.rotate_page_left) | ||
1280 | 230 | popup_crop.connect('activate', self.crop_page_dialog) | ||
1281 | 231 | popup_delete.connect('activate', self.clear_selected) | ||
1282 | 232 | popup_rotate_right.show() | ||
1283 | 233 | popup_rotate_left.show() | ||
1284 | 234 | popup_crop.show() | ||
1285 | 235 | popup_delete.show() | ||
1286 | 236 | self.popup.append(popup_rotate_right) | ||
1287 | 237 | self.popup.append(popup_rotate_left) | ||
1288 | 238 | self.popup.append(popup_crop) | ||
1289 | 239 | self.popup.append(popup_delete) | ||
1290 | 240 | |||
1291 | 241 | # Initializing variables | ||
1292 | 242 | self.export_directory = os.getenv('HOME') | ||
1293 | 243 | self.import_directory = self.export_directory | ||
1294 | 244 | self.nfile = 0 | ||
1295 | 245 | self.iv_auto_scroll_timer = None | ||
1296 | 246 | self.pdfqueue = [] | ||
1297 | 247 | |||
1298 | 248 | gobject.type_register(PDF_Renderer) | ||
1299 | 249 | gobject.signal_new('update_thumbnail', PDF_Renderer, | ||
1300 | 250 | gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, | ||
1301 | 251 | [gobject.TYPE_INT, gobject.TYPE_PYOBJECT]) | ||
1302 | 252 | self.rendering_thread = 0 | ||
1303 | 253 | |||
1304 | 254 | self.set_unsaved(False) | ||
1305 | 255 | |||
1306 | 256 | # Importing documents passed as command line arguments | ||
1307 | 257 | for filename in sys.argv[1:]: | ||
1308 | 258 | self.add_pdf_pages(filename) | ||
1309 | 259 | |||
1310 | 260 | def render(self): | ||
1311 | 261 | if self.rendering_thread: | ||
1312 | 262 | self.rendering_thread.quit = True | ||
1313 | 263 | self.rendering_thread = PDF_Renderer(self.model, self.pdfqueue) | ||
1314 | 264 | self.rendering_thread.connect('update_thumbnail', self.update_thumbnail) | ||
1315 | 265 | self.rendering_thread.start() | ||
1316 | 266 | |||
1317 | 267 | if self.progress_bar_timeout_id: | ||
1318 | 268 | gobject.source_remove(self.progress_bar_timeout_id) | ||
1319 | 269 | self.progress_bar_timout_id = \ | ||
1320 | 270 | gobject.timeout_add(50, self.progress_bar_timeout) | ||
1321 | 271 | |||
1322 | 272 | def set_unsaved(self, flag): | ||
1323 | 273 | self.is_unsaved = flag | ||
1324 | 274 | gobject.idle_add(self.retitle) | ||
1325 | 275 | |||
1326 | 276 | def retitle(self): | ||
1327 | 277 | title = '' | ||
1328 | 278 | if len(self.pdfqueue) == 1: | ||
1329 | 279 | title += self.pdfqueue[0].filename | ||
1330 | 280 | elif len(self.pdfqueue) == 0: | ||
1331 | 281 | title += _("No document") | ||
1332 | 282 | else: | ||
1333 | 283 | title += _("Several documents") | ||
1334 | 284 | if self.is_unsaved: | ||
1335 | 285 | title += '*' | ||
1336 | 286 | title += ' - ' + APPNAME | ||
1337 | 287 | self.window.set_title(title) | ||
1338 | 288 | |||
1339 | 289 | def progress_bar_timeout(self): | ||
1340 | 290 | cnt_finished = 0 | ||
1341 | 291 | cnt_all = 0 | ||
1342 | 292 | for row in self.model: | ||
1343 | 293 | cnt_all += 1 | ||
1344 | 294 | if row[1].surface: | ||
1345 | 295 | cnt_finished += 1 | ||
1346 | 296 | fraction = float(cnt_finished)/float(cnt_all) | ||
1347 | 297 | |||
1348 | 298 | self.progress_bar.set_fraction(fraction) | ||
1349 | 299 | self.progress_bar.set_text(_('Rendering thumbnails... [%(i1)s/%(i2)s]') | ||
1350 | 300 | % {'i1' : cnt_finished, 'i2' : cnt_all}) | ||
1351 | 301 | if fraction >= 0.999: | ||
1352 | 302 | self.progress_bar.hide_all() | ||
1353 | 303 | return False | ||
1354 | 304 | elif not self.progress_bar.flags() & gtk.VISIBLE: | ||
1355 | 305 | self.progress_bar.show_all() | ||
1356 | 306 | |||
1357 | 307 | return True | ||
1358 | 308 | |||
1359 | 309 | def update_thumbnail(self, object, num, thumbnail): | ||
1360 | 310 | row = self.model[num] | ||
1361 | 311 | row[4] = self.zoom_scale | ||
1362 | 312 | row[1] = thumbnail | ||
1363 | 313 | |||
1364 | 314 | def on_window_size_request(self, window, event): | ||
1365 | 315 | """Main Window resize - workaround for autosetting of | ||
1366 | 316 | iconview cols no.""" | ||
1367 | 317 | |||
1368 | 318 | #add 12 because of: http://bugzilla.gnome.org/show_bug.cgi?id=570152 | ||
1369 | 319 | col_num = 9 * window.get_size()[0] \ | ||
1370 | 320 | / (10 * (self.iv_col_width + self.iconview.get_column_spacing() * 2)) | ||
1371 | 321 | self.iconview.set_columns(col_num) | ||
1372 | 322 | |||
1373 | 323 | def update_geometry(self, iter): | ||
1374 | 324 | """Recomputes the width and height of the rotated page and saves | ||
1375 | 325 | the result in the ListStore""" | ||
1376 | 326 | |||
1377 | 327 | if not self.model.iter_is_valid(iter): | ||
1378 | 328 | return | ||
1379 | 329 | |||
1380 | 330 | nfile, npage, rotation = self.model.get(iter, 2, 3, 6) | ||
1381 | 331 | crop = self.model.get(iter, 7, 8, 9, 10) | ||
1382 | 332 | page = self.pdfqueue[nfile-1].document.get_page(npage-1) | ||
1383 | 333 | w0, h0 = page.get_size() | ||
1384 | 334 | |||
1385 | 335 | rotation = int(rotation) % 360 | ||
1386 | 336 | rotation = ((rotation + 45) / 90) * 90 | ||
1387 | 337 | if rotation == 90 or rotation == 270: | ||
1388 | 338 | w1, h1 = h0, w0 | ||
1389 | 339 | else: | ||
1390 | 340 | w1, h1 = w0, h0 | ||
1391 | 341 | |||
1392 | 342 | self.model.set(iter, 11, w1, 12, h1) | ||
1393 | 343 | |||
1394 | 344 | def reset_iv_width(self, renderer=None): | ||
1395 | 345 | """Reconfigures the width of the iconview columns""" | ||
1396 | 346 | |||
1397 | 347 | if not self.model.get_iter_first(): #just checking if model is empty | ||
1398 | 348 | return | ||
1399 | 349 | |||
1400 | 350 | max_w = 10 + int(max(row[4]*row[11]*(1.-row[7]-row[8]) for row in self.model)) | ||
1401 | 351 | if max_w != self.iv_col_width: | ||
1402 | 352 | self.iv_col_width = max_w | ||
1403 | 353 | self.celltxt.set_property('width', self.iv_col_width) | ||
1404 | 354 | self.celltxt.set_property('wrap-width', self.iv_col_width) | ||
1405 | 355 | self.iconview.set_item_width(self.iv_col_width + 12) #-1) | ||
1406 | 356 | self.on_window_size_request(self.window, None) | ||
1407 | 357 | |||
1408 | 358 | def on_keypress_event(self, widget, event): | ||
1409 | 359 | """Keypress events in Main Window""" | ||
1410 | 360 | |||
1411 | 361 | #keyname = gtk.gdk.keyval_name(event.keyval) | ||
1412 | 362 | if event.keyval == 65535: # Delete keystroke | ||
1413 | 363 | self.clear_selected() | ||
1414 | 364 | |||
1415 | 365 | def close_application(self, widget, event=None, data=None): | ||
1416 | 366 | """Termination""" | ||
1417 | 367 | |||
1418 | 368 | if self.rendering_thread: | ||
1419 | 369 | self.rendering_thread.quit = True | ||
1420 | 370 | while self.rendering_thread.isAlive(): | ||
1421 | 371 | time.sleep(0.5) | ||
1422 | 372 | |||
1423 | 373 | if os.path.isdir(self.tmp_dir): | ||
1424 | 374 | shutil.rmtree(self.tmp_dir) | ||
1425 | 375 | if gtk.main_level(): | ||
1426 | 376 | gtk.main_quit() | ||
1427 | 377 | else: | ||
1428 | 378 | sys.exit(0) | ||
1429 | 379 | return False | ||
1430 | 380 | |||
1431 | 381 | def add_pdf_pages(self, filename, | ||
1432 | 382 | firstpage=None, lastpage=None, | ||
1433 | 383 | angle=0, crop=[0.,0.,0.,0.]): | ||
1434 | 384 | """Add pages of a pdf document to the model""" | ||
1435 | 385 | |||
1436 | 386 | res = False | ||
1437 | 387 | # Check if the document has already been loaded | ||
1438 | 388 | pdfdoc = None | ||
1439 | 389 | for it_pdfdoc in self.pdfqueue: | ||
1440 | 390 | if os.path.isfile(it_pdfdoc.filename) and \ | ||
1441 | 391 | os.path.samefile(filename, it_pdfdoc.filename) and \ | ||
1442 | 392 | os.path.getmtime(filename) is it_pdfdoc.mtime: | ||
1443 | 393 | pdfdoc = it_pdfdoc | ||
1444 | 394 | break | ||
1445 | 395 | |||
1446 | 396 | if not pdfdoc: | ||
1447 | 397 | pdfdoc = PDF_Doc(filename, self.nfile, self.tmp_dir) | ||
1448 | 398 | self.import_directory = os.path.split(filename)[0] | ||
1449 | 399 | self.export_directory = self.import_directory | ||
1450 | 400 | if pdfdoc.nfile != 0 and pdfdoc != []: | ||
1451 | 401 | self.nfile = pdfdoc.nfile | ||
1452 | 402 | self.pdfqueue.append(pdfdoc) | ||
1453 | 403 | else: | ||
1454 | 404 | return res | ||
1455 | 405 | |||
1456 | 406 | n_start = 1 | ||
1457 | 407 | n_end = pdfdoc.npage | ||
1458 | 408 | if firstpage: | ||
1459 | 409 | n_start = min(n_end, max(1, firstpage)) | ||
1460 | 410 | if lastpage: | ||
1461 | 411 | n_end = max(n_start, min(n_end, lastpage)) | ||
1462 | 412 | |||
1463 | 413 | for npage in range(n_start, n_end + 1): | ||
1464 | 414 | descriptor = ''.join([pdfdoc.shortname, '\n', _('page'), ' ', str(npage)]) | ||
1465 | 415 | page = pdfdoc.document.get_page(npage-1) | ||
1466 | 416 | w, h = page.get_size() | ||
1467 | 417 | iter = self.model.append((descriptor, # 0 | ||
1468 | 418 | CairoImage(), # 1 | ||
1469 | 419 | pdfdoc.nfile, # 2 | ||
1470 | 420 | npage, # 3 | ||
1471 | 421 | self.zoom_scale, # 4 | ||
1472 | 422 | pdfdoc.filename, # 5 | ||
1473 | 423 | angle, # 6 | ||
1474 | 424 | crop[0],crop[1], # 7-8 | ||
1475 | 425 | crop[2],crop[3], # 9-10 | ||
1476 | 426 | w,h )) # 11-12 | ||
1477 | 427 | self.update_geometry(iter) | ||
1478 | 428 | res = True | ||
1479 | 429 | |||
1480 | 430 | self.reset_iv_width() | ||
1481 | 431 | gobject.idle_add(self.retitle) | ||
1482 | 432 | if res: | ||
1483 | 433 | gobject.idle_add(self.render) | ||
1484 | 434 | return res | ||
1485 | 435 | |||
1486 | 436 | def choose_export_pdf_name(self, widget=None): | ||
1487 | 437 | """Handles choosing a name for exporting """ | ||
1488 | 438 | |||
1489 | 439 | chooser = gtk.FileChooserDialog(title=_('Export ...'), | ||
1490 | 440 | action=gtk.FILE_CHOOSER_ACTION_SAVE, | ||
1491 | 441 | buttons=(gtk.STOCK_CANCEL, | ||
1492 | 442 | gtk.RESPONSE_CANCEL, | ||
1493 | 443 | gtk.STOCK_SAVE, | ||
1494 | 444 | gtk.RESPONSE_OK)) | ||
1495 | 445 | chooser.set_do_overwrite_confirmation(True) | ||
1496 | 446 | chooser.set_current_folder(self.export_directory) | ||
1497 | 447 | filter_pdf = gtk.FileFilter() | ||
1498 | 448 | filter_pdf.set_name(_('PDF files')) | ||
1499 | 449 | filter_pdf.add_mime_type('application/pdf') | ||
1500 | 450 | chooser.add_filter(filter_pdf) | ||
1501 | 451 | |||
1502 | 452 | filter_all = gtk.FileFilter() | ||
1503 | 453 | filter_all.set_name(_('All files')) | ||
1504 | 454 | filter_all.add_pattern('*') | ||
1505 | 455 | chooser.add_filter(filter_all) | ||
1506 | 456 | |||
1507 | 457 | while True: | ||
1508 | 458 | response = chooser.run() | ||
1509 | 459 | if response == gtk.RESPONSE_OK: | ||
1510 | 460 | file_out = chooser.get_filename() | ||
1511 | 461 | (path, shortname) = os.path.split(file_out) | ||
1512 | 462 | (shortname, ext) = os.path.splitext(shortname) | ||
1513 | 463 | if ext.lower() != '.pdf': | ||
1514 | 464 | file_out = file_out + '.pdf' | ||
1515 | 465 | try: | ||
1516 | 466 | self.export_to_file(file_out) | ||
1517 | 467 | self.export_directory = path | ||
1518 | 468 | self.set_unsaved(False) | ||
1519 | 469 | except Exception, e: | ||
1520 | 470 | chooser.destroy() | ||
1521 | 471 | error_msg_dlg = gtk.MessageDialog(flags=gtk.DIALOG_MODAL, | ||
1522 | 472 | type=gtk.MESSAGE_ERROR, | ||
1523 | 473 | message_format=str(e), | ||
1524 | 474 | buttons=gtk.BUTTONS_OK) | ||
1525 | 475 | response = error_msg_dlg.run() | ||
1526 | 476 | if response == gtk.RESPONSE_OK: | ||
1527 | 477 | error_msg_dlg.destroy() | ||
1528 | 478 | return | ||
1529 | 479 | break | ||
1530 | 480 | chooser.destroy() | ||
1531 | 481 | |||
1532 | 482 | def export_to_file(self, file_out): | ||
1533 | 483 | """Export to file""" | ||
1534 | 484 | |||
1535 | 485 | pdf_output = PdfFileWriter() | ||
1536 | 486 | pdf_input = [] | ||
1537 | 487 | for pdfdoc in self.pdfqueue: | ||
1538 | 488 | pdfdoc_inp = PdfFileReader(file(pdfdoc.copyname, 'rb')) | ||
1539 | 489 | if pdfdoc_inp.getIsEncrypted(): | ||
1540 | 490 | try: # Workaround for lp:#355479 | ||
1541 | 491 | stat = pdfdoc_inp.decrypt('') | ||
1542 | 492 | except: | ||
1543 | 493 | stat = 0 | ||
1544 | 494 | if (stat!=1): | ||
1545 | 495 | errmsg = _('File %s is encrypted.\n' | ||
1546 | 496 | 'Support for encrypted files has not been implemented yet.\n' | ||
1547 | 497 | 'File export failed.') % pdfdoc.filename | ||
1548 | 498 | raise Exception, errmsg | ||
1549 | 499 | #FIXME | ||
1550 | 500 | #else | ||
1551 | 501 | # ask for password and decrypt file | ||
1552 | 502 | pdf_input.append(pdfdoc_inp) | ||
1553 | 503 | |||
1554 | 504 | for row in self.model: | ||
1555 | 505 | # add pages from input to output document | ||
1556 | 506 | nfile = row[2] | ||
1557 | 507 | npage = row[3] | ||
1558 | 508 | current_page = copy(pdf_input[nfile-1].getPage(npage-1)) | ||
1559 | 509 | angle = row[6] | ||
1560 | 510 | angle0 = current_page.get("/Rotate",0) | ||
1561 | 511 | crop = [row[7],row[8],row[9],row[10]] | ||
1562 | 512 | if angle != 0: | ||
1563 | 513 | current_page.rotateClockwise(angle) | ||
1564 | 514 | if crop != [0.,0.,0.,0.]: | ||
1565 | 515 | rotate_times = (((angle + angle0) % 360 + 45) / 90) % 4 | ||
1566 | 516 | crop_init = crop | ||
1567 | 517 | if rotate_times != 0: | ||
1568 | 518 | perm = [0,2,1,3] | ||
1569 | 519 | for it in range(rotate_times): | ||
1570 | 520 | perm.append(perm.pop(0)) | ||
1571 | 521 | perm.insert(1,perm.pop(2)) | ||
1572 | 522 | crop = [crop_init[perm[side]] for side in range(4)] | ||
1573 | 523 | #(x1, y1) = current_page.cropBox.lowerLeft | ||
1574 | 524 | #(x2, y2) = current_page.cropBox.upperRight | ||
1575 | 525 | (x1, y1) = [float(xy) for xy in current_page.mediaBox.lowerLeft] | ||
1576 | 526 | (x2, y2) = [float(xy) for xy in current_page.mediaBox.upperRight] | ||
1577 | 527 | x1_new = int(x1 + (x2-x1) * crop[0]) | ||
1578 | 528 | x2_new = int(x2 - (x2-x1) * crop[1]) | ||
1579 | 529 | y1_new = int(y1 + (y2-y1) * crop[3]) | ||
1580 | 530 | y2_new = int(y2 - (y2-y1) * crop[2]) | ||
1581 | 531 | #current_page.cropBox.lowerLeft = (x1_new, y1_new) | ||
1582 | 532 | #current_page.cropBox.upperRight = (x2_new, y2_new) | ||
1583 | 533 | current_page.mediaBox.lowerLeft = (x1_new, y1_new) | ||
1584 | 534 | current_page.mediaBox.upperRight = (x2_new, y2_new) | ||
1585 | 535 | |||
1586 | 536 | pdf_output.addPage(current_page) | ||
1587 | 537 | |||
1588 | 538 | # finally, write "output" to document-output.pdf | ||
1589 | 539 | print(_('exporting to:'), file_out) | ||
1590 | 540 | pdf_output.write(file(file_out, 'wb')) | ||
1591 | 541 | |||
1592 | 542 | def on_action_add_doc_activate(self, widget, data=None): | ||
1593 | 543 | """Import doc""" | ||
1594 | 544 | |||
1595 | 545 | chooser = gtk.FileChooserDialog(title=_('_Import...'), | ||
1596 | 546 | action=gtk.FILE_CHOOSER_ACTION_OPEN, | ||
1597 | 547 | buttons=(gtk.STOCK_CANCEL, | ||
1598 | 548 | gtk.RESPONSE_CANCEL, | ||
1599 | 549 | gtk.STOCK_OPEN, | ||
1600 | 550 | gtk.RESPONSE_OK)) | ||
1601 | 551 | chooser.set_current_folder(self.import_directory) | ||
1602 | 552 | chooser.set_select_multiple(True) | ||
1603 | 553 | |||
1604 | 554 | filter_all = gtk.FileFilter() | ||
1605 | 555 | filter_all.set_name(_('All files')) | ||
1606 | 556 | filter_all.add_pattern('*') | ||
1607 | 557 | chooser.add_filter(filter_all) | ||
1608 | 558 | |||
1609 | 559 | filter_pdf = gtk.FileFilter() | ||
1610 | 560 | filter_pdf.set_name(_('PDF files')) | ||
1611 | 561 | filter_pdf.add_mime_type('application/pdf') | ||
1612 | 562 | chooser.add_filter(filter_pdf) | ||
1613 | 563 | chooser.set_filter(filter_pdf) | ||
1614 | 564 | |||
1615 | 565 | response = chooser.run() | ||
1616 | 566 | if response == gtk.RESPONSE_OK: | ||
1617 | 567 | for filename in chooser.get_filenames(): | ||
1618 | 568 | if os.path.isfile(filename): | ||
1619 | 569 | # FIXME | ||
1620 | 570 | f = gio.File(filename) | ||
1621 | 571 | f_info = f.query_info('standard::content-type') | ||
1622 | 572 | mime_type = f_info.get_content_type() | ||
1623 | 573 | expected_mime_type = 'application/pdf' | ||
1624 | 574 | |||
1625 | 575 | if mime_type == expected_mime_type: | ||
1626 | 576 | self.add_pdf_pages(filename) | ||
1627 | 577 | elif mime_type[:34] == 'application/vnd.oasis.opendocument': | ||
1628 | 578 | print(_('OpenDocument not supported yet!')) | ||
1629 | 579 | elif mime_type[:5] == 'image': | ||
1630 | 580 | print(_('Image file not supported yet!')) | ||
1631 | 581 | else: | ||
1632 | 582 | print(_('File type not supported!')) | ||
1633 | 583 | else: | ||
1634 | 584 | print(_('File %s does not exist') % filename) | ||
1635 | 585 | elif response == gtk.RESPONSE_CANCEL: | ||
1636 | 586 | print(_('Closed, no files selected')) | ||
1637 | 587 | chooser.destroy() | ||
1638 | 588 | gobject.idle_add(self.retitle) | ||
1639 | 589 | |||
1640 | 590 | def clear_selected(self, button=None): | ||
1641 | 591 | """Removes the selected elements in the IconView""" | ||
1642 | 592 | |||
1643 | 593 | model = self.iconview.get_model() | ||
1644 | 594 | selection = self.iconview.get_selected_items() | ||
1645 | 595 | if selection: | ||
1646 | 596 | selection.sort(reverse=True) | ||
1647 | 597 | self.set_unsaved(True) | ||
1648 | 598 | for path in selection: | ||
1649 | 599 | iter = model.get_iter(path) | ||
1650 | 600 | model.remove(iter) | ||
1651 | 601 | path = selection[-1] | ||
1652 | 602 | self.iconview.select_path(path) | ||
1653 | 603 | if not self.iconview.path_is_selected(path): | ||
1654 | 604 | if len(model) > 0: # select the last row | ||
1655 | 605 | row = model[-1] | ||
1656 | 606 | path = row.path | ||
1657 | 607 | self.iconview.select_path(path) | ||
1658 | 608 | self.iconview.grab_focus() | ||
1659 | 609 | |||
1660 | 610 | def iv_drag_begin(self, iconview, context): | ||
1661 | 611 | """Sets custom icon on drag begin for multiple items selected""" | ||
1662 | 612 | |||
1663 | 613 | if len(iconview.get_selected_items()) > 1: | ||
1664 | 614 | iconview.stop_emission('drag_begin') | ||
1665 | 615 | context.set_icon_stock(gtk.STOCK_DND_MULTIPLE, 0, 0) | ||
1666 | 616 | |||
1667 | 617 | def iv_dnd_get_data(self, iconview, context, | ||
1668 | 618 | selection_data, target_id, etime): | ||
1669 | 619 | """Handles requests for data by drag and drop in iconview""" | ||
1670 | 620 | |||
1671 | 621 | model = iconview.get_model() | ||
1672 | 622 | selection = self.iconview.get_selected_items() | ||
1673 | 623 | selection.sort(key=lambda x: x[0]) | ||
1674 | 624 | data = [] | ||
1675 | 625 | for path in selection: | ||
1676 | 626 | if selection_data.target == 'MODEL_ROW_INTERN': | ||
1677 | 627 | data.append(str(path[0])) | ||
1678 | 628 | elif selection_data.target == 'MODEL_ROW_EXTERN': | ||
1679 | 629 | iter = model.get_iter(path) | ||
1680 | 630 | nfile, npage, angle = model.get(iter, 2, 3, 6) | ||
1681 | 631 | crop = model.get(iter, 7, 8, 9, 10) | ||
1682 | 632 | pdfdoc = self.pdfqueue[nfile - 1] | ||
1683 | 633 | data.append('\n'.join([pdfdoc.filename, | ||
1684 | 634 | str(npage), | ||
1685 | 635 | str(angle)] + | ||
1686 | 636 | [str(side) for side in crop])) | ||
1687 | 637 | if data: | ||
1688 | 638 | data = '\n;\n'.join(data) | ||
1689 | 639 | selection_data.set(selection_data.target, 8, data) | ||
1690 | 640 | |||
1691 | 641 | def iv_dnd_received_data(self, iconview, context, x, y, | ||
1692 | 642 | selection_data, target_id, etime): | ||
1693 | 643 | """Handles received data by drag and drop in iconview""" | ||
1694 | 644 | |||
1695 | 645 | model = iconview.get_model() | ||
1696 | 646 | data = selection_data.data | ||
1697 | 647 | if data: | ||
1698 | 648 | data = data.split('\n;\n') | ||
1699 | 649 | drop_info = iconview.get_dest_item_at_pos(x, y) | ||
1700 | 650 | iter_to = None | ||
1701 | 651 | if drop_info: | ||
1702 | 652 | path, position = drop_info | ||
1703 | 653 | ref_to = gtk.TreeRowReference(model,path) | ||
1704 | 654 | else: | ||
1705 | 655 | position = gtk.ICON_VIEW_DROP_RIGHT | ||
1706 | 656 | if len(model) > 0: #find the iterator of the last row | ||
1707 | 657 | row = model[-1] | ||
1708 | 658 | path = row.path | ||
1709 | 659 | ref_to = gtk.TreeRowReference(model,path) | ||
1710 | 660 | if ref_to: | ||
1711 | 661 | before = (position == gtk.ICON_VIEW_DROP_LEFT | ||
1712 | 662 | or position == gtk.ICON_VIEW_DROP_ABOVE) | ||
1713 | 663 | #if target_id == self.MODEL_ROW_INTERN: | ||
1714 | 664 | if selection_data.target == 'MODEL_ROW_INTERN': | ||
1715 | 665 | if before: | ||
1716 | 666 | data.sort(key=int) | ||
1717 | 667 | else: | ||
1718 | 668 | data.sort(key=int,reverse=True) | ||
1719 | 669 | ref_from_list = [gtk.TreeRowReference(model,path) | ||
1720 | 670 | for path in data] | ||
1721 | 671 | for ref_from in ref_from_list: | ||
1722 | 672 | path = ref_to.get_path() | ||
1723 | 673 | iter_to = model.get_iter(path) | ||
1724 | 674 | path = ref_from.get_path() | ||
1725 | 675 | iter_from = model.get_iter(path) | ||
1726 | 676 | row = model[iter_from] | ||
1727 | 677 | if before: | ||
1728 | 678 | model.insert_before(iter_to, row) | ||
1729 | 679 | else: | ||
1730 | 680 | model.insert_after(iter_to, row) | ||
1731 | 681 | if context.action == gtk.gdk.ACTION_MOVE: | ||
1732 | 682 | for ref_from in ref_from_list: | ||
1733 | 683 | path = ref_from.get_path() | ||
1734 | 684 | iter_from = model.get_iter(path) | ||
1735 | 685 | model.remove(iter_from) | ||
1736 | 686 | |||
1737 | 687 | #elif target_id == self.MODEL_ROW_EXTERN: | ||
1738 | 688 | elif selection_data.target == 'MODEL_ROW_EXTERN': | ||
1739 | 689 | if not before: | ||
1740 | 690 | data.reverse() | ||
1741 | 691 | while data: | ||
1742 | 692 | tmp = data.pop(0).split('\n') | ||
1743 | 693 | filename = tmp[0] | ||
1744 | 694 | npage, angle = [int(k) for k in tmp[1:3]] | ||
1745 | 695 | crop = [float(side) for side in tmp[3:7]] | ||
1746 | 696 | if self.add_pdf_pages(filename, npage, npage, | ||
1747 | 697 | angle, crop): | ||
1748 | 698 | if len(model) > 0: | ||
1749 | 699 | path = ref_to.get_path() | ||
1750 | 700 | iter_to = model.get_iter(path) | ||
1751 | 701 | row = model[-1] #the last row | ||
1752 | 702 | path = row.path | ||
1753 | 703 | iter_from = model.get_iter(path) | ||
1754 | 704 | if before: | ||
1755 | 705 | model.move_before(iter_from, iter_to) | ||
1756 | 706 | else: | ||
1757 | 707 | model.move_after(iter_from, iter_to) | ||
1758 | 708 | if context.action == gtk.gdk.ACTION_MOVE: | ||
1759 | 709 | context.finish(True, True, etime) | ||
1760 | 710 | |||
1761 | 711 | def iv_dnd_data_delete(self, widget, context): | ||
1762 | 712 | """Deletes dnd items after a successful move operation""" | ||
1763 | 713 | |||
1764 | 714 | model = self.iconview.get_model() | ||
1765 | 715 | selection = self.iconview.get_selected_items() | ||
1766 | 716 | ref_del_list = [gtk.TreeRowReference(model,path) for path in selection] | ||
1767 | 717 | for ref_del in ref_del_list: | ||
1768 | 718 | path = ref_del.get_path() | ||
1769 | 719 | iter = model.get_iter(path) | ||
1770 | 720 | model.remove(iter) | ||
1771 | 721 | |||
1772 | 722 | def iv_dnd_motion(self, iconview, context, x, y, etime): | ||
1773 | 723 | """Handles the drag-motion signal in order to auto-scroll the view""" | ||
1774 | 724 | |||
1775 | 725 | autoscroll_area = 40 | ||
1776 | 726 | sw_vadj = self.sw.get_vadjustment() | ||
1777 | 727 | sw_height = self.sw.get_allocation().height | ||
1778 | 728 | if y -sw_vadj.get_value() < autoscroll_area: | ||
1779 | 729 | if not self.iv_auto_scroll_timer: | ||
1780 | 730 | self.iv_auto_scroll_direction = gtk.DIR_UP | ||
1781 | 731 | self.iv_auto_scroll_timer = gobject.timeout_add(150, | ||
1782 | 732 | self.iv_auto_scroll) | ||
1783 | 733 | elif y -sw_vadj.get_value() > sw_height - autoscroll_area: | ||
1784 | 734 | if not self.iv_auto_scroll_timer: | ||
1785 | 735 | self.iv_auto_scroll_direction = gtk.DIR_DOWN | ||
1786 | 736 | self.iv_auto_scroll_timer = gobject.timeout_add(150, | ||
1787 | 737 | self.iv_auto_scroll) | ||
1788 | 738 | elif self.iv_auto_scroll_timer: | ||
1789 | 739 | gobject.source_remove(self.iv_auto_scroll_timer) | ||
1790 | 740 | self.iv_auto_scroll_timer = None | ||
1791 | 741 | |||
1792 | 742 | def iv_dnd_leave_end(self, widget, context, ignored=None): | ||
1793 | 743 | """Ends the auto-scroll during DND""" | ||
1794 | 744 | |||
1795 | 745 | if self.iv_auto_scroll_timer: | ||
1796 | 746 | gobject.source_remove(self.iv_auto_scroll_timer) | ||
1797 | 747 | self.iv_auto_scroll_timer = None | ||
1798 | 748 | |||
1799 | 749 | def iv_auto_scroll(self): | ||
1800 | 750 | """Timeout routine for auto-scroll""" | ||
1801 | 751 | |||
1802 | 752 | sw_vadj = self.sw.get_vadjustment() | ||
1803 | 753 | sw_vpos = sw_vadj.get_value() | ||
1804 | 754 | if self.iv_auto_scroll_direction == gtk.DIR_UP: | ||
1805 | 755 | sw_vpos -= sw_vadj.step_increment | ||
1806 | 756 | sw_vadj.set_value(max(sw_vpos, sw_vadj.lower)) | ||
1807 | 757 | elif self.iv_auto_scroll_direction == gtk.DIR_DOWN: | ||
1808 | 758 | sw_vpos += sw_vadj.step_increment | ||
1809 | 759 | sw_vadj.set_value(min(sw_vpos, sw_vadj.upper - sw_vadj.page_size)) | ||
1810 | 760 | return True #call me again | ||
1811 | 761 | |||
1812 | 762 | def iv_button_press_event(self, iconview, event): | ||
1813 | 763 | """Manages mouse clicks on the iconview""" | ||
1814 | 764 | |||
1815 | 765 | if event.button == 3: | ||
1816 | 766 | x = int(event.x) | ||
1817 | 767 | y = int(event.y) | ||
1818 | 768 | time = event.time | ||
1819 | 769 | path = iconview.get_path_at_pos(x, y) | ||
1820 | 770 | selection = iconview.get_selected_items() | ||
1821 | 771 | if path: | ||
1822 | 772 | if path not in selection: | ||
1823 | 773 | iconview.unselect_all() | ||
1824 | 774 | iconview.select_path(path) | ||
1825 | 775 | iconview.grab_focus() | ||
1826 | 776 | self.popup.popup(None, None, None, event.button, time) | ||
1827 | 777 | return 1 | ||
1828 | 778 | |||
1829 | 779 | def sw_dnd_received_data(self, scrolledwindow, context, x, y, | ||
1830 | 780 | selection_data, target_id, etime): | ||
1831 | 781 | """Handles received data by drag and drop in scrolledwindow""" | ||
1832 | 782 | |||
1833 | 783 | data = selection_data.data | ||
1834 | 784 | if target_id == self.MODEL_ROW_EXTERN: | ||
1835 | 785 | self.model | ||
1836 | 786 | if data: | ||
1837 | 787 | data = data.split('\n;\n') | ||
1838 | 788 | while data: | ||
1839 | 789 | tmp = data.pop(0).split('\n') | ||
1840 | 790 | filename = tmp[0] | ||
1841 | 791 | npage, angle = [int(k) for k in tmp[1:3]] | ||
1842 | 792 | crop = [float(side) for side in tmp[3:7]] | ||
1843 | 793 | if self.add_pdf_pages(filename, npage, npage, angle, crop): | ||
1844 | 794 | if context.action == gtk.gdk.ACTION_MOVE: | ||
1845 | 795 | context.finish(True, True, etime) | ||
1846 | 796 | elif target_id == self.TEXT_URI_LIST: | ||
1847 | 797 | uri = data.strip() | ||
1848 | 798 | uri_splitted = uri.split() # we may have more than one file dropped | ||
1849 | 799 | for uri in uri_splitted: | ||
1850 | 800 | filename = self.get_file_path_from_dnd_dropped_uri(uri) | ||
1851 | 801 | if os.path.isfile(filename): # is it file? | ||
1852 | 802 | self.add_pdf_pages(filename) | ||
1853 | 803 | |||
1854 | 804 | def sw_button_press_event(self, scrolledwindow, event): | ||
1855 | 805 | """Unselects all items in iconview on mouse click in scrolledwindow""" | ||
1856 | 806 | |||
1857 | 807 | if event.button == 1: | ||
1858 | 808 | self.iconview.unselect_all() | ||
1859 | 809 | |||
1860 | 810 | def sw_scroll_event(self, scrolledwindow, event): | ||
1861 | 811 | """Manages mouse scroll events in scrolledwindow""" | ||
1862 | 812 | |||
1863 | 813 | if event.state & gtk.gdk.CONTROL_MASK: | ||
1864 | 814 | if event.direction == gtk.gdk.SCROLL_UP: | ||
1865 | 815 | self.zoom_change(1) | ||
1866 | 816 | return 1 | ||
1867 | 817 | elif event.direction == gtk.gdk.SCROLL_DOWN: | ||
1868 | 818 | self.zoom_change(-1) | ||
1869 | 819 | return 1 | ||
1870 | 820 | |||
1871 | 821 | def zoom_set(self, level): | ||
1872 | 822 | """Sets the zoom level""" | ||
1873 | 823 | self.zoom_level = max(min(level, 5), -24) | ||
1874 | 824 | self.zoom_scale = 1.1 ** self.zoom_level | ||
1875 | 825 | for row in self.model: | ||
1876 | 826 | row[4] = self.zoom_scale | ||
1877 | 827 | self.reset_iv_width() | ||
1878 | 828 | |||
1879 | 829 | def zoom_change(self, step=5): | ||
1880 | 830 | """Modifies the zoom level""" | ||
1881 | 831 | self.zoom_set(self.zoom_level + step) | ||
1882 | 832 | |||
1883 | 833 | def zoom_in(self, widget=None): | ||
1884 | 834 | """Increases the zoom level by 5 steps""" | ||
1885 | 835 | self.zoom_change(5) | ||
1886 | 836 | |||
1887 | 837 | def zoom_out(self, widget=None, step=5): | ||
1888 | 838 | """Reduces the zoom level by 5 steps""" | ||
1889 | 839 | self.zoom_change(-5) | ||
1890 | 840 | |||
1891 | 841 | def get_file_path_from_dnd_dropped_uri(self, uri): | ||
1892 | 842 | """Extracts the path from an uri""" | ||
1893 | 843 | |||
1894 | 844 | path = urllib.url2pathname(uri) # escape special chars | ||
1895 | 845 | path = path.strip('\r\n\x00') # remove \r\n and NULL | ||
1896 | 846 | |||
1897 | 847 | # get the path to file | ||
1898 | 848 | if path.startswith('file:\\\\\\'): # windows | ||
1899 | 849 | path = path[8:] # 8 is len('file:///') | ||
1900 | 850 | elif path.startswith('file://'): # nautilus, rox | ||
1901 | 851 | path = path[7:] # 7 is len('file://') | ||
1902 | 852 | elif path.startswith('file:'): # xffm | ||
1903 | 853 | path = path[5:] # 5 is len('file:') | ||
1904 | 854 | return path | ||
1905 | 855 | |||
1906 | 856 | def rotate_page_right(self, widget, data=None): | ||
1907 | 857 | self.rotate_page(90) | ||
1908 | 858 | |||
1909 | 859 | def rotate_page_left(self, widget, data=None): | ||
1910 | 860 | self.rotate_page(-90) | ||
1911 | 861 | |||
1912 | 862 | def rotate_page(self, angle): | ||
1913 | 863 | """Rotates the selected page in the IconView""" | ||
1914 | 864 | |||
1915 | 865 | model = self.iconview.get_model() | ||
1916 | 866 | selection = self.iconview.get_selected_items() | ||
1917 | 867 | if len(selection) > 0: | ||
1918 | 868 | self.set_unsaved(True) | ||
1919 | 869 | rotate_times = (((-angle) % 360 + 45) / 90) % 4 | ||
1920 | 870 | if rotate_times is not 0: | ||
1921 | 871 | for path in selection: | ||
1922 | 872 | iter = model.get_iter(path) | ||
1923 | 873 | nfile = model.get_value(iter, 2) | ||
1924 | 874 | npage = model.get_value(iter, 3) | ||
1925 | 875 | |||
1926 | 876 | crop = [0.,0.,0.,0.] | ||
1927 | 877 | perm = [0,2,1,3] | ||
1928 | 878 | for it in range(rotate_times): | ||
1929 | 879 | perm.append(perm.pop(0)) | ||
1930 | 880 | perm.insert(1,perm.pop(2)) | ||
1931 | 881 | crop = [model.get_value(iter, 7 + perm[side]) for side in range(4)] | ||
1932 | 882 | for side in range(4): | ||
1933 | 883 | model.set_value(iter, 7 + side, crop[side]) | ||
1934 | 884 | |||
1935 | 885 | new_angle = model.get_value(iter, 6) + int(angle) | ||
1936 | 886 | new_angle = new_angle % 360 | ||
1937 | 887 | model.set_value(iter, 6, new_angle) | ||
1938 | 888 | self.update_geometry(iter) | ||
1939 | 889 | self.reset_iv_width() | ||
1940 | 890 | |||
1941 | 891 | def crop_page_dialog(self, widget): | ||
1942 | 892 | """Opens a dialog box to define margins for page cropping""" | ||
1943 | 893 | |||
1944 | 894 | sides = ('L', 'R', 'T', 'B') | ||
1945 | 895 | side_names = {'L':_('Left'), 'R':_('Right'), | ||
1946 | 896 | 'T':_('Top'), 'B':_('Bottom') } | ||
1947 | 897 | opposite_sides = {'L':'R', 'R':'L', 'T':'B', 'B':'T' } | ||
1948 | 898 | |||
1949 | 899 | def set_crop_value(spinbutton, side): | ||
1950 | 900 | opp_side = opposite_sides[side] | ||
1951 | 901 | pos = sides.index(opp_side) | ||
1952 | 902 | adj = spin_list[pos].get_adjustment() | ||
1953 | 903 | adj.set_upper(99.0 - spinbutton.get_value()) | ||
1954 | 904 | |||
1955 | 905 | model = self.iconview.get_model() | ||
1956 | 906 | selection = self.iconview.get_selected_items() | ||
1957 | 907 | |||
1958 | 908 | crop = [0.,0.,0.,0.] | ||
1959 | 909 | if selection: | ||
1960 | 910 | path = selection[0] | ||
1961 | 911 | pos = model.get_iter(path) | ||
1962 | 912 | crop = [model.get_value(pos, 7 + side) for side in range(4)] | ||
1963 | 913 | |||
1964 | 914 | dialog = gtk.Dialog(title=(_('Crop Selected Pages')), | ||
1965 | 915 | parent=self.window, | ||
1966 | 916 | flags=gtk.DIALOG_MODAL, | ||
1967 | 917 | buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, | ||
1968 | 918 | gtk.STOCK_OK, gtk.RESPONSE_OK)) | ||
1969 | 919 | dialog.set_size_request(340, 250) | ||
1970 | 920 | dialog.set_default_response(gtk.RESPONSE_OK) | ||
1971 | 921 | |||
1972 | 922 | frame = gtk.Frame(_('Crop Margins')) | ||
1973 | 923 | dialog.vbox.pack_start(frame, False, False, 20) | ||
1974 | 924 | |||
1975 | 925 | vbox = gtk.VBox(False, 0) | ||
1976 | 926 | frame.add(vbox) | ||
1977 | 927 | |||
1978 | 928 | spin_list = [] | ||
1979 | 929 | units = 2 * [_('% of width')] + 2 * [_('% of height')] | ||
1980 | 930 | for side in sides: | ||
1981 | 931 | hbox = gtk.HBox(True, 0) | ||
1982 | 932 | vbox.pack_start(hbox, False, False, 5) | ||
1983 | 933 | |||
1984 | 934 | label = gtk.Label(side_names[side]) | ||
1985 | 935 | label.set_alignment(0, 0.0) | ||
1986 | 936 | hbox.pack_start(label, True, True, 20) | ||
1987 | 937 | |||
1988 | 938 | adj = gtk.Adjustment(100.*crop.pop(0), 0.0, 99.0, 1.0, 5.0, 0.0) | ||
1989 | 939 | spin = gtk.SpinButton(adj, 0, 1) | ||
1990 | 940 | spin.set_activates_default(True) | ||
1991 | 941 | spin.connect('value-changed', set_crop_value, side) | ||
1992 | 942 | spin_list.append(spin) | ||
1993 | 943 | hbox.pack_start(spin, False, False, 30) | ||
1994 | 944 | |||
1995 | 945 | label = gtk.Label(units.pop(0)) | ||
1996 | 946 | label.set_alignment(0, 0.0) | ||
1997 | 947 | hbox.pack_start(label, True, True, 0) | ||
1998 | 948 | |||
1999 | 949 | dialog.show_all() | ||
2000 | 950 | result = dialog.run() | ||
2001 | 951 | |||
2002 | 952 | if result == gtk.RESPONSE_OK: | ||
2003 | 953 | modified = False | ||
2004 | 954 | crop = [spin.get_value()/100. for spin in spin_list] | ||
2005 | 955 | for path in selection: | ||
2006 | 956 | pos = model.get_iter(path) | ||
2007 | 957 | for it in range(4): | ||
2008 | 958 | old_val = model.get_value(pos, 7 + it) | ||
2009 | 959 | model.set_value(pos, 7 + it, crop[it]) | ||
2010 | 960 | if crop[it] != old_val: | ||
2011 | 961 | modified = True | ||
2012 | 962 | self.update_geometry(pos) | ||
2013 | 963 | if modified: | ||
2014 | 964 | self.set_unsaved(True) | ||
2015 | 965 | self.reset_iv_width() | ||
2016 | 966 | elif result == gtk.RESPONSE_CANCEL: | ||
2017 | 967 | print(_('Dialog closed')) | ||
2018 | 968 | dialog.destroy() | ||
2019 | 969 | |||
2020 | 970 | def about_dialog(self, widget, data=None): | ||
2021 | 971 | about_dialog = gtk.AboutDialog() | ||
2022 | 972 | try: | ||
2023 | 973 | about_dialog.set_transient_for(self.window) | ||
2024 | 974 | about_dialog.set_modal(True) | ||
2025 | 975 | except: | ||
2026 | 976 | pass | ||
2027 | 977 | # FIXME | ||
2028 | 978 | about_dialog.set_name(APPNAME) | ||
2029 | 979 | about_dialog.set_version(VERSION) | ||
2030 | 980 | about_dialog.set_comments(_( | ||
2031 | 981 | '%s is a tool for rearranging and modifying PDF files.' \ | ||
2032 | 982 | 'Developed using GTK+ and Python') % APPNAME) | ||
2033 | 983 | about_dialog.set_authors(['Konstantinos Poulios',]) | ||
2034 | 984 | about_dialog.set_website_label(WEBSITE) | ||
2035 | 985 | about_dialog.set_logo_icon_name('pdfshuffler') | ||
2036 | 986 | about_dialog.set_license(LICENSE) | ||
2037 | 987 | about_dialog.connect('response', lambda w, a: about_dialog.destroy()) | ||
2038 | 988 | about_dialog.connect('delete_event', lambda w, a: about_dialog.destroy()) | ||
2039 | 989 | about_dialog.show_all() | ||
2040 | 990 | |||
2041 | 991 | |||
2042 | 992 | class PDF_Doc: | ||
2043 | 993 | """Class handling PDF documents""" | ||
2044 | 994 | |||
2045 | 995 | def __init__(self, filename, nfile, tmp_dir): | ||
2046 | 996 | |||
2047 | 997 | self.filename = os.path.abspath(filename) | ||
2048 | 998 | (self.path, self.shortname) = os.path.split(self.filename) | ||
2049 | 999 | (self.shortname, self.ext) = os.path.splitext(self.shortname) | ||
2050 | 1000 | f = gio.File(filename) | ||
2051 | 1001 | mime_type = f.query_info('standard::content-type').get_content_type() | ||
2052 | 1002 | expected_mime_type = 'application/pdf' | ||
2053 | 1003 | file_prefix = 'file://' | ||
2054 | 1004 | |||
2055 | 1005 | if mime_type == expected_mime_type: | ||
2056 | 1006 | self.nfile = nfile + 1 | ||
2057 | 1007 | self.mtime = os.path.getmtime(filename) | ||
2058 | 1008 | self.copyname = os.path.join(tmp_dir, '%02d_' % self.nfile + | ||
2059 | 1009 | self.shortname + '.pdf') | ||
2060 | 1010 | shutil.copy(self.filename, self.copyname) | ||
2061 | 1011 | self.document = poppler.document_new_from_file (file_prefix + self.copyname, None) | ||
2062 | 1012 | self.npage = self.document.get_n_pages() | ||
2063 | 1013 | else: | ||
2064 | 1014 | self.nfile = 0 | ||
2065 | 1015 | self.npage = 0 | ||
2066 | 1016 | |||
2067 | 1017 | |||
2068 | 1018 | class PDF_Renderer(threading.Thread,gobject.GObject): | ||
2069 | 1019 | |||
2070 | 1020 | def __init__(self, model, pdfqueue): | ||
2071 | 1021 | threading.Thread.__init__(self) | ||
2072 | 1022 | gobject.GObject.__init__(self) | ||
2073 | 1023 | self.model = model | ||
2074 | 1024 | self.pdfqueue = pdfqueue | ||
2075 | 1025 | self.quit = False | ||
2076 | 1026 | |||
2077 | 1027 | def run(self): | ||
2078 | 1028 | for idx, row in enumerate(self.model): | ||
2079 | 1029 | if self.quit: | ||
2080 | 1030 | return | ||
2081 | 1031 | if not row[1].surface: | ||
2082 | 1032 | try: | ||
2083 | 1033 | nfile = row[2] | ||
2084 | 1034 | npage = row[3] | ||
2085 | 1035 | pdfdoc = self.pdfqueue[nfile - 1] | ||
2086 | 1036 | page = pdfdoc.document.get_page(npage-1) | ||
2087 | 1037 | w, h = page.get_size() | ||
2088 | 1038 | thumbnail = CairoImage(int(w), int(h)) | ||
2089 | 1039 | cr = cairo.Context(thumbnail.surface) | ||
2090 | 1040 | page.render(cr) | ||
2091 | 1041 | time.sleep(0.003) | ||
2092 | 1042 | gobject.idle_add(self.emit,'update_thumbnail', idx, thumbnail, | ||
2093 | 1043 | priority=gobject.PRIORITY_LOW) | ||
2094 | 1044 | except Exception,e: | ||
2095 | 1045 | print e | ||
2096 | 1046 | |||
2097 | 1047 | |||
2098 | 1048 | def main(): | ||
2099 | 1049 | """This function starts PdfShuffler""" | ||
2100 | 1050 | gtk.gdk.threads_init() | ||
2101 | 1051 | gobject.threads_init() | ||
2102 | 1052 | PdfShuffler() | ||
2103 | 1053 | gtk.main() | ||
2104 | 1054 | |||
2105 | 1055 | if __name__ == '__main__': | ||
2106 | 1056 | main() | ||
2107 | 1057 | |||
2108 | 0 | 1058 | ||
2109 | === added file '.pc/install_and_look_for_UI_file_in_right_dirs.patch/setup.py' | |||
2110 | --- .pc/install_and_look_for_UI_file_in_right_dirs.patch/setup.py 1970-01-01 00:00:00 +0000 | |||
2111 | +++ .pc/install_and_look_for_UI_file_in_right_dirs.patch/setup.py 2012-02-12 00:35:23 +0000 | |||
2112 | @@ -0,0 +1,66 @@ | |||
2113 | 1 | #!/usr/bin/python | ||
2114 | 2 | |||
2115 | 3 | # | ||
2116 | 4 | # PdfShuffler 0.6.0 - GTK+ based utility for splitting, rearrangement and | ||
2117 | 5 | # modification of PDF documents. | ||
2118 | 6 | # Copyright (C) 2008-2011 Konstantinos Poulios | ||
2119 | 7 | # <https://sourceforge.net/projects/pdfshuffler> | ||
2120 | 8 | # | ||
2121 | 9 | # This program is free software; you can redistribute it and/or modify | ||
2122 | 10 | # it under the terms of the GNU General Public License as published by | ||
2123 | 11 | # the Free Software Foundation; either version 3 of the License, or | ||
2124 | 12 | # (at your option) any later version. | ||
2125 | 13 | # | ||
2126 | 14 | # This program is distributed in the hope that it will be useful, | ||
2127 | 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2128 | 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2129 | 17 | # GNU General Public License for more details. | ||
2130 | 18 | # | ||
2131 | 19 | # You should have received a copy of the GNU General Public License along | ||
2132 | 20 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
2133 | 21 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
2134 | 22 | # | ||
2135 | 23 | |||
2136 | 24 | import os | ||
2137 | 25 | import re | ||
2138 | 26 | from distutils.core import setup | ||
2139 | 27 | |||
2140 | 28 | data_files=[('share/applications/pdfshuffler', ['data/pdfshuffler.ui']), | ||
2141 | 29 | ('share/applications', ['data/pdfshuffler.desktop']), | ||
2142 | 30 | ('share/man/man1', ['doc/pdfshuffler.1']), | ||
2143 | 31 | ('share/pixmaps', ['data/pdfshuffler.svg']), | ||
2144 | 32 | ('share/pixmaps', ['data/pdfshuffler.png']) ] | ||
2145 | 33 | |||
2146 | 34 | |||
2147 | 35 | # Freshly generate .mo from .po, add to data_files: | ||
2148 | 36 | if os.path.isdir('mo/'): | ||
2149 | 37 | os.system ('rm -r mo/') | ||
2150 | 38 | for name in os.listdir('po'): | ||
2151 | 39 | m = re.match(r'(.+)\.po$', name) | ||
2152 | 40 | if m != None: | ||
2153 | 41 | lang = m.group(1) | ||
2154 | 42 | out_dir = 'mo/%s/LC_MESSAGES' % lang | ||
2155 | 43 | out_name = os.path.join(out_dir, 'pdfshuffler.mo') | ||
2156 | 44 | install_dir = 'share/locale/%s/LC_MESSAGES/' % lang | ||
2157 | 45 | os.makedirs(out_dir) | ||
2158 | 46 | os.system('msgfmt -o %s po/%s' % (out_name, name)) | ||
2159 | 47 | data_files.append((install_dir, [out_name])) | ||
2160 | 48 | |||
2161 | 49 | setup(name='pdfshuffler', | ||
2162 | 50 | version='0.6.0', | ||
2163 | 51 | author='Konstantinos Poulios', | ||
2164 | 52 | author_email='logari81 at gmail dot com', | ||
2165 | 53 | description='A simple application for PDF Merging, Rearranging, and Splitting', | ||
2166 | 54 | url = 'https://sourceforge.net/projects/pdfshuffler', | ||
2167 | 55 | license='GNU GPL-3', | ||
2168 | 56 | scripts=['bin/pdfshuffler'], | ||
2169 | 57 | packages=['pdfshuffler'], | ||
2170 | 58 | data_files=data_files | ||
2171 | 59 | ) | ||
2172 | 60 | |||
2173 | 61 | # Clean up temporary files | ||
2174 | 62 | if os.path.isdir('mo/'): | ||
2175 | 63 | os.system ('rm -r mo/') | ||
2176 | 64 | if os.path.isdir('build/'): | ||
2177 | 65 | os.system ('rm -r build/') | ||
2178 | 66 | |||
2179 | 0 | 67 | ||
2180 | === modified file 'TODO' | |||
2181 | --- TODO 2011-02-15 00:43:28 +0000 | |||
2182 | +++ TODO 2012-02-12 00:35:23 +0000 | |||
2183 | @@ -11,5 +11,3 @@ | |||
2184 | 11 | 11 | ||
2185 | 12 | * Add support for encrypted files. | 12 | * Add support for encrypted files. |
2186 | 13 | 13 | ||
2187 | 14 | * Show a progress bar while loading a document. | ||
2188 | 15 | |||
2189 | 16 | 14 | ||
2190 | === added directory 'bin' | |||
2191 | === added file 'bin/pdfshuffler' | |||
2192 | --- bin/pdfshuffler 1970-01-01 00:00:00 +0000 | |||
2193 | +++ bin/pdfshuffler 2012-02-12 00:35:23 +0000 | |||
2194 | @@ -0,0 +1,33 @@ | |||
2195 | 1 | #!/usr/bin/python | ||
2196 | 2 | # -*- coding: utf-8 -*- | ||
2197 | 3 | |||
2198 | 4 | """ | ||
2199 | 5 | |||
2200 | 6 | PdfShuffler 0.6.0 - GTK+ based utility for splitting, rearrangement and | ||
2201 | 7 | modification of PDF documents. | ||
2202 | 8 | Copyright (C) 2008-2011 Konstantinos Poulios | ||
2203 | 9 | <https://sourceforge.net/projects/pdfshuffler> | ||
2204 | 10 | |||
2205 | 11 | This file is part of PdfShuffler. | ||
2206 | 12 | |||
2207 | 13 | PdfShuffler is free software; you can redistribute it and/or modify | ||
2208 | 14 | it under the terms of the GNU General Public License as published by | ||
2209 | 15 | the Free Software Foundation; either version 3 of the License, or | ||
2210 | 16 | (at your option) any later version. | ||
2211 | 17 | |||
2212 | 18 | This program is distributed in the hope that it will be useful, | ||
2213 | 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2214 | 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2215 | 21 | GNU General Public License for more details. | ||
2216 | 22 | |||
2217 | 23 | You should have received a copy of the GNU General Public License along | ||
2218 | 24 | with this program; if not, write to the Free Software Foundation, Inc., | ||
2219 | 25 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
2220 | 26 | |||
2221 | 27 | """ | ||
2222 | 28 | |||
2223 | 29 | try: | ||
2224 | 30 | from pdfshuffler.pdfshuffler import main | ||
2225 | 31 | main() | ||
2226 | 32 | except ImportError: | ||
2227 | 33 | print('Error: could not import pdfshuffler') | ||
2228 | 0 | 34 | ||
2229 | === added file 'data/pdfshuffler.ui' | |||
2230 | --- data/pdfshuffler.ui 1970-01-01 00:00:00 +0000 | |||
2231 | +++ data/pdfshuffler.ui 2012-02-12 00:35:23 +0000 | |||
2232 | @@ -0,0 +1,359 @@ | |||
2233 | 1 | <?xml version="1.0"?> | ||
2234 | 2 | <interface> | ||
2235 | 3 | <requires lib="gtk+" version="2.16"/> | ||
2236 | 4 | <!-- interface-naming-policy project-wide --> | ||
2237 | 5 | <object class="GtkWindow" id="main_window"> | ||
2238 | 6 | <child> | ||
2239 | 7 | <object class="GtkVBox" id="main_vbox"> | ||
2240 | 8 | <property name="visible">True</property> | ||
2241 | 9 | <child> | ||
2242 | 10 | <object class="GtkMenuBar" id="menubar"> | ||
2243 | 11 | <property name="visible">True</property> | ||
2244 | 12 | <child> | ||
2245 | 13 | <object class="GtkMenuItem" id="menuitem_file"> | ||
2246 | 14 | <property name="visible">True</property> | ||
2247 | 15 | <property name="label" translatable="yes">_File</property> | ||
2248 | 16 | <property name="use_underline">True</property> | ||
2249 | 17 | <child type="submenu"> | ||
2250 | 18 | <object class="GtkMenu" id="menu_file"> | ||
2251 | 19 | <property name="visible">True</property> | ||
2252 | 20 | <child> | ||
2253 | 21 | <object class="GtkImageMenuItem" id="imagemenuitem_import"> | ||
2254 | 22 | <property name="label">gtk-open</property> | ||
2255 | 23 | <property name="visible">True</property> | ||
2256 | 24 | <property name="use_underline">True</property> | ||
2257 | 25 | <property name="use_stock">True</property> | ||
2258 | 26 | <signal name="activate" handler="on_action_add_doc_activate"/> | ||
2259 | 27 | </object> | ||
2260 | 28 | </child> | ||
2261 | 29 | <child> | ||
2262 | 30 | <object class="GtkImageMenuItem" id="imagemenuitem_save"> | ||
2263 | 31 | <property name="label">gtk-save</property> | ||
2264 | 32 | <property name="visible">True</property> | ||
2265 | 33 | <property name="sensitive">False</property> | ||
2266 | 34 | <property name="use_underline">True</property> | ||
2267 | 35 | <property name="use_stock">True</property> | ||
2268 | 36 | </object> | ||
2269 | 37 | </child> | ||
2270 | 38 | <child> | ||
2271 | 39 | <object class="GtkImageMenuItem" id="imagemenuitem_save_as"> | ||
2272 | 40 | <property name="label">gtk-save-as</property> | ||
2273 | 41 | <property name="visible">True</property> | ||
2274 | 42 | <property name="use_underline">True</property> | ||
2275 | 43 | <property name="use_stock">True</property> | ||
2276 | 44 | <signal name="activate" handler="choose_export_pdf_name"/> | ||
2277 | 45 | </object> | ||
2278 | 46 | </child> | ||
2279 | 47 | <child> | ||
2280 | 48 | <object class="GtkSeparatorMenuItem" id="separatormenuitem_file"> | ||
2281 | 49 | <property name="visible">True</property> | ||
2282 | 50 | </object> | ||
2283 | 51 | </child> | ||
2284 | 52 | <child> | ||
2285 | 53 | <object class="GtkImageMenuItem" id="imagemenuitem_quit"> | ||
2286 | 54 | <property name="label">gtk-quit</property> | ||
2287 | 55 | <property name="visible">True</property> | ||
2288 | 56 | <property name="use_underline">True</property> | ||
2289 | 57 | <property name="use_stock">True</property> | ||
2290 | 58 | <signal name="activate" handler="close_application"/> | ||
2291 | 59 | </object> | ||
2292 | 60 | </child> | ||
2293 | 61 | </object> | ||
2294 | 62 | </child> | ||
2295 | 63 | </object> | ||
2296 | 64 | </child> | ||
2297 | 65 | <child> | ||
2298 | 66 | <object class="GtkMenuItem" id="menuitem_edit"> | ||
2299 | 67 | <property name="visible">True</property> | ||
2300 | 68 | <property name="label" translatable="yes">_Edit</property> | ||
2301 | 69 | <property name="use_underline">True</property> | ||
2302 | 70 | <child type="submenu"> | ||
2303 | 71 | <object class="GtkMenu" id="menu_edit"> | ||
2304 | 72 | <property name="visible">True</property> | ||
2305 | 73 | <child> | ||
2306 | 74 | <object class="GtkImageMenuItem" id="imagemenuitem_undo"> | ||
2307 | 75 | <property name="label">gtk-undo</property> | ||
2308 | 76 | <property name="sensitive">False</property> | ||
2309 | 77 | <property name="use_underline">True</property> | ||
2310 | 78 | <property name="use_stock">True</property> | ||
2311 | 79 | </object> | ||
2312 | 80 | </child> | ||
2313 | 81 | <child> | ||
2314 | 82 | <object class="GtkImageMenuItem" id="imagemenuitem_redo"> | ||
2315 | 83 | <property name="label">gtk-redo</property> | ||
2316 | 84 | <property name="sensitive">False</property> | ||
2317 | 85 | <property name="use_underline">True</property> | ||
2318 | 86 | <property name="use_stock">True</property> | ||
2319 | 87 | </object> | ||
2320 | 88 | </child> | ||
2321 | 89 | <child> | ||
2322 | 90 | <object class="GtkSeparatorMenuItem" id="separatormenuitem_edit1"> | ||
2323 | 91 | <property name="sensitive">False</property> | ||
2324 | 92 | </object> | ||
2325 | 93 | </child> | ||
2326 | 94 | <child> | ||
2327 | 95 | <object class="GtkImageMenuItem" id="imagemenuitem_rotate_left"> | ||
2328 | 96 | <property name="label" translatable="yes">Rotate left</property> | ||
2329 | 97 | <property name="visible">True</property> | ||
2330 | 98 | <property name="use_stock">False</property> | ||
2331 | 99 | <signal name="activate" handler="rotate_page_left"/> | ||
2332 | 100 | </object> | ||
2333 | 101 | </child> | ||
2334 | 102 | <child> | ||
2335 | 103 | <object class="GtkImageMenuItem" id="imagemenuitem_rotate_right"> | ||
2336 | 104 | <property name="label" translatable="yes">Rotate right</property> | ||
2337 | 105 | <property name="visible">True</property> | ||
2338 | 106 | <property name="use_stock">False</property> | ||
2339 | 107 | <signal name="activate" handler="rotate_page_right"/> | ||
2340 | 108 | </object> | ||
2341 | 109 | </child> | ||
2342 | 110 | <child> | ||
2343 | 111 | <object class="GtkSeparatorMenuItem" id="separatormenuitem_edit2"> | ||
2344 | 112 | <property name="visible">True</property> | ||
2345 | 113 | </object> | ||
2346 | 114 | </child> | ||
2347 | 115 | <child> | ||
2348 | 116 | <object class="GtkImageMenuItem" id="imagemenuitem_crop"> | ||
2349 | 117 | <property name="label" translatable="yes">Crop</property> | ||
2350 | 118 | <property name="visible">True</property> | ||
2351 | 119 | <property name="use_stock">False</property> | ||
2352 | 120 | <signal name="activate" handler="crop_page_dialog"/> | ||
2353 | 121 | </object> | ||
2354 | 122 | </child> | ||
2355 | 123 | <child> | ||
2356 | 124 | <object class="GtkImageMenuItem" id="imagemenuitem_delete"> | ||
2357 | 125 | <property name="label">gtk-delete</property> | ||
2358 | 126 | <property name="visible">True</property> | ||
2359 | 127 | <property name="use_underline">True</property> | ||
2360 | 128 | <property name="use_stock">True</property> | ||
2361 | 129 | <signal name="activate" handler="clear_selected"/> | ||
2362 | 130 | </object> | ||
2363 | 131 | </child> | ||
2364 | 132 | </object> | ||
2365 | 133 | </child> | ||
2366 | 134 | </object> | ||
2367 | 135 | </child> | ||
2368 | 136 | <child> | ||
2369 | 137 | <object class="GtkMenuItem" id="menuitem_view"> | ||
2370 | 138 | <property name="visible">True</property> | ||
2371 | 139 | <property name="label" translatable="yes">_View</property> | ||
2372 | 140 | <property name="use_underline">True</property> | ||
2373 | 141 | <child type="submenu"> | ||
2374 | 142 | <object class="GtkMenu" id="menu_view"> | ||
2375 | 143 | <property name="visible">True</property> | ||
2376 | 144 | <child> | ||
2377 | 145 | <object class="GtkImageMenuItem" id="imagemenuitem_zoomin"> | ||
2378 | 146 | <property name="label">Zoom in</property> | ||
2379 | 147 | <property name="visible">True</property> | ||
2380 | 148 | <property name="use_underline">True</property> | ||
2381 | 149 | <property name="use_stock">True</property> | ||
2382 | 150 | <signal name="activate" handler="zoom_in"/> | ||
2383 | 151 | </object> | ||
2384 | 152 | </child> | ||
2385 | 153 | <child> | ||
2386 | 154 | <object class="GtkImageMenuItem" id="imagemenuitem_zoomout"> | ||
2387 | 155 | <property name="label">Zoom out</property> | ||
2388 | 156 | <property name="visible">True</property> | ||
2389 | 157 | <property name="use_underline">True</property> | ||
2390 | 158 | <property name="use_stock">True</property> | ||
2391 | 159 | <signal name="activate" handler="zoom_out"/> | ||
2392 | 160 | </object> | ||
2393 | 161 | </child> | ||
2394 | 162 | </object> | ||
2395 | 163 | </child> | ||
2396 | 164 | </object> | ||
2397 | 165 | </child> | ||
2398 | 166 | <child> | ||
2399 | 167 | <object class="GtkMenuItem" id="menuitem_help"> | ||
2400 | 168 | <property name="visible">True</property> | ||
2401 | 169 | <property name="label" translatable="yes">_Help</property> | ||
2402 | 170 | <property name="use_underline">True</property> | ||
2403 | 171 | <child type="submenu"> | ||
2404 | 172 | <object class="GtkMenu" id="menu_help"> | ||
2405 | 173 | <property name="visible">True</property> | ||
2406 | 174 | <child> | ||
2407 | 175 | <object class="GtkImageMenuItem" id="imagemenuitem_about"> | ||
2408 | 176 | <property name="label">gtk-about</property> | ||
2409 | 177 | <property name="visible">True</property> | ||
2410 | 178 | <property name="use_underline">True</property> | ||
2411 | 179 | <property name="use_stock">True</property> | ||
2412 | 180 | <signal name="activate" handler="about_dialog"/> | ||
2413 | 181 | </object> | ||
2414 | 182 | </child> | ||
2415 | 183 | </object> | ||
2416 | 184 | </child> | ||
2417 | 185 | </object> | ||
2418 | 186 | </child> | ||
2419 | 187 | </object> | ||
2420 | 188 | <packing> | ||
2421 | 189 | <property name="expand">False</property> | ||
2422 | 190 | <property name="position">0</property> | ||
2423 | 191 | </packing> | ||
2424 | 192 | </child> | ||
2425 | 193 | <child> | ||
2426 | 194 | <object class="GtkHBox" id="hbox_toolbar"> | ||
2427 | 195 | <property name="visible">True</property> | ||
2428 | 196 | <child> | ||
2429 | 197 | <object class="GtkToolbar" id="toolbar"> | ||
2430 | 198 | <property name="visible">True</property> | ||
2431 | 199 | <child> | ||
2432 | 200 | <object class="GtkToolButton" id="toolbutton_open"> | ||
2433 | 201 | <property name="visible">True</property> | ||
2434 | 202 | <property name="label" translatable="yes">Open</property> | ||
2435 | 203 | <property name="use_underline">True</property> | ||
2436 | 204 | <property name="stock_id">gtk-open</property> | ||
2437 | 205 | <signal name="clicked" handler="on_action_add_doc_activate"/> | ||
2438 | 206 | </object> | ||
2439 | 207 | <packing> | ||
2440 | 208 | <property name="expand">False</property> | ||
2441 | 209 | <property name="homogeneous">True</property> | ||
2442 | 210 | </packing> | ||
2443 | 211 | </child> | ||
2444 | 212 | <child> | ||
2445 | 213 | <object class="GtkToolButton" id="toolbutton_save"> | ||
2446 | 214 | <property name="visible">True</property> | ||
2447 | 215 | <property name="label" translatable="yes">Save</property> | ||
2448 | 216 | <property name="use_underline">True</property> | ||
2449 | 217 | <property name="stock_id">gtk-save</property> | ||
2450 | 218 | </object> | ||
2451 | 219 | <packing> | ||
2452 | 220 | <property name="expand">False</property> | ||
2453 | 221 | <property name="homogeneous">True</property> | ||
2454 | 222 | </packing> | ||
2455 | 223 | </child> | ||
2456 | 224 | <child> | ||
2457 | 225 | <object class="GtkToolButton" id="toolbutton_save_as"> | ||
2458 | 226 | <property name="visible">True</property> | ||
2459 | 227 | <property name="label" translatable="yes">Save as</property> | ||
2460 | 228 | <property name="use_underline">True</property> | ||
2461 | 229 | <property name="stock_id">gtk-save-as</property> | ||
2462 | 230 | <signal name="clicked" handler="choose_export_pdf_name"/> | ||
2463 | 231 | </object> | ||
2464 | 232 | <packing> | ||
2465 | 233 | <property name="expand">False</property> | ||
2466 | 234 | <property name="homogeneous">True</property> | ||
2467 | 235 | </packing> | ||
2468 | 236 | </child> | ||
2469 | 237 | <child> | ||
2470 | 238 | <object class="GtkSeparatorToolItem" id="toolbutton_separator1"> | ||
2471 | 239 | <property name="visible">True</property> | ||
2472 | 240 | </object> | ||
2473 | 241 | <packing> | ||
2474 | 242 | <property name="expand">False</property> | ||
2475 | 243 | <property name="homogeneous">True</property> | ||
2476 | 244 | </packing> | ||
2477 | 245 | </child> | ||
2478 | 246 | <child> | ||
2479 | 247 | <object class="GtkToolButton" id="toolbutton_zoom_in"> | ||
2480 | 248 | <property name="visible">True</property> | ||
2481 | 249 | <property name="label" translatable="yes">Zoom in</property> | ||
2482 | 250 | <property name="use_underline">True</property> | ||
2483 | 251 | <property name="stock_id">gtk-zoom-in</property> | ||
2484 | 252 | <signal name="clicked" handler="zoom_in"/> | ||
2485 | 253 | </object> | ||
2486 | 254 | <packing> | ||
2487 | 255 | <property name="expand">False</property> | ||
2488 | 256 | <property name="homogeneous">True</property> | ||
2489 | 257 | </packing> | ||
2490 | 258 | </child> | ||
2491 | 259 | <child> | ||
2492 | 260 | <object class="GtkToolButton" id="toolbutton_zoom_out"> | ||
2493 | 261 | <property name="visible">True</property> | ||
2494 | 262 | <property name="label" translatable="yes">Zoom out</property> | ||
2495 | 263 | <property name="use_underline">True</property> | ||
2496 | 264 | <property name="stock_id">gtk-zoom-out</property> | ||
2497 | 265 | <signal name="clicked" handler="zoom_out"/> | ||
2498 | 266 | </object> | ||
2499 | 267 | <packing> | ||
2500 | 268 | <property name="expand">False</property> | ||
2501 | 269 | <property name="homogeneous">True</property> | ||
2502 | 270 | </packing> | ||
2503 | 271 | </child> | ||
2504 | 272 | <child> | ||
2505 | 273 | <object class="GtkSeparatorToolItem" id="toolbutton_separator2"> | ||
2506 | 274 | <property name="visible">True</property> | ||
2507 | 275 | </object> | ||
2508 | 276 | <packing> | ||
2509 | 277 | <property name="expand">False</property> | ||
2510 | 278 | <property name="homogeneous">True</property> | ||
2511 | 279 | </packing> | ||
2512 | 280 | </child> | ||
2513 | 281 | <child> | ||
2514 | 282 | <object class="GtkToolButton" id="toolbutton_rotate_left"> | ||
2515 | 283 | <property name="visible">True</property> | ||
2516 | 284 | <property name="label" translatable="yes">Rotate left</property> | ||
2517 | 285 | <property name="use_underline">True</property> | ||
2518 | 286 | <property name="icon_name">object-rotate-left</property> | ||
2519 | 287 | <signal name="clicked" handler="rotate_page_left"/> | ||
2520 | 288 | </object> | ||
2521 | 289 | <packing> | ||
2522 | 290 | <property name="expand">False</property> | ||
2523 | 291 | <property name="homogeneous">True</property> | ||
2524 | 292 | </packing> | ||
2525 | 293 | </child> | ||
2526 | 294 | <child> | ||
2527 | 295 | <object class="GtkToolButton" id="toolbutton_rotate_right"> | ||
2528 | 296 | <property name="visible">True</property> | ||
2529 | 297 | <property name="label" translatable="yes">Rotate right</property> | ||
2530 | 298 | <property name="use_underline">True</property> | ||
2531 | 299 | <property name="icon_name">object-rotate-right</property> | ||
2532 | 300 | <signal name="clicked" handler="rotate_page_right"/> | ||
2533 | 301 | </object> | ||
2534 | 302 | <packing> | ||
2535 | 303 | <property name="expand">False</property> | ||
2536 | 304 | <property name="homogeneous">True</property> | ||
2537 | 305 | </packing> | ||
2538 | 306 | </child> | ||
2539 | 307 | <child> | ||
2540 | 308 | <object class="GtkToolButton" id="toolbutton_delete"> | ||
2541 | 309 | <property name="visible">True</property> | ||
2542 | 310 | <property name="label" translatable="yes">Delete</property> | ||
2543 | 311 | <property name="use_underline">True</property> | ||
2544 | 312 | <property name="stock_id">gtk-delete</property> | ||
2545 | 313 | <signal name="clicked" handler="clear_selected"/> | ||
2546 | 314 | </object> | ||
2547 | 315 | <packing> | ||
2548 | 316 | <property name="expand">False</property> | ||
2549 | 317 | <property name="homogeneous">True</property> | ||
2550 | 318 | </packing> | ||
2551 | 319 | </child> | ||
2552 | 320 | </object> | ||
2553 | 321 | <packing> | ||
2554 | 322 | <property name="position">0</property> | ||
2555 | 323 | </packing> | ||
2556 | 324 | </child> | ||
2557 | 325 | </object> | ||
2558 | 326 | <packing> | ||
2559 | 327 | <property name="expand">False</property> | ||
2560 | 328 | <property name="position">1</property> | ||
2561 | 329 | </packing> | ||
2562 | 330 | </child> | ||
2563 | 331 | <child> | ||
2564 | 332 | <object class="GtkScrolledWindow" id="scrolledwindow"> | ||
2565 | 333 | <property name="visible">True</property> | ||
2566 | 334 | <property name="can_focus">True</property> | ||
2567 | 335 | <property name="hscrollbar_policy">never</property> | ||
2568 | 336 | <property name="vscrollbar_policy">automatic</property> | ||
2569 | 337 | <child> | ||
2570 | 338 | <placeholder/> | ||
2571 | 339 | </child> | ||
2572 | 340 | </object> | ||
2573 | 341 | <packing> | ||
2574 | 342 | <property name="position">2</property> | ||
2575 | 343 | </packing> | ||
2576 | 344 | </child> | ||
2577 | 345 | <child> | ||
2578 | 346 | <object class="GtkProgressBar" id="progressbar"> | ||
2579 | 347 | <property name="height_request">20</property> | ||
2580 | 348 | <property name="visible">True</property> | ||
2581 | 349 | </object> | ||
2582 | 350 | <packing> | ||
2583 | 351 | <property name="expand">False</property> | ||
2584 | 352 | <property name="padding">5</property> | ||
2585 | 353 | <property name="position">3</property> | ||
2586 | 354 | </packing> | ||
2587 | 355 | </child> | ||
2588 | 356 | </object> | ||
2589 | 357 | </child> | ||
2590 | 358 | </object> | ||
2591 | 359 | </interface> | ||
2592 | 0 | 360 | ||
2593 | === modified file 'debian/changelog' | |||
2594 | --- debian/changelog 2011-12-31 02:06:27 +0000 | |||
2595 | +++ debian/changelog 2012-02-12 00:35:23 +0000 | |||
2596 | @@ -1,3 +1,14 @@ | |||
2597 | 1 | pdfshuffler (0.5.99~svn64-0ubuntu1) UNRELEASED; urgency=low | ||
2598 | 2 | |||
2599 | 3 | * New upstream snapshot. (LP: #930819) | ||
2600 | 4 | * This version fixes a annoying bug due to the new version of poppler | ||
2601 | 5 | * debian/patches: | ||
2602 | 6 | - Removed the previous patch (fix-shebang: now in upstream) | ||
2603 | 7 | - Added a patch to install pdfshuffler in /usr/share/data as required | ||
2604 | 8 | by pdfshuffler.py | ||
2605 | 9 | |||
2606 | 10 | -- Matthieu Baerts (matttbe) <matttbe@gmail.com> Sun, 12 Feb 2012 01:27:07 +0100 | ||
2607 | 11 | |||
2608 | 1 | pdfshuffler (0.5.1-2build1) precise; urgency=low | 12 | pdfshuffler (0.5.1-2build1) precise; urgency=low |
2609 | 2 | 13 | ||
2610 | 3 | * Rebuild to drop python2.6 dependencies. | 14 | * Rebuild to drop python2.6 dependencies. |
2611 | 4 | 15 | ||
2612 | === removed file 'debian/patches/fix-shebang' | |||
2613 | --- debian/patches/fix-shebang 2010-01-24 17:04:08 +0000 | |||
2614 | +++ debian/patches/fix-shebang 1970-01-01 00:00:00 +0000 | |||
2615 | @@ -1,12 +0,0 @@ | |||
2616 | 1 | # Description: fix shebang | ||
2617 | 2 | # Forward: not applicable | ||
2618 | 3 | # Author: Serafeim Zanikolas <serzan@hellug.gr> | ||
2619 | 4 | # Last-Update: 2010-01-24 | ||
2620 | 5 | --- pdfshuffler-0.3.1.orig/pdfshuffler 2009-02-08 20:59:26.000000000 +0000 | ||
2621 | 6 | +++ pdfshuffler-0.3.1/pdfshuffler 2009-02-08 20:59:27.000000000 +0000 | ||
2622 | 7 | @@ -1,4 +1,4 @@ | ||
2623 | 8 | -#!/usr/bin/env python | ||
2624 | 9 | +#!/usr/bin/python | ||
2625 | 10 | # -*- coding: utf-8 -*- | ||
2626 | 11 | |||
2627 | 12 | """ | ||
2628 | 13 | 0 | ||
2629 | === added file 'debian/patches/install_and_look_for_UI_file_in_right_dirs.patch' | |||
2630 | --- debian/patches/install_and_look_for_UI_file_in_right_dirs.patch 1970-01-01 00:00:00 +0000 | |||
2631 | +++ debian/patches/install_and_look_for_UI_file_in_right_dirs.patch 2012-02-12 00:35:23 +0000 | |||
2632 | @@ -0,0 +1,31 @@ | |||
2633 | 1 | # Description: Install and look for the UI file in the right dirs | ||
2634 | 2 | # Forward: yes | ||
2635 | 3 | # Author: Matthieu Baerts (matttbe) <matttbe@gmail.com> | ||
2636 | 4 | # Last-Update: 2012-02-12 | ||
2637 | 5 | |||
2638 | 6 | Index: pdfshuffler/pdfshuffler/pdfshuffler.py | ||
2639 | 7 | =================================================================== | ||
2640 | 8 | --- pdfshuffler.orig/pdfshuffler/pdfshuffler.py 2012-02-12 01:15:27.115323000 +0100 | ||
2641 | 9 | +++ pdfshuffler/pdfshuffler/pdfshuffler.py 2012-02-12 01:24:18.630058194 +0100 | ||
2642 | 10 | @@ -112,7 +112,7 @@ | ||
2643 | 11 | # Import the user interface file, trying different possible locations | ||
2644 | 12 | ui_path = '/usr/share/pdfshuffler/pdfshuffler.ui' | ||
2645 | 13 | if not os.path.exists(ui_path): | ||
2646 | 14 | - ui_path = '/usr/local/share/applications/pdfshuffler/pdfshuffler.ui' | ||
2647 | 15 | + ui_path = '/usr/local/share/pdfshuffler/pdfshuffler.ui' | ||
2648 | 16 | if not os.path.exists(ui_path): | ||
2649 | 17 | parent_dir = os.path.dirname( \ | ||
2650 | 18 | os.path.dirname(os.path.realpath(__file__))) | ||
2651 | 19 | Index: pdfshuffler/setup.py | ||
2652 | 20 | =================================================================== | ||
2653 | 21 | --- pdfshuffler.orig/setup.py 2012-02-12 01:15:27.115323000 +0100 | ||
2654 | 22 | +++ pdfshuffler/setup.py 2012-02-12 01:24:18.630058194 +0100 | ||
2655 | 23 | @@ -25,7 +25,7 @@ | ||
2656 | 24 | import re | ||
2657 | 25 | from distutils.core import setup | ||
2658 | 26 | |||
2659 | 27 | -data_files=[('share/applications/pdfshuffler', ['data/pdfshuffler.ui']), | ||
2660 | 28 | +data_files=[('share/pdfshuffler', ['data/pdfshuffler.ui']), | ||
2661 | 29 | ('share/applications', ['data/pdfshuffler.desktop']), | ||
2662 | 30 | ('share/man/man1', ['doc/pdfshuffler.1']), | ||
2663 | 31 | ('share/pixmaps', ['data/pdfshuffler.svg']), | ||
2664 | 0 | 32 | ||
2665 | === modified file 'debian/patches/series' | |||
2666 | --- debian/patches/series 2009-05-16 23:26:32 +0000 | |||
2667 | +++ debian/patches/series 2012-02-12 00:35:23 +0000 | |||
2668 | @@ -1,1 +1,1 @@ | |||
2670 | 1 | fix-shebang | 1 | install_and_look_for_UI_file_in_right_dirs.patch |
2671 | 2 | 2 | ||
2672 | === modified file 'doc/pdfshuffler.1' | |||
2673 | --- doc/pdfshuffler.1 2011-02-15 00:43:28 +0000 | |||
2674 | +++ doc/pdfshuffler.1 2012-02-12 00:35:23 +0000 | |||
2675 | @@ -1,4 +1,4 @@ | |||
2677 | 1 | .TH PDFSHUFFLER 1 "December 2009" "version 0.5.1" "User Manuals" | 1 | .TH PDFSHUFFLER 1 "December 2011" "version 0.6" "User Manuals" |
2678 | 2 | .SH "NAME" | 2 | .SH "NAME" |
2679 | 3 | PDF-Shuffler \- Application for PDF Merging, Rearranging, and Splitting | 3 | PDF-Shuffler \- Application for PDF Merging, Rearranging, and Splitting |
2680 | 4 | .SH "SYNOPSIS" | 4 | .SH "SYNOPSIS" |
2681 | @@ -19,5 +19,5 @@ | |||
2682 | 19 | .SH "DIAGNOSTICS" | 19 | .SH "DIAGNOSTICS" |
2683 | 20 | Common python and gtk diagnostics. | 20 | Common python and gtk diagnostics. |
2684 | 21 | .SH "AUTHOR" | 21 | .SH "AUTHOR" |
2686 | 22 | Konstantinos Poulios <poulios.konstantinos@gmail.com> | 22 | Konstantinos Poulios <logari81@gmail.com> |
2687 | 23 | 23 | ||
2688 | 24 | 24 | ||
2689 | === modified file 'pdfshuffler' | |||
2690 | --- pdfshuffler 2011-02-15 00:43:28 +0000 | |||
2691 | +++ pdfshuffler 1970-01-01 00:00:00 +0000 | |||
2692 | @@ -1,1031 +0,0 @@ | |||
2693 | 1 | #!/usr/bin/python | ||
2694 | 2 | # -*- coding: utf-8 -*- | ||
2695 | 3 | |||
2696 | 4 | """ | ||
2697 | 5 | -------------------------------------------------------------------------- | ||
2698 | 6 | |||
2699 | 7 | PDF-Shuffler 0.5.1 - pyGTK PDF Merging, Rearranging, and Splitting | ||
2700 | 8 | Copyright (C) 2008-2010 Konstantinos Poulios | ||
2701 | 9 | <https://sourceforge.net/projects/pdfshuffler> | ||
2702 | 10 | |||
2703 | 11 | -------------------------------------------------------------------------- | ||
2704 | 12 | |||
2705 | 13 | This program is free software; you can redistribute it and/or modify | ||
2706 | 14 | it under the terms of the GNU General Public License as published by | ||
2707 | 15 | the Free Software Foundation; either version 3 of the License, or | ||
2708 | 16 | (at your option) any later version. | ||
2709 | 17 | |||
2710 | 18 | This program is distributed in the hope that it will be useful, | ||
2711 | 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2712 | 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2713 | 21 | GNU General Public License for more details. | ||
2714 | 22 | |||
2715 | 23 | You should have received a copy of the GNU General Public License along | ||
2716 | 24 | with this program; if not, write to the Free Software Foundation, Inc., | ||
2717 | 25 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
2718 | 26 | |||
2719 | 27 | -------------------------------------------------------------------------- | ||
2720 | 28 | """ | ||
2721 | 29 | |||
2722 | 30 | import os | ||
2723 | 31 | import shutil #needed for file operations like whole directory deletion | ||
2724 | 32 | import sys #needed for proccessing of command line args | ||
2725 | 33 | import urllib #needed to parse filename information passed by DnD | ||
2726 | 34 | import threading | ||
2727 | 35 | import tempfile | ||
2728 | 36 | from copy import copy | ||
2729 | 37 | |||
2730 | 38 | import locale #for multilanguage support | ||
2731 | 39 | import gettext | ||
2732 | 40 | gettext.install('pdfshuffler', unicode=1) | ||
2733 | 41 | |||
2734 | 42 | try: | ||
2735 | 43 | import pygtk | ||
2736 | 44 | pygtk.require('2.0') | ||
2737 | 45 | import gtk | ||
2738 | 46 | assert gtk.gtk_version >= (2, 10, 0) | ||
2739 | 47 | assert gtk.pygtk_version >= (2, 10, 0) | ||
2740 | 48 | except AssertionError: | ||
2741 | 49 | print('You do not have the required versions of GTK+ and/or PyGTK ' + | ||
2742 | 50 | 'installed.\n\n' + | ||
2743 | 51 | 'Installed GTK+ version is ' + | ||
2744 | 52 | '.'.join([str(n) for n in gtk.gtk_version]) + '\n' + | ||
2745 | 53 | 'Required GTK+ version is 2.10.0 or higher\n\n' | ||
2746 | 54 | 'Installed PyGTK version is ' + | ||
2747 | 55 | '.'.join([str(n) for n in gtk.pygtk_version]) + '\n' + | ||
2748 | 56 | 'Required PyGTK version is 2.10.0 or higher') | ||
2749 | 57 | sys.exit(1) | ||
2750 | 58 | except: | ||
2751 | 59 | print('PyGTK version 2.10.0 or higher is required to run this program.') | ||
2752 | 60 | print('No version of PyGTK was found on your system.') | ||
2753 | 61 | sys.exit(1) | ||
2754 | 62 | |||
2755 | 63 | gtk.gdk.threads_init() | ||
2756 | 64 | import gobject #to use custom signals | ||
2757 | 65 | import pango #to adjust the text alignment in CellRendererText | ||
2758 | 66 | import gio #to inquire mime types information | ||
2759 | 67 | |||
2760 | 68 | import poppler #for the rendering of pdf pages | ||
2761 | 69 | from pyPdf import PdfFileWriter, PdfFileReader | ||
2762 | 70 | |||
2763 | 71 | class PDFshuffler: | ||
2764 | 72 | # ======================================================= | ||
2765 | 73 | # All the preferences are stored here. | ||
2766 | 74 | # ======================================================= | ||
2767 | 75 | prefs = { | ||
2768 | 76 | 'window width': min (700, gtk.gdk.screen_get_default().get_width() / 2 ), | ||
2769 | 77 | 'window height': min(600, gtk.gdk.screen_get_default().get_height() - 50 ), | ||
2770 | 78 | 'window x': 0, | ||
2771 | 79 | 'window y': 0, | ||
2772 | 80 | 'initial thumbnail size': 530, | ||
2773 | 81 | 'initial zoom scale': 0.25, | ||
2774 | 82 | } | ||
2775 | 83 | |||
2776 | 84 | MODEL_ROW_INTERN = 1001 | ||
2777 | 85 | MODEL_ROW_EXTERN = 1002 | ||
2778 | 86 | TEXT_URI_LIST = 1003 | ||
2779 | 87 | MODEL_ROW_MOTION = 1004 | ||
2780 | 88 | TARGETS_IV = [('MODEL_ROW_INTERN', gtk.TARGET_SAME_WIDGET, MODEL_ROW_INTERN), | ||
2781 | 89 | ('MODEL_ROW_EXTERN', gtk.TARGET_OTHER_APP, MODEL_ROW_EXTERN), | ||
2782 | 90 | ('MODEL_ROW_MOTION', 0, MODEL_ROW_MOTION)] | ||
2783 | 91 | TARGETS_SW = [('text/uri-list', 0, TEXT_URI_LIST), | ||
2784 | 92 | ('MODEL_ROW_EXTERN', gtk.TARGET_OTHER_APP, MODEL_ROW_EXTERN)] | ||
2785 | 93 | |||
2786 | 94 | def __init__(self): | ||
2787 | 95 | # Create the temporary directory | ||
2788 | 96 | self.tmp_dir = tempfile.mkdtemp("pdfshuffler") | ||
2789 | 97 | os.chmod(self.tmp_dir, 0700) | ||
2790 | 98 | |||
2791 | 99 | pixmap = os.path.join(sys.prefix,'share','pixmaps','pdfshuffler.png') | ||
2792 | 100 | try: | ||
2793 | 101 | gtk.window_set_default_icon_from_file(pixmap) | ||
2794 | 102 | except: | ||
2795 | 103 | print(_('File %s does not exist') % pixmap) | ||
2796 | 104 | |||
2797 | 105 | # Create the main window, and attach delete_event signal to terminating | ||
2798 | 106 | # the application | ||
2799 | 107 | self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) | ||
2800 | 108 | self.window.set_title('PDF-Shuffler') | ||
2801 | 109 | self.window.set_border_width(0) | ||
2802 | 110 | self.window.move(self.prefs['window x'], self.prefs['window y']) | ||
2803 | 111 | self.window.set_size_request(self.prefs['window width'], | ||
2804 | 112 | self.prefs['window height']) | ||
2805 | 113 | self.window.connect('delete_event', self.close_application) | ||
2806 | 114 | self.window.show() | ||
2807 | 115 | |||
2808 | 116 | # Create a vbox to hold the thumnails-container | ||
2809 | 117 | vbox = gtk.VBox() | ||
2810 | 118 | self.window.add(vbox) | ||
2811 | 119 | |||
2812 | 120 | # Create a scrolled window to hold the thumbnails-container | ||
2813 | 121 | self.sw = gtk.ScrolledWindow() | ||
2814 | 122 | self.sw.set_border_width(0) | ||
2815 | 123 | self.sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) | ||
2816 | 124 | self.sw.drag_dest_set(gtk.DEST_DEFAULT_MOTION | | ||
2817 | 125 | gtk.DEST_DEFAULT_HIGHLIGHT | | ||
2818 | 126 | gtk.DEST_DEFAULT_DROP | | ||
2819 | 127 | gtk.DEST_DEFAULT_MOTION, | ||
2820 | 128 | self.TARGETS_SW, | ||
2821 | 129 | gtk.gdk.ACTION_COPY | | ||
2822 | 130 | gtk.gdk.ACTION_MOVE ) | ||
2823 | 131 | self.sw.connect('drag_data_received', self.sw_dnd_received_data) | ||
2824 | 132 | self.sw.connect('button_press_event', self.sw_button_press_event) | ||
2825 | 133 | vbox.pack_start(self.sw) | ||
2826 | 134 | |||
2827 | 135 | # Create an alignment to keep the thumbnails center-aligned | ||
2828 | 136 | align = gtk.Alignment(0.5, 0.5, 0, 0) | ||
2829 | 137 | self.sw.add_with_viewport(align) | ||
2830 | 138 | |||
2831 | 139 | # Create ListStore model and IconView | ||
2832 | 140 | self.model = gtk.ListStore(str, # 0.Text descriptor | ||
2833 | 141 | gtk.gdk.Pixbuf, # 1.Thumbnail image | ||
2834 | 142 | int, # 2.Document number | ||
2835 | 143 | int, # 3.Page number | ||
2836 | 144 | int, # 4.Thumbnail width | ||
2837 | 145 | str, # 5.Document filename | ||
2838 | 146 | bool, # 6.Rendered | ||
2839 | 147 | int, # 7.Rotation angle | ||
2840 | 148 | float, # 8.Crop left | ||
2841 | 149 | float, # 9.Crop right | ||
2842 | 150 | float, # 10.Crop top | ||
2843 | 151 | float) # 11.Crop bottom | ||
2844 | 152 | |||
2845 | 153 | self.zoom_scale = self.prefs['initial zoom scale'] | ||
2846 | 154 | self.iv_col_width = self.prefs['initial thumbnail size'] | ||
2847 | 155 | |||
2848 | 156 | self.iconview = gtk.IconView(self.model) | ||
2849 | 157 | self.iconview.set_item_width(self.iv_col_width + 12) | ||
2850 | 158 | |||
2851 | 159 | self.iconview.set_pixbuf_column(1) | ||
2852 | 160 | # self.cellpb = gtk.CellRendererPixbuf() | ||
2853 | 161 | # self.cellpb.set_property('follow-state', True) | ||
2854 | 162 | # self.iconview.pack_start(self.cellpb, False) | ||
2855 | 163 | # self.iconview.set_attributes(self.cellpb, pixbuf=1) | ||
2856 | 164 | |||
2857 | 165 | # self.iconview.set_text_column(0) | ||
2858 | 166 | self.celltxt = gtk.CellRendererText() | ||
2859 | 167 | self.celltxt.set_property('width', self.iv_col_width) | ||
2860 | 168 | self.celltxt.set_property('wrap-width', self.iv_col_width) | ||
2861 | 169 | self.celltxt.set_property('alignment', pango.ALIGN_CENTER) | ||
2862 | 170 | self.iconview.pack_start(self.celltxt, False) | ||
2863 | 171 | self.iconview.set_attributes(self.celltxt, text=0) | ||
2864 | 172 | |||
2865 | 173 | self.iconview.set_selection_mode(gtk.SELECTION_MULTIPLE) | ||
2866 | 174 | self.iconview.enable_model_drag_source(gtk.gdk.BUTTON1_MASK, | ||
2867 | 175 | self.TARGETS_IV, | ||
2868 | 176 | gtk.gdk.ACTION_COPY | | ||
2869 | 177 | gtk.gdk.ACTION_MOVE ) | ||
2870 | 178 | self.iconview.enable_model_drag_dest(self.TARGETS_IV, | ||
2871 | 179 | gtk.gdk.ACTION_DEFAULT) | ||
2872 | 180 | self.iconview.connect('drag_begin', self.iv_drag_begin) | ||
2873 | 181 | self.iconview.connect('drag_data_get', self.iv_dnd_get_data) | ||
2874 | 182 | self.iconview.connect('drag_data_received', self.iv_dnd_received_data) | ||
2875 | 183 | self.iconview.connect('drag_data_delete', self.iv_dnd_data_delete) | ||
2876 | 184 | self.iconview.connect('drag_motion', self.iv_dnd_motion) | ||
2877 | 185 | self.iconview.connect('drag_leave', self.iv_dnd_leave_end) | ||
2878 | 186 | self.iconview.connect('drag_end', self.iv_dnd_leave_end) | ||
2879 | 187 | self.iconview.connect('button_press_event', self.iv_button_press_event) | ||
2880 | 188 | self.iv_auto_scroll_direction = 0 | ||
2881 | 189 | |||
2882 | 190 | style = self.iconview.get_style().copy() | ||
2883 | 191 | style_sw = self.sw.get_style() | ||
2884 | 192 | for state in (gtk.STATE_NORMAL, gtk.STATE_PRELIGHT, gtk.STATE_ACTIVE): | ||
2885 | 193 | style.base[state] = style_sw.bg[gtk.STATE_NORMAL] | ||
2886 | 194 | self.iconview.set_style(style) | ||
2887 | 195 | |||
2888 | 196 | align.add(self.iconview) | ||
2889 | 197 | |||
2890 | 198 | # Create a horizontal box to hold the buttons | ||
2891 | 199 | hbox = gtk.HBox() | ||
2892 | 200 | vbox.pack_start(hbox, expand=False, fill=False) | ||
2893 | 201 | |||
2894 | 202 | # Create buttons | ||
2895 | 203 | self.button_exit = gtk.Button(stock=gtk.STOCK_QUIT) | ||
2896 | 204 | self.button_exit.connect('clicked', self.close_application) | ||
2897 | 205 | hbox.pack_start(self.button_exit, expand=True, fill=True, padding=20) | ||
2898 | 206 | |||
2899 | 207 | self.button_del = gtk.Button(_('Delete Page(s)')) | ||
2900 | 208 | image = gtk.Image() | ||
2901 | 209 | image.set_from_stock(gtk.STOCK_DELETE, gtk.ICON_SIZE_BUTTON) | ||
2902 | 210 | self.button_del.set_image(image) | ||
2903 | 211 | self.button_del.connect('clicked', self.clear_selected) | ||
2904 | 212 | hbox.pack_start(self.button_del, expand=True, fill=True, padding=20) | ||
2905 | 213 | |||
2906 | 214 | self.button_import = gtk.Button(_('Import pdf')) | ||
2907 | 215 | image = gtk.Image() | ||
2908 | 216 | image.set_from_stock(gtk.STOCK_OPEN, gtk.ICON_SIZE_BUTTON) | ||
2909 | 217 | self.button_import.set_image(image) | ||
2910 | 218 | self.button_import.connect('clicked', self.on_action_add_doc_activate) | ||
2911 | 219 | hbox.pack_start(self.button_import, expand=True, fill=True, padding=20) | ||
2912 | 220 | |||
2913 | 221 | self.button_export = gtk.Button(_('Export pdf')) | ||
2914 | 222 | image = gtk.Image() | ||
2915 | 223 | image.set_from_stock(gtk.STOCK_SAVE_AS, gtk.ICON_SIZE_BUTTON) | ||
2916 | 224 | self.button_export.set_image(image) | ||
2917 | 225 | self.button_export.connect('clicked', self.choose_export_pdf_name) | ||
2918 | 226 | hbox.pack_start(self.button_export, expand=True, fill=True, padding=20) | ||
2919 | 227 | |||
2920 | 228 | self.button_export = gtk.Button(_('About')) | ||
2921 | 229 | image = gtk.Image() | ||
2922 | 230 | image.set_from_stock(gtk.STOCK_ABOUT, gtk.ICON_SIZE_BUTTON) | ||
2923 | 231 | self.button_export.set_image(image) | ||
2924 | 232 | self.button_export.connect('clicked', self.about_dialog) | ||
2925 | 233 | hbox.pack_start(self.button_export, expand=True, fill=True, padding=20) | ||
2926 | 234 | |||
2927 | 235 | # Define window callback function and show window | ||
2928 | 236 | self.window.connect('size_allocate', self.on_window_size_request) # resize | ||
2929 | 237 | self.window.connect('key_press_event', self.on_keypress_event ) # keypress | ||
2930 | 238 | self.window.show_all() | ||
2931 | 239 | |||
2932 | 240 | #Creating the popup menu | ||
2933 | 241 | self.popup = gtk.Menu() | ||
2934 | 242 | popup_rotate_right = gtk.MenuItem(_('Rotate Page(s) Clockwise')) | ||
2935 | 243 | popup_rotate_left = gtk.MenuItem(_('Rotate Page(s) Counterclockwise')) | ||
2936 | 244 | popup_crop = gtk.MenuItem(_('Crop Page(s)')) | ||
2937 | 245 | popup_delete = gtk.MenuItem(_('Delete Page(s)')) | ||
2938 | 246 | popup_rotate_right.connect('activate', self.rotate_page_right) | ||
2939 | 247 | popup_rotate_left.connect('activate', self.rotate_page_left) | ||
2940 | 248 | popup_crop.connect('activate', self.crop_page_dialog) | ||
2941 | 249 | popup_delete.connect('activate', self.clear_selected) | ||
2942 | 250 | popup_rotate_right.show() | ||
2943 | 251 | popup_rotate_left.show() | ||
2944 | 252 | popup_crop.show() | ||
2945 | 253 | popup_delete.show() | ||
2946 | 254 | self.popup.append(popup_rotate_right) | ||
2947 | 255 | self.popup.append(popup_rotate_left) | ||
2948 | 256 | self.popup.append(popup_crop) | ||
2949 | 257 | self.popup.append(popup_delete) | ||
2950 | 258 | |||
2951 | 259 | # Initializing variables | ||
2952 | 260 | self.export_directory = os.getenv('HOME') | ||
2953 | 261 | self.import_directory = os.getenv('HOME') | ||
2954 | 262 | self.nfile = 0 | ||
2955 | 263 | self.iv_auto_scroll_timer = None | ||
2956 | 264 | self.pdfqueue = [] | ||
2957 | 265 | |||
2958 | 266 | gobject.type_register(PDF_Renderer) | ||
2959 | 267 | gobject.signal_new('reset_iv_width', PDF_Renderer, | ||
2960 | 268 | gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ()) | ||
2961 | 269 | self.rendering_thread = PDF_Renderer(self.model, self.pdfqueue, | ||
2962 | 270 | self.zoom_scale, self.iv_col_width) | ||
2963 | 271 | self.rendering_thread.connect('reset_iv_width', self.reset_iv_width) | ||
2964 | 272 | self.rendering_thread.start() | ||
2965 | 273 | |||
2966 | 274 | # Importing documents passed as command line arguments | ||
2967 | 275 | for filename in sys.argv[1:]: | ||
2968 | 276 | self.add_pdf_pages(filename) | ||
2969 | 277 | |||
2970 | 278 | # ======================================================= | ||
2971 | 279 | def render(self): | ||
2972 | 280 | if self.rendering_thread.paused: | ||
2973 | 281 | self.rendering_thread.paused = False | ||
2974 | 282 | self.rendering_thread.evnt.set() | ||
2975 | 283 | self.rendering_thread.evnt.clear() | ||
2976 | 284 | |||
2977 | 285 | # ======================================================= | ||
2978 | 286 | def on_window_size_request(self, window, event): | ||
2979 | 287 | """Main Window resize - workaround for autosetting of | ||
2980 | 288 | iconview cols no.""" | ||
2981 | 289 | |||
2982 | 290 | #add 12 because of: http://bugzilla.gnome.org/show_bug.cgi?id=570152 | ||
2983 | 291 | col_num = 9 * window.get_size()[0] / (10 * (self.iv_col_width + 12)) | ||
2984 | 292 | self.iconview.set_columns(col_num) | ||
2985 | 293 | |||
2986 | 294 | # ======================================================= | ||
2987 | 295 | def reset_iv_width(self, renderer=None): | ||
2988 | 296 | """Reconfigures the width of the iconview columns""" | ||
2989 | 297 | |||
2990 | 298 | max_w = max(row[4] for row in self.model) | ||
2991 | 299 | if max_w != self.iv_col_width: | ||
2992 | 300 | self.iv_col_width = max_w | ||
2993 | 301 | self.celltxt.set_property('width', self.iv_col_width) | ||
2994 | 302 | self.celltxt.set_property('wrap-width', self.iv_col_width) | ||
2995 | 303 | self.iconview.set_item_width(self.iv_col_width + 12) #-1) | ||
2996 | 304 | self.on_window_size_request(self.window, None) | ||
2997 | 305 | |||
2998 | 306 | # ======================================================= | ||
2999 | 307 | def on_keypress_event(self, widget, event): | ||
3000 | 308 | """Keypress events in Main Window""" | ||
3001 | 309 | |||
3002 | 310 | #keyname = gtk.gdk.keyval_name(event.keyval) | ||
3003 | 311 | if event.keyval == 65535: # Delete keystroke | ||
3004 | 312 | self.clear_selected() | ||
3005 | 313 | |||
3006 | 314 | # ======================================================= | ||
3007 | 315 | def close_application(self, widget, event=None, data=None): | ||
3008 | 316 | """Termination""" | ||
3009 | 317 | |||
3010 | 318 | #gtk.gdk.threads_leave() | ||
3011 | 319 | self.rendering_thread.quit = True | ||
3012 | 320 | #gtk.gdk.threads_enter() | ||
3013 | 321 | if self.rendering_thread.paused == True: | ||
3014 | 322 | self.rendering_thread.evnt.set() | ||
3015 | 323 | self.rendering_thread.evnt.clear() | ||
3016 | 324 | if os.path.isdir(self.tmp_dir): | ||
3017 | 325 | shutil.rmtree(self.tmp_dir) | ||
3018 | 326 | if gtk.main_level(): | ||
3019 | 327 | gtk.main_quit() | ||
3020 | 328 | else: | ||
3021 | 329 | sys.exit(0) | ||
3022 | 330 | return False | ||
3023 | 331 | |||
3024 | 332 | # ======================================================= | ||
3025 | 333 | def add_pdf_pages(self, filename, | ||
3026 | 334 | firstpage=None, lastpage=None, | ||
3027 | 335 | angle=0, crop=[0.,0.,0.,0.] ): | ||
3028 | 336 | """Add pages of a pdf document to the model""" | ||
3029 | 337 | |||
3030 | 338 | res = False | ||
3031 | 339 | # Check if the document has already been loaded | ||
3032 | 340 | pdfdoc = None | ||
3033 | 341 | for it_pdfdoc in self.pdfqueue: | ||
3034 | 342 | if os.path.isfile(it_pdfdoc.filename) and \ | ||
3035 | 343 | os.path.samefile(filename, it_pdfdoc.filename) and \ | ||
3036 | 344 | os.path.getmtime(filename) is it_pdfdoc.mtime: | ||
3037 | 345 | pdfdoc = it_pdfdoc | ||
3038 | 346 | break | ||
3039 | 347 | |||
3040 | 348 | if not pdfdoc: | ||
3041 | 349 | pdfdoc = PDF_Doc(filename, self.nfile, self.tmp_dir) | ||
3042 | 350 | self.import_directory = os.path.split(filename)[0] | ||
3043 | 351 | self.export_directory = self.import_directory | ||
3044 | 352 | if pdfdoc.nfile != 0 and pdfdoc != []: | ||
3045 | 353 | self.nfile = pdfdoc.nfile | ||
3046 | 354 | self.pdfqueue.append(pdfdoc) | ||
3047 | 355 | else: | ||
3048 | 356 | return res | ||
3049 | 357 | |||
3050 | 358 | n_start = 1 | ||
3051 | 359 | n_end = pdfdoc.npage | ||
3052 | 360 | if firstpage: | ||
3053 | 361 | n_start = min(n_end, max(1, firstpage)) | ||
3054 | 362 | if lastpage: | ||
3055 | 363 | n_end = max(n_start, min(n_end, lastpage)) | ||
3056 | 364 | |||
3057 | 365 | for npage in range(n_start, n_end + 1): | ||
3058 | 366 | descriptor = ''.join([pdfdoc.shortname, '\n', _('page'), ' ', str(npage)]) | ||
3059 | 367 | width = self.iv_col_width | ||
3060 | 368 | thumbnail = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, | ||
3061 | 369 | 8, width, width) | ||
3062 | 370 | self.model.append((descriptor, # 0 | ||
3063 | 371 | thumbnail, # 1 | ||
3064 | 372 | pdfdoc.nfile, # 2 | ||
3065 | 373 | npage, # 3 | ||
3066 | 374 | width, # 4 | ||
3067 | 375 | pdfdoc.filename, # 5 | ||
3068 | 376 | False, # 6 | ||
3069 | 377 | angle, # 7 | ||
3070 | 378 | crop[0],crop[1], # 8-9 | ||
3071 | 379 | crop[2],crop[3] )) # 10-11 | ||
3072 | 380 | res = True | ||
3073 | 381 | |||
3074 | 382 | if res: | ||
3075 | 383 | self.render() | ||
3076 | 384 | return res | ||
3077 | 385 | |||
3078 | 386 | # ======================================================= | ||
3079 | 387 | def choose_export_pdf_name(self, widget=None): | ||
3080 | 388 | """Handles choosing a name for exporting """ | ||
3081 | 389 | |||
3082 | 390 | chooser = gtk.FileChooserDialog(title=_('Export ...'), | ||
3083 | 391 | action=gtk.FILE_CHOOSER_ACTION_SAVE, | ||
3084 | 392 | buttons=(gtk.STOCK_CANCEL, | ||
3085 | 393 | gtk.RESPONSE_CANCEL, | ||
3086 | 394 | gtk.STOCK_SAVE, | ||
3087 | 395 | gtk.RESPONSE_OK)) | ||
3088 | 396 | chooser.set_do_overwrite_confirmation(True) | ||
3089 | 397 | chooser.set_current_folder(self.export_directory) | ||
3090 | 398 | filter_pdf = gtk.FileFilter() | ||
3091 | 399 | filter_pdf.set_name(_('PDF files')) | ||
3092 | 400 | filter_pdf.add_mime_type('application/pdf') | ||
3093 | 401 | chooser.add_filter(filter_pdf) | ||
3094 | 402 | |||
3095 | 403 | filter_all = gtk.FileFilter() | ||
3096 | 404 | filter_all.set_name(_('All files')) | ||
3097 | 405 | filter_all.add_pattern('*') | ||
3098 | 406 | chooser.add_filter(filter_all) | ||
3099 | 407 | |||
3100 | 408 | while True: | ||
3101 | 409 | response = chooser.run() | ||
3102 | 410 | if response == gtk.RESPONSE_OK: | ||
3103 | 411 | file_out = chooser.get_filename() | ||
3104 | 412 | (path, shortname) = os.path.split(file_out) | ||
3105 | 413 | (shortname, ext) = os.path.splitext(shortname) | ||
3106 | 414 | if ext.lower() != '.pdf': | ||
3107 | 415 | file_out = file_out + '.pdf' | ||
3108 | 416 | try: | ||
3109 | 417 | self.export_to_file(file_out) | ||
3110 | 418 | self.export_directory = path | ||
3111 | 419 | except IOError: | ||
3112 | 420 | error_msg_win = gtk.MessageDialog(flags=gtk.DIALOG_MODAL, | ||
3113 | 421 | type=gtk.MESSAGE_ERROR, | ||
3114 | 422 | message_format=_("Error writing file: %s") % file_out, | ||
3115 | 423 | buttons=gtk.BUTTONS_OK) | ||
3116 | 424 | response = error_msg_win.run() | ||
3117 | 425 | if response == gtk.RESPONSE_OK: | ||
3118 | 426 | error_msg_win.destroy() | ||
3119 | 427 | continue | ||
3120 | 428 | break | ||
3121 | 429 | chooser.destroy() | ||
3122 | 430 | |||
3123 | 431 | # ======================================================= | ||
3124 | 432 | def export_to_file(self, file_out): | ||
3125 | 433 | """Export to file""" | ||
3126 | 434 | |||
3127 | 435 | pdf_output = PdfFileWriter() | ||
3128 | 436 | pdf_input = [] | ||
3129 | 437 | for pdfdoc in self.pdfqueue: | ||
3130 | 438 | pdfdoc_inp = PdfFileReader(file(pdfdoc.copyname, 'rb')) | ||
3131 | 439 | if pdfdoc_inp.getIsEncrypted(): | ||
3132 | 440 | if (pdfdoc_inp.decrypt('')!=1): # Workaround for lp:#355479 | ||
3133 | 441 | print(_('File %s is encrypted.') % pdfdoc.filename) | ||
3134 | 442 | print(_('Support for such files has not been implemented yet.')) | ||
3135 | 443 | print(_('File export failed.')) | ||
3136 | 444 | return | ||
3137 | 445 | #FIXME | ||
3138 | 446 | #else | ||
3139 | 447 | # ask for password and decrypt file | ||
3140 | 448 | pdf_input.append(pdfdoc_inp) | ||
3141 | 449 | |||
3142 | 450 | for row in self.model: | ||
3143 | 451 | # add pages from input to output document | ||
3144 | 452 | nfile = row[2] | ||
3145 | 453 | npage = row[3] | ||
3146 | 454 | current_page = copy(pdf_input[nfile-1].getPage(npage-1)) | ||
3147 | 455 | angle = row[7] | ||
3148 | 456 | angle0 = current_page.get("/Rotate",0) | ||
3149 | 457 | crop = [row[8],row[9],row[10],row[11]] | ||
3150 | 458 | if angle is not 0: | ||
3151 | 459 | current_page.rotateClockwise(angle) | ||
3152 | 460 | if crop != [0.,0.,0.,0.]: | ||
3153 | 461 | rotate_times = ( ( ( angle + angle0 ) % 360 + 45 ) / 90 ) % 4 | ||
3154 | 462 | crop_init = crop | ||
3155 | 463 | if rotate_times is not 0: | ||
3156 | 464 | perm = [0,2,1,3] | ||
3157 | 465 | for it in range(rotate_times): | ||
3158 | 466 | perm.append(perm.pop(0)) | ||
3159 | 467 | perm.insert(1,perm.pop(2)) | ||
3160 | 468 | crop = [crop_init[perm[side]] for side in range(4)] | ||
3161 | 469 | #(x1, y1) = current_page.cropBox.lowerLeft | ||
3162 | 470 | #(x2, y2) = current_page.cropBox.upperRight | ||
3163 | 471 | (x1, y1) = [float(xy) for xy in current_page.mediaBox.lowerLeft] | ||
3164 | 472 | (x2, y2) = [float(xy) for xy in current_page.mediaBox.upperRight] | ||
3165 | 473 | x1_new = int( x1 + (x2-x1) * crop[0] ) | ||
3166 | 474 | x2_new = int( x2 - (x2-x1) * crop[1] ) | ||
3167 | 475 | y1_new = int( y1 + (y2-y1) * crop[3] ) | ||
3168 | 476 | y2_new = int( y2 - (y2-y1) * crop[2] ) | ||
3169 | 477 | #current_page.cropBox.lowerLeft = (x1_new, y1_new) | ||
3170 | 478 | #current_page.cropBox.upperRight = (x2_new, y2_new) | ||
3171 | 479 | current_page.mediaBox.lowerLeft = (x1_new, y1_new) | ||
3172 | 480 | current_page.mediaBox.upperRight = (x2_new, y2_new) | ||
3173 | 481 | |||
3174 | 482 | pdf_output.addPage(current_page) | ||
3175 | 483 | |||
3176 | 484 | # finally, write "output" to document-output.pdf | ||
3177 | 485 | print(_('exporting to:'), file_out) | ||
3178 | 486 | pdf_output.write(file(file_out, 'wb')) | ||
3179 | 487 | |||
3180 | 488 | # ======================================================= | ||
3181 | 489 | def on_action_add_doc_activate(self, widget, data=None): | ||
3182 | 490 | """Import doc""" | ||
3183 | 491 | |||
3184 | 492 | chooser = gtk.FileChooserDialog(title=_('Import...'), | ||
3185 | 493 | action=gtk.FILE_CHOOSER_ACTION_OPEN, | ||
3186 | 494 | buttons=(gtk.STOCK_CANCEL, | ||
3187 | 495 | gtk.RESPONSE_CANCEL, | ||
3188 | 496 | gtk.STOCK_OPEN, | ||
3189 | 497 | gtk.RESPONSE_OK)) | ||
3190 | 498 | chooser.set_current_folder(self.import_directory) | ||
3191 | 499 | chooser.set_select_multiple(True) | ||
3192 | 500 | |||
3193 | 501 | filter_all = gtk.FileFilter() | ||
3194 | 502 | filter_all.set_name(_('All files')) | ||
3195 | 503 | filter_all.add_pattern('*') | ||
3196 | 504 | chooser.add_filter(filter_all) | ||
3197 | 505 | |||
3198 | 506 | filter_pdf = gtk.FileFilter() | ||
3199 | 507 | filter_pdf.set_name(_('PDF files')) | ||
3200 | 508 | filter_pdf.add_mime_type('application/pdf') | ||
3201 | 509 | chooser.add_filter(filter_pdf) | ||
3202 | 510 | chooser.set_filter(filter_pdf) | ||
3203 | 511 | |||
3204 | 512 | response = chooser.run() | ||
3205 | 513 | if response == gtk.RESPONSE_OK: | ||
3206 | 514 | for filename in chooser.get_filenames(): | ||
3207 | 515 | if os.path.isfile(filename): | ||
3208 | 516 | # FIXME | ||
3209 | 517 | f = gio.File(filename) | ||
3210 | 518 | f_info = f.query_info('standard::content-type') | ||
3211 | 519 | mime_type = f_info.get_content_type() | ||
3212 | 520 | if mime_type == 'application/pdf': | ||
3213 | 521 | self.add_pdf_pages(filename) | ||
3214 | 522 | elif mime_type[:34] == 'application/vnd.oasis.opendocument': | ||
3215 | 523 | print(_('OpenDocument not supported yet!')) | ||
3216 | 524 | elif mime_type[:5] == 'image': | ||
3217 | 525 | print(_('Image file not supported yet!')) | ||
3218 | 526 | else: | ||
3219 | 527 | print(_('File type not supported!')) | ||
3220 | 528 | else: | ||
3221 | 529 | print(_('File %s does not exist') % filename) | ||
3222 | 530 | elif response == gtk.RESPONSE_CANCEL: | ||
3223 | 531 | print(_('Closed, no files selected')) | ||
3224 | 532 | chooser.destroy() | ||
3225 | 533 | |||
3226 | 534 | # ======================================================= | ||
3227 | 535 | def clear_selected(self, button=None): | ||
3228 | 536 | """Removes the selected Element in the IconView""" | ||
3229 | 537 | |||
3230 | 538 | model = self.iconview.get_model() | ||
3231 | 539 | selection = self.iconview.get_selected_items() | ||
3232 | 540 | if selection: | ||
3233 | 541 | selection.sort(reverse=True) | ||
3234 | 542 | for path in selection: | ||
3235 | 543 | iter = model.get_iter(path) | ||
3236 | 544 | model.remove(iter) | ||
3237 | 545 | path = selection[-1] | ||
3238 | 546 | self.iconview.select_path(path) | ||
3239 | 547 | if not self.iconview.path_is_selected(path): | ||
3240 | 548 | if len(model) > 0: # select the last row | ||
3241 | 549 | row = model[-1] | ||
3242 | 550 | path = row.path | ||
3243 | 551 | self.iconview.select_path(path) | ||
3244 | 552 | self.iconview.grab_focus() | ||
3245 | 553 | |||
3246 | 554 | # ======================================================= | ||
3247 | 555 | def iv_drag_begin(self, iconview, context): | ||
3248 | 556 | """Sets custom icon on drag begin for multiple items selected""" | ||
3249 | 557 | |||
3250 | 558 | if len(iconview.get_selected_items()) > 1: | ||
3251 | 559 | iconview.stop_emission('drag_begin') | ||
3252 | 560 | context.set_icon_stock(gtk.STOCK_DND_MULTIPLE, 0, 0) | ||
3253 | 561 | |||
3254 | 562 | # ======================================================= | ||
3255 | 563 | def iv_dnd_get_data(self, iconview, context, | ||
3256 | 564 | selection_data, target_id, etime): | ||
3257 | 565 | """Handles requests for data by drag and drop in iconview""" | ||
3258 | 566 | |||
3259 | 567 | model = iconview.get_model() | ||
3260 | 568 | selection = self.iconview.get_selected_items() | ||
3261 | 569 | selection.sort(key=lambda x: x[0]) | ||
3262 | 570 | data = [] | ||
3263 | 571 | for path in selection: | ||
3264 | 572 | if selection_data.target == 'MODEL_ROW_INTERN': | ||
3265 | 573 | data.append( str(path[0]) ) | ||
3266 | 574 | elif selection_data.target == 'MODEL_ROW_EXTERN': | ||
3267 | 575 | iter = model.get_iter(path) | ||
3268 | 576 | nfile, npage, angle = model.get(iter, 2, 3, 7) | ||
3269 | 577 | crop = model.get(iter, 8, 9, 10, 11) | ||
3270 | 578 | pdfdoc = self.pdfqueue[nfile - 1] | ||
3271 | 579 | data.append('\n'.join([pdfdoc.filename, | ||
3272 | 580 | str(npage), | ||
3273 | 581 | str(angle) ] + | ||
3274 | 582 | [str(side) for side in crop] ) ) | ||
3275 | 583 | if data: | ||
3276 | 584 | data = '\n;\n'.join(data) | ||
3277 | 585 | selection_data.set(selection_data.target, 8, data) | ||
3278 | 586 | |||
3279 | 587 | # ======================================================= | ||
3280 | 588 | def iv_dnd_received_data(self, iconview, context, x, y, | ||
3281 | 589 | selection_data, target_id, etime): | ||
3282 | 590 | """Handles received data by drag and drop in iconview""" | ||
3283 | 591 | |||
3284 | 592 | model = iconview.get_model() | ||
3285 | 593 | data = selection_data.data | ||
3286 | 594 | if data: | ||
3287 | 595 | data = data.split('\n;\n') | ||
3288 | 596 | drop_info = iconview.get_dest_item_at_pos(x, y) | ||
3289 | 597 | iter_to = None | ||
3290 | 598 | if drop_info: | ||
3291 | 599 | path, position = drop_info | ||
3292 | 600 | ref_to = gtk.TreeRowReference(model,path) | ||
3293 | 601 | else: | ||
3294 | 602 | position = gtk.ICON_VIEW_DROP_RIGHT | ||
3295 | 603 | if len(model) > 0: #find the iterator of the last row | ||
3296 | 604 | row = model[-1] | ||
3297 | 605 | path = row.path | ||
3298 | 606 | ref_to = gtk.TreeRowReference(model,path) | ||
3299 | 607 | if ref_to: | ||
3300 | 608 | before = ( position == gtk.ICON_VIEW_DROP_LEFT | ||
3301 | 609 | or position == gtk.ICON_VIEW_DROP_ABOVE) | ||
3302 | 610 | #if target_id == self.MODEL_ROW_INTERN: | ||
3303 | 611 | if selection_data.target == 'MODEL_ROW_INTERN': | ||
3304 | 612 | if before: | ||
3305 | 613 | data.sort(key=int) | ||
3306 | 614 | else: | ||
3307 | 615 | data.sort(key=int,reverse=True) | ||
3308 | 616 | ref_from_list = [gtk.TreeRowReference(model,path) | ||
3309 | 617 | for path in data] | ||
3310 | 618 | for ref_from in ref_from_list: | ||
3311 | 619 | path = ref_to.get_path() | ||
3312 | 620 | iter_to = model.get_iter(path) | ||
3313 | 621 | path = ref_from.get_path() | ||
3314 | 622 | iter_from = model.get_iter(path) | ||
3315 | 623 | row = model[iter_from] | ||
3316 | 624 | if before: | ||
3317 | 625 | model.insert_before(iter_to, row) | ||
3318 | 626 | else: | ||
3319 | 627 | model.insert_after(iter_to, row) | ||
3320 | 628 | if context.action == gtk.gdk.ACTION_MOVE: | ||
3321 | 629 | for ref_from in ref_from_list: | ||
3322 | 630 | path = ref_from.get_path() | ||
3323 | 631 | iter_from = model.get_iter(path) | ||
3324 | 632 | model.remove(iter_from) | ||
3325 | 633 | |||
3326 | 634 | #elif target_id == self.MODEL_ROW_EXTERN: | ||
3327 | 635 | elif selection_data.target == 'MODEL_ROW_EXTERN': | ||
3328 | 636 | if not before: | ||
3329 | 637 | data.reverse() | ||
3330 | 638 | while data: | ||
3331 | 639 | tmp = data.pop(0).split('\n') | ||
3332 | 640 | filename = tmp[0] | ||
3333 | 641 | npage, angle = [int(k) for k in tmp[1:3]] | ||
3334 | 642 | crop = [float(side) for side in tmp[3:7]] | ||
3335 | 643 | if self.add_pdf_pages(filename, npage, npage, | ||
3336 | 644 | angle, crop ): | ||
3337 | 645 | if len(model) > 0: | ||
3338 | 646 | path = ref_to.get_path() | ||
3339 | 647 | iter_to = model.get_iter(path) | ||
3340 | 648 | row = model[-1] #the last row | ||
3341 | 649 | path = row.path | ||
3342 | 650 | iter_from = model.get_iter(path) | ||
3343 | 651 | if before: | ||
3344 | 652 | model.move_before(iter_from, iter_to) | ||
3345 | 653 | else: | ||
3346 | 654 | model.move_after(iter_from, iter_to) | ||
3347 | 655 | if context.action == gtk.gdk.ACTION_MOVE: | ||
3348 | 656 | context.finish(True, True, etime) | ||
3349 | 657 | |||
3350 | 658 | # ======================================================= | ||
3351 | 659 | def iv_dnd_data_delete(self, widget, context): | ||
3352 | 660 | """Deletes dnd items after a successful move operation""" | ||
3353 | 661 | |||
3354 | 662 | model = self.iconview.get_model() | ||
3355 | 663 | selection = self.iconview.get_selected_items() | ||
3356 | 664 | ref_del_list = [gtk.TreeRowReference(model,path) for path in selection] | ||
3357 | 665 | for ref_del in ref_del_list: | ||
3358 | 666 | path = ref_del.get_path() | ||
3359 | 667 | iter = model.get_iter(path) | ||
3360 | 668 | model.remove(iter) | ||
3361 | 669 | |||
3362 | 670 | # ======================================================= | ||
3363 | 671 | def iv_dnd_motion(self, iconview, context, x, y, etime): | ||
3364 | 672 | """Handles the drag-motion signal in order to auto-scroll the view""" | ||
3365 | 673 | |||
3366 | 674 | autoscroll_area = 40 | ||
3367 | 675 | sw_vadj = self.sw.get_vadjustment() | ||
3368 | 676 | sw_height = self.sw.get_allocation().height | ||
3369 | 677 | if y -sw_vadj.get_value() < autoscroll_area: | ||
3370 | 678 | if not self.iv_auto_scroll_timer: | ||
3371 | 679 | self.iv_auto_scroll_direction = gtk.DIR_UP | ||
3372 | 680 | self.iv_auto_scroll_timer = gobject.timeout_add(150, | ||
3373 | 681 | self.iv_auto_scroll) | ||
3374 | 682 | elif y -sw_vadj.get_value() > sw_height - autoscroll_area: | ||
3375 | 683 | if not self.iv_auto_scroll_timer: | ||
3376 | 684 | self.iv_auto_scroll_direction = gtk.DIR_DOWN | ||
3377 | 685 | self.iv_auto_scroll_timer = gobject.timeout_add(150, | ||
3378 | 686 | self.iv_auto_scroll) | ||
3379 | 687 | elif self.iv_auto_scroll_timer: | ||
3380 | 688 | gobject.source_remove(self.iv_auto_scroll_timer) | ||
3381 | 689 | self.iv_auto_scroll_timer = None | ||
3382 | 690 | |||
3383 | 691 | # ======================================================= | ||
3384 | 692 | def iv_dnd_leave_end(self, widget, context, ignored=None): | ||
3385 | 693 | """Ends the auto-scroll during DND""" | ||
3386 | 694 | |||
3387 | 695 | if self.iv_auto_scroll_timer: | ||
3388 | 696 | gobject.source_remove(self.iv_auto_scroll_timer) | ||
3389 | 697 | self.iv_auto_scroll_timer = None | ||
3390 | 698 | |||
3391 | 699 | # ======================================================= | ||
3392 | 700 | def iv_auto_scroll(self): | ||
3393 | 701 | """Timeout routine for auto-scroll""" | ||
3394 | 702 | |||
3395 | 703 | sw_vadj = self.sw.get_vadjustment() | ||
3396 | 704 | sw_vpos = sw_vadj.get_value() | ||
3397 | 705 | if self.iv_auto_scroll_direction == gtk.DIR_UP: | ||
3398 | 706 | sw_vpos -= sw_vadj.step_increment | ||
3399 | 707 | sw_vadj.set_value( max(sw_vpos, sw_vadj.lower) ) | ||
3400 | 708 | elif self.iv_auto_scroll_direction == gtk.DIR_DOWN: | ||
3401 | 709 | sw_vpos += sw_vadj.step_increment | ||
3402 | 710 | sw_vadj.set_value( min(sw_vpos, sw_vadj.upper - sw_vadj.page_size) ) | ||
3403 | 711 | return True #call me again | ||
3404 | 712 | |||
3405 | 713 | # ======================================================= | ||
3406 | 714 | def iv_button_press_event(self, iconview, event): | ||
3407 | 715 | """Manages mouse clicks on the iconview""" | ||
3408 | 716 | |||
3409 | 717 | if event.button == 3: | ||
3410 | 718 | x = int(event.x) | ||
3411 | 719 | y = int(event.y) | ||
3412 | 720 | time = event.time | ||
3413 | 721 | path = iconview.get_path_at_pos(x, y) | ||
3414 | 722 | selection = iconview.get_selected_items() | ||
3415 | 723 | if path: | ||
3416 | 724 | if path not in selection: | ||
3417 | 725 | iconview.unselect_all() | ||
3418 | 726 | iconview.select_path(path) | ||
3419 | 727 | iconview.grab_focus() | ||
3420 | 728 | self.popup.popup( None, None, None, event.button, time) | ||
3421 | 729 | return 1 | ||
3422 | 730 | |||
3423 | 731 | # ======================================================= | ||
3424 | 732 | def sw_dnd_received_data(self, scrolledwindow, context, x, y, | ||
3425 | 733 | selection_data, target_id, etime): | ||
3426 | 734 | """Handles received data by drag and drop in scrolledwindow""" | ||
3427 | 735 | |||
3428 | 736 | data = selection_data.data | ||
3429 | 737 | if target_id == self.MODEL_ROW_EXTERN: | ||
3430 | 738 | self.model | ||
3431 | 739 | if data: | ||
3432 | 740 | data = data.split('\n;\n') | ||
3433 | 741 | while data: | ||
3434 | 742 | tmp = data.pop(0).split('\n') | ||
3435 | 743 | filename = tmp[0] | ||
3436 | 744 | npage, angle = [int(k) for k in tmp[1:3]] | ||
3437 | 745 | crop = [float(side) for side in tmp[3:7]] | ||
3438 | 746 | if self.add_pdf_pages(filename, npage, npage, angle, crop): | ||
3439 | 747 | if context.action == gtk.gdk.ACTION_MOVE: | ||
3440 | 748 | context.finish(True, True, etime) | ||
3441 | 749 | elif target_id == self.TEXT_URI_LIST: | ||
3442 | 750 | uri = data.strip() | ||
3443 | 751 | uri_splitted = uri.split() # we may have more than one file dropped | ||
3444 | 752 | for uri in uri_splitted: | ||
3445 | 753 | filename = self.get_file_path_from_dnd_dropped_uri(uri) | ||
3446 | 754 | if os.path.isfile(filename): # is it file? | ||
3447 | 755 | self.add_pdf_pages(filename) | ||
3448 | 756 | |||
3449 | 757 | # ======================================================= | ||
3450 | 758 | def sw_button_press_event(self, scrolledwindow, event): | ||
3451 | 759 | """Unselects all items in iconview on mouse click in scrolledwindow""" | ||
3452 | 760 | |||
3453 | 761 | if event.button == 1: | ||
3454 | 762 | self.iconview.unselect_all() | ||
3455 | 763 | |||
3456 | 764 | # ======================================================= | ||
3457 | 765 | def get_file_path_from_dnd_dropped_uri(self, uri): | ||
3458 | 766 | """Extracts the path from an uri""" | ||
3459 | 767 | |||
3460 | 768 | path = urllib.url2pathname(uri) # escape special chars | ||
3461 | 769 | path = path.strip('\r\n\x00') # remove \r\n and NULL | ||
3462 | 770 | |||
3463 | 771 | # get the path to file | ||
3464 | 772 | if path.startswith('file:\\\\\\'): # windows | ||
3465 | 773 | path = path[8:] # 8 is len('file:///') | ||
3466 | 774 | elif path.startswith('file://'): # nautilus, rox | ||
3467 | 775 | path = path[7:] # 7 is len('file://') | ||
3468 | 776 | elif path.startswith('file:'): # xffm | ||
3469 | 777 | path = path[5:] # 5 is len('file:') | ||
3470 | 778 | return path | ||
3471 | 779 | |||
3472 | 780 | # ======================================================= | ||
3473 | 781 | def rotate_page_right(self, widget, data=None): | ||
3474 | 782 | self.rotate_page(90) | ||
3475 | 783 | |||
3476 | 784 | def rotate_page_left(self, widget, data=None): | ||
3477 | 785 | self.rotate_page(-90) | ||
3478 | 786 | |||
3479 | 787 | def rotate_page(self, angle): | ||
3480 | 788 | """Rotates the selected page in the IconView""" | ||
3481 | 789 | |||
3482 | 790 | model = self.iconview.get_model() | ||
3483 | 791 | selection = self.iconview.get_selected_items() | ||
3484 | 792 | for path in selection: | ||
3485 | 793 | iter = model.get_iter(path) | ||
3486 | 794 | nfile = model.get_value(iter, 2) | ||
3487 | 795 | npage = model.get_value(iter, 3) | ||
3488 | 796 | |||
3489 | 797 | rotate_times = ( ( (-angle) % 360 + 45 ) / 90 ) % 4 | ||
3490 | 798 | crop = [0.,0.,0.,0.] | ||
3491 | 799 | if rotate_times is not 0: | ||
3492 | 800 | perm = [0,2,1,3] | ||
3493 | 801 | for it in range(rotate_times): | ||
3494 | 802 | perm.append(perm.pop(0)) | ||
3495 | 803 | perm.insert(1,perm.pop(2)) | ||
3496 | 804 | crop = [model.get_value(iter, 8 + perm[side]) for side in range(4)] | ||
3497 | 805 | for side in range(4): | ||
3498 | 806 | model.set_value(iter, 8 + side, crop[side]) | ||
3499 | 807 | |||
3500 | 808 | new_angle = model.get_value(iter, 7) + angle | ||
3501 | 809 | model.set_value(iter, 7, new_angle) | ||
3502 | 810 | model.set_value(iter, 6, False) #rendering request | ||
3503 | 811 | |||
3504 | 812 | self.render() | ||
3505 | 813 | |||
3506 | 814 | # ======================================================= | ||
3507 | 815 | def crop_page_dialog(self, widget): | ||
3508 | 816 | """Opens a dialog box to define margins for page cropping""" | ||
3509 | 817 | |||
3510 | 818 | sides = ('L', 'R', 'T', 'B') | ||
3511 | 819 | side_names = {'L':_('Left'), 'R':_('Right'), | ||
3512 | 820 | 'T':_('Top'), 'B':_('Bottom') } | ||
3513 | 821 | opposite_sides = {'L':'R', 'R':'L', 'T':'B', 'B':'T' } | ||
3514 | 822 | |||
3515 | 823 | def set_crop_value(spinbutton, side): | ||
3516 | 824 | opp_side = opposite_sides[side] | ||
3517 | 825 | pos = sides.index(opp_side) | ||
3518 | 826 | adj = spin_list[pos].get_adjustment() | ||
3519 | 827 | adj.set_upper(99.0 - spinbutton.get_value()) | ||
3520 | 828 | |||
3521 | 829 | model = self.iconview.get_model() | ||
3522 | 830 | selection = self.iconview.get_selected_items() | ||
3523 | 831 | |||
3524 | 832 | crop = [0.,0.,0.,0.] | ||
3525 | 833 | if selection: | ||
3526 | 834 | path = selection[0] | ||
3527 | 835 | pos = model.get_iter(path) | ||
3528 | 836 | crop = [model.get_value(pos, 8 + side) for side in range(4)] | ||
3529 | 837 | |||
3530 | 838 | dialog = gtk.Dialog(title=(_('Crop Selected Page(s)')), | ||
3531 | 839 | parent=self.window, | ||
3532 | 840 | flags=gtk.DIALOG_MODAL, | ||
3533 | 841 | buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, | ||
3534 | 842 | gtk.STOCK_OK, gtk.RESPONSE_OK)) | ||
3535 | 843 | dialog.set_size_request(340, 250) | ||
3536 | 844 | dialog.set_default_response(gtk.RESPONSE_OK) | ||
3537 | 845 | |||
3538 | 846 | frame = gtk.Frame(_('Crop Margins')) | ||
3539 | 847 | dialog.vbox.pack_start(frame, False, False, 20) | ||
3540 | 848 | |||
3541 | 849 | vbox = gtk.VBox(False, 0) | ||
3542 | 850 | frame.add(vbox) | ||
3543 | 851 | |||
3544 | 852 | spin_list = [] | ||
3545 | 853 | units = 2 * [_('% of width')] + 2 * [_('% of height')] | ||
3546 | 854 | for side in sides: | ||
3547 | 855 | hbox = gtk.HBox(True, 0) | ||
3548 | 856 | vbox.pack_start(hbox, False, False, 5) | ||
3549 | 857 | |||
3550 | 858 | label = gtk.Label(side_names[side]) | ||
3551 | 859 | label.set_alignment(0, 0.0) | ||
3552 | 860 | hbox.pack_start(label, True, True, 20) | ||
3553 | 861 | |||
3554 | 862 | adj = gtk.Adjustment(100.*crop.pop(0), 0.0, 99.0, 1.0, 5.0, 0.0) | ||
3555 | 863 | spin = gtk.SpinButton(adj, 0, 1) | ||
3556 | 864 | spin.set_activates_default(True) | ||
3557 | 865 | spin.connect('value-changed', set_crop_value, side) | ||
3558 | 866 | spin_list.append(spin) | ||
3559 | 867 | hbox.pack_start(spin, False, False, 30) | ||
3560 | 868 | |||
3561 | 869 | label = gtk.Label(units.pop(0)) | ||
3562 | 870 | label.set_alignment(0, 0.0) | ||
3563 | 871 | hbox.pack_start(label, True, True, 0) | ||
3564 | 872 | |||
3565 | 873 | dialog.show_all() | ||
3566 | 874 | result = dialog.run() | ||
3567 | 875 | |||
3568 | 876 | if result == gtk.RESPONSE_OK: | ||
3569 | 877 | crop = [] | ||
3570 | 878 | for spin in spin_list: | ||
3571 | 879 | crop.append( spin.get_value()/100. ) | ||
3572 | 880 | for path in selection: | ||
3573 | 881 | pos = model.get_iter(path) | ||
3574 | 882 | for it in range(4): | ||
3575 | 883 | model.set_value(pos, 8 + it, crop[it]) | ||
3576 | 884 | model.set_value(pos, 6, False) #rendering request | ||
3577 | 885 | self.render() | ||
3578 | 886 | elif result == gtk.RESPONSE_CANCEL: | ||
3579 | 887 | print(_('Dialog closed')) | ||
3580 | 888 | dialog.destroy() | ||
3581 | 889 | |||
3582 | 890 | # ======================================================= | ||
3583 | 891 | def about_dialog(self, widget, data=None): | ||
3584 | 892 | about_dialog = gtk.AboutDialog() | ||
3585 | 893 | try: | ||
3586 | 894 | about_dialog.set_transient_for(self.window) | ||
3587 | 895 | about_dialog.set_modal(True) | ||
3588 | 896 | except: | ||
3589 | 897 | pass | ||
3590 | 898 | # FIXME | ||
3591 | 899 | about_dialog.set_name('PDF-Shuffler') | ||
3592 | 900 | about_dialog.set_version('0.5.1') | ||
3593 | 901 | about_dialog.set_comments(_( | ||
3594 | 902 | 'PDF-Shuffler is a simple pyGTK utility which lets you merge, \ | ||
3595 | 903 | split and rearrange PDF documents. You can also rotate and crop individual \ | ||
3596 | 904 | pages of a pdf document.')) | ||
3597 | 905 | about_dialog.set_authors(['Konstantinos Poulios',]) | ||
3598 | 906 | about_dialog.set_website_label('http://pdfshuffler.sourceforge.net/') | ||
3599 | 907 | about_dialog.set_logo_icon_name('pdfshuffler') | ||
3600 | 908 | about_dialog.connect('response', lambda w, a: about_dialog.destroy()) | ||
3601 | 909 | about_dialog.connect('delete_event', lambda w, a: about_dialog.destroy()) | ||
3602 | 910 | about_dialog.show_all() | ||
3603 | 911 | |||
3604 | 912 | # ======================================================= | ||
3605 | 913 | class PDF_Doc: | ||
3606 | 914 | """Class handling pdf documents""" | ||
3607 | 915 | |||
3608 | 916 | def __init__(self, filename, nfile, tmp_dir): | ||
3609 | 917 | |||
3610 | 918 | self.filename = os.path.abspath(filename) | ||
3611 | 919 | (self.path, self.shortname) = os.path.split(self.filename) | ||
3612 | 920 | (self.shortname, self.ext) = os.path.splitext(self.shortname) | ||
3613 | 921 | f = gio.File(filename) | ||
3614 | 922 | mime_type = f.query_info('standard::content-type').get_content_type() | ||
3615 | 923 | if mime_type == 'application/pdf': | ||
3616 | 924 | self.nfile = nfile + 1 | ||
3617 | 925 | self.mtime = os.path.getmtime(filename) | ||
3618 | 926 | self.copyname = os.path.join(tmp_dir, '%02d_' % self.nfile + | ||
3619 | 927 | self.shortname + '.pdf') | ||
3620 | 928 | shutil.copy(self.filename, self.copyname) | ||
3621 | 929 | self.document = poppler.document_new_from_file("file://" + self.copyname, None) | ||
3622 | 930 | self.npage = self.document.get_n_pages() | ||
3623 | 931 | else: | ||
3624 | 932 | self.nfile = 0 | ||
3625 | 933 | |||
3626 | 934 | |||
3627 | 935 | # ======================================================= | ||
3628 | 936 | class PDF_Renderer(threading.Thread,gobject.GObject): | ||
3629 | 937 | |||
3630 | 938 | def __init__(self, model, pdfqueue, scale=1., width=100): | ||
3631 | 939 | threading.Thread.__init__(self) | ||
3632 | 940 | gobject.GObject.__init__(self) | ||
3633 | 941 | self.model = model | ||
3634 | 942 | self.scale = scale | ||
3635 | 943 | self.default_width = width | ||
3636 | 944 | self.pdfqueue = pdfqueue | ||
3637 | 945 | self.quit = False | ||
3638 | 946 | self.evnt = threading.Event() | ||
3639 | 947 | self.paused = False | ||
3640 | 948 | |||
3641 | 949 | def run(self): | ||
3642 | 950 | while not self.quit: | ||
3643 | 951 | rendered_all = True | ||
3644 | 952 | for row in self.model: | ||
3645 | 953 | if self.quit: | ||
3646 | 954 | break | ||
3647 | 955 | if not row[6]: | ||
3648 | 956 | rendered_all = False | ||
3649 | 957 | gtk.gdk.threads_enter() | ||
3650 | 958 | try: | ||
3651 | 959 | nfile = row[2] | ||
3652 | 960 | npage = row[3] | ||
3653 | 961 | angle = row[7] | ||
3654 | 962 | crop = [row[8],row[9],row[10],row[11]] | ||
3655 | 963 | pdfdoc = self.pdfqueue[nfile - 1] | ||
3656 | 964 | thumbnail = self.load_pdf_thumbnail(pdfdoc, npage, angle, crop) | ||
3657 | 965 | row[6] = True | ||
3658 | 966 | row[4] = thumbnail.get_width() | ||
3659 | 967 | row[1] = thumbnail | ||
3660 | 968 | finally: | ||
3661 | 969 | gtk.gdk.threads_leave() | ||
3662 | 970 | if rendered_all: | ||
3663 | 971 | if self.model.get_iter_first(): #just checking if model isn't empty | ||
3664 | 972 | self.emit('reset_iv_width') | ||
3665 | 973 | self.paused = True | ||
3666 | 974 | self.evnt.wait() | ||
3667 | 975 | |||
3668 | 976 | # ======================================================= | ||
3669 | 977 | def load_pdf_thumbnail(self, pdfdoc, npage, rotation=0, crop=[0.,0.,0.,0.]): | ||
3670 | 978 | """Create pdf pixbuf""" | ||
3671 | 979 | |||
3672 | 980 | page = pdfdoc.document.get_page(npage-1) | ||
3673 | 981 | try: | ||
3674 | 982 | pix_w, pix_h = page.get_size() | ||
3675 | 983 | pix_w = int(pix_w * self.scale) | ||
3676 | 984 | pix_h = int(pix_h * self.scale) | ||
3677 | 985 | thumbnail = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, | ||
3678 | 986 | 8, pix_w , pix_h) | ||
3679 | 987 | page.render_to_pixbuf(0,0,pix_w,pix_h,self.scale,0,thumbnail) | ||
3680 | 988 | rotation = (-rotation) % 360 | ||
3681 | 989 | rotation = ((rotation + 45) / 90) * 90 | ||
3682 | 990 | thumbnail = thumbnail.rotate_simple(rotation) | ||
3683 | 991 | pix_w = thumbnail.get_width() | ||
3684 | 992 | pix_h = thumbnail.get_height() | ||
3685 | 993 | if crop != [0.,0.,0.,0.]: | ||
3686 | 994 | src_x = int( crop[0] * pix_w ) | ||
3687 | 995 | src_y = int( crop[2] * pix_h ) | ||
3688 | 996 | width = int( (1. - crop[0] - crop[1]) * pix_w ) | ||
3689 | 997 | height = int( (1. - crop[2] - crop[3]) * pix_h ) | ||
3690 | 998 | new_thumbnail = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, | ||
3691 | 999 | 8, width, height) | ||
3692 | 1000 | thumbnail.copy_area(src_x, src_y, width, height, | ||
3693 | 1001 | new_thumbnail, 0, 0) | ||
3694 | 1002 | thumbnail = new_thumbnail | ||
3695 | 1003 | pix_w = thumbnail.get_width() | ||
3696 | 1004 | pix_h = thumbnail.get_height() | ||
3697 | 1005 | except: | ||
3698 | 1006 | pix_w = self.default_width | ||
3699 | 1007 | pix_h = pix_w | ||
3700 | 1008 | thumbnail = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, | ||
3701 | 1009 | 8, pix_w, pix_h) | ||
3702 | 1010 | pixbuf.fill(0xffffffff) | ||
3703 | 1011 | |||
3704 | 1012 | #add border | ||
3705 | 1013 | thickness = 3 | ||
3706 | 1014 | color = 0x000000FF | ||
3707 | 1015 | canvas = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, | ||
3708 | 1016 | pix_w + thickness + 1, | ||
3709 | 1017 | pix_h + thickness + 1) | ||
3710 | 1018 | canvas.fill(color) | ||
3711 | 1019 | thumbnail.copy_area(0, 0, pix_w, pix_h, canvas, 1, 1) | ||
3712 | 1020 | thumbnail = canvas | ||
3713 | 1021 | |||
3714 | 1022 | return thumbnail | ||
3715 | 1023 | |||
3716 | 1024 | |||
3717 | 1025 | # ======================================================= | ||
3718 | 1026 | if __name__ == '__main__': | ||
3719 | 1027 | PDFshuffler() | ||
3720 | 1028 | gtk.gdk.threads_enter() | ||
3721 | 1029 | gtk.main() | ||
3722 | 1030 | gtk.gdk.threads_leave() | ||
3723 | 1031 | |||
3724 | 1032 | 0 | ||
3725 | === added file 'pdfshuffler/__init__.py' | |||
3726 | === added file 'pdfshuffler/cairorendering.py' | |||
3727 | --- pdfshuffler/cairorendering.py 1970-01-01 00:00:00 +0000 | |||
3728 | +++ pdfshuffler/cairorendering.py 2012-02-12 00:35:23 +0000 | |||
3729 | @@ -0,0 +1,168 @@ | |||
3730 | 1 | #!/usr/bin/python | ||
3731 | 2 | # -*- coding: utf-8 -*- | ||
3732 | 3 | |||
3733 | 4 | """ | ||
3734 | 5 | |||
3735 | 6 | PdfShuffler 0.6.0 - GTK+ based utility for splitting, rearrangement and | ||
3736 | 7 | modification of PDF documents. | ||
3737 | 8 | Copyright (C) 2008-2011 Konstantinos Poulios | ||
3738 | 9 | <https://sourceforge.net/projects/pdfshuffler> | ||
3739 | 10 | |||
3740 | 11 | This file is part of PdfShuffler. | ||
3741 | 12 | |||
3742 | 13 | PdfShuffler is free software; you can redistribute it and/or modify | ||
3743 | 14 | it under the terms of the GNU General Public License as published by | ||
3744 | 15 | the Free Software Foundation; either version 3 of the License, or | ||
3745 | 16 | (at your option) any later version. | ||
3746 | 17 | |||
3747 | 18 | This program is distributed in the hope that it will be useful, | ||
3748 | 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3749 | 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3750 | 21 | GNU General Public License for more details. | ||
3751 | 22 | |||
3752 | 23 | You should have received a copy of the GNU General Public License along | ||
3753 | 24 | with this program; if not, write to the Free Software Foundation, Inc., | ||
3754 | 25 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
3755 | 26 | |||
3756 | 27 | """ | ||
3757 | 28 | |||
3758 | 29 | import gtk | ||
3759 | 30 | import gobject | ||
3760 | 31 | import cairo | ||
3761 | 32 | |||
3762 | 33 | from math import pi as M_PI | ||
3763 | 34 | |||
3764 | 35 | class CellRendererImage(gtk.GenericCellRenderer): | ||
3765 | 36 | __gproperties__ = { | ||
3766 | 37 | "image": (gobject.TYPE_OBJECT, "Image", "Image", | ||
3767 | 38 | gobject.PARAM_READWRITE), | ||
3768 | 39 | "width": (gobject.TYPE_DOUBLE, "Width", "Width", | ||
3769 | 40 | 0., 1.e4, 0., gobject.PARAM_READWRITE), | ||
3770 | 41 | "height": (gobject.TYPE_DOUBLE, "Height", "Height", | ||
3771 | 42 | 0., 1.e4, 0., gobject.PARAM_READWRITE), | ||
3772 | 43 | "rotation": (gobject.TYPE_INT, "Rotation", "Rotation", | ||
3773 | 44 | 0, 360, 0, gobject.PARAM_READWRITE), | ||
3774 | 45 | "scale": (gobject.TYPE_DOUBLE, "Scale", "Scale", | ||
3775 | 46 | 0.01, 100., 1., gobject.PARAM_READWRITE), | ||
3776 | 47 | "cropL": (gobject.TYPE_DOUBLE, "CropL", "CropL", | ||
3777 | 48 | 0., 1., 0., gobject.PARAM_READWRITE), | ||
3778 | 49 | "cropR": (gobject.TYPE_DOUBLE, "CropR", "CropR", | ||
3779 | 50 | 0., 1., 0., gobject.PARAM_READWRITE), | ||
3780 | 51 | "cropT": (gobject.TYPE_DOUBLE, "CropT", "CropT", | ||
3781 | 52 | 0., 1., 0., gobject.PARAM_READWRITE), | ||
3782 | 53 | "cropB": (gobject.TYPE_DOUBLE, "CropB", "CropB", | ||
3783 | 54 | 0., 1., 0., gobject.PARAM_READWRITE), | ||
3784 | 55 | } | ||
3785 | 56 | |||
3786 | 57 | def __init__(self): | ||
3787 | 58 | self.__gobject_init__() | ||
3788 | 59 | self.th1 = 2. # border thickness | ||
3789 | 60 | self.th2 = 3. # shadow thickness | ||
3790 | 61 | |||
3791 | 62 | def get_geometry(self): | ||
3792 | 63 | |||
3793 | 64 | rotation = int(self.rotation) % 360 | ||
3794 | 65 | rotation = ((rotation + 45) / 90) * 90 | ||
3795 | 66 | if not self.image.surface: | ||
3796 | 67 | w0 = w1 = self.width | ||
3797 | 68 | h0 = h1 = self.height | ||
3798 | 69 | else: | ||
3799 | 70 | w0 = self.image.surface.get_width() | ||
3800 | 71 | h0 = self.image.surface.get_height() | ||
3801 | 72 | if rotation == 90 or rotation == 270: | ||
3802 | 73 | w1, h1 = h0, w0 | ||
3803 | 74 | else: | ||
3804 | 75 | w1, h1 = w0, h0 | ||
3805 | 76 | |||
3806 | 77 | x = self.cropL * w1 | ||
3807 | 78 | y = self.cropT * h1 | ||
3808 | 79 | |||
3809 | 80 | w2 = int(self.scale * (1. - self.cropL - self.cropR) * w1) | ||
3810 | 81 | h2 = int(self.scale * (1. - self.cropT - self.cropB) * h1) | ||
3811 | 82 | |||
3812 | 83 | return w0,h0,w1,h1,w2,h2,rotation | ||
3813 | 84 | |||
3814 | 85 | def do_set_property(self, pspec, value): | ||
3815 | 86 | setattr(self, pspec.name, value) | ||
3816 | 87 | |||
3817 | 88 | def do_get_property(self, pspec): | ||
3818 | 89 | return getattr(self, pspec.name) | ||
3819 | 90 | |||
3820 | 91 | def on_render(self, window, widget, background_area, cell_area, \ | ||
3821 | 92 | expose_area, flags): | ||
3822 | 93 | if not self.image.surface: | ||
3823 | 94 | return | ||
3824 | 95 | |||
3825 | 96 | w0,h0,w1,h1,w2,h2,rotation = self.get_geometry() | ||
3826 | 97 | th = int(2*self.th1+self.th2) | ||
3827 | 98 | w = w2 + th | ||
3828 | 99 | h = h2 + th | ||
3829 | 100 | |||
3830 | 101 | x = cell_area.x | ||
3831 | 102 | y = cell_area.y | ||
3832 | 103 | if cell_area and w > 0 and h > 0: | ||
3833 | 104 | x += self.get_property('xalign') * \ | ||
3834 | 105 | (cell_area.width - w - self.get_property('xpad')) | ||
3835 | 106 | y += self.get_property('yalign') * \ | ||
3836 | 107 | (cell_area.height - h - self.get_property('ypad')) | ||
3837 | 108 | |||
3838 | 109 | cr = window.cairo_create() | ||
3839 | 110 | cr.translate(x,y) | ||
3840 | 111 | |||
3841 | 112 | x = self.cropL * w1 | ||
3842 | 113 | y = self.cropT * h1 | ||
3843 | 114 | |||
3844 | 115 | #shadow | ||
3845 | 116 | cr.set_source_rgb(0.5, 0.5, 0.5) | ||
3846 | 117 | cr.rectangle(th, th, w2, h2) | ||
3847 | 118 | cr.fill() | ||
3848 | 119 | |||
3849 | 120 | #border | ||
3850 | 121 | cr.set_source_rgb(0, 0, 0) | ||
3851 | 122 | cr.rectangle(0, 0, w2+2*self.th1, h2+2*self.th1) | ||
3852 | 123 | cr.fill() | ||
3853 | 124 | |||
3854 | 125 | #image | ||
3855 | 126 | cr.set_source_rgb(1, 1, 1) | ||
3856 | 127 | cr.rectangle(self.th1, self.th1, w2, h2) | ||
3857 | 128 | cr.fill_preserve() | ||
3858 | 129 | cr.clip() | ||
3859 | 130 | |||
3860 | 131 | cr.translate(self.th1,self.th1) | ||
3861 | 132 | cr.scale(self.scale, self.scale) | ||
3862 | 133 | cr.translate(-x,-y) | ||
3863 | 134 | if rotation > 0: | ||
3864 | 135 | cr.translate(w1/2,h1/2) | ||
3865 | 136 | cr.rotate(rotation * M_PI / 180) | ||
3866 | 137 | cr.translate(-w0/2,-h0/2) | ||
3867 | 138 | |||
3868 | 139 | cr.set_source_surface(self.image.surface) | ||
3869 | 140 | cr.paint() | ||
3870 | 141 | |||
3871 | 142 | def on_get_size(self, widget, cell_area=None): | ||
3872 | 143 | x = y = 0 | ||
3873 | 144 | w0,h0,w1,h1,w2,h2,rotation = self.get_geometry() | ||
3874 | 145 | th = int(2*self.th1+self.th2) | ||
3875 | 146 | w = w2 + th | ||
3876 | 147 | h = h2 + th | ||
3877 | 148 | |||
3878 | 149 | if cell_area and w > 0 and h > 0: | ||
3879 | 150 | x = self.get_property('xalign') * \ | ||
3880 | 151 | (cell_area.width - w - self.get_property('xpad')) | ||
3881 | 152 | y = self.get_property('yalign') * \ | ||
3882 | 153 | (cell_area.height - h - self.get_property('ypad')) | ||
3883 | 154 | w += 2 * self.get_property('xpad') | ||
3884 | 155 | h += 2 * self.get_property('ypad') | ||
3885 | 156 | return int(x), int(y), w, h | ||
3886 | 157 | |||
3887 | 158 | |||
3888 | 159 | class CairoImage(gobject.GObject): | ||
3889 | 160 | |||
3890 | 161 | def __init__(self, width=0, height=0): | ||
3891 | 162 | gobject.GObject.__init__(self) | ||
3892 | 163 | if width > 0 and height > 0: | ||
3893 | 164 | self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) | ||
3894 | 165 | else: | ||
3895 | 166 | self.surface = None | ||
3896 | 167 | |||
3897 | 168 | |||
3898 | 0 | 169 | ||
3899 | === added file 'pdfshuffler/pdfshuffler.py' | |||
3900 | --- pdfshuffler/pdfshuffler.py 1970-01-01 00:00:00 +0000 | |||
3901 | +++ pdfshuffler/pdfshuffler.py 2012-02-12 00:35:23 +0000 | |||
3902 | @@ -0,0 +1,1057 @@ | |||
3903 | 1 | #!/usr/bin/python | ||
3904 | 2 | # -*- coding: utf-8 -*- | ||
3905 | 3 | |||
3906 | 4 | """ | ||
3907 | 5 | |||
3908 | 6 | PdfShuffler 0.6.0 - GTK+ based utility for splitting, rearrangement and | ||
3909 | 7 | modification of PDF documents. | ||
3910 | 8 | Copyright (C) 2008-2011 Konstantinos Poulios | ||
3911 | 9 | <https://sourceforge.net/projects/pdfshuffler> | ||
3912 | 10 | |||
3913 | 11 | This file is part of PdfShuffler. | ||
3914 | 12 | |||
3915 | 13 | PdfShuffler is free software; you can redistribute it and/or modify | ||
3916 | 14 | it under the terms of the GNU General Public License as published by | ||
3917 | 15 | the Free Software Foundation; either version 3 of the License, or | ||
3918 | 16 | (at your option) any later version. | ||
3919 | 17 | |||
3920 | 18 | This program is distributed in the hope that it will be useful, | ||
3921 | 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3922 | 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3923 | 21 | GNU General Public License for more details. | ||
3924 | 22 | |||
3925 | 23 | You should have received a copy of the GNU General Public License along | ||
3926 | 24 | with this program; if not, write to the Free Software Foundation, Inc., | ||
3927 | 25 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
3928 | 26 | |||
3929 | 27 | """ | ||
3930 | 28 | |||
3931 | 29 | import os | ||
3932 | 30 | import shutil #needed for file operations like whole directory deletion | ||
3933 | 31 | import sys #needed for proccessing of command line args | ||
3934 | 32 | import urllib #needed to parse filename information passed by DnD | ||
3935 | 33 | import threading | ||
3936 | 34 | import tempfile | ||
3937 | 35 | from copy import copy | ||
3938 | 36 | |||
3939 | 37 | import locale #for multilanguage support | ||
3940 | 38 | import gettext | ||
3941 | 39 | gettext.install('pdfshuffler', unicode=1) | ||
3942 | 40 | |||
3943 | 41 | |||
3944 | 42 | APPNAME = 'PdfShuffler' # PDF-Shuffler, PDFShuffler, pdfshuffler | ||
3945 | 43 | VERSION = '0.6.0' | ||
3946 | 44 | WEBSITE = 'http://pdfshuffler.sourceforge.net/' | ||
3947 | 45 | LICENSE = 'GNU General Public License (GPL) Version 3.' | ||
3948 | 46 | |||
3949 | 47 | try: | ||
3950 | 48 | import pygtk | ||
3951 | 49 | pygtk.require('2.0') | ||
3952 | 50 | import gtk | ||
3953 | 51 | assert gtk.gtk_version >= (2, 10, 0) | ||
3954 | 52 | assert gtk.pygtk_version >= (2, 10, 0) | ||
3955 | 53 | except AssertionError: | ||
3956 | 54 | print('You do not have the required versions of GTK+ and PyGTK ' + | ||
3957 | 55 | 'installed.\n\n' + | ||
3958 | 56 | 'Installed GTK+ version is ' + | ||
3959 | 57 | '.'.join([str(n) for n in gtk.gtk_version]) + '\n' + | ||
3960 | 58 | 'Required GTK+ version is 2.10.0 or higher\n\n' | ||
3961 | 59 | 'Installed PyGTK version is ' + | ||
3962 | 60 | '.'.join([str(n) for n in gtk.pygtk_version]) + '\n' + | ||
3963 | 61 | 'Required PyGTK version is 2.10.0 or higher') | ||
3964 | 62 | sys.exit(1) | ||
3965 | 63 | except: | ||
3966 | 64 | print('PyGTK version 2.10.0 or higher is required to run this program.') | ||
3967 | 65 | print('No version of PyGTK was found on your system.') | ||
3968 | 66 | sys.exit(1) | ||
3969 | 67 | |||
3970 | 68 | import gobject #to use custom signals | ||
3971 | 69 | import pango #to adjust the text alignment in CellRendererText | ||
3972 | 70 | import gio #to inquire mime types information | ||
3973 | 71 | import cairo | ||
3974 | 72 | |||
3975 | 73 | import poppler #for the rendering of pdf pages | ||
3976 | 74 | from pyPdf import PdfFileWriter, PdfFileReader | ||
3977 | 75 | |||
3978 | 76 | from cairorendering import CellRendererImage, CairoImage | ||
3979 | 77 | gobject.type_register(CellRendererImage) | ||
3980 | 78 | |||
3981 | 79 | import time | ||
3982 | 80 | |||
3983 | 81 | class PdfShuffler: | ||
3984 | 82 | prefs = { | ||
3985 | 83 | 'window width': min(700, gtk.gdk.screen_get_default().get_width() / 2), | ||
3986 | 84 | 'window height': min(600, gtk.gdk.screen_get_default().get_height() - 50), | ||
3987 | 85 | 'window x': 0, | ||
3988 | 86 | 'window y': 0, | ||
3989 | 87 | 'initial thumbnail size': 300, | ||
3990 | 88 | 'initial zoom level': -14, | ||
3991 | 89 | } | ||
3992 | 90 | |||
3993 | 91 | MODEL_ROW_INTERN = 1001 | ||
3994 | 92 | MODEL_ROW_EXTERN = 1002 | ||
3995 | 93 | TEXT_URI_LIST = 1003 | ||
3996 | 94 | MODEL_ROW_MOTION = 1004 | ||
3997 | 95 | TARGETS_IV = [('MODEL_ROW_INTERN', gtk.TARGET_SAME_WIDGET, MODEL_ROW_INTERN), | ||
3998 | 96 | ('MODEL_ROW_EXTERN', gtk.TARGET_OTHER_APP, MODEL_ROW_EXTERN), | ||
3999 | 97 | ('MODEL_ROW_MOTION', 0, MODEL_ROW_MOTION)] | ||
4000 | 98 | TARGETS_SW = [('text/uri-list', 0, TEXT_URI_LIST), | ||
4001 | 99 | ('MODEL_ROW_EXTERN', gtk.TARGET_OTHER_APP, MODEL_ROW_EXTERN)] | ||
4002 | 100 | |||
4003 | 101 | def __init__(self): | ||
4004 | 102 | # Create the temporary directory | ||
4005 | 103 | self.tmp_dir = tempfile.mkdtemp("pdfshuffler") | ||
4006 | 104 | os.chmod(self.tmp_dir, 0700) | ||
4007 | 105 | |||
4008 | 106 | icon_theme = gtk.icon_theme_get_default() | ||
4009 | 107 | try: | ||
4010 | 108 | gtk.window_set_default_icon(icon_theme.load_icon("pdfshuffler", 64, 0)) | ||
4011 | 109 | except: | ||
4012 | 110 | print(_("Can't load icon. Application is not installed correctly.")) | ||
4013 | 111 | |||
4014 | 112 | # Import the user interface file, trying different possible locations | ||
4015 | 113 | ui_path = '/usr/share/pdfshuffler/pdfshuffler.ui' | ||
4016 | 114 | if not os.path.exists(ui_path): | ||
4017 | 115 | ui_path = '/usr/local/share/pdfshuffler/pdfshuffler.ui' | ||
4018 | 116 | if not os.path.exists(ui_path): | ||
4019 | 117 | parent_dir = os.path.dirname( \ | ||
4020 | 118 | os.path.dirname(os.path.realpath(__file__))) | ||
4021 | 119 | ui_path = os.path.join(parent_dir, 'data', 'pdfshuffler.ui') | ||
4022 | 120 | self.uiXML = gtk.Builder() | ||
4023 | 121 | self.uiXML.add_from_file(ui_path) | ||
4024 | 122 | self.uiXML.connect_signals(self) | ||
4025 | 123 | |||
4026 | 124 | # Create the main window, and attach delete_event signal to terminating | ||
4027 | 125 | # the application | ||
4028 | 126 | self.window = self.uiXML.get_object('main_window') | ||
4029 | 127 | self.window.set_title(APPNAME) | ||
4030 | 128 | self.window.set_border_width(0) | ||
4031 | 129 | self.window.move(self.prefs['window x'], self.prefs['window y']) | ||
4032 | 130 | self.window.set_default_size(self.prefs['window width'], | ||
4033 | 131 | self.prefs['window height']) | ||
4034 | 132 | self.window.connect('delete_event', self.close_application) | ||
4035 | 133 | self.window.show_all() | ||
4036 | 134 | |||
4037 | 135 | # Create a scrolled window to hold the thumbnails-container | ||
4038 | 136 | self.sw = self.uiXML.get_object('scrolledwindow') | ||
4039 | 137 | self.sw.drag_dest_set(gtk.DEST_DEFAULT_MOTION | | ||
4040 | 138 | gtk.DEST_DEFAULT_HIGHLIGHT | | ||
4041 | 139 | gtk.DEST_DEFAULT_DROP | | ||
4042 | 140 | gtk.DEST_DEFAULT_MOTION, | ||
4043 | 141 | self.TARGETS_SW, | ||
4044 | 142 | gtk.gdk.ACTION_COPY | | ||
4045 | 143 | gtk.gdk.ACTION_MOVE) | ||
4046 | 144 | self.sw.connect('drag_data_received', self.sw_dnd_received_data) | ||
4047 | 145 | self.sw.connect('button_press_event', self.sw_button_press_event) | ||
4048 | 146 | self.sw.connect('scroll_event', self.sw_scroll_event) | ||
4049 | 147 | |||
4050 | 148 | # Create an alignment to keep the thumbnails center-aligned | ||
4051 | 149 | align = gtk.Alignment(0.5, 0.5, 0, 0) | ||
4052 | 150 | self.sw.add_with_viewport(align) | ||
4053 | 151 | |||
4054 | 152 | # Create ListStore model and IconView | ||
4055 | 153 | self.model = gtk.ListStore(str, # 0.Text descriptor | ||
4056 | 154 | CairoImage, # 1.Cached page image | ||
4057 | 155 | int, # 2.Document number | ||
4058 | 156 | int, # 3.Page number | ||
4059 | 157 | float, # 4.Scale | ||
4060 | 158 | str, # 5.Document filename | ||
4061 | 159 | int, # 6.Rotation angle | ||
4062 | 160 | float, # 7.Crop left | ||
4063 | 161 | float, # 8.Crop right | ||
4064 | 162 | float, # 9.Crop top | ||
4065 | 163 | float, # 10.Crop bottom | ||
4066 | 164 | int, # 11.Page width | ||
4067 | 165 | int) # 12.Page height | ||
4068 | 166 | |||
4069 | 167 | self.zoom_set(self.prefs['initial zoom level']) | ||
4070 | 168 | self.iv_col_width = self.prefs['initial thumbnail size'] | ||
4071 | 169 | |||
4072 | 170 | self.iconview = gtk.IconView(self.model) | ||
4073 | 171 | self.iconview.set_item_width(self.iv_col_width + 12) | ||
4074 | 172 | |||
4075 | 173 | self.cellthmb = CellRendererImage() | ||
4076 | 174 | self.iconview.pack_start(self.cellthmb, False) | ||
4077 | 175 | self.iconview.set_attributes(self.cellthmb, image=1, | ||
4078 | 176 | scale=4, rotation=6, cropL=7, cropR=8, cropT=9, cropB=10, | ||
4079 | 177 | width=11, height=12) | ||
4080 | 178 | |||
4081 | 179 | # self.iconview.set_text_column(0) | ||
4082 | 180 | self.celltxt = gtk.CellRendererText() | ||
4083 | 181 | self.celltxt.set_property('width', self.iv_col_width) | ||
4084 | 182 | self.celltxt.set_property('wrap-width', self.iv_col_width) | ||
4085 | 183 | self.celltxt.set_property('alignment', pango.ALIGN_CENTER) | ||
4086 | 184 | self.iconview.pack_start(self.celltxt, False) | ||
4087 | 185 | self.iconview.set_attributes(self.celltxt, text=0) | ||
4088 | 186 | |||
4089 | 187 | self.iconview.set_selection_mode(gtk.SELECTION_MULTIPLE) | ||
4090 | 188 | self.iconview.enable_model_drag_source(gtk.gdk.BUTTON1_MASK, | ||
4091 | 189 | self.TARGETS_IV, | ||
4092 | 190 | gtk.gdk.ACTION_COPY | | ||
4093 | 191 | gtk.gdk.ACTION_MOVE) | ||
4094 | 192 | self.iconview.enable_model_drag_dest(self.TARGETS_IV, | ||
4095 | 193 | gtk.gdk.ACTION_DEFAULT) | ||
4096 | 194 | self.iconview.connect('drag_begin', self.iv_drag_begin) | ||
4097 | 195 | self.iconview.connect('drag_data_get', self.iv_dnd_get_data) | ||
4098 | 196 | self.iconview.connect('drag_data_received', self.iv_dnd_received_data) | ||
4099 | 197 | self.iconview.connect('drag_data_delete', self.iv_dnd_data_delete) | ||
4100 | 198 | self.iconview.connect('drag_motion', self.iv_dnd_motion) | ||
4101 | 199 | self.iconview.connect('drag_leave', self.iv_dnd_leave_end) | ||
4102 | 200 | self.iconview.connect('drag_end', self.iv_dnd_leave_end) | ||
4103 | 201 | self.iconview.connect('button_press_event', self.iv_button_press_event) | ||
4104 | 202 | self.iv_auto_scroll_direction = 0 | ||
4105 | 203 | |||
4106 | 204 | style = self.iconview.get_style().copy() | ||
4107 | 205 | style_sw = self.sw.get_style() | ||
4108 | 206 | for state in (gtk.STATE_NORMAL, gtk.STATE_PRELIGHT, gtk.STATE_ACTIVE): | ||
4109 | 207 | style.base[state] = style_sw.bg[gtk.STATE_NORMAL] | ||
4110 | 208 | self.iconview.set_style(style) | ||
4111 | 209 | |||
4112 | 210 | align.add(self.iconview) | ||
4113 | 211 | |||
4114 | 212 | # Progress bar | ||
4115 | 213 | self.progress_bar = self.uiXML.get_object('progressbar') | ||
4116 | 214 | self.progress_bar_timeout_id = 0 | ||
4117 | 215 | |||
4118 | 216 | # Define window callback function and show window | ||
4119 | 217 | self.window.connect('size_allocate', self.on_window_size_request) # resize | ||
4120 | 218 | self.window.connect('key_press_event', self.on_keypress_event ) # keypress | ||
4121 | 219 | self.window.show_all() | ||
4122 | 220 | self.progress_bar.hide_all() | ||
4123 | 221 | |||
4124 | 222 | # Creating the popup menu | ||
4125 | 223 | self.popup = gtk.Menu() | ||
4126 | 224 | popup_rotate_right = gtk.ImageMenuItem(_('_Rotate Right')) | ||
4127 | 225 | popup_rotate_left = gtk.ImageMenuItem(_('Rotate _Left')) | ||
4128 | 226 | popup_crop = gtk.MenuItem(_('C_rop...')) | ||
4129 | 227 | popup_delete = gtk.ImageMenuItem(gtk.STOCK_DELETE) | ||
4130 | 228 | popup_rotate_right.connect('activate', self.rotate_page_right) | ||
4131 | 229 | popup_rotate_left.connect('activate', self.rotate_page_left) | ||
4132 | 230 | popup_crop.connect('activate', self.crop_page_dialog) | ||
4133 | 231 | popup_delete.connect('activate', self.clear_selected) | ||
4134 | 232 | popup_rotate_right.show() | ||
4135 | 233 | popup_rotate_left.show() | ||
4136 | 234 | popup_crop.show() | ||
4137 | 235 | popup_delete.show() | ||
4138 | 236 | self.popup.append(popup_rotate_right) | ||
4139 | 237 | self.popup.append(popup_rotate_left) | ||
4140 | 238 | self.popup.append(popup_crop) | ||
4141 | 239 | self.popup.append(popup_delete) | ||
4142 | 240 | |||
4143 | 241 | # Initializing variables | ||
4144 | 242 | self.export_directory = os.getenv('HOME') | ||
4145 | 243 | self.import_directory = self.export_directory | ||
4146 | 244 | self.nfile = 0 | ||
4147 | 245 | self.iv_auto_scroll_timer = None | ||
4148 | 246 | self.pdfqueue = [] | ||
4149 | 247 | |||
4150 | 248 | gobject.type_register(PDF_Renderer) | ||
4151 | 249 | gobject.signal_new('update_thumbnail', PDF_Renderer, | ||
4152 | 250 | gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, | ||
4153 | 251 | [gobject.TYPE_INT, gobject.TYPE_PYOBJECT]) | ||
4154 | 252 | self.rendering_thread = 0 | ||
4155 | 253 | |||
4156 | 254 | self.set_unsaved(False) | ||
4157 | 255 | |||
4158 | 256 | # Importing documents passed as command line arguments | ||
4159 | 257 | for filename in sys.argv[1:]: | ||
4160 | 258 | self.add_pdf_pages(filename) | ||
4161 | 259 | |||
4162 | 260 | def render(self): | ||
4163 | 261 | if self.rendering_thread: | ||
4164 | 262 | self.rendering_thread.quit = True | ||
4165 | 263 | self.rendering_thread = PDF_Renderer(self.model, self.pdfqueue) | ||
4166 | 264 | self.rendering_thread.connect('update_thumbnail', self.update_thumbnail) | ||
4167 | 265 | self.rendering_thread.start() | ||
4168 | 266 | |||
4169 | 267 | if self.progress_bar_timeout_id: | ||
4170 | 268 | gobject.source_remove(self.progress_bar_timeout_id) | ||
4171 | 269 | self.progress_bar_timout_id = \ | ||
4172 | 270 | gobject.timeout_add(50, self.progress_bar_timeout) | ||
4173 | 271 | |||
4174 | 272 | def set_unsaved(self, flag): | ||
4175 | 273 | self.is_unsaved = flag | ||
4176 | 274 | gobject.idle_add(self.retitle) | ||
4177 | 275 | |||
4178 | 276 | def retitle(self): | ||
4179 | 277 | title = '' | ||
4180 | 278 | if len(self.pdfqueue) == 1: | ||
4181 | 279 | title += self.pdfqueue[0].filename | ||
4182 | 280 | elif len(self.pdfqueue) == 0: | ||
4183 | 281 | title += _("No document") | ||
4184 | 282 | else: | ||
4185 | 283 | title += _("Several documents") | ||
4186 | 284 | if self.is_unsaved: | ||
4187 | 285 | title += '*' | ||
4188 | 286 | title += ' - ' + APPNAME | ||
4189 | 287 | self.window.set_title(title) | ||
4190 | 288 | |||
4191 | 289 | def progress_bar_timeout(self): | ||
4192 | 290 | cnt_finished = 0 | ||
4193 | 291 | cnt_all = 0 | ||
4194 | 292 | for row in self.model: | ||
4195 | 293 | cnt_all += 1 | ||
4196 | 294 | if row[1].surface: | ||
4197 | 295 | cnt_finished += 1 | ||
4198 | 296 | fraction = float(cnt_finished)/float(cnt_all) | ||
4199 | 297 | |||
4200 | 298 | self.progress_bar.set_fraction(fraction) | ||
4201 | 299 | self.progress_bar.set_text(_('Rendering thumbnails... [%(i1)s/%(i2)s]') | ||
4202 | 300 | % {'i1' : cnt_finished, 'i2' : cnt_all}) | ||
4203 | 301 | if fraction >= 0.999: | ||
4204 | 302 | self.progress_bar.hide_all() | ||
4205 | 303 | return False | ||
4206 | 304 | elif not self.progress_bar.flags() & gtk.VISIBLE: | ||
4207 | 305 | self.progress_bar.show_all() | ||
4208 | 306 | |||
4209 | 307 | return True | ||
4210 | 308 | |||
4211 | 309 | def update_thumbnail(self, object, num, thumbnail): | ||
4212 | 310 | row = self.model[num] | ||
4213 | 311 | row[4] = self.zoom_scale | ||
4214 | 312 | row[1] = thumbnail | ||
4215 | 313 | |||
4216 | 314 | def on_window_size_request(self, window, event): | ||
4217 | 315 | """Main Window resize - workaround for autosetting of | ||
4218 | 316 | iconview cols no.""" | ||
4219 | 317 | |||
4220 | 318 | #add 12 because of: http://bugzilla.gnome.org/show_bug.cgi?id=570152 | ||
4221 | 319 | col_num = 9 * window.get_size()[0] \ | ||
4222 | 320 | / (10 * (self.iv_col_width + self.iconview.get_column_spacing() * 2)) | ||
4223 | 321 | self.iconview.set_columns(col_num) | ||
4224 | 322 | |||
4225 | 323 | def update_geometry(self, iter): | ||
4226 | 324 | """Recomputes the width and height of the rotated page and saves | ||
4227 | 325 | the result in the ListStore""" | ||
4228 | 326 | |||
4229 | 327 | if not self.model.iter_is_valid(iter): | ||
4230 | 328 | return | ||
4231 | 329 | |||
4232 | 330 | nfile, npage, rotation = self.model.get(iter, 2, 3, 6) | ||
4233 | 331 | crop = self.model.get(iter, 7, 8, 9, 10) | ||
4234 | 332 | page = self.pdfqueue[nfile-1].document.get_page(npage-1) | ||
4235 | 333 | w0, h0 = page.get_size() | ||
4236 | 334 | |||
4237 | 335 | rotation = int(rotation) % 360 | ||
4238 | 336 | rotation = ((rotation + 45) / 90) * 90 | ||
4239 | 337 | if rotation == 90 or rotation == 270: | ||
4240 | 338 | w1, h1 = h0, w0 | ||
4241 | 339 | else: | ||
4242 | 340 | w1, h1 = w0, h0 | ||
4243 | 341 | |||
4244 | 342 | self.model.set(iter, 11, w1, 12, h1) | ||
4245 | 343 | |||
4246 | 344 | def reset_iv_width(self, renderer=None): | ||
4247 | 345 | """Reconfigures the width of the iconview columns""" | ||
4248 | 346 | |||
4249 | 347 | if not self.model.get_iter_first(): #just checking if model is empty | ||
4250 | 348 | return | ||
4251 | 349 | |||
4252 | 350 | max_w = 10 + int(max(row[4]*row[11]*(1.-row[7]-row[8]) for row in self.model)) | ||
4253 | 351 | if max_w != self.iv_col_width: | ||
4254 | 352 | self.iv_col_width = max_w | ||
4255 | 353 | self.celltxt.set_property('width', self.iv_col_width) | ||
4256 | 354 | self.celltxt.set_property('wrap-width', self.iv_col_width) | ||
4257 | 355 | self.iconview.set_item_width(self.iv_col_width + 12) #-1) | ||
4258 | 356 | self.on_window_size_request(self.window, None) | ||
4259 | 357 | |||
4260 | 358 | def on_keypress_event(self, widget, event): | ||
4261 | 359 | """Keypress events in Main Window""" | ||
4262 | 360 | |||
4263 | 361 | #keyname = gtk.gdk.keyval_name(event.keyval) | ||
4264 | 362 | if event.keyval == 65535: # Delete keystroke | ||
4265 | 363 | self.clear_selected() | ||
4266 | 364 | |||
4267 | 365 | def close_application(self, widget, event=None, data=None): | ||
4268 | 366 | """Termination""" | ||
4269 | 367 | |||
4270 | 368 | if self.rendering_thread: | ||
4271 | 369 | self.rendering_thread.quit = True | ||
4272 | 370 | while self.rendering_thread.isAlive(): | ||
4273 | 371 | time.sleep(0.5) | ||
4274 | 372 | |||
4275 | 373 | if os.path.isdir(self.tmp_dir): | ||
4276 | 374 | shutil.rmtree(self.tmp_dir) | ||
4277 | 375 | if gtk.main_level(): | ||
4278 | 376 | gtk.main_quit() | ||
4279 | 377 | else: | ||
4280 | 378 | sys.exit(0) | ||
4281 | 379 | return False | ||
4282 | 380 | |||
4283 | 381 | def add_pdf_pages(self, filename, | ||
4284 | 382 | firstpage=None, lastpage=None, | ||
4285 | 383 | angle=0, crop=[0.,0.,0.,0.]): | ||
4286 | 384 | """Add pages of a pdf document to the model""" | ||
4287 | 385 | |||
4288 | 386 | res = False | ||
4289 | 387 | # Check if the document has already been loaded | ||
4290 | 388 | pdfdoc = None | ||
4291 | 389 | for it_pdfdoc in self.pdfqueue: | ||
4292 | 390 | if os.path.isfile(it_pdfdoc.filename) and \ | ||
4293 | 391 | os.path.samefile(filename, it_pdfdoc.filename) and \ | ||
4294 | 392 | os.path.getmtime(filename) is it_pdfdoc.mtime: | ||
4295 | 393 | pdfdoc = it_pdfdoc | ||
4296 | 394 | break | ||
4297 | 395 | |||
4298 | 396 | if not pdfdoc: | ||
4299 | 397 | pdfdoc = PDF_Doc(filename, self.nfile, self.tmp_dir) | ||
4300 | 398 | self.import_directory = os.path.split(filename)[0] | ||
4301 | 399 | self.export_directory = self.import_directory | ||
4302 | 400 | if pdfdoc.nfile != 0 and pdfdoc != []: | ||
4303 | 401 | self.nfile = pdfdoc.nfile | ||
4304 | 402 | self.pdfqueue.append(pdfdoc) | ||
4305 | 403 | else: | ||
4306 | 404 | return res | ||
4307 | 405 | |||
4308 | 406 | n_start = 1 | ||
4309 | 407 | n_end = pdfdoc.npage | ||
4310 | 408 | if firstpage: | ||
4311 | 409 | n_start = min(n_end, max(1, firstpage)) | ||
4312 | 410 | if lastpage: | ||
4313 | 411 | n_end = max(n_start, min(n_end, lastpage)) | ||
4314 | 412 | |||
4315 | 413 | for npage in range(n_start, n_end + 1): | ||
4316 | 414 | descriptor = ''.join([pdfdoc.shortname, '\n', _('page'), ' ', str(npage)]) | ||
4317 | 415 | page = pdfdoc.document.get_page(npage-1) | ||
4318 | 416 | w, h = page.get_size() | ||
4319 | 417 | iter = self.model.append((descriptor, # 0 | ||
4320 | 418 | CairoImage(), # 1 | ||
4321 | 419 | pdfdoc.nfile, # 2 | ||
4322 | 420 | npage, # 3 | ||
4323 | 421 | self.zoom_scale, # 4 | ||
4324 | 422 | pdfdoc.filename, # 5 | ||
4325 | 423 | angle, # 6 | ||
4326 | 424 | crop[0],crop[1], # 7-8 | ||
4327 | 425 | crop[2],crop[3], # 9-10 | ||
4328 | 426 | w,h )) # 11-12 | ||
4329 | 427 | self.update_geometry(iter) | ||
4330 | 428 | res = True | ||
4331 | 429 | |||
4332 | 430 | self.reset_iv_width() | ||
4333 | 431 | gobject.idle_add(self.retitle) | ||
4334 | 432 | if res: | ||
4335 | 433 | gobject.idle_add(self.render) | ||
4336 | 434 | return res | ||
4337 | 435 | |||
4338 | 436 | def choose_export_pdf_name(self, widget=None): | ||
4339 | 437 | """Handles choosing a name for exporting """ | ||
4340 | 438 | |||
4341 | 439 | chooser = gtk.FileChooserDialog(title=_('Export ...'), | ||
4342 | 440 | action=gtk.FILE_CHOOSER_ACTION_SAVE, | ||
4343 | 441 | buttons=(gtk.STOCK_CANCEL, | ||
4344 | 442 | gtk.RESPONSE_CANCEL, | ||
4345 | 443 | gtk.STOCK_SAVE, | ||
4346 | 444 | gtk.RESPONSE_OK)) | ||
4347 | 445 | chooser.set_do_overwrite_confirmation(True) | ||
4348 | 446 | chooser.set_current_folder(self.export_directory) | ||
4349 | 447 | filter_pdf = gtk.FileFilter() | ||
4350 | 448 | filter_pdf.set_name(_('PDF files')) | ||
4351 | 449 | filter_pdf.add_mime_type('application/pdf') | ||
4352 | 450 | chooser.add_filter(filter_pdf) | ||
4353 | 451 | |||
4354 | 452 | filter_all = gtk.FileFilter() | ||
4355 | 453 | filter_all.set_name(_('All files')) | ||
4356 | 454 | filter_all.add_pattern('*') | ||
4357 | 455 | chooser.add_filter(filter_all) | ||
4358 | 456 | |||
4359 | 457 | while True: | ||
4360 | 458 | response = chooser.run() | ||
4361 | 459 | if response == gtk.RESPONSE_OK: | ||
4362 | 460 | file_out = chooser.get_filename() | ||
4363 | 461 | (path, shortname) = os.path.split(file_out) | ||
4364 | 462 | (shortname, ext) = os.path.splitext(shortname) | ||
4365 | 463 | if ext.lower() != '.pdf': | ||
4366 | 464 | file_out = file_out + '.pdf' | ||
4367 | 465 | try: | ||
4368 | 466 | self.export_to_file(file_out) | ||
4369 | 467 | self.export_directory = path | ||
4370 | 468 | self.set_unsaved(False) | ||
4371 | 469 | except Exception, e: | ||
4372 | 470 | chooser.destroy() | ||
4373 | 471 | error_msg_dlg = gtk.MessageDialog(flags=gtk.DIALOG_MODAL, | ||
4374 | 472 | type=gtk.MESSAGE_ERROR, | ||
4375 | 473 | message_format=str(e), | ||
4376 | 474 | buttons=gtk.BUTTONS_OK) | ||
4377 | 475 | response = error_msg_dlg.run() | ||
4378 | 476 | if response == gtk.RESPONSE_OK: | ||
4379 | 477 | error_msg_dlg.destroy() | ||
4380 | 478 | return | ||
4381 | 479 | break | ||
4382 | 480 | chooser.destroy() | ||
4383 | 481 | |||
4384 | 482 | def export_to_file(self, file_out): | ||
4385 | 483 | """Export to file""" | ||
4386 | 484 | |||
4387 | 485 | pdf_output = PdfFileWriter() | ||
4388 | 486 | pdf_input = [] | ||
4389 | 487 | for pdfdoc in self.pdfqueue: | ||
4390 | 488 | pdfdoc_inp = PdfFileReader(file(pdfdoc.copyname, 'rb')) | ||
4391 | 489 | if pdfdoc_inp.getIsEncrypted(): | ||
4392 | 490 | try: # Workaround for lp:#355479 | ||
4393 | 491 | stat = pdfdoc_inp.decrypt('') | ||
4394 | 492 | except: | ||
4395 | 493 | stat = 0 | ||
4396 | 494 | if (stat!=1): | ||
4397 | 495 | errmsg = _('File %s is encrypted.\n' | ||
4398 | 496 | 'Support for encrypted files has not been implemented yet.\n' | ||
4399 | 497 | 'File export failed.') % pdfdoc.filename | ||
4400 | 498 | raise Exception, errmsg | ||
4401 | 499 | #FIXME | ||
4402 | 500 | #else | ||
4403 | 501 | # ask for password and decrypt file | ||
4404 | 502 | pdf_input.append(pdfdoc_inp) | ||
4405 | 503 | |||
4406 | 504 | for row in self.model: | ||
4407 | 505 | # add pages from input to output document | ||
4408 | 506 | nfile = row[2] | ||
4409 | 507 | npage = row[3] | ||
4410 | 508 | current_page = copy(pdf_input[nfile-1].getPage(npage-1)) | ||
4411 | 509 | angle = row[6] | ||
4412 | 510 | angle0 = current_page.get("/Rotate",0) | ||
4413 | 511 | crop = [row[7],row[8],row[9],row[10]] | ||
4414 | 512 | if angle != 0: | ||
4415 | 513 | current_page.rotateClockwise(angle) | ||
4416 | 514 | if crop != [0.,0.,0.,0.]: | ||
4417 | 515 | rotate_times = (((angle + angle0) % 360 + 45) / 90) % 4 | ||
4418 | 516 | crop_init = crop | ||
4419 | 517 | if rotate_times != 0: | ||
4420 | 518 | perm = [0,2,1,3] | ||
4421 | 519 | for it in range(rotate_times): | ||
4422 | 520 | perm.append(perm.pop(0)) | ||
4423 | 521 | perm.insert(1,perm.pop(2)) | ||
4424 | 522 | crop = [crop_init[perm[side]] for side in range(4)] | ||
4425 | 523 | #(x1, y1) = current_page.cropBox.lowerLeft | ||
4426 | 524 | #(x2, y2) = current_page.cropBox.upperRight | ||
4427 | 525 | (x1, y1) = [float(xy) for xy in current_page.mediaBox.lowerLeft] | ||
4428 | 526 | (x2, y2) = [float(xy) for xy in current_page.mediaBox.upperRight] | ||
4429 | 527 | x1_new = int(x1 + (x2-x1) * crop[0]) | ||
4430 | 528 | x2_new = int(x2 - (x2-x1) * crop[1]) | ||
4431 | 529 | y1_new = int(y1 + (y2-y1) * crop[3]) | ||
4432 | 530 | y2_new = int(y2 - (y2-y1) * crop[2]) | ||
4433 | 531 | #current_page.cropBox.lowerLeft = (x1_new, y1_new) | ||
4434 | 532 | #current_page.cropBox.upperRight = (x2_new, y2_new) | ||
4435 | 533 | current_page.mediaBox.lowerLeft = (x1_new, y1_new) | ||
4436 | 534 | current_page.mediaBox.upperRight = (x2_new, y2_new) | ||
4437 | 535 | |||
4438 | 536 | pdf_output.addPage(current_page) | ||
4439 | 537 | |||
4440 | 538 | # finally, write "output" to document-output.pdf | ||
4441 | 539 | print(_('exporting to:'), file_out) | ||
4442 | 540 | pdf_output.write(file(file_out, 'wb')) | ||
4443 | 541 | |||
4444 | 542 | def on_action_add_doc_activate(self, widget, data=None): | ||
4445 | 543 | """Import doc""" | ||
4446 | 544 | |||
4447 | 545 | chooser = gtk.FileChooserDialog(title=_('_Import...'), | ||
4448 | 546 | action=gtk.FILE_CHOOSER_ACTION_OPEN, | ||
4449 | 547 | buttons=(gtk.STOCK_CANCEL, | ||
4450 | 548 | gtk.RESPONSE_CANCEL, | ||
4451 | 549 | gtk.STOCK_OPEN, | ||
4452 | 550 | gtk.RESPONSE_OK)) | ||
4453 | 551 | chooser.set_current_folder(self.import_directory) | ||
4454 | 552 | chooser.set_select_multiple(True) | ||
4455 | 553 | |||
4456 | 554 | filter_all = gtk.FileFilter() | ||
4457 | 555 | filter_all.set_name(_('All files')) | ||
4458 | 556 | filter_all.add_pattern('*') | ||
4459 | 557 | chooser.add_filter(filter_all) | ||
4460 | 558 | |||
4461 | 559 | filter_pdf = gtk.FileFilter() | ||
4462 | 560 | filter_pdf.set_name(_('PDF files')) | ||
4463 | 561 | filter_pdf.add_mime_type('application/pdf') | ||
4464 | 562 | chooser.add_filter(filter_pdf) | ||
4465 | 563 | chooser.set_filter(filter_pdf) | ||
4466 | 564 | |||
4467 | 565 | response = chooser.run() | ||
4468 | 566 | if response == gtk.RESPONSE_OK: | ||
4469 | 567 | for filename in chooser.get_filenames(): | ||
4470 | 568 | if os.path.isfile(filename): | ||
4471 | 569 | # FIXME | ||
4472 | 570 | f = gio.File(filename) | ||
4473 | 571 | f_info = f.query_info('standard::content-type') | ||
4474 | 572 | mime_type = f_info.get_content_type() | ||
4475 | 573 | expected_mime_type = 'application/pdf' | ||
4476 | 574 | |||
4477 | 575 | if mime_type == expected_mime_type: | ||
4478 | 576 | self.add_pdf_pages(filename) | ||
4479 | 577 | elif mime_type[:34] == 'application/vnd.oasis.opendocument': | ||
4480 | 578 | print(_('OpenDocument not supported yet!')) | ||
4481 | 579 | elif mime_type[:5] == 'image': | ||
4482 | 580 | print(_('Image file not supported yet!')) | ||
4483 | 581 | else: | ||
4484 | 582 | print(_('File type not supported!')) | ||
4485 | 583 | else: | ||
4486 | 584 | print(_('File %s does not exist') % filename) | ||
4487 | 585 | elif response == gtk.RESPONSE_CANCEL: | ||
4488 | 586 | print(_('Closed, no files selected')) | ||
4489 | 587 | chooser.destroy() | ||
4490 | 588 | gobject.idle_add(self.retitle) | ||
4491 | 589 | |||
4492 | 590 | def clear_selected(self, button=None): | ||
4493 | 591 | """Removes the selected elements in the IconView""" | ||
4494 | 592 | |||
4495 | 593 | model = self.iconview.get_model() | ||
4496 | 594 | selection = self.iconview.get_selected_items() | ||
4497 | 595 | if selection: | ||
4498 | 596 | selection.sort(reverse=True) | ||
4499 | 597 | self.set_unsaved(True) | ||
4500 | 598 | for path in selection: | ||
4501 | 599 | iter = model.get_iter(path) | ||
4502 | 600 | model.remove(iter) | ||
4503 | 601 | path = selection[-1] | ||
4504 | 602 | self.iconview.select_path(path) | ||
4505 | 603 | if not self.iconview.path_is_selected(path): | ||
4506 | 604 | if len(model) > 0: # select the last row | ||
4507 | 605 | row = model[-1] | ||
4508 | 606 | path = row.path | ||
4509 | 607 | self.iconview.select_path(path) | ||
4510 | 608 | self.iconview.grab_focus() | ||
4511 | 609 | |||
4512 | 610 | def iv_drag_begin(self, iconview, context): | ||
4513 | 611 | """Sets custom icon on drag begin for multiple items selected""" | ||
4514 | 612 | |||
4515 | 613 | if len(iconview.get_selected_items()) > 1: | ||
4516 | 614 | iconview.stop_emission('drag_begin') | ||
4517 | 615 | context.set_icon_stock(gtk.STOCK_DND_MULTIPLE, 0, 0) | ||
4518 | 616 | |||
4519 | 617 | def iv_dnd_get_data(self, iconview, context, | ||
4520 | 618 | selection_data, target_id, etime): | ||
4521 | 619 | """Handles requests for data by drag and drop in iconview""" | ||
4522 | 620 | |||
4523 | 621 | model = iconview.get_model() | ||
4524 | 622 | selection = self.iconview.get_selected_items() | ||
4525 | 623 | selection.sort(key=lambda x: x[0]) | ||
4526 | 624 | data = [] | ||
4527 | 625 | for path in selection: | ||
4528 | 626 | if selection_data.target == 'MODEL_ROW_INTERN': | ||
4529 | 627 | data.append(str(path[0])) | ||
4530 | 628 | elif selection_data.target == 'MODEL_ROW_EXTERN': | ||
4531 | 629 | iter = model.get_iter(path) | ||
4532 | 630 | nfile, npage, angle = model.get(iter, 2, 3, 6) | ||
4533 | 631 | crop = model.get(iter, 7, 8, 9, 10) | ||
4534 | 632 | pdfdoc = self.pdfqueue[nfile - 1] | ||
4535 | 633 | data.append('\n'.join([pdfdoc.filename, | ||
4536 | 634 | str(npage), | ||
4537 | 635 | str(angle)] + | ||
4538 | 636 | [str(side) for side in crop])) | ||
4539 | 637 | if data: | ||
4540 | 638 | data = '\n;\n'.join(data) | ||
4541 | 639 | selection_data.set(selection_data.target, 8, data) | ||
4542 | 640 | |||
4543 | 641 | def iv_dnd_received_data(self, iconview, context, x, y, | ||
4544 | 642 | selection_data, target_id, etime): | ||
4545 | 643 | """Handles received data by drag and drop in iconview""" | ||
4546 | 644 | |||
4547 | 645 | model = iconview.get_model() | ||
4548 | 646 | data = selection_data.data | ||
4549 | 647 | if data: | ||
4550 | 648 | data = data.split('\n;\n') | ||
4551 | 649 | drop_info = iconview.get_dest_item_at_pos(x, y) | ||
4552 | 650 | iter_to = None | ||
4553 | 651 | if drop_info: | ||
4554 | 652 | path, position = drop_info | ||
4555 | 653 | ref_to = gtk.TreeRowReference(model,path) | ||
4556 | 654 | else: | ||
4557 | 655 | position = gtk.ICON_VIEW_DROP_RIGHT | ||
4558 | 656 | if len(model) > 0: #find the iterator of the last row | ||
4559 | 657 | row = model[-1] | ||
4560 | 658 | path = row.path | ||
4561 | 659 | ref_to = gtk.TreeRowReference(model,path) | ||
4562 | 660 | if ref_to: | ||
4563 | 661 | before = (position == gtk.ICON_VIEW_DROP_LEFT | ||
4564 | 662 | or position == gtk.ICON_VIEW_DROP_ABOVE) | ||
4565 | 663 | #if target_id == self.MODEL_ROW_INTERN: | ||
4566 | 664 | if selection_data.target == 'MODEL_ROW_INTERN': | ||
4567 | 665 | if before: | ||
4568 | 666 | data.sort(key=int) | ||
4569 | 667 | else: | ||
4570 | 668 | data.sort(key=int,reverse=True) | ||
4571 | 669 | ref_from_list = [gtk.TreeRowReference(model,path) | ||
4572 | 670 | for path in data] | ||
4573 | 671 | for ref_from in ref_from_list: | ||
4574 | 672 | path = ref_to.get_path() | ||
4575 | 673 | iter_to = model.get_iter(path) | ||
4576 | 674 | path = ref_from.get_path() | ||
4577 | 675 | iter_from = model.get_iter(path) | ||
4578 | 676 | row = model[iter_from] | ||
4579 | 677 | if before: | ||
4580 | 678 | model.insert_before(iter_to, row) | ||
4581 | 679 | else: | ||
4582 | 680 | model.insert_after(iter_to, row) | ||
4583 | 681 | if context.action == gtk.gdk.ACTION_MOVE: | ||
4584 | 682 | for ref_from in ref_from_list: | ||
4585 | 683 | path = ref_from.get_path() | ||
4586 | 684 | iter_from = model.get_iter(path) | ||
4587 | 685 | model.remove(iter_from) | ||
4588 | 686 | |||
4589 | 687 | #elif target_id == self.MODEL_ROW_EXTERN: | ||
4590 | 688 | elif selection_data.target == 'MODEL_ROW_EXTERN': | ||
4591 | 689 | if not before: | ||
4592 | 690 | data.reverse() | ||
4593 | 691 | while data: | ||
4594 | 692 | tmp = data.pop(0).split('\n') | ||
4595 | 693 | filename = tmp[0] | ||
4596 | 694 | npage, angle = [int(k) for k in tmp[1:3]] | ||
4597 | 695 | crop = [float(side) for side in tmp[3:7]] | ||
4598 | 696 | if self.add_pdf_pages(filename, npage, npage, | ||
4599 | 697 | angle, crop): | ||
4600 | 698 | if len(model) > 0: | ||
4601 | 699 | path = ref_to.get_path() | ||
4602 | 700 | iter_to = model.get_iter(path) | ||
4603 | 701 | row = model[-1] #the last row | ||
4604 | 702 | path = row.path | ||
4605 | 703 | iter_from = model.get_iter(path) | ||
4606 | 704 | if before: | ||
4607 | 705 | model.move_before(iter_from, iter_to) | ||
4608 | 706 | else: | ||
4609 | 707 | model.move_after(iter_from, iter_to) | ||
4610 | 708 | if context.action == gtk.gdk.ACTION_MOVE: | ||
4611 | 709 | context.finish(True, True, etime) | ||
4612 | 710 | |||
4613 | 711 | def iv_dnd_data_delete(self, widget, context): | ||
4614 | 712 | """Deletes dnd items after a successful move operation""" | ||
4615 | 713 | |||
4616 | 714 | model = self.iconview.get_model() | ||
4617 | 715 | selection = self.iconview.get_selected_items() | ||
4618 | 716 | ref_del_list = [gtk.TreeRowReference(model,path) for path in selection] | ||
4619 | 717 | for ref_del in ref_del_list: | ||
4620 | 718 | path = ref_del.get_path() | ||
4621 | 719 | iter = model.get_iter(path) | ||
4622 | 720 | model.remove(iter) | ||
4623 | 721 | |||
4624 | 722 | def iv_dnd_motion(self, iconview, context, x, y, etime): | ||
4625 | 723 | """Handles the drag-motion signal in order to auto-scroll the view""" | ||
4626 | 724 | |||
4627 | 725 | autoscroll_area = 40 | ||
4628 | 726 | sw_vadj = self.sw.get_vadjustment() | ||
4629 | 727 | sw_height = self.sw.get_allocation().height | ||
4630 | 728 | if y -sw_vadj.get_value() < autoscroll_area: | ||
4631 | 729 | if not self.iv_auto_scroll_timer: | ||
4632 | 730 | self.iv_auto_scroll_direction = gtk.DIR_UP | ||
4633 | 731 | self.iv_auto_scroll_timer = gobject.timeout_add(150, | ||
4634 | 732 | self.iv_auto_scroll) | ||
4635 | 733 | elif y -sw_vadj.get_value() > sw_height - autoscroll_area: | ||
4636 | 734 | if not self.iv_auto_scroll_timer: | ||
4637 | 735 | self.iv_auto_scroll_direction = gtk.DIR_DOWN | ||
4638 | 736 | self.iv_auto_scroll_timer = gobject.timeout_add(150, | ||
4639 | 737 | self.iv_auto_scroll) | ||
4640 | 738 | elif self.iv_auto_scroll_timer: | ||
4641 | 739 | gobject.source_remove(self.iv_auto_scroll_timer) | ||
4642 | 740 | self.iv_auto_scroll_timer = None | ||
4643 | 741 | |||
4644 | 742 | def iv_dnd_leave_end(self, widget, context, ignored=None): | ||
4645 | 743 | """Ends the auto-scroll during DND""" | ||
4646 | 744 | |||
4647 | 745 | if self.iv_auto_scroll_timer: | ||
4648 | 746 | gobject.source_remove(self.iv_auto_scroll_timer) | ||
4649 | 747 | self.iv_auto_scroll_timer = None | ||
4650 | 748 | |||
4651 | 749 | def iv_auto_scroll(self): | ||
4652 | 750 | """Timeout routine for auto-scroll""" | ||
4653 | 751 | |||
4654 | 752 | sw_vadj = self.sw.get_vadjustment() | ||
4655 | 753 | sw_vpos = sw_vadj.get_value() | ||
4656 | 754 | if self.iv_auto_scroll_direction == gtk.DIR_UP: | ||
4657 | 755 | sw_vpos -= sw_vadj.step_increment | ||
4658 | 756 | sw_vadj.set_value(max(sw_vpos, sw_vadj.lower)) | ||
4659 | 757 | elif self.iv_auto_scroll_direction == gtk.DIR_DOWN: | ||
4660 | 758 | sw_vpos += sw_vadj.step_increment | ||
4661 | 759 | sw_vadj.set_value(min(sw_vpos, sw_vadj.upper - sw_vadj.page_size)) | ||
4662 | 760 | return True #call me again | ||
4663 | 761 | |||
4664 | 762 | def iv_button_press_event(self, iconview, event): | ||
4665 | 763 | """Manages mouse clicks on the iconview""" | ||
4666 | 764 | |||
4667 | 765 | if event.button == 3: | ||
4668 | 766 | x = int(event.x) | ||
4669 | 767 | y = int(event.y) | ||
4670 | 768 | time = event.time | ||
4671 | 769 | path = iconview.get_path_at_pos(x, y) | ||
4672 | 770 | selection = iconview.get_selected_items() | ||
4673 | 771 | if path: | ||
4674 | 772 | if path not in selection: | ||
4675 | 773 | iconview.unselect_all() | ||
4676 | 774 | iconview.select_path(path) | ||
4677 | 775 | iconview.grab_focus() | ||
4678 | 776 | self.popup.popup(None, None, None, event.button, time) | ||
4679 | 777 | return 1 | ||
4680 | 778 | |||
4681 | 779 | def sw_dnd_received_data(self, scrolledwindow, context, x, y, | ||
4682 | 780 | selection_data, target_id, etime): | ||
4683 | 781 | """Handles received data by drag and drop in scrolledwindow""" | ||
4684 | 782 | |||
4685 | 783 | data = selection_data.data | ||
4686 | 784 | if target_id == self.MODEL_ROW_EXTERN: | ||
4687 | 785 | self.model | ||
4688 | 786 | if data: | ||
4689 | 787 | data = data.split('\n;\n') | ||
4690 | 788 | while data: | ||
4691 | 789 | tmp = data.pop(0).split('\n') | ||
4692 | 790 | filename = tmp[0] | ||
4693 | 791 | npage, angle = [int(k) for k in tmp[1:3]] | ||
4694 | 792 | crop = [float(side) for side in tmp[3:7]] | ||
4695 | 793 | if self.add_pdf_pages(filename, npage, npage, angle, crop): | ||
4696 | 794 | if context.action == gtk.gdk.ACTION_MOVE: | ||
4697 | 795 | context.finish(True, True, etime) | ||
4698 | 796 | elif target_id == self.TEXT_URI_LIST: | ||
4699 | 797 | uri = data.strip() | ||
4700 | 798 | uri_splitted = uri.split() # we may have more than one file dropped | ||
4701 | 799 | for uri in uri_splitted: | ||
4702 | 800 | filename = self.get_file_path_from_dnd_dropped_uri(uri) | ||
4703 | 801 | if os.path.isfile(filename): # is it file? | ||
4704 | 802 | self.add_pdf_pages(filename) | ||
4705 | 803 | |||
4706 | 804 | def sw_button_press_event(self, scrolledwindow, event): | ||
4707 | 805 | """Unselects all items in iconview on mouse click in scrolledwindow""" | ||
4708 | 806 | |||
4709 | 807 | if event.button == 1: | ||
4710 | 808 | self.iconview.unselect_all() | ||
4711 | 809 | |||
4712 | 810 | def sw_scroll_event(self, scrolledwindow, event): | ||
4713 | 811 | """Manages mouse scroll events in scrolledwindow""" | ||
4714 | 812 | |||
4715 | 813 | if event.state & gtk.gdk.CONTROL_MASK: | ||
4716 | 814 | if event.direction == gtk.gdk.SCROLL_UP: | ||
4717 | 815 | self.zoom_change(1) | ||
4718 | 816 | return 1 | ||
4719 | 817 | elif event.direction == gtk.gdk.SCROLL_DOWN: | ||
4720 | 818 | self.zoom_change(-1) | ||
4721 | 819 | return 1 | ||
4722 | 820 | |||
4723 | 821 | def zoom_set(self, level): | ||
4724 | 822 | """Sets the zoom level""" | ||
4725 | 823 | self.zoom_level = max(min(level, 5), -24) | ||
4726 | 824 | self.zoom_scale = 1.1 ** self.zoom_level | ||
4727 | 825 | for row in self.model: | ||
4728 | 826 | row[4] = self.zoom_scale | ||
4729 | 827 | self.reset_iv_width() | ||
4730 | 828 | |||
4731 | 829 | def zoom_change(self, step=5): | ||
4732 | 830 | """Modifies the zoom level""" | ||
4733 | 831 | self.zoom_set(self.zoom_level + step) | ||
4734 | 832 | |||
4735 | 833 | def zoom_in(self, widget=None): | ||
4736 | 834 | """Increases the zoom level by 5 steps""" | ||
4737 | 835 | self.zoom_change(5) | ||
4738 | 836 | |||
4739 | 837 | def zoom_out(self, widget=None, step=5): | ||
4740 | 838 | """Reduces the zoom level by 5 steps""" | ||
4741 | 839 | self.zoom_change(-5) | ||
4742 | 840 | |||
4743 | 841 | def get_file_path_from_dnd_dropped_uri(self, uri): | ||
4744 | 842 | """Extracts the path from an uri""" | ||
4745 | 843 | |||
4746 | 844 | path = urllib.url2pathname(uri) # escape special chars | ||
4747 | 845 | path = path.strip('\r\n\x00') # remove \r\n and NULL | ||
4748 | 846 | |||
4749 | 847 | # get the path to file | ||
4750 | 848 | if path.startswith('file:\\\\\\'): # windows | ||
4751 | 849 | path = path[8:] # 8 is len('file:///') | ||
4752 | 850 | elif path.startswith('file://'): # nautilus, rox | ||
4753 | 851 | path = path[7:] # 7 is len('file://') | ||
4754 | 852 | elif path.startswith('file:'): # xffm | ||
4755 | 853 | path = path[5:] # 5 is len('file:') | ||
4756 | 854 | return path | ||
4757 | 855 | |||
4758 | 856 | def rotate_page_right(self, widget, data=None): | ||
4759 | 857 | self.rotate_page(90) | ||
4760 | 858 | |||
4761 | 859 | def rotate_page_left(self, widget, data=None): | ||
4762 | 860 | self.rotate_page(-90) | ||
4763 | 861 | |||
4764 | 862 | def rotate_page(self, angle): | ||
4765 | 863 | """Rotates the selected page in the IconView""" | ||
4766 | 864 | |||
4767 | 865 | model = self.iconview.get_model() | ||
4768 | 866 | selection = self.iconview.get_selected_items() | ||
4769 | 867 | if len(selection) > 0: | ||
4770 | 868 | self.set_unsaved(True) | ||
4771 | 869 | rotate_times = (((-angle) % 360 + 45) / 90) % 4 | ||
4772 | 870 | if rotate_times is not 0: | ||
4773 | 871 | for path in selection: | ||
4774 | 872 | iter = model.get_iter(path) | ||
4775 | 873 | nfile = model.get_value(iter, 2) | ||
4776 | 874 | npage = model.get_value(iter, 3) | ||
4777 | 875 | |||
4778 | 876 | crop = [0.,0.,0.,0.] | ||
4779 | 877 | perm = [0,2,1,3] | ||
4780 | 878 | for it in range(rotate_times): | ||
4781 | 879 | perm.append(perm.pop(0)) | ||
4782 | 880 | perm.insert(1,perm.pop(2)) | ||
4783 | 881 | crop = [model.get_value(iter, 7 + perm[side]) for side in range(4)] | ||
4784 | 882 | for side in range(4): | ||
4785 | 883 | model.set_value(iter, 7 + side, crop[side]) | ||
4786 | 884 | |||
4787 | 885 | new_angle = model.get_value(iter, 6) + int(angle) | ||
4788 | 886 | new_angle = new_angle % 360 | ||
4789 | 887 | model.set_value(iter, 6, new_angle) | ||
4790 | 888 | self.update_geometry(iter) | ||
4791 | 889 | self.reset_iv_width() | ||
4792 | 890 | |||
4793 | 891 | def crop_page_dialog(self, widget): | ||
4794 | 892 | """Opens a dialog box to define margins for page cropping""" | ||
4795 | 893 | |||
4796 | 894 | sides = ('L', 'R', 'T', 'B') | ||
4797 | 895 | side_names = {'L':_('Left'), 'R':_('Right'), | ||
4798 | 896 | 'T':_('Top'), 'B':_('Bottom') } | ||
4799 | 897 | opposite_sides = {'L':'R', 'R':'L', 'T':'B', 'B':'T' } | ||
4800 | 898 | |||
4801 | 899 | def set_crop_value(spinbutton, side): | ||
4802 | 900 | opp_side = opposite_sides[side] | ||
4803 | 901 | pos = sides.index(opp_side) | ||
4804 | 902 | adj = spin_list[pos].get_adjustment() | ||
4805 | 903 | adj.set_upper(99.0 - spinbutton.get_value()) | ||
4806 | 904 | |||
4807 | 905 | model = self.iconview.get_model() | ||
4808 | 906 | selection = self.iconview.get_selected_items() | ||
4809 | 907 | |||
4810 | 908 | crop = [0.,0.,0.,0.] | ||
4811 | 909 | if selection: | ||
4812 | 910 | path = selection[0] | ||
4813 | 911 | pos = model.get_iter(path) | ||
4814 | 912 | crop = [model.get_value(pos, 7 + side) for side in range(4)] | ||
4815 | 913 | |||
4816 | 914 | dialog = gtk.Dialog(title=(_('Crop Selected Pages')), | ||
4817 | 915 | parent=self.window, | ||
4818 | 916 | flags=gtk.DIALOG_MODAL, | ||
4819 | 917 | buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, | ||
4820 | 918 | gtk.STOCK_OK, gtk.RESPONSE_OK)) | ||
4821 | 919 | dialog.set_size_request(340, 250) | ||
4822 | 920 | dialog.set_default_response(gtk.RESPONSE_OK) | ||
4823 | 921 | |||
4824 | 922 | frame = gtk.Frame(_('Crop Margins')) | ||
4825 | 923 | dialog.vbox.pack_start(frame, False, False, 20) | ||
4826 | 924 | |||
4827 | 925 | vbox = gtk.VBox(False, 0) | ||
4828 | 926 | frame.add(vbox) | ||
4829 | 927 | |||
4830 | 928 | spin_list = [] | ||
4831 | 929 | units = 2 * [_('% of width')] + 2 * [_('% of height')] | ||
4832 | 930 | for side in sides: | ||
4833 | 931 | hbox = gtk.HBox(True, 0) | ||
4834 | 932 | vbox.pack_start(hbox, False, False, 5) | ||
4835 | 933 | |||
4836 | 934 | label = gtk.Label(side_names[side]) | ||
4837 | 935 | label.set_alignment(0, 0.0) | ||
4838 | 936 | hbox.pack_start(label, True, True, 20) | ||
4839 | 937 | |||
4840 | 938 | adj = gtk.Adjustment(100.*crop.pop(0), 0.0, 99.0, 1.0, 5.0, 0.0) | ||
4841 | 939 | spin = gtk.SpinButton(adj, 0, 1) | ||
4842 | 940 | spin.set_activates_default(True) | ||
4843 | 941 | spin.connect('value-changed', set_crop_value, side) | ||
4844 | 942 | spin_list.append(spin) | ||
4845 | 943 | hbox.pack_start(spin, False, False, 30) | ||
4846 | 944 | |||
4847 | 945 | label = gtk.Label(units.pop(0)) | ||
4848 | 946 | label.set_alignment(0, 0.0) | ||
4849 | 947 | hbox.pack_start(label, True, True, 0) | ||
4850 | 948 | |||
4851 | 949 | dialog.show_all() | ||
4852 | 950 | result = dialog.run() | ||
4853 | 951 | |||
4854 | 952 | if result == gtk.RESPONSE_OK: | ||
4855 | 953 | modified = False | ||
4856 | 954 | crop = [spin.get_value()/100. for spin in spin_list] | ||
4857 | 955 | for path in selection: | ||
4858 | 956 | pos = model.get_iter(path) | ||
4859 | 957 | for it in range(4): | ||
4860 | 958 | old_val = model.get_value(pos, 7 + it) | ||
4861 | 959 | model.set_value(pos, 7 + it, crop[it]) | ||
4862 | 960 | if crop[it] != old_val: | ||
4863 | 961 | modified = True | ||
4864 | 962 | self.update_geometry(pos) | ||
4865 | 963 | if modified: | ||
4866 | 964 | self.set_unsaved(True) | ||
4867 | 965 | self.reset_iv_width() | ||
4868 | 966 | elif result == gtk.RESPONSE_CANCEL: | ||
4869 | 967 | print(_('Dialog closed')) | ||
4870 | 968 | dialog.destroy() | ||
4871 | 969 | |||
4872 | 970 | def about_dialog(self, widget, data=None): | ||
4873 | 971 | about_dialog = gtk.AboutDialog() | ||
4874 | 972 | try: | ||
4875 | 973 | about_dialog.set_transient_for(self.window) | ||
4876 | 974 | about_dialog.set_modal(True) | ||
4877 | 975 | except: | ||
4878 | 976 | pass | ||
4879 | 977 | # FIXME | ||
4880 | 978 | about_dialog.set_name(APPNAME) | ||
4881 | 979 | about_dialog.set_version(VERSION) | ||
4882 | 980 | about_dialog.set_comments(_( | ||
4883 | 981 | '%s is a tool for rearranging and modifying PDF files.' \ | ||
4884 | 982 | 'Developed using GTK+ and Python') % APPNAME) | ||
4885 | 983 | about_dialog.set_authors(['Konstantinos Poulios',]) | ||
4886 | 984 | about_dialog.set_website_label(WEBSITE) | ||
4887 | 985 | about_dialog.set_logo_icon_name('pdfshuffler') | ||
4888 | 986 | about_dialog.set_license(LICENSE) | ||
4889 | 987 | about_dialog.connect('response', lambda w, a: about_dialog.destroy()) | ||
4890 | 988 | about_dialog.connect('delete_event', lambda w, a: about_dialog.destroy()) | ||
4891 | 989 | about_dialog.show_all() | ||
4892 | 990 | |||
4893 | 991 | |||
4894 | 992 | class PDF_Doc: | ||
4895 | 993 | """Class handling PDF documents""" | ||
4896 | 994 | |||
4897 | 995 | def __init__(self, filename, nfile, tmp_dir): | ||
4898 | 996 | |||
4899 | 997 | self.filename = os.path.abspath(filename) | ||
4900 | 998 | (self.path, self.shortname) = os.path.split(self.filename) | ||
4901 | 999 | (self.shortname, self.ext) = os.path.splitext(self.shortname) | ||
4902 | 1000 | f = gio.File(filename) | ||
4903 | 1001 | mime_type = f.query_info('standard::content-type').get_content_type() | ||
4904 | 1002 | expected_mime_type = 'application/pdf' | ||
4905 | 1003 | file_prefix = 'file://' | ||
4906 | 1004 | |||
4907 | 1005 | if mime_type == expected_mime_type: | ||
4908 | 1006 | self.nfile = nfile + 1 | ||
4909 | 1007 | self.mtime = os.path.getmtime(filename) | ||
4910 | 1008 | self.copyname = os.path.join(tmp_dir, '%02d_' % self.nfile + | ||
4911 | 1009 | self.shortname + '.pdf') | ||
4912 | 1010 | shutil.copy(self.filename, self.copyname) | ||
4913 | 1011 | self.document = poppler.document_new_from_file (file_prefix + self.copyname, None) | ||
4914 | 1012 | self.npage = self.document.get_n_pages() | ||
4915 | 1013 | else: | ||
4916 | 1014 | self.nfile = 0 | ||
4917 | 1015 | self.npage = 0 | ||
4918 | 1016 | |||
4919 | 1017 | |||
4920 | 1018 | class PDF_Renderer(threading.Thread,gobject.GObject): | ||
4921 | 1019 | |||
4922 | 1020 | def __init__(self, model, pdfqueue): | ||
4923 | 1021 | threading.Thread.__init__(self) | ||
4924 | 1022 | gobject.GObject.__init__(self) | ||
4925 | 1023 | self.model = model | ||
4926 | 1024 | self.pdfqueue = pdfqueue | ||
4927 | 1025 | self.quit = False | ||
4928 | 1026 | |||
4929 | 1027 | def run(self): | ||
4930 | 1028 | for idx, row in enumerate(self.model): | ||
4931 | 1029 | if self.quit: | ||
4932 | 1030 | return | ||
4933 | 1031 | if not row[1].surface: | ||
4934 | 1032 | try: | ||
4935 | 1033 | nfile = row[2] | ||
4936 | 1034 | npage = row[3] | ||
4937 | 1035 | pdfdoc = self.pdfqueue[nfile - 1] | ||
4938 | 1036 | page = pdfdoc.document.get_page(npage-1) | ||
4939 | 1037 | w, h = page.get_size() | ||
4940 | 1038 | thumbnail = CairoImage(int(w), int(h)) | ||
4941 | 1039 | cr = cairo.Context(thumbnail.surface) | ||
4942 | 1040 | page.render(cr) | ||
4943 | 1041 | time.sleep(0.003) | ||
4944 | 1042 | gobject.idle_add(self.emit,'update_thumbnail', idx, thumbnail, | ||
4945 | 1043 | priority=gobject.PRIORITY_LOW) | ||
4946 | 1044 | except Exception,e: | ||
4947 | 1045 | print e | ||
4948 | 1046 | |||
4949 | 1047 | |||
4950 | 1048 | def main(): | ||
4951 | 1049 | """This function starts PdfShuffler""" | ||
4952 | 1050 | gtk.gdk.threads_init() | ||
4953 | 1051 | gobject.threads_init() | ||
4954 | 1052 | PdfShuffler() | ||
4955 | 1053 | gtk.main() | ||
4956 | 1054 | |||
4957 | 1055 | if __name__ == '__main__': | ||
4958 | 1056 | main() | ||
4959 | 1057 | |||
4960 | 0 | 1058 | ||
4961 | === modified file 'po/genpot.sh' | |||
4962 | --- po/genpot.sh 2009-05-16 23:26:32 +0000 | |||
4963 | +++ po/genpot.sh 2012-02-12 00:35:23 +0000 | |||
4964 | @@ -21,4 +21,5 @@ | |||
4965 | 21 | # | 21 | # |
4966 | 22 | 22 | ||
4967 | 23 | # Make translation files | 23 | # Make translation files |
4969 | 24 | xgettext -L python -o po/pdfshuffler.pot pdfshuffler | 24 | intltool-extract --type=gettext/glade data/pdfshuffler.ui |
4970 | 25 | xgettext --language=Python --keyword=_ --keyword=N_ --output=po/pdfshuffler.pot pdfshuffler data/pdfshuffler.ui.h | ||
4971 | 25 | 26 | ||
4972 | === added file 'po/ja.po' | |||
4973 | --- po/ja.po 1970-01-01 00:00:00 +0000 | |||
4974 | +++ po/ja.po 2012-02-12 00:35:23 +0000 | |||
4975 | @@ -0,0 +1,156 @@ | |||
4976 | 1 | # SOME DESCRIPTIVE TITLE. | ||
4977 | 2 | # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER | ||
4978 | 3 | # This file is distributed under the same license as the PACKAGE package. | ||
4979 | 4 | # Toshiharu Kudoh <toshi.kd2@gmail.com>, 2010. | ||
4980 | 5 | # | ||
4981 | 6 | |||
4982 | 7 | msgid "" | ||
4983 | 8 | msgstr "" | ||
4984 | 9 | "Project-Id-Version: 0.5.1\n" | ||
4985 | 10 | "Report-Msgid-Bugs-To: \n" | ||
4986 | 11 | "POT-Creation-Date: 2010-02-11 12:51+JST\n" | ||
4987 | 12 | "PO-Revision-Date: 2010-11-20 01:48+JST\n" | ||
4988 | 13 | "Last-Translator: Toshiharu Kudoh <toshi.kd2@gmail.com>\n" | ||
4989 | 14 | "Language-Team: Japanese <LL@li.org>\n" | ||
4990 | 15 | "MIME-Version: 1.0\n" | ||
4991 | 16 | "Content-Type: text/plain; charset=UTF-8\n" | ||
4992 | 17 | "Content-Transfer-Encoding: 8bit\n" | ||
4993 | 18 | |||
4994 | 19 | #: pdfshuffler:101 pdfshuffler:520 | ||
4995 | 20 | #, python-format | ||
4996 | 21 | msgid "File %s does not exist" | ||
4997 | 22 | msgstr "%s は存在しません" | ||
4998 | 23 | |||
4999 | 24 | #: pdfshuffler:205 pdfshuffler:243 | ||
5000 | 25 | msgid "Delete Page(s)" |
Eep, this breaks my bzr AttributeError: 'DirStateRevisi onTree' object has no attribute 'last_revision'
bzr: ERROR: exceptions.