lib/alexandria/ui/ui_manager.rb in alexandria-book-collection-manager-0.7.5 vs lib/alexandria/ui/ui_manager.rb in alexandria-book-collection-manager-0.7.6

- old
+ new

@@ -13,10 +13,11 @@ module UI class UIManager < BuilderBase attr_accessor :main_app, :actiongroup, :appbar, :prefs, :listview, :iconview, :listview_model, :iconview_model, :filtered_model attr_reader :model + include Logging include GetText GetText.bindtextdomain(Alexandria::TEXTDOMAIN, charset: "UTF-8") def initialize(parent) @@ -72,11 +73,11 @@ def setup_dependents @listview_model = Gtk::TreeModelSort.new(@filtered_model) @iconview_model = Gtk::TreeModelSort.new(@filtered_model) @listview_manager = ListViewManager.new @listview, self @iconview_manager = IconViewManager.new @iconview, self - @sidepane_manager = SidePaneManager.new @library_listview, self + @sidepane_manager = SidepaneManager.new @library_listview, self @library_listview = @sidepane_manager.library_listview @listview_manager.setup_listview_columns_visibility @listview_manager.setup_listview_columns_width end @@ -181,19 +182,20 @@ end def setup_book_providers log.debug { "setup_book_providers" } mid = @uimanager.new_merge_id - BookProviders.each do |provider| + ui_paths = ["ui/MainMenubar/ViewMenu/OnlineInformation/", + "ui/BookPopup/OnlineInformation/", + "ui/NoBookPopup/OnlineInformation/"] + BookProviders.list.each do |provider| name = provider.action_name - ["ui/MainMenubar/ViewMenu/OnlineInformation/", - "ui/BookPopup/OnlineInformation/", - "ui/NoBookPopup/OnlineInformation/"].each do |path| - log.debug { "Adding #{name} to #{path}" } - @uimanager.add_ui(mid, path, name, name, - :menuitem, false) - end + ui_paths.each do |path| + log.debug { "Adding #{name} to #{path}" } + @uimanager.add_ui(mid, path, name, name, + :menuitem, false) + end end end def add_menus_and_popups_from_xml log.debug { "add_menus_and_popups_from_xml" } @@ -369,29 +371,29 @@ (event.event_type == :button_press) && (event.button == 3) end def on_books_button_press_event(widget, event) log.debug { "books_button_press_event" } - if event_is_right_click event - widget.grab_focus + event_is_right_click event or return - if (path = widget.get_path_at_pos(event.x.to_i, event.y.to_i)) - obj, path = - widget.is_a?(Gtk::TreeView) ? [widget.selection, path.first] : [widget, path] + widget.grab_focus - unless obj.path_is_selected?(path) - log.debug { "Select #{path}" } - widget.unselect_all - obj.select_path(path) - end - else + if (path = widget.get_path_at_pos(event.x.to_i, event.y.to_i)) + obj, path = + widget.is_a?(Gtk::TreeView) ? [widget.selection, path.first] : [widget, path] + + unless obj.path_is_selected?(path) + log.debug { "Select #{path}" } widget.unselect_all + obj.select_path(path) end - - menu = selected_books.empty? ? @nobook_popup : @book_popup - menu.popup(nil, nil, event.button, event.time) + else + widget.unselect_all end + + menu = selected_books.empty? ? @nobook_popup : @book_popup + menu.popup(nil, nil, event.button, event.time) end def get_library_selection_text(library) case library.length when 0 @@ -473,11 +475,11 @@ # Sensitize providers URL if books.length == 1 b = books.first # FIXME: Clean up endless negation in this logic no_urls = true - BookProviders.each do |provider| + BookProviders.list.each do |provider| has_no_url = true begin has_no_url = (b.isbn.nil? || b.isbn.strip.empty? || provider.url(b).nil?) rescue StandardError => ex log.warn { "Error determining URL from #{provider.name}; #{ex.message}" } @@ -520,47 +522,29 @@ def on_close_sidepane log.debug { "on_close_sidepane" } @actiongroup["Sidepane"].active = false end + # TODO: Figure out why this frequently selects the wrong book! def select_a_book(book) - select_this_book = proc do |bk, view| - @filtered_model.refilter - iter = iter_from_book bk - next unless iter - - path = iter.path - next unless view.model - - path = view_path_to_model_path(view, path) - log.debug { "Path for #{bk.ident} is #{path}" } - selection = view.respond_to?(:selection) ? @listview.selection : @iconview - selection.unselect_all - selection.select_path(path) - end - begin - log.debug { "select_a_book: listview" } - select_this_book.call(book, @listview) - log.debug { "select_a_book: listview" } - select_this_book.call(book, @iconview) - rescue StandardError => ex - trace = ex.backtrace.join("\n> ") - log.warn { "Failed to automatically select book: #{ex.message} #{trace}" } - end - # TODO: Figure out why this frequently selects the wrong book! + log.debug { "select_a_book: listview" } + select_book_in_view(book, @listview) + log.debug { "select_a_book: iconview" } + select_book_in_view(book, @iconview) end def update(*ary) log.debug { "on_update #{ary}" } caller = ary.first - if caller.is_a?(UndoManager) + case caller + when UndoManager @actiongroup["Undo"].sensitive = caller.can_undo? @actiongroup["Redo"].sensitive = caller.can_redo? - elsif caller.is_a?(Library) + when Library handle_update_caller_library ary unless caller.updating? else - raise "unrecognized update event" + raise _("unrecognized update event") end end def handle_update_caller_library(ary) library, kind, book = ary @@ -582,12 +566,10 @@ elsif selected_library.is_a?(SmartLibrary) refresh_books end end - # private - def open_web_browser(url) if url.nil? log.warn("Attempt to open browser with nil url") return end @@ -603,11 +585,11 @@ end end end def load_libraries - log.info { "Loading libraries..." } + log.info { _("Loading libraries...") } @completion_models = CompletionModels.instance if @libraries detach_old_libraries @libraries.reload else @@ -748,11 +730,11 @@ if icon.height > ICON_HEIGHT new_width = icon.width / (icon.height / ICON_HEIGHT.to_f) new_height = [ICON_HEIGHT, icon.height].min icon = cache_scaled_icon(icon, new_width, new_height) end - icon = icon.tag(Icons::FAVORITE_TAG) if rating == Book::MAX_RATING_STARS + icon = Icons.tag_icon(icon, Icons::FAVORITE_TAG) if rating == Book::MAX_RATING_STARS iter[Columns::COVER_ICON] = icon log.debug { "Full iter: " + (0..15).map { |num| iter[num].inspect }.join(", ") } end def append_book(book, _tail = nil) @@ -889,11 +871,11 @@ end def iter_from_ident(ident) log.debug { ident.to_s } iter = @model.iter_first - ok = true + ok = true if iter while ok return iter if iter[Columns::IDENT] == ident ok = iter.next! end @@ -969,11 +951,11 @@ @actiongroup["AsList"] end end def restore_preferences - log.debug { "Restoring preferences..." } + log.debug { "Restoring preferences" } if @prefs.maximized @main_app.maximize else @main_app.move(*@prefs.position) unless @prefs.position == [0, 0] @main_app.resize(*@prefs.size) @@ -1043,11 +1025,11 @@ undoable_move(selected_library, library, books) end def setup_move_actions @actiongroup.actions.each do |action| - next unless /^MoveIn/.match?(action.name) + next unless action.name.start_with?("MoveIn") @actiongroup.remove_action(action) end actions = [] @libraries.all_regular_libraries.each do |library| @@ -1058,14 +1040,15 @@ ] end @actiongroup.add_actions(actions) @uimanager.remove_ui(@move_mid) if @move_mid @move_mid = @uimanager.new_merge_id + ui_paths = ["ui/MainMenubar/EditMenu/Move/", + "ui/BookPopup/Move/"] @libraries.all_regular_libraries.each do |library| name = library.action_name - ["ui/MainMenubar/EditMenu/Move/", - "ui/BookPopup/Move/"].each do |path| + ui_paths.each do |path| @uimanager.add_ui(@move_mid, path, name, name, :menuitem, false) end end end @@ -1145,14 +1128,14 @@ end UndoManager.instance.push { undoable_undelete(library, books) } end def remove_library_separator - if !@library_separator_iter.nil? && @libraries.all_smart_libraries.empty? - @library_listview.model.remove(@library_separator_iter) - @library_separator_iter = nil - end + return if @library_separator_iter.nil? || @libraries.all_smart_libraries.any? + + @library_listview.model.remove(@library_separator_iter) + @library_separator_iter = nil end def undoable_undelete(library, books = nil) # Undeleting a library. if books.nil? @@ -1187,9 +1170,24 @@ @iconview_model.set_sort_column_id(mode, sort_order) @filtered_model.refilter # force redraw end private + + def select_book_in_view(book, view) + @filtered_model.refilter + iter = iter_from_book book + return unless iter + + path = iter.path + return unless view.model + + path = view_path_to_model_path(view, path) + log.debug { "Path for #{book.ident} is #{path}" } + selection = view.respond_to?(:selection) ? view.selection : view + selection.unselect_all + selection.select_path(path) + end def view_path_to_model_path(view, path) path = view.model.convert_path_to_child_path(path) @filtered_model.convert_path_to_child_path(path) if path end