|
| 1 | +""" |
| 2 | +BigOcrPdf - Image OCR Application Module |
| 3 | +
|
| 4 | +Standalone GTK application for Image OCR, separate from the PDF application. |
| 5 | +Uses a different application_id for proper Wayland taskbar grouping. |
| 6 | +""" |
| 7 | + |
| 8 | +import gi |
| 9 | + |
| 10 | +gi.require_version("Gtk", "4.0") |
| 11 | +gi.require_version("Adw", "1") |
| 12 | +from gi.repository import Adw, Gio, GLib |
| 13 | + |
| 14 | +from bigocrpdf.ui.image_ocr_window import ImageOcrWindow |
| 15 | +from bigocrpdf.ui.widgets import load_css |
| 16 | +from bigocrpdf.utils.i18n import _ |
| 17 | +from bigocrpdf.utils.logger import logger |
| 18 | + |
| 19 | +# Constants for the standalone image app (avoid circular import from config) |
| 20 | +IMAGE_APP_ID = "br.com.biglinux.bigocrimage" |
| 21 | +IMAGE_APP_ICON = "bigocrimage" |
| 22 | +IMAGE_APP_VERSION = "3.0.0" |
| 23 | + |
| 24 | + |
| 25 | +class ImageOcrApp(Adw.Application): |
| 26 | + """Standalone application for Image OCR. |
| 27 | +
|
| 28 | + Uses a separate application_id (br.com.biglinux.bigocrimage) so that |
| 29 | + it appears as a separate application in the Wayland taskbar. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self) -> None: |
| 33 | + """Initialize the image OCR application.""" |
| 34 | + super().__init__( |
| 35 | + application_id=IMAGE_APP_ID, |
| 36 | + flags=Gio.ApplicationFlags.HANDLES_OPEN, |
| 37 | + ) |
| 38 | + |
| 39 | + # Add version command line option |
| 40 | + self.add_main_option( |
| 41 | + "version", |
| 42 | + ord("v"), |
| 43 | + GLib.OptionFlags.NONE, |
| 44 | + GLib.OptionArg.NONE, |
| 45 | + _("Print version information and exit"), |
| 46 | + None, |
| 47 | + ) |
| 48 | + |
| 49 | + self.connect("activate", self.on_activate) |
| 50 | + self.connect("open", self.on_open) |
| 51 | + self.connect("handle-local-options", self.on_handle_local_options) |
| 52 | + |
| 53 | + # Set up application icon |
| 54 | + self._setup_actions() |
| 55 | + |
| 56 | + def _setup_actions(self) -> None: |
| 57 | + """Set up application actions.""" |
| 58 | + # About action |
| 59 | + about_action = Gio.SimpleAction.new("about", None) |
| 60 | + about_action.connect("activate", self.on_about_action) |
| 61 | + self.add_action(about_action) |
| 62 | + |
| 63 | + # Quit action |
| 64 | + quit_action = Gio.SimpleAction.new("quit", None) |
| 65 | + quit_action.connect("activate", lambda *_: self.quit()) |
| 66 | + self.add_action(quit_action) |
| 67 | + |
| 68 | + # Set keyboard shortcuts |
| 69 | + self.set_accels_for_action("app.quit", ["<Control>q"]) |
| 70 | + self.set_accels_for_action("app.about", ["F1"]) |
| 71 | + |
| 72 | + def on_handle_local_options(self, app: Adw.Application, options: GLib.VariantDict) -> int: |
| 73 | + """Handle command line options.""" |
| 74 | + if options.contains("version"): |
| 75 | + print(f"Big Image OCR {IMAGE_APP_VERSION}") |
| 76 | + return 0 |
| 77 | + return -1 |
| 78 | + |
| 79 | + def on_activate(self, app: Adw.Application) -> None: |
| 80 | + """Callback for application activation.""" |
| 81 | + try: |
| 82 | + load_css() |
| 83 | + win = self.get_active_window() |
| 84 | + if not win: |
| 85 | + win = ImageOcrWindow(app) |
| 86 | + logger.info("Started Image OCR application") |
| 87 | + win.present() |
| 88 | + except Exception as e: |
| 89 | + logger.error(f"Error activating Image OCR: {e}") |
| 90 | + |
| 91 | + def on_open( |
| 92 | + self, |
| 93 | + app: Adw.Application, |
| 94 | + files: list[Gio.File], |
| 95 | + n_files: int, |
| 96 | + hint: str, |
| 97 | + ) -> None: |
| 98 | + """Handle opening files.""" |
| 99 | + self.on_activate(app) |
| 100 | + win = self.get_active_window() |
| 101 | + |
| 102 | + if win and files: |
| 103 | + # Open the first image file |
| 104 | + file_path = files[0].get_path() |
| 105 | + if file_path and hasattr(win, "open_image"): |
| 106 | + win.open_image(file_path) |
| 107 | + logger.info(f"Opened image: {file_path}") |
| 108 | + |
| 109 | + def on_about_action(self, action: Gio.SimpleAction, param: None) -> None: |
| 110 | + """Show the About dialog.""" |
| 111 | + about = Adw.AboutDialog.new() |
| 112 | + about.set_application_name("Big Image OCR") |
| 113 | + about.set_application_icon(IMAGE_APP_ICON) |
| 114 | + about.set_version(IMAGE_APP_VERSION) |
| 115 | + about.set_comments(_("Extract text from images using OCR")) |
| 116 | + about.set_website("https://www.biglinux.com.br") |
| 117 | + about.set_developers(["BigLinux https://github.com/biglinux/bigocrpdf"]) |
| 118 | + about.set_license_type(Gio.License.GPL_3_0) |
| 119 | + about.present(self.get_active_window()) |
0 commit comments