home *** CD-ROM | disk | FTP | other *** search
/ Freelog 116 / FreelogNo116-JuilletSeptembre2013.iso / Bureautique / gImageReader / gimagereader_0.9-1_win32.exe / bin / gimagereader / acquire.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2011-03-24  |  11KB  |  326 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import os
  5. import sys
  6. import gtk
  7. import gobject
  8. import subprocess
  9. import json
  10. import time
  11. import gettext
  12. _ = gettext.gettext
  13. import dialogs
  14. import threadtools
  15.  
  16. class SaneAPI:
  17.     
  18.     def __init__(self):
  19.         sane.init()
  20.         self.devices = None
  21.         self.device = None
  22.         self.img = None
  23.  
  24.     
  25.     def get_devices(self):
  26.         ''' Returns a list of devices. '''
  27.         self.devices = sane.get_devices()
  28.         return [ device[2] for device in self.devices ]
  29.  
  30.     
  31.     def open_device(self, idx):
  32.         ''' Attempts to load the specified device. '''
  33.         
  34.         try:
  35.             self.device = sane.open(self.devices[idx][0])
  36.             return (True, None)
  37.         except sane._sane.error:
  38.             e = None
  39.             return (False, '%s:\n%s' % (_('Failed to open the selected device'), e))
  40.  
  41.  
  42.     
  43.     def close_device(self):
  44.         ''' Closes the current device. '''
  45.         if self.device != None:
  46.             self.device.close()
  47.             self.device = None
  48.  
  49.     
  50.     def scan(self, output, resolution, mode):
  51.         ''' Acquires an image from the current device with specified resolution and mode. '''
  52.         self.device.resolution = resolution
  53.         self.device.mode = mode
  54.         
  55.         try:
  56.             img = self.device.scan()
  57.         except sane._sane.error:
  58.             e = None
  59.             return (False, '%s:\n%s' % (_('Failed to retreive data'), e))
  60.  
  61.         
  62.         try:
  63.             img.save(output)
  64.         except Exception:
  65.             e = None
  66.             return (False, '%s:\n%s' % (_('Failed to save image'), e))
  67.  
  68.         return (True, None)
  69.  
  70.     
  71.     def cancel_scan(self):
  72.         ''' Cancels the scan. '''
  73.         self.device.cancel()
  74.  
  75.  
  76.  
  77. class TwainAPI:
  78.     
  79.     def __init__(self):
  80.         self.device = None
  81.         self.devices = None
  82.  
  83.     
  84.     def get_devices(self):
  85.         ''' Returns a list of devices. '''
  86.         
  87.         try:
  88.             self.sm.destroy()
  89.         except:
  90.             pass
  91.  
  92.         self.sm = twain.SourceManager(0)
  93.         self.devices = self.sm.GetSourceList()
  94.         return self.devices
  95.  
  96.     
  97.     def open_device(self, idx):
  98.         ''' Attempts to load the specified device. '''
  99.         
  100.         try:
  101.             self.device = self.sm.OpenSource(self.devices[idx])
  102.             return (True, None)
  103.         except Exception:
  104.             e = None
  105.             return (False, '%s:\n%s' % (_('Failed to open the selected device'), e))
  106.  
  107.  
  108.     
  109.     def close_device(self):
  110.         ''' Closes the current device. '''
  111.         
  112.         try:
  113.             self.device.destroy()
  114.         except Exception:
  115.             e = None
  116.  
  117.  
  118.     
  119.     def scan(self, output, resolution, mode):
  120.         ''' Acquires an image from the current device with specified resolution and mode. '''
  121.         ext = os.path.splitext(output)[1].lower()
  122.         
  123.         try:
  124.             if ext == '.jpg' or ext == '.jpeg':
  125.                 self.device.SetXferFileName(output, twain.TWFF_JFIF)
  126.             elif ext == '.tif' or ext == '.tiff':
  127.                 self.device.SetXferFileName(output, twain.TWFF_TIFF)
  128.             else:
  129.                 self.device.SetXferFileName(output, twain.TWFF_PNG)
  130.         except Exception:
  131.             e = None
  132.             return (False, '%s:\n%s' % (_('Failed to set output file'), e))
  133.  
  134.         
  135.         try:
  136.             self.device.SetCapability(twain.ICAP_XRESOLUTION, twain.TWTY_FIX32, resolution)
  137.             self.device.SetCapability(twain.ICAP_YRESOLUTION, twain.TWTY_FIX32, resolution)
  138.         except:
  139.             pass
  140.  
  141.         
  142.         try:
  143.             self.device.RequestAcquire(0, 1)
  144.             while not self.device.GetImageInfo():
  145.                 time.sleep(0.5)
  146.             self.device.XferImageByFile()
  147.         except Exception:
  148.             e = None
  149.             return (False, '%s:\n%s' % (_('Failed to retreive data'), e))
  150.  
  151.         return (True, None)
  152.  
  153.     
  154.     def cancel_scan(self):
  155.         ''' Cancels the scan. '''
  156.         
  157.         try:
  158.             self.device.CancelAllPendingXfers()
  159.         except Exception:
  160.             e = None
  161.  
  162.  
  163.  
  164.  
  165. try:
  166.     import sane
  167.     scan_api = SaneAPI()
  168. except:
  169.     
  170.     try:
  171.         import twain
  172.         scan_api = TwainAPI()
  173.     scan_api = None
  174.     print >>sys.stderr, 'Could not load sane or twain, scanning will not work.'
  175.  
  176.  
  177.  
  178. class Acquire:
  179.     
  180.     def __init__(self, builder, main):
  181.         if scan_api == None:
  182.             builder.get_object('tb_acquire').hide()
  183.             builder.get_object('menu_acquire').hide()
  184.             builder.get_object('table_acquire').hide()
  185.             return None
  186.         self.main = None
  187.         self.devices = []
  188.         self.resolutions = (75, 100, 200, 300, 600, 1200)
  189.         self.output = os.path.join(os.path.expanduser('~'), 'scan1.png')
  190.         self.dialog_output = dialogs.image_save_dialog(main.window)
  191.         self.scan_count = 1
  192.         while os.path.exists(self.output):
  193.             self.scan_count += 1
  194.             self.output = os.path.join(os.path.expanduser('~'), 'scan' + str(self.scan_count) + '.png')
  195.         self.notebook_sources = builder.get_object('notebook_sources')
  196.         self.menu_toggle_sources = builder.get_object('menu_toggle_sources')
  197.         self.button_scan = builder.get_object('button_acquire_scan')
  198.         self.label_messages = builder.get_object('label_acquire_messages')
  199.         self.label_output = builder.get_object('label_acquire_outputfile')
  200.         self.button_output = builder.get_object('button_acquire_output')
  201.         self.hbox_source = builder.get_object('hbox_acquire_source')
  202.         self.button_refresh = builder.get_object('button_acquire_refresh')
  203.         self.spinner = threadtools.Spinner()
  204.         self.hbuttonbox = builder.get_object('hbuttonbox_acquire')
  205.         self.button_cancel = gtk.Button(stock = gtk.STOCK_CANCEL)
  206.         self.button_cancel.show()
  207.         self._Acquire__set_output(None, self.output)
  208.         self.combo_source = gtk.combo_box_new_text()
  209.         builder.get_object('hbox_acquire_source').pack_start(self.combo_source)
  210.         self.combo_source.show()
  211.         self.combo_resolution = gtk.combo_box_new_text()
  212.         builder.get_object('table_acquire').attach(self.combo_resolution, 1, 2, 2, 3, xoptions = gtk.EXPAND | gtk.FILL, yoptions = gtk.FILL)
  213.         self.combo_resolution.show()
  214.         for resolution in self.resolutions:
  215.             self.combo_resolution.append_text('%d dpi' % resolution)
  216.         
  217.         self.combo_resolution.set_active(2)
  218.         builder.get_object('tb_acquire').connect('clicked', self._Acquire__show)
  219.         builder.get_object('menu_acquire').connect('activate', self._Acquire__show)
  220.         self.button_refresh.connect('clicked', self._Acquire__load_devices)
  221.         self.button_output.connect('clicked', self._Acquire__set_output)
  222.         self.button_scan.connect('clicked', self._Acquire__scan)
  223.         self.button_cancel.connect('clicked', self._Acquire__scan_cancel)
  224.         self.spinner.connect('size-allocate', self._Acquire__set_spinner_size)
  225.         self._Acquire__load_devices()
  226.  
  227.     
  228.     def __set_spinner_size(self, widget, allocation):
  229.         ''' Called when the spinner needs to be resized. '''
  230.         self.spinner.set_size_request(allocation.height - 4, allocation.height - 4)
  231.  
  232.     
  233.     def __show(self, widget = None):
  234.         ''' Shows the acquire UI in the left pane. '''
  235.         self.menu_toggle_sources.set_active(True)
  236.         self.notebook_sources.set_current_page(1)
  237.  
  238.     
  239.     def __load_devices(self, widget = None):
  240.         ''' Initiates loading devices. '''
  241.         self.label_messages.set_markup('')
  242.         self.combo_source.get_model().clear()
  243.         self.button_scan.set_sensitive(False)
  244.         self.hbox_source.remove(self.button_refresh)
  245.         self.hbox_source.pack_end(self.spinner, False)
  246.         self.spinner.start()
  247.         threadtools.SubprocessThread(self._Acquire__load_devices_do, self._Acquire__load_devices_end).start()
  248.  
  249.     
  250.     def __load_devices_do(self):
  251.         ''' Queries devices. '''
  252.         self.devices = scan_api.get_devices()
  253.         if not self.devices:
  254.             return False
  255.         return None
  256.  
  257.     
  258.     def __load_devices_end(self, result):
  259.         ''' Called when done loading devices. '''
  260.         self.spinner.stop()
  261.         self.hbox_source.remove(self.spinner)
  262.         self.hbox_source.pack_end(self.button_refresh, False)
  263.         if result == False:
  264.             self.label_messages.set_markup('<span color="red">%s</span>' % _('No scanners were detected.'))
  265.             return None
  266.         None.button_scan.set_sensitive(True)
  267.         for device in self.devices:
  268.             self.combo_source.append_text(device)
  269.         
  270.         self.combo_source.set_active(0)
  271.  
  272.     
  273.     def __scan(self, widget = None):
  274.         ''' Starts the scan process. '''
  275.         self.hbuttonbox.remove(self.button_scan)
  276.         self.button_cancel.set_sensitive(False)
  277.         self.hbuttonbox.pack_start(self.button_cancel)
  278.         self.label_messages.set_text(_('Opening device...'))
  279.         threadtools.SubprocessThread(self._Acquire__scan_do, self._Acquire__scan_done).start()
  280.  
  281.     
  282.     def __scan_do(self):
  283.         ''' Performs the scan. '''
  284.         ret = scan_api.open_device(self.combo_source.get_active())
  285.         if ret[0] == False:
  286.             self.label_messages.set_markup('<span color="red">%s</span>' % ret[1])
  287.             return False
  288.         None.label_messages.set_text(_('Transferring data...'))
  289.         self.button_cancel.set_sensitive(True)
  290.         ret = scan_api.scan(self.output, self.resolutions[self.combo_resolution.get_active()], 'color')
  291.         if ret[0] == False:
  292.             self.label_messages.set_markup('<span color="red">%s</span>' % ret[1])
  293.         scan_api.close_device()
  294.         return ret[0]
  295.  
  296.     
  297.     def __scan_done(self, result):
  298.         ''' Called when scan is complete. '''
  299.         self.hbuttonbox.remove(self.button_cancel)
  300.         self.hbuttonbox.pack_start(self.button_scan)
  301.         if result == False:
  302.             return None
  303.         None.label_messages.set_text('')
  304.         self.main.get_filelist().open_images(None, [
  305.             self.output])
  306.         self.scan_count += 1
  307.         self.output = os.path.join(os.path.dirname(self.output), 'scan' + str(self.scan_count) + '.png')
  308.         self._Acquire__set_output(None, self.output)
  309.  
  310.     
  311.     def __scan_cancel(self, widget = None):
  312.         ''' Callback to cancel scan button. '''
  313.         scan_api.cancel_scan()
  314.  
  315.     
  316.     def __set_output(self, widget = None, path = None):
  317.         ''' Callback to output file chooser. '''
  318.         if path == None:
  319.             path = self.dialog_output.run(self.output)
  320.         if path != None:
  321.             self.output = path
  322.             self.label_output.set_text(path)
  323.             self.button_output.set_tooltip_text(self.output)
  324.  
  325.  
  326.