Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Jan 26, 2025
2 parents c78cfdd + f939f85 commit 5a0e9aa
Show file tree
Hide file tree
Showing 14 changed files with 1,083 additions and 778 deletions.
30 changes: 28 additions & 2 deletions src/calibre/gui2/preferences/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ def initial_tab_changed(self):
def do_on_child_tabs(self, method, *args):
r = False
for t in self.child_tabs:
lazy_init_called = getattr(t, 'lazy_init_called', True)
if method in ('commit', 'refresh_gui') and not lazy_init_called:
continue
if method == 'restore_defaults' and not lazy_init_called:
if hasattr(t, 'lazy_initialize'):
t.lazy_initialize()
t.lazy_init_called = True
r = r | bool(getattr(t, method)(*args))
return r

Expand Down Expand Up @@ -291,7 +298,7 @@ def __init__(self, parent=None):
self.settings = {}
self.child_tabs = []
for v in self.__dict__.values():
if isinstance(v, ConfigTabWidget):
if isinstance(v, LazyConfigWidgetBase):
self.child_tabs.append(v)

def register(self, name, config_obj, gui_name=None, choices=None,
Expand Down Expand Up @@ -349,11 +356,30 @@ def get_plugin(category, name):
(category, name))


class ConfigTabWidget(ConfigWidgetBase):
class LazyConfigWidgetBase(ConfigWidgetBase):
'''
Use this for dialogs that are tabs, accessed either from the left or on the
top. It directly replaces ConfigWidgetBase, supporting the lazy operations.
'''

def __init__(self, parent=None):
super().__init__(parent)
self.lazy_init_called = False

def set_changed_signal(self, changed_signal):
self.changed_signal.connect(changed_signal)

def showEvent(self, event):
# called when the widget is actually displays. We can't do something like
# lazy_genesis because Qt does "things" before showEvent() is called. In
# particular, the register function doesn't work with combo boxes if
# genesis isn't called before everythign else. Why is a mystery.
if not self.lazy_init_called:
if hasattr(self, 'lazy_initialize'):
self.lazy_initialize()
self.lazy_init_called = True
super().showEvent(event)


class ConfigDialog(QDialog):

Expand Down
169 changes: 12 additions & 157 deletions src/calibre/gui2/preferences/look_feel.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

from calibre import human_readable
from calibre.constants import ismacos, iswindows
from calibre.db.categories import is_standard_category
from calibre.ebooks.metadata.book.render import DEFAULT_AUTHOR_LINK
from calibre.ebooks.metadata.sources.prefs import msprefs
from calibre.gui2 import (
Expand All @@ -59,13 +58,20 @@
qt_app,
question_dialog,
)
from calibre.gui2.actions.show_quickview import get_quickview_action_plugin

from calibre.gui2.custom_column_widgets import get_field_list as em_get_field_list
from calibre.gui2.dialogs.quickview import get_qv_field_list
from calibre.gui2.library.alternate_views import CM_TO_INCH, auto_height
from calibre.gui2.preferences import ConfigWidgetBase, Setting, set_help_tips, test_widget
from calibre.gui2.preferences.coloring import EditRules
from calibre.gui2.preferences.look_feel_tabs import DisplayedFields, move_field_down, move_field_up
from calibre.gui2.preferences.look_feel_tabs import (
DisplayedFields,
export_layout,
import_layout,
move_field_down,
move_field_up,
reset_layout,
)
from calibre.gui2.preferences.look_feel_ui import Ui_Form
from calibre.gui2.widgets import BusyCursor
from calibre.gui2.widgets2 import Dialog
Expand Down Expand Up @@ -481,12 +487,6 @@ def genesis(self, gui):
r('show_sb_all_actions_button', gprefs)
# r('show_sb_preference_button', gprefs)
r('row_numbers_in_book_list', gprefs)
r('tag_browser_old_look', gprefs)
r('tag_browser_hide_empty_categories', gprefs)
r('tag_browser_always_autocollapse', gprefs)
r('tag_browser_restore_tree_expansion', gprefs)
r('tag_browser_show_tooltips', gprefs)
r('tag_browser_allow_keyboard_focus', gprefs)
r('bd_show_cover', gprefs)
r('bd_overlay_cover_size', gprefs)
r('cover_corner_radius', gprefs)
Expand All @@ -497,13 +497,6 @@ def genesis(self, gui):
r('cover_grid_disk_cache_size', gprefs)
r('cover_grid_spacing', gprefs)
r('cover_grid_show_title', gprefs)
r('tag_browser_show_counts', gprefs)
r('tag_browser_item_padding', gprefs)

r('qv_respects_vls', gprefs)
r('qv_dclick_changes_column', gprefs)
r('qv_retkey_changes_column', gprefs)
r('qv_follows_column', gprefs)

r('emblem_size', gprefs)
r('emblem_position', gprefs, choices=[
Expand Down Expand Up @@ -536,10 +529,6 @@ def get_esc_lang(l):
choices.insert(1, ((get_language(lul), lul)))
r('language', prefs, choices=choices, restart_required=True, setting=LanguageSetting)

r('show_avg_rating', config)
r('show_links_in_tag_browser', gprefs)
r('show_notes_in_tag_browser', gprefs)
r('icons_on_right_in_tag_browser', gprefs)
r('disable_animations', config)
r('systray_icon', config, restart_required=True)
r('show_splash_screen', gprefs)
Expand All @@ -554,12 +543,6 @@ def get_esc_lang(l):
(_('Never'), 'never')]
r('toolbar_text', gprefs, choices=choices)

choices = [(_('Disabled'), 'disable'), (_('By first letter'), 'first letter'),
(_('Partitioned'), 'partition')]
r('tags_browser_partition_method', gprefs, choices=choices)
r('tags_browser_collapse_at', gprefs)
r('tags_browser_collapse_fl_at', gprefs)

fm = db.field_metadata
choices = sorted(((fm[k]['name'], k) for k in fm.displayable_field_keys() if fm[k]['name']),
key=lambda x:sort_key(x[0]))
Expand All @@ -583,15 +566,6 @@ def get_esc_lang(l):
self.current_font = self.initial_font = None
self.change_font_button.clicked.connect(self.change_font)

self.display_model = DisplayedFields(self.gui.current_db, self.field_display_order)
self.display_model.dataChanged.connect(self.changed_signal)
self.field_display_order.setModel(self.display_model)
mu = partial(move_field_up, self.field_display_order, self.display_model)
md = partial(move_field_down, self.field_display_order, self.display_model)
self.df_up_button.clicked.connect(mu)
self.df_down_button.clicked.connect(md)
self.field_display_order.set_movement_functions(mu, md)

self.em_display_model = EMDisplayedFields(self.gui.current_db, self.em_display_order)
self.em_display_model.dataChanged.connect(self.changed_signal)
self.em_display_order.setModel(self.em_display_model)
Expand All @@ -600,41 +574,9 @@ def get_esc_lang(l):
self.em_display_order.set_movement_functions(mu, md)
self.em_up_button.clicked.connect(mu)
self.em_down_button.clicked.connect(md)
self.em_export_layout_button.clicked.connect(partial(self.export_layout, model=self.em_display_model))
self.em_import_layout_button.clicked.connect(partial(self.import_layout, model=self.em_display_model))
self.em_reset_layout_button.clicked.connect(partial(self.reset_layout, model=self.em_display_model))

self.qv_display_model = QVDisplayedFields(self.gui.current_db, self.qv_display_order)
self.qv_display_model.dataChanged.connect(self.changed_signal)
self.qv_display_order.setModel(self.qv_display_model)
mu = partial(move_field_up, self.qv_display_order, self.qv_display_model)
md = partial(move_field_down, self.qv_display_order, self.qv_display_model)
self.qv_display_order.set_movement_functions(mu, md)
self.qv_up_button.clicked.connect(mu)
self.qv_down_button.clicked.connect(md)

self.tb_display_model = TBDisplayedFields(self.gui.current_db, self.tb_display_order,
category_icons=self.gui.tags_view.model().category_custom_icons)
self.tb_display_model.dataChanged.connect(self.changed_signal)
self.tb_display_order.setModel(self.tb_display_model)
self.tb_reset_layout_button.clicked.connect(partial(self.reset_layout, model=self.tb_display_model))
self.tb_export_layout_button.clicked.connect(partial(self.export_layout, model=self.tb_display_model))
self.tb_import_layout_button.clicked.connect(partial(self.import_layout, model=self.tb_display_model))
self.tb_up_button.clicked.connect(self.tb_up_button_clicked)
self.tb_down_button.clicked.connect(self.tb_down_button_clicked)
self.tb_display_order.set_movement_functions(self.tb_up_button_clicked, self.tb_down_button_clicked)

self.tb_categories_to_part_model = TBPartitionedFields(self.gui.current_db,
self.tb_cats_to_partition,
category_icons=self.gui.tags_view.model().category_custom_icons)
self.tb_categories_to_part_model.dataChanged.connect(self.changed_signal)
self.tb_cats_to_partition.setModel(self.tb_categories_to_part_model)
self.tb_partition_reset_button.clicked.connect(partial(self.reset_layout,
model=self.tb_categories_to_part_model))
self.tb_partition_export_layout_button.clicked.connect(partial(self.export_layout,
model=self.tb_categories_to_part_model))
self.tb_partition_import_layout_button.clicked.connect(partial(self.import_layout,
model=self.tb_categories_to_part_model))
self.em_export_layout_button.clicked.connect(partial(export_layout, self, model=self.em_display_model))
self.em_import_layout_button.clicked.connect(partial(import_layout, self, model=self.em_display_model))
self.em_reset_layout_button.clicked.connect(partial(reset_layout, model=self.em_display_model))

self.bd_vertical_cats_model = BDVerticalCats(self.gui.current_db, self.tb_hierarchy_tab.tb_hierarchical_cats)
self.bd_vertical_cats_model.dataChanged.connect(self.changed_signal)
Expand Down Expand Up @@ -690,16 +632,8 @@ def get_esc_lang(l):
self.css_highlighter = get_highlighter('css')()
self.css_highlighter.apply_theme(get_theme(None))
self.css_highlighter.set_document(self.opt_book_details_css.document())
self.lazy_tabs = {}
for i in range(self.tabWidget.count()):
self.sections_view.addItem(QListWidgetItem(self.tabWidget.tabIcon(i), self.tabWidget.tabText(i).replace('&', '')))
# retrieve tabs and subtabs of look & feel to load their content later when clicking of them
w = self.tabWidget.widget(i).widget()
self.lazy_tabs[(i, None)] = w
if isinstance(w, QTabWidget):
w.currentChanged.connect(partial(self.lazy_tab_operations, i))
for ii in range(w.count()):
self.lazy_tabs[(i, ii)] = w.widget(ii)
self.sections_view.setCurrentRow(self.tabWidget.currentIndex())
self.sections_view.currentRowChanged.connect(self.tabWidget.setCurrentIndex)
self.sections_view.setMaximumWidth(self.sections_view.sizeHintForColumn(0) + 16)
Expand Down Expand Up @@ -728,59 +662,6 @@ def update_color_palette_state(self):
enabled = self.opt_ui_style.currentData() == 'calibre'
self.button_adjust_colors.setEnabled(enabled)

def export_layout(self, model=None):
filename = choose_save_file(self, 'em_import_export_field_list',
_('Save column list to file'),
filters=[(_('Column list'), ['json'])])
if filename:
try:
with open(filename, 'w') as f:
json.dump(model.fields, f, indent=1)
except Exception as err:
error_dialog(self, _('Export field layout'),
_('<p>Could not write field list. Error:<br>%s')%err, show=True)

def import_layout(self, model=None):
filename = choose_files(self, 'em_import_export_field_list',
_('Load column list from file'),
filters=[(_('Column list'), ['json'])])
if filename:
try:
with open(filename[0]) as f:
fields = json.load(f)
model.initialize(pref_data_override=fields)
self.changed_signal.emit()
except Exception as err:
error_dialog(self, _('Import layout'),
_('<p>Could not read field list. Error:<br>%s')%err, show=True)

def reset_layout(self, model=None):
model.initialize(use_defaults=True)
self.changed_signal.emit()

def tb_down_button_clicked(self):
idx = self.tb_display_order.currentIndex()
if idx.isValid():
row = idx.row()
model = self.tb_display_model
fields = model.fields
key = fields[row][0]
if not is_standard_category(key):
return
if row < len(fields) and is_standard_category(fields[row+1][0]):
move_field_down(self.tb_display_order, model)

def tb_up_button_clicked(self):
idx = self.tb_display_order.currentIndex()
if idx.isValid():
row = idx.row()
model = self.tb_display_model
fields = model.fields
key = fields[row][0]
if not is_standard_category(key):
return
move_field_up(self.tb_display_order, model)

def choose_icon_theme(self):
from calibre.gui2.icon_theme import ChooseTheme
d = ChooseTheme(self)
Expand Down Expand Up @@ -833,11 +714,7 @@ def initialize(self):
font.append(gprefs.get('font_stretch', QFont.Stretch.Unstretched))
self.current_font = self.initial_font = font
self.update_font_display()
self.display_model.initialize()
self.em_display_model.initialize()
self.qv_display_model.initialize()
self.tb_display_model.initialize()
self.tb_categories_to_part_model.initialize()
self.bd_vertical_cats_model.initialize()
db = self.gui.current_db
mi = []
Expand All @@ -857,10 +734,8 @@ def initialize(self):
self.opt_book_details_css.blockSignals(True)
self.opt_book_details_css.setPlainText(P('templates/book_details.css', data=True).decode('utf-8'))
self.opt_book_details_css.blockSignals(False)
self.tb_focus_label.setVisible(self.opt_tag_browser_allow_keyboard_focus.isChecked())
self.update_color_palette_state()
self.opt_gui_layout.setCurrentIndex(0 if self.gui.layout_container.is_wide else 1)
self.lazy_tab_operations(self.tabWidget.currentIndex(), None)

def open_cg_cache(self):
open_local_file(self.gui.grid_view.thumbnail_cache.location)
Expand All @@ -870,20 +745,9 @@ def update_cg_cache_size(self, size):
_('Current space used: %s') % human_readable(size))

def tab_changed(self, index):
self.lazy_tab_operations(index, None)
if self.tabWidget.currentWidget() is self.cover_grid_tab:
self.show_current_cache_usage()

def lazy_tab_operations(self, idx_section, idx_subtab):
'''
Check if the tab has lazy operations.
Perfom the lazy operations only once, the first time the tab is selected.
'''
tab = self.lazy_tabs.get((idx_section, idx_subtab), None)
if hasattr(tab, 'lazy_populate_content'):
tab.lazy_populate_content()
self.lazy_tabs.pop((idx_section, idx_subtab), None)

def show_current_cache_usage(self):
t = Thread(target=self.calc_cache_size)
t.daemon = True
Expand Down Expand Up @@ -912,9 +776,7 @@ def restore_defaults(self):
if ofont is not None:
self.changed_signal.emit()
self.update_font_display()
self.display_model.restore_defaults()
self.em_display_model.restore_defaults()
self.qv_display_model.restore_defaults()
self.bd_vertical_cats_model.restore_defaults()
gprefs.set('tb_search_order', gprefs.defaults['tb_search_order'])
self.edit_rules.clear()
Expand Down Expand Up @@ -986,11 +848,7 @@ def commit(self, *args):
is not None else QFont.Stretch.Unstretched)
QApplication.setFont(self.font_display.font())
rr = True
self.display_model.commit()
self.em_display_model.commit()
self.qv_display_model.commit()
self.tb_display_model.commit()
self.tb_categories_to_part_model.commit()
self.bd_vertical_cats_model.commit()
self.edit_rules.commit(self.gui.current_db.prefs)
self.icon_rules.commit(self.gui.current_db.prefs)
Expand Down Expand Up @@ -1027,9 +885,6 @@ def refresh_gui(self, gui):
gui.library_view.refresh_row_sizing()
gui.grid_view.refresh_settings()
gui.update_auto_scroll_timeout()
qv = get_quickview_action_plugin()
if qv:
qv.refill_quickview()
gui.sb_all_gui_actions_button.setVisible(gprefs['show_sb_all_actions_button'])
# gui.sb_preferences_button.setVisible(gprefs['show_sb_preference_button'])

Expand Down
Loading

0 comments on commit 5a0e9aa

Please sign in to comment.