def on_btn_img_chooser_file_set(self, chooser: Gtk.FileChooserButton): uri: str = chooser.get_uri() logger.debug('Chose file: {}', uri) # There is a limitation of Gio when handling HTTP remote files, # like sometimes it can not read the same file twice (server doesn't handle Range header). # So, for HTTP file, we can support Gio by caching to temporary local file. if uri.startswith(('http://', 'https://')): # Prevent freezing GUI Gtk.main_iteration() chosen_file = cache_http_file(uri) else: chosen_file: Gio.File = Gio.file_new_for_uri(uri) # Check file content type try: content_type = guess_content_type(chosen_file) except GLib.Error as e: logger.error('Failed to open file. Error {}', e) self.show_error('Failed to open file.') return logger.debug('Content type: {}', content_type) if not content_type.startswith('image/'): self.show_error(_('Unsuported file type %s!') % content_type) return self.process_passed_image_file(chosen_file, content_type) self.grab_focus_on_event_box()
def on_btn_img_chooser_file_set(self, chooser: Gtk.FileChooserButton): chosen_file: Gio.File = chooser.get_file() self.raw_result_buffer.set_text('') stream: Gio.FileInputStream = chosen_file.read(None) widget = self.stack_img_source.get_visible_child() size, b = widget.get_allocated_size() # type: Gdk.Rectangle, int scaled_pix = GdkPixbuf.Pixbuf.new_from_stream_at_scale( stream, size.width, size.height, True, None) self.insert_image_to_placeholder(scaled_pix) stream.seek(0, GLib.SeekType.SET) full_buf, etag_out = chosen_file.load_bytes( ) # type: GLib.Bytes, Optional[str] immediate = io.BytesIO(full_buf.get_data()) pim = Image.open(immediate) grayscale = pim.convert('L') w, h = grayscale.size img = zbar.Image(w, h, 'Y800', grayscale.tobytes()) n = self.zbar_scanner.scan(img) logger.debug('Any QR code?: {}', n) if not n: return try: sym = next(iter(img.symbols)) except StopIteration: logger.error( 'Something wrong. Failed to extract symbol from zbar image!') return logger.info('QR type: {}', sym.type) logger.info('Decoded string: {}', sym.data) self.raw_result_buffer.set_text(sym.data)
def on_btn_img_chooser_update_preview(self, chooser: Gtk.FileChooserButton): file_uri: Optional[str] = chooser.get_preview_uri() logger.debug('Chose file: {}', file_uri) if not file_uri: chooser.set_preview_widget_active(False) return gfile = Gio.file_new_for_uri(file_uri) ftype: Gio.FileType = gfile.query_file_type(Gio.FileQueryInfoFlags.NONE, None) if ftype != Gio.FileType.REGULAR: chooser.set_preview_widget_active(False) return stream: Gio.FileInputStream = gfile.read(None) pix = GdkPixbuf.Pixbuf.new_from_stream_at_scale(stream, 200, 400, True, None) preview = chooser.get_preview_widget() logger.debug('Preview: {}', preview) preview.set_from_pixbuf(pix) chooser.set_preview_widget_active(True) return
def _setFileChoosers(self) -> List[FileChooser]: """ Set up the file choosers for the grid. :return: The file choosers created. """ f_cs: List[FileChooser] = [FileChooserButton() for _ in range(4)] for (idx, f_c) in enumerate(f_cs): f_c.set_hexpand(True) setMargin(f_c, 10, 5, 10, 5) f_c.set_halign(Align.FILL) self._fst_grid.attach(f_c, 1, idx, 1, 1) return f_cs
def _filechooserbutton_selection_changed_cb( self, filechooserbutton: Gtk.FileChooserButton, param_name: str): self._params[param_name] = filechooserbutton.get_filename()
def on_blend_file_clicked(self, button: Gtk.FileChooserButton) -> None: self.update_render_settings(button.get_filename())
def on_file_set(button: Gtk.FileChooserButton, callback: Callable): callback(button.get_filename())