[ocrfeeder] Add system-wide OCR engines chooser dialog



commit 6749e6a7990ad69c194af344b1d9ad7cc880e03d
Author: Joaquim Rocha <jrocha cookiemonster local igalia com>
Date:   Mon Mar 22 19:01:22 2010 +0100

    Add system-wide OCR engines chooser dialog

 studio/configuration.py   |   10 ++++++++--
 studio/studioBuilder.py   |   11 +++++++++++
 studio/widgetPresenter.py |   40 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 58 insertions(+), 3 deletions(-)
---
diff --git a/studio/configuration.py b/studio/configuration.py
index e59226c..6c0771d 100644
--- a/studio/configuration.py
+++ b/studio/configuration.py
@@ -277,6 +277,12 @@ class ConfigurationManager:
         if [file_name for file_name in os.listdir(self.user_engines_folder)\
             if file_name.endswith('.xml')]:
             return
+        for engine in self.getEnginesInSystem():
+            engine_file = os.path.join(self.user_engines_folder, engine.name)
+            engine.saveToXml('%s.xml' % engine_file)
+
+    def getEnginesInSystem(self):
+        existing_engines = []
         engines_paths = [(name, getExecPath(conf['engine_path']))\
                          for name, conf in PREDEFINED_ENGINES.items()]
         for name, path in engines_paths:
@@ -293,8 +299,8 @@ class ConfigurationManager:
                             arguments,
                             image_format = image_format,
                             failure_string = failure_string)
-            engine_file = os.path.join(self.user_engines_folder, name)
-            engine.saveToXml('%s.xml' % engine_file)
+            existing_engines.append(engine)
+        return existing_engines
     
     def setTemporaryDir(self, temp_dir):
         self.temporary_dir = temp_dir
diff --git a/studio/studioBuilder.py b/studio/studioBuilder.py
index 1d4b54a..f9ebaf5 100644
--- a/studio/studioBuilder.py
+++ b/studio/studioBuilder.py
@@ -95,6 +95,17 @@ class Studio:
             self.__addImagesToReviewer(lib.getImagesFromFolder(dirs[0]))
         
         self.main_window.setHasImages(not self.source_images_selector.isEmpty())
+
+        # Show dialog to choose system-wide OCR engines when no engine was found
+        if not self.ocr_engines:
+            engines = self.configuration_manager.getEnginesInSystem()
+            if engines:
+                add_engines_dialog = widgetPresenter.SystemEnginesDialog(engines)
+                response = add_engines_dialog.run()
+                if response == gtk.RESPONSE_ACCEPT:
+                    for engine in add_engines_dialog.getChosenEngines():
+                        self.ocr_engines_manager.addNewEngine(engine)
+                add_engines_dialog.destroy()
     
     def run(self):
         gtk.main()
diff --git a/studio/widgetPresenter.py b/studio/widgetPresenter.py
index bd267fd..0c06162 100644
--- a/studio/widgetPresenter.py
+++ b/studio/widgetPresenter.py
@@ -1054,7 +1054,45 @@ class PreferencesDialog(gtk.Dialog):
         color_button.set_use_alpha(True)
         color_button.set_alpha(values[3] << 8)
         return color_button
-        
+
+class SystemEnginesDialog(gtk.Dialog):
+
+    def __init__(self, engines):
+        super(SystemEnginesDialog, self).__init__(_('OCR Engines'),
+                                                  flags = gtk.DIALOG_MODAL |
+                                                          gtk.DIALOG_DESTROY_WITH_PARENT,
+                                                  buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
+                                                             gtk.STOCK_ADD, gtk.RESPONSE_ACCEPT))
+        self.set_size_request(300, -1)
+        self.set_icon_from_file(WINDOW_ICON)
+        self.list_store = gtk.ListStore(bool, str, gobject.TYPE_PYOBJECT)
+        for engine in engines:
+            self.list_store.append((True, engine.name, engine))
+        self.vbox.pack_start(self.__makeMainArea(), True, True, 0)
+        self.vbox.show_all()
+
+    def __makeMainArea(self):
+        frame = PlainFrame(_('Engines to be added'))
+        self.tree_view = gtk.TreeView(self.list_store)
+        renderer = gtk.CellRendererToggle()
+        renderer.set_property('activatable', True)
+        renderer.connect('toggled', self.__includeEngineToggledCb, 0)
+        column = gtk.TreeViewColumn(_('Include'), renderer)
+        column.add_attribute(renderer, "active", 0)
+        self.tree_view.append_column(column)
+        renderer = gtk.CellRendererText()
+        column = gtk.TreeViewColumn(_('Engine'), renderer, text = 1)
+        self.tree_view.append_column(column)
+        self.tree_view.set_search_column(1)
+        frame.add(self.tree_view)
+        return frame
+
+    def __includeEngineToggledCb(self, renderer, path, column):
+        self.list_store[path][column] = not self.list_store[path][column]
+
+    def getChosenEngines(self):
+        return [engine for include, name, engine in self.list_store if include]
+
 class OcrManagerDialog(gtk.Dialog):
     
     def __init__(self, engines_manager):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]