A spot of refactoring

This commit is contained in:
Kovid Goyal 2014-05-13 21:39:02 +05:30
parent 4cca8ca820
commit f20efd3177
2 changed files with 18 additions and 54 deletions

View File

@ -75,6 +75,11 @@ editors = NonReplaceDict()
TOP = object() TOP = object()
dictionaries = Dictionaries() dictionaries = Dictionaries()
def editor_name(editor):
for n, ed in editors.iteritems():
if ed is editor:
return n
def set_book_locale(lang): def set_book_locale(lang):
dictionaries.initialize() dictionaries.initialize()
try: try:

View File

@ -29,7 +29,8 @@ from calibre.ebooks.oeb.polish.utils import link_stylesheets, setup_cssutils_ser
from calibre.gui2 import error_dialog, choose_files, question_dialog, info_dialog, choose_save_file from calibre.gui2 import error_dialog, choose_files, question_dialog, info_dialog, choose_save_file
from calibre.gui2.dialogs.confirm_delete import confirm from calibre.gui2.dialogs.confirm_delete import confirm
from calibre.gui2.tweak_book import ( from calibre.gui2.tweak_book import (
set_current_container, current_container, tprefs, actions, editors, set_book_locale, dictionaries) set_current_container, current_container, tprefs, actions, editors,
set_book_locale, dictionaries, editor_name)
from calibre.gui2.tweak_book.undo import GlobalUndoHistory from calibre.gui2.tweak_book.undo import GlobalUndoHistory
from calibre.gui2.tweak_book.file_list import NewFileDialog from calibre.gui2.tweak_book.file_list import NewFileDialog
from calibre.gui2.tweak_book.save import SaveManager, save_container, find_first_existing_ancestor from calibre.gui2.tweak_book.save import SaveManager, save_container, find_first_existing_ancestor
@ -618,10 +619,7 @@ class Boss(QObject):
def pretty_print(self, current): def pretty_print(self, current):
if current: if current:
ed = self.gui.central.current_editor ed = self.gui.central.current_editor
for name, x in editors.iteritems(): ed.pretty_print(editor_name(ed))
if x is ed:
break
ed.pretty_print(name)
else: else:
with BusyCursor(): with BusyCursor():
self.add_savepoint(_('Before: Beautify files')) self.add_savepoint(_('Before: Beautify files'))
@ -638,10 +636,7 @@ class Boss(QObject):
def editor_action(self, action): def editor_action(self, action):
ed = self.gui.central.current_editor ed = self.gui.central.current_editor
for n, x in editors.iteritems(): edname = editor_name(ed)
if x is ed:
edname = n
break
if hasattr(ed, 'action_triggered'): if hasattr(ed, 'action_triggered'):
if action and action[0] == 'insert_resource': if action and action[0] == 'insert_resource':
rtype = action[1] rtype = action[1]
@ -688,12 +683,7 @@ class Boss(QObject):
self.commit_all_editors_to_container() self.commit_all_editors_to_container()
c = current_container() c = current_container()
ed = self.gui.central.current_editor ed = self.gui.central.current_editor
for name, q in editors.iteritems(): current_name = editor_name(ed)
if ed is q:
current_name = name
break
else:
current_name = None
if current_name and c.mime_map[current_name] not in OEB_DOCS | OEB_STYLES: if current_name and c.mime_map[current_name] not in OEB_DOCS | OEB_STYLES:
current_name = None current_name = None
d = FilterCSS(current_name=current_name, parent=self.gui) d = FilterCSS(current_name=current_name, parent=self.gui)
@ -723,11 +713,7 @@ class Boss(QObject):
# Ensure the search panel is visible # Ensure the search panel is visible
sp.setVisible(True) sp.setVisible(True)
ed = self.gui.central.current_editor ed = self.gui.central.current_editor
name = None name = editor_name(ed)
for n, x in editors.iteritems():
if x is ed:
name = n
break
state = sp.state state = sp.state
if overrides: if overrides:
state.update(overrides) state.update(overrides)
@ -741,21 +727,13 @@ class Boss(QObject):
def find_word(self, word, locations): def find_word(self, word, locations):
' Go to a word from the spell check dialog ' ' Go to a word from the spell check dialog '
ed = self.gui.central.current_editor ed = self.gui.central.current_editor
name = None name = editor_name(ed)
for n, x in editors.iteritems():
if x is ed:
name = n
break
find_next_word(word, locations, ed, name, self.gui, self.show_editor, self.edit_file) find_next_word(word, locations, ed, name, self.gui, self.show_editor, self.edit_file)
def next_spell_error(self): def next_spell_error(self):
' Go to the next spelling error ' ' Go to the next spelling error '
ed = self.gui.central.current_editor ed = self.gui.central.current_editor
name = None name = editor_name(ed)
for n, x in editors.iteritems():
if x is ed:
name = n
break
find_next_error(ed, name, self.gui, self.show_editor, self.edit_file) find_next_error(ed, name, self.gui, self.show_editor, self.edit_file)
def word_replaced(self, changed_names): def word_replaced(self, changed_names):
@ -783,11 +761,7 @@ class Boss(QObject):
def run_saved_searches(self, searches, action): def run_saved_searches(self, searches, action):
ed = self.gui.central.current_editor ed = self.gui.central.current_editor
name = None name = editor_name(ed)
for n, x in editors.iteritems():
if x is ed:
name = n
break
searchable_names = self.gui.file_list.searchable_names searchable_names = self.gui.file_list.searchable_names
if not searches or not validate_search_request(name, searchable_names, getattr(ed, 'has_marked_text', False), searches[0], self.gui): if not searches or not validate_search_request(name, searchable_names, getattr(ed, 'has_marked_text', False), searches[0], self.gui):
return return
@ -916,11 +890,7 @@ class Boss(QObject):
ed = self.gui.central.current_editor ed = self.gui.central.current_editor
if ed.syntax != 'html': if ed.syntax != 'html':
return return
name = None name = editor_name(ed)
for n, x in editors.iteritems():
if ed is x:
name = n
break
if name is None: if name is None:
return return
d = MultiSplit(self.gui) d = MultiSplit(self.gui)
@ -1054,11 +1024,7 @@ class Boss(QObject):
return return
ed = self.gui.central.current_editor ed = self.gui.central.current_editor
if ed is not None: if ed is not None:
name = None name = editor_name(ed)
for n, x in editors.iteritems():
if ed is x:
name = n
break
if name is not None and getattr(ed, 'syntax', None) == 'html': if name is not None and getattr(ed, 'syntax', None) == 'html':
self.gui.preview.sync_to_editor(name, ed.current_line) self.gui.preview.sync_to_editor(name, ed.current_line)
@ -1177,11 +1143,7 @@ class Boss(QObject):
actions['editor-cut'].setEnabled(ed.cut_available) actions['editor-cut'].setEnabled(ed.cut_available)
actions['go-to-line-number'].setEnabled(ed.has_line_numbers) actions['go-to-line-number'].setEnabled(ed.has_line_numbers)
actions['fix-html-current'].setEnabled(ed.syntax == 'html') actions['fix-html-current'].setEnabled(ed.syntax == 'html')
name = None name = editor_name(ed)
for n, x in editors.iteritems():
if ed is x:
name = n
break
if name is not None and getattr(ed, 'syntax', None) == 'html': if name is not None and getattr(ed, 'syntax', None) == 'html':
if self.gui.preview.show(name): if self.gui.preview.show(name):
# The file being displayed by the preview has changed. # The file being displayed by the preview has changed.
@ -1207,10 +1169,7 @@ class Boss(QObject):
self.gui.cursor_position_widget.update_position() self.gui.cursor_position_widget.update_position()
def editor_close_requested(self, editor): def editor_close_requested(self, editor):
name = None name = editor_name(editor)
for n, ed in editors.iteritems():
if ed is editor:
name = n
if not name: if not name:
return return
if not editor.is_synced_to_container: if not editor.is_synced_to_container: