Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit deb187b9d665771683964c76e2f0932d79cc16d3

Allow changing the order of pages via drag&drop

By storing everything in the GTK ListStore, we can have the TreeView swap
columns and allow the user to rearrange pages before saving the PDF.
scan.py
(32 / 13)
  
433433 'scanning': _('Scanning...'),
434434 'error': _('Error: %s'),
435435 }
436 _MODEL_COLUMNS = [
437 ('preview', gtk.gdk.Pixbuf),
438 ('file', object),
439 ('width_pixel', int),
440 ('height_pixel', int),
441 ('dpi', int),
442 ]
443 _MODEL_COLUMNS_MAP = dict([(name, idx)
444 for idx, (name, type_) in enumerate(_MODEL_COLUMNS)])
436445
437446 def __init__(self, handle):
438447 activity.Activity.__init__(self, handle)
449449 self._scanner_name = None
450450 self._scanners = []
451451 self._scanner_options = {'source': 'ADF'}
452 self._image_infos = []
453452 self._status = 'init'
454453 self._msg_box_buf = None
455454 self._status_entry = None
466466 self._scan_thread.stop_thread()
467467 self._scan_thread.join()
468468 logging.debug('ScanThread finished')
469 [info['file'].close() for info in self._image_infos]
469 self._remove_image_files()
470470 activity.Activity.destroy(self)
471471
472472 def read_file(self, file_path):
579579
580580 images_window = gtk.ScrolledWindow()
581581 images_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
582 self._images_model = gtk.ListStore(gtk.gdk.Pixbuf)
582 model_types = [_type for name_, _type in self._MODEL_COLUMNS]
583 self._images_model = gtk.ListStore(*model_types)
583584 self._images_view = gtk.TreeView(self._images_model)
584585 image_column = gtk.TreeViewColumn('Image', gtk.CellRendererPixbuf(),
585586 pixbuf=0)
586587 self._images_view.append_column(image_column)
587588 self._images_view.set_headers_visible(False)
589 self._images_view.set_reorderable(True)
588590 images_window.add(self._images_view)
589591 images_window.show_all()
590592 vbox.pack_start(images_window, expand=True)
673673 self._reopen_scanner()
674674
675675 def _add_images(self, image_infos):
676 self._image_infos += image_infos
677676 for info in image_infos:
678 self._images_model.append([info['preview']])
677 row = [info[name] for name, type_ in self._MODEL_COLUMNS]
678 self._images_model.append(row)
679679
680680 @trace()
681681 def _save_button_cb(self, *args):
698698
699699 @trace()
700700 def _save_pdf(self, f):
701 width_idx = self._MODEL_COLUMNS_MAP['width_pixel']
702 height_idx = self._MODEL_COLUMNS_MAP['height_pixel']
703 dpi_idx = self._MODEL_COLUMNS_MAP['dpi']
704 file_idx = self._MODEL_COLUMNS_MAP['file']
705
701706 max_width_inch = reduce(max,
702 [info['width_pixel']/info['dpi'] for info in self._image_infos])
707 [row[width_idx] / row[dpi_idx] for row in self._images_model])
703708 max_height_inch = reduce(max,
704 [info['height_pixel']/info['dpi'] for info in self._image_infos])
709 [row[height_idx] / row[dpi_idx] for row in self._images_model])
705710 canvas = reportlab.pdfgen.canvas.Canvas(f,
706711 pagesize=(max_width_inch*units.inch, max_height_inch*units.inch))
707712
708 for info in self._image_infos:
709 width_inch = float(info['width_pixel'])/info['dpi']
710 height_inch = float(info['height_pixel'])/info['dpi']
713 for row in self._images_model:
714 width_inch = float(row[width_idx]) / row[dpi_idx]
715 height_inch = float(row[height_idx]) / row[dpi_idx]
711716 canvas.setFillColorRGB(1, 1, 1)
712717 canvas.rect(0, 0, max_width_inch*units.inch,
713718 max_height_inch*units.inch, fill=1)
714 canvas.drawImage(info['file'].name, 0, 0, width_inch*units.inch,
719 canvas.drawImage(row[file_idx].name, 0, 0, width_inch*units.inch,
715720 height_inch*units.inch)
716721 canvas.showPage()
717722
718723 canvas.save()
719724
725 def _remove_image_files(self):
726 for row in self._images_model:
727 row[self._MODEL_COLUMNS_MAP['file']].close()
728
720729 @trace()
721730 def _save_reply_cb(self, *args):
722 [info['file'].close() for info in self._image_infos]
723 self._image_infos = []
731 self._remove_image_files()
732 self._images_model.clear()
724733
725734 @trace()
726735 def _save_error_cb(self, error):