| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import functools |
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
from qt.core import ( |
| 8 |
QAction, |
| 9 |
QApplication, |
| 10 |
QDialog, |
| 11 |
QEvent, |
| 12 |
QIcon, |
| 13 |
QLabel, |
| 14 |
QMenu, |
| 15 |
QModelIndex, |
| 16 |
QPixmap, |
| 17 |
QStackedWidget, |
| 18 |
QStatusBar, |
| 19 |
QStyle, |
| 20 |
QStyleOption, |
| 21 |
QStylePainter, |
| 22 |
Qt, |
| 23 |
QTabBar, |
| 24 |
QTimer, |
| 25 |
QToolButton, |
| 26 |
QUrl, |
| 27 |
) |
| 28 |
|
| 29 |
from calibre.constants import get_appname_for_display, get_version, ismacos |
| 30 |
from calibre.customize.ui import find_plugin |
| 31 |
from calibre.gui2 import config, error_dialog, gprefs, open_local_file, open_url, qapplication_or_fail |
| 32 |
from calibre.gui2.book_details import BookDetails |
| 33 |
from calibre.gui2.central import CentralContainer, LayoutButton |
| 34 |
from calibre.gui2.layout_menu import LayoutMenu |
| 35 |
from calibre.gui2.library.alternate_views import GridView |
| 36 |
from calibre.gui2.library.bookshelf_view import BookshelfView |
| 37 |
from calibre.gui2.library.views import BooksView, DeviceBooksView |
| 38 |
from calibre.gui2.notify import get_notifier |
| 39 |
from calibre.gui2.tag_browser.ui import TagBrowserWidget |
| 40 |
from calibre.utils.config import prefs |
| 41 |
from calibre.utils.icu import sort_key |
| 42 |
from calibre.utils.localization import _, localize_website_link, ngettext |
| 43 |
|
| 44 |
if TYPE_CHECKING: |
| 45 |
from calibre.gui2.ui import Main |
| 46 |
|
| 47 |
_keep_refs = [] |
| 48 |
|
| 49 |
|
| 50 |
def partial(*args, **kwargs): |
| 51 |
ans = functools.partial(*args, **kwargs) |
| 52 |
_keep_refs.append(ans) |
| 53 |
return ans |
| 54 |
|
| 55 |
|
| 56 |
class LibraryViewMixin: |
| 57 |
library_view: BooksView |
| 58 |
|
| 59 |
def init_library_view_mixin(self: Main, db): |
| 60 |
self.library_view.files_dropped.connect(self.iactions['Add Books'].files_dropped, type=Qt.ConnectionType.QueuedConnection) |
| 61 |
self.library_view.books_dropped.connect(self.iactions['Edit Metadata'].books_dropped, type=Qt.ConnectionType.QueuedConnection) |
| 62 |
self.library_view.add_column_signal.connect( |
| 63 |
partial( |
| 64 |
self.iactions['Preferences'].do_config, |
| 65 |
initial_plugin=('Interface', 'Custom Columns'), |
| 66 |
close_after_initial=True, |
| 67 |
), |
| 68 |
type=Qt.ConnectionType.QueuedConnection, |
| 69 |
) |
| 70 |
for func, args in [ |
| 71 |
('connect_to_search_box', (self.search, self.search_done)), |
| 72 |
('connect_to_book_display', (self.book_details.show_data,)), |
| 73 |
]: |
| 74 |
for view in (self.library_view, self.memory_view, self.card_a_view, self.card_b_view): |
| 75 |
getattr(view, func)(*args) |
| 76 |
|
| 77 |
self.memory_view.connect_dirtied_signal(self.upload_dirtied_booklists) |
| 78 |
self.memory_view.connect_upload_collections_signal(func=self.upload_collections, oncard=None) |
| 79 |
self.card_a_view.connect_dirtied_signal(self.upload_dirtied_booklists) |
| 80 |
self.card_a_view.connect_upload_collections_signal(func=self.upload_collections, oncard='carda') |
| 81 |
self.card_b_view.connect_dirtied_signal(self.upload_dirtied_booklists) |
| 82 |
self.card_b_view.connect_upload_collections_signal(func=self.upload_collections, oncard='cardb') |
| 83 |
self.book_on_device(None, reset=True) |
| 84 |
db.set_book_on_device_func(self.book_on_device) |
| 85 |
self.library_view.set_database(db) |
| 86 |
self.library_view._model.set_book_on_device_func(self.book_on_device) |
| 87 |
prefs['library_path'] = self.library_path |
| 88 |
|
| 89 |
for view in ('library', 'memory', 'card_a', 'card_b'): |
| 90 |
view = getattr(self, view + '_view') |
| 91 |
view.verticalHeader().sectionDoubleClicked.connect(self.iactions['View'].view_specific_book) |
| 92 |
|
| 93 |
self.library_view._model.set_highlight_only(config['highlight_search_matches']) |
| 94 |
|
| 95 |
def context_menu_action_hovered(self, ac): |
| 96 |
ac.showStatusText(self) |
| 97 |
|
| 98 |
def build_context_menus(self: Main): |
| 99 |
from calibre.gui2.bars import populate_menu |
| 100 |
|
| 101 |
def connect_hovered(menu): |
| 102 |
menu.hovered.connect(self.context_menu_action_hovered) |
| 103 |
for action in menu.actions(): |
| 104 |
if m := action.menu(): |
| 105 |
connect_hovered(m) |
| 106 |
|
| 107 |
lm = QMenu(self) |
| 108 |
populate_menu(lm, gprefs['action-layout-context-menu'], self.iactions) |
| 109 |
connect_hovered(lm) |
| 110 |
dm = QMenu(self) |
| 111 |
populate_menu(dm, gprefs['action-layout-context-menu-device'], self.iactions) |
| 112 |
connect_hovered(dm) |
| 113 |
ec = self.iactions['Edit Collections'].qaction |
| 114 |
self.library_view.set_context_menu(lm, ec) |
| 115 |
sm = QMenu(self) |
| 116 |
populate_menu(sm, gprefs['action-layout-context-menu-split'], self.iactions) |
| 117 |
connect_hovered(sm) |
| 118 |
self.library_view.pin_view.set_context_menu(sm) |
| 119 |
for v in (self.memory_view, self.card_a_view, self.card_b_view): |
| 120 |
v.set_context_menu(dm, ec) |
| 121 |
|
| 122 |
if hasattr(self.cover_flow, 'set_context_menu'): |
| 123 |
cm = QMenu(self.cover_flow) |
| 124 |
populate_menu(cm, gprefs['action-layout-context-menu-cover-browser'], self.iactions) |
| 125 |
connect_hovered(cm) |
| 126 |
self.cover_flow.set_context_menu(cm) |
| 127 |
|
| 128 |
def search_done(self: Main, view, ok): |
| 129 |
if view is self.current_view(): |
| 130 |
self.search.search_done(ok) |
| 131 |
self.set_number_of_books_shown() |
| 132 |
if ok: |
| 133 |
v = self.current_view() |
| 134 |
if hasattr(v, 'set_current_row'): |
| 135 |
v.set_current_row(0) |
| 136 |
if v is self.library_view and v.row_count() == 0: |
| 137 |
self.book_details.reset_info() |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
class UpdateLabel(QLabel): |
| 144 |
def __init__(self, *args, **kwargs): |
| 145 |
QLabel.__init__(self, *args, **kwargs) |
| 146 |
self.setCursor(Qt.CursorShape.PointingHandCursor) |
| 147 |
|
| 148 |
def contextMenuEvent(self, ev): |
| 149 |
pass |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
class VersionLabel(QLabel): |
| 156 |
def __init__(self, parent): |
| 157 |
QLabel.__init__(self, parent) |
| 158 |
self.mouse_over = False |
| 159 |
self.setCursor(Qt.CursorShape.PointingHandCursor) |
| 160 |
self.setToolTip(_("See what's new in this calibre release")) |
| 161 |
|
| 162 |
def mouseReleaseEvent(self, ev): |
| 163 |
open_url(localize_website_link('https://calibre-ebook.com/whats-new')) |
| 164 |
ev.accept() |
| 165 |
return QLabel.mouseReleaseEvent(self, ev) |
| 166 |
|
| 167 |
def event(self, e): |
| 168 |
m = None |
| 169 |
et = e.type() |
| 170 |
if et == QEvent.Type.Enter: |
| 171 |
m = True |
| 172 |
elif et == QEvent.Type.Leave: |
| 173 |
m = False |
| 174 |
if m is not None and m != self.mouse_over: |
| 175 |
self.mouse_over = m |
| 176 |
self.update() |
| 177 |
return QLabel.event(self, e) |
| 178 |
|
| 179 |
def paintEvent(self, a0): |
| 180 |
if self.mouse_over: |
| 181 |
p = QStylePainter(self) |
| 182 |
tool = QStyleOption() |
| 183 |
tool.initFrom(self) |
| 184 |
tool.rect = self.rect() |
| 185 |
tool.state = QStyle.StateFlag.State_Raised | QStyle.StateFlag.State_Active | QStyle.StateFlag.State_MouseOver |
| 186 |
p.drawPrimitive(QStyle.PrimitiveElement.PE_PanelButtonTool, tool) |
| 187 |
p.end() |
| 188 |
return QLabel.paintEvent(self, a0) |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
class StatusBar(QStatusBar): |
| 195 |
def __init__(self, parent=None): |
| 196 |
QStatusBar.__init__(self, parent) |
| 197 |
self.version = get_version() |
| 198 |
self.base_msg = f'{get_appname_for_display()} {self.version}' |
| 199 |
self.device_string = '' |
| 200 |
self.update_label = UpdateLabel('') |
| 201 |
self.total = self.current = self.selected = self.library_total = 0 |
| 202 |
self.addPermanentWidget(self.update_label) |
| 203 |
self.update_label.setVisible(False) |
| 204 |
self.defmsg = VersionLabel(self) |
| 205 |
self.addWidget(self.defmsg) |
| 206 |
self.set_label() |
| 207 |
|
| 208 |
def initialize(self, systray=None): |
| 209 |
self.systray = systray |
| 210 |
self.notifier = get_notifier(systray) |
| 211 |
|
| 212 |
def device_connected(self, devname): |
| 213 |
self.device_string = _('Connected ') + devname |
| 214 |
self.set_label() |
| 215 |
|
| 216 |
def update_state(self, library_total, total, current, selected): |
| 217 |
self.library_total = library_total |
| 218 |
self.total, self.current, self.selected = total, current, selected |
| 219 |
self.set_label() |
| 220 |
|
| 221 |
def set_label(self): |
| 222 |
try: |
| 223 |
self._set_label() |
| 224 |
except Exception: |
| 225 |
import traceback |
| 226 |
|
| 227 |
traceback.print_exc() |
| 228 |
|
| 229 |
def _set_label(self): |
| 230 |
msg = self.base_msg |
| 231 |
if self.device_string: |
| 232 |
msg += ' ..::.. ' + self.device_string |
| 233 |
else: |
| 234 |
msg += _(' %(created)s %(name)s') % dict(created=_('created by'), name='Kovid Goyal') |
| 235 |
|
| 236 |
if self.total != self.current: |
| 237 |
base = _('%(num)d of %(total)d books') % dict(num=self.current, total=self.total) |
| 238 |
else: |
| 239 |
base = ngettext('one book', '{} books', self.total).format(self.total) |
| 240 |
if self.selected > 0: |
| 241 |
base = ngettext('%(num)s, %(sel)d selected', '%(num)s, %(sel)d selected', self.selected) % dict(num=base, sel=self.selected) |
| 242 |
if self.library_total != self.total: |
| 243 |
base = _('{0}, {1} total').format(base, self.library_total) |
| 244 |
|
| 245 |
self.defmsg.setText(f'\xa0{msg}\xa0\xa0\xa0\xa0[{base}] ') |
| 246 |
self.clearMessage() |
| 247 |
|
| 248 |
def device_disconnected(self): |
| 249 |
self.device_string = '' |
| 250 |
self.set_label() |
| 251 |
|
| 252 |
def show_message(self, msg, timeout=0, show_notification=True): |
| 253 |
self.showMessage(msg, timeout) |
| 254 |
if self.notifier is not None and not config['disable_tray_notification'] and show_notification: |
| 255 |
self.notifier(msg) |
| 256 |
|
| 257 |
def clear_message(self): |
| 258 |
self.clearMessage() |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
class AlternateViewsButtons(LayoutButton): |
| 265 |
buttons = set() |
| 266 |
ignore_toggles = False |
| 267 |
needs_group_by = False |
| 268 |
|
| 269 |
def __init__(self, name: str, icon: str, label: str, view_name: str, gui: Main, shortcut=None, config_key=None): |
| 270 |
super().__init__(name, icon, label, gui.layout_container, shortcut=shortcut) |
| 271 |
self.set_state_to_show() |
| 272 |
self.action_toggle = QAction(self.icon(), _('Toggle') + ' ' + self.label, self) |
| 273 |
self.gui = gui |
| 274 |
self.gui.addAction(self.action_toggle) |
| 275 |
self.ck = config_key or name |
| 276 |
self.view_name = view_name |
| 277 |
if shortcut: |
| 278 |
self.gui.keyboard.register_shortcut( |
| 279 |
f'{self.ck} toggle {self.label}', |
| 280 |
str(self.action_toggle.text()), |
| 281 |
default_keys=(shortcut,), |
| 282 |
action=self.action_toggle, |
| 283 |
group=_('Main window layout'), |
| 284 |
) |
| 285 |
self.action_toggle.triggered.connect(self.toggle) |
| 286 |
self.action_toggle.changed.connect(self.update_shortcut) |
| 287 |
self.toggled.connect(self.update_state) |
| 288 |
self.toggled.connect(self.toggle_view) |
| 289 |
self.buttons.add(self) |
| 290 |
|
| 291 |
@property |
| 292 |
def is_visible(self): |
| 293 |
return self.isChecked() |
| 294 |
|
| 295 |
def update_state(self, *args): |
| 296 |
checked = args[0] if args else None |
| 297 |
if checked: |
| 298 |
self.set_state_to_hide() |
| 299 |
else: |
| 300 |
self.set_state_to_show() |
| 301 |
|
| 302 |
def save_state(self): |
| 303 |
gprefs[f'{self.ck} visible'] = bool(self.isChecked()) |
| 304 |
|
| 305 |
def restore_state(self): |
| 306 |
if gprefs.get(f'{self.ck} visible', False): |
| 307 |
self.toggle() |
| 308 |
|
| 309 |
def toggle_view(self, show): |
| 310 |
if AlternateViewsButtons.ignore_toggles: |
| 311 |
return |
| 312 |
AlternateViewsButtons.ignore_toggles = True |
| 313 |
for btn in self.buttons: |
| 314 |
if btn == self: |
| 315 |
continue |
| 316 |
if btn.isChecked(): |
| 317 |
btn.update_state(False) |
| 318 |
self.gui.library_view.alternate_views.show_view(self.view_name if show else None) |
| 319 |
self.gui.show_sort_button_for_alternate_view(show) |
| 320 |
self.gui.group_by_button.setVisible(self.needs_group_by and show) |
| 321 |
AlternateViewsButtons.ignore_toggles = False |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
class GridViewButton(AlternateViewsButtons): |
| 328 |
def __init__(self, gui): |
| 329 |
super().__init__( |
| 330 |
'cover_grid', |
| 331 |
'grid.png', |
| 332 |
_('Cover grid'), |
| 333 |
'grid', |
| 334 |
gui, |
| 335 |
shortcut='Alt+Shift+G', |
| 336 |
config_key='grid view', |
| 337 |
) |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
class BookshelfViewButton(AlternateViewsButtons): |
| 344 |
needs_group_by = True |
| 345 |
|
| 346 |
def __init__(self, gui): |
| 347 |
super().__init__( |
| 348 |
'bookshelf_view', |
| 349 |
'bookshelf.png', |
| 350 |
_('Bookshelf view'), |
| 351 |
'bookshelf', |
| 352 |
gui, |
| 353 |
shortcut='Alt+Shift+H', |
| 354 |
config_key='bookshelf view', |
| 355 |
) |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
class SearchBarButton(LayoutButton): |
| 362 |
def __init__(self, gui): |
| 363 |
sc = 'Alt+Shift+F' |
| 364 |
LayoutButton.__init__(self, 'search', 'search.png', _('Search bar'), gui, shortcut=sc) |
| 365 |
self.set_state_to_hide() |
| 366 |
self.action_toggle = QAction(self.icon(), _('Toggle') + ' ' + self.label, self) |
| 367 |
gui.addAction(self.action_toggle) |
| 368 |
gui.keyboard.register_shortcut( |
| 369 |
'search bar toggle' + self.label, |
| 370 |
str(self.action_toggle.text()), |
| 371 |
default_keys=(sc,), |
| 372 |
action=self.action_toggle, |
| 373 |
group=_('Main window layout'), |
| 374 |
) |
| 375 |
self.action_toggle.triggered.connect(self.toggle) |
| 376 |
self.action_toggle.changed.connect(self.update_shortcut) |
| 377 |
self.toggled.connect(self.update_state) |
| 378 |
self.update_shortcut() |
| 379 |
|
| 380 |
@property |
| 381 |
def is_visible(self): |
| 382 |
return self.isChecked() |
| 383 |
|
| 384 |
def update_state(self, *args): |
| 385 |
checked = args[0] if args else None |
| 386 |
if checked: |
| 387 |
self.set_state_to_hide() |
| 388 |
else: |
| 389 |
self.set_state_to_show() |
| 390 |
|
| 391 |
def save_state(self): |
| 392 |
gprefs['search bar visible'] = bool(self.isChecked()) |
| 393 |
|
| 394 |
def restore_state(self): |
| 395 |
self.setChecked(bool(gprefs.get('search bar visible', True))) |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
class VLTabs(QTabBar): |
| 402 |
def __init__(self, parent): |
| 403 |
QTabBar.__init__(self, parent) |
| 404 |
self.setDocumentMode(True) |
| 405 |
self.setDrawBase(False) |
| 406 |
self.setTabsClosable(gprefs['vl_tabs_closable']) |
| 407 |
self.setMovable(self.tabsClosable()) |
| 408 |
self.gui = parent |
| 409 |
self.ignore_tab_changed = False |
| 410 |
self.currentChanged.connect(self.tab_changed) |
| 411 |
self.tabMoved.connect(self.tab_moved, type=Qt.ConnectionType.QueuedConnection) |
| 412 |
self.tabCloseRequested.connect(self.tab_close) |
| 413 |
self.update_visibility() |
| 414 |
self.next_action = a = QAction(self) |
| 415 |
a.triggered.connect(partial(self.next_tab, delta=1)), self.gui.addAction(a) |
| 416 |
self.previous_action = a = QAction(self) |
| 417 |
a.triggered.connect(partial(self.next_tab, delta=-1)), self.gui.addAction(a) |
| 418 |
self.gui.keyboard.register_shortcut( |
| 419 |
'virtual-library-tab-bar-next', |
| 420 |
_('Next Virtual library'), |
| 421 |
action=self.next_action, |
| 422 |
default_keys=('Ctrl+Tab',), |
| 423 |
group=_('Virtual library'), |
| 424 |
description=_('Switch to the next Virtual library in the Virtual library tab bar'), |
| 425 |
) |
| 426 |
self.gui.keyboard.register_shortcut( |
| 427 |
'virtual-library-tab-bar-previous', |
| 428 |
_('Previous Virtual library'), |
| 429 |
action=self.previous_action, |
| 430 |
default_keys=('Ctrl+Shift+Tab',), |
| 431 |
group=_('Virtual library'), |
| 432 |
description=_('Switch to the previous Virtual library in the Virtual library tab bar'), |
| 433 |
) |
| 434 |
|
| 435 |
def next_tab(self, delta=1): |
| 436 |
if self.count() > 1 and self.isVisible(): |
| 437 |
idx = (self.currentIndex() + delta) % self.count() |
| 438 |
self.setCurrentIndex(idx) |
| 439 |
|
| 440 |
def update_visibility(self): |
| 441 |
cv = self.gui.current_view() |
| 442 |
self.setVisible(gprefs['show_vl_tabs'] and self.count() > 1 and (cv is self.gui.library_view or cv is None)) |
| 443 |
|
| 444 |
def enable_bar(self): |
| 445 |
gprefs['show_vl_tabs'] = True |
| 446 |
self.update_visibility() |
| 447 |
self.gui.set_number_of_books_shown() |
| 448 |
|
| 449 |
def disable_bar(self): |
| 450 |
gprefs['show_vl_tabs'] = False |
| 451 |
self.update_visibility() |
| 452 |
self.gui.set_number_of_books_shown() |
| 453 |
|
| 454 |
def lock_tab(self): |
| 455 |
gprefs['vl_tabs_closable'] = False |
| 456 |
self.setTabsClosable(False) |
| 457 |
self.setMovable(False) |
| 458 |
|
| 459 |
for idx in range(self.count()): |
| 460 |
self.setTabButton(idx, QTabBar.ButtonPosition.RightSide, None) |
| 461 |
self.setTabButton(idx, QTabBar.ButtonPosition.LeftSide, None) |
| 462 |
|
| 463 |
def unlock_tab(self): |
| 464 |
gprefs['vl_tabs_closable'] = True |
| 465 |
self.setTabsClosable(True) |
| 466 |
self.setMovable(True) |
| 467 |
|
| 468 |
for idx in range(self.count()): |
| 469 |
if not self.tabData(idx): |
| 470 |
btn = self.tabButton(idx, QTabBar.ButtonPosition.RightSide) |
| 471 |
if btn is not None: |
| 472 |
btn.setVisible(False) |
| 473 |
else: |
| 474 |
btn = self.tabButton(idx, QTabBar.ButtonPosition.LeftSide) |
| 475 |
if btn is not None: |
| 476 |
btn.setVisible(False) |
| 477 |
break |
| 478 |
|
| 479 |
def tab_changed(self, idx): |
| 480 |
if self.ignore_tab_changed: |
| 481 |
return |
| 482 |
vl = str(self.tabData(idx) or '').strip() or None |
| 483 |
self.gui.apply_virtual_library(vl, update_tabs=False) |
| 484 |
|
| 485 |
def tab_moved(self, from_, to): |
| 486 |
self.current_db.new_api.set_pref('virt_libs_order', [str(self.tabData(i) or '') for i in range(self.count())]) |
| 487 |
|
| 488 |
def tab_close(self, index): |
| 489 |
vl = str(self.tabData(index) or '') |
| 490 |
if vl: |
| 491 |
self.current_db.new_api.set_pref('virt_libs_hidden', list(self.current_db.new_api.pref('virt_libs_hidden', ())) + [vl]) |
| 492 |
self.removeTab(index) |
| 493 |
|
| 494 |
@property |
| 495 |
def current_db(self): |
| 496 |
return self.gui.current_db |
| 497 |
|
| 498 |
def rebuild(self): |
| 499 |
self.ignore_tab_changed = True |
| 500 |
try: |
| 501 |
self._rebuild() |
| 502 |
finally: |
| 503 |
self.ignore_tab_changed = False |
| 504 |
|
| 505 |
def _rebuild(self): |
| 506 |
db = self.current_db |
| 507 |
vl_map = db.new_api.pref('virtual_libraries', {}) |
| 508 |
virt_libs = frozenset(vl_map) |
| 509 |
hidden = set(db.new_api.pref('virt_libs_hidden', ())) |
| 510 |
if hidden - virt_libs: |
| 511 |
hidden = hidden.intersection(virt_libs) |
| 512 |
db.new_api.set_pref('virt_libs_hidden', list(hidden)) |
| 513 |
order = db.new_api.pref('virt_libs_order', ()) |
| 514 |
while self.count(): |
| 515 |
self.removeTab(0) |
| 516 |
current_lib = db.data.get_base_restriction_name() |
| 517 |
if current_lib in hidden: |
| 518 |
hidden.discard(current_lib) |
| 519 |
db.new_api.set_pref('virt_libs_hidden', list(hidden)) |
| 520 |
current_idx = all_idx = None |
| 521 |
virt_libs = (set(virt_libs) - hidden) | {''} |
| 522 |
order = {x: i for i, x in enumerate(order)} |
| 523 |
for i, vl in enumerate(sorted(virt_libs, key=lambda x: (order.get(x, 0), sort_key(x)))): |
| 524 |
self.addTab(vl.replace('&', '&&') or _('All books')) |
| 525 |
sexp = vl_map.get(vl, None) |
| 526 |
if sexp is not None: |
| 527 |
self.setTabToolTip(i, _('Search expression for this Virtual library:') + '\n\n' + sexp) |
| 528 |
self.setTabData(i, vl) |
| 529 |
if vl == current_lib: |
| 530 |
current_idx = i |
| 531 |
if not vl: |
| 532 |
all_idx = i |
| 533 |
assert all_idx is not None |
| 534 |
self.setCurrentIndex(all_idx if current_idx is None else current_idx) |
| 535 |
if current_idx is None and current_lib: |
| 536 |
self.setTabText(all_idx, current_lib) |
| 537 |
btn = self.tabButton(all_idx, QTabBar.ButtonPosition.RightSide) |
| 538 |
if btn is not None: |
| 539 |
btn.setVisible(False) |
| 540 |
else: |
| 541 |
btn = self.tabButton(all_idx, QTabBar.ButtonPosition.LeftSide) |
| 542 |
if btn is not None: |
| 543 |
btn.setVisible(False) |
| 544 |
self.update_visibility() |
| 545 |
|
| 546 |
def update_current(self): |
| 547 |
self.rebuild() |
| 548 |
|
| 549 |
def contextMenuEvent(self, a0): |
| 550 |
m = QMenu(self) |
| 551 |
m.addAction(QIcon.ic('sort.png'), _('Sort tabs alphabetically'), self.sort_alphabetically) |
| 552 |
hidden = self.current_db.new_api.pref('virt_libs_hidden') |
| 553 |
if hidden: |
| 554 |
s = m.addMenu(_('Restore hidden tabs')) |
| 555 |
assert s is not None |
| 556 |
for x in hidden: |
| 557 |
s.addAction(x, partial(self.restore, x)) |
| 558 |
m.addAction(_('Hide Virtual library tabs'), self.disable_bar) |
| 559 |
if gprefs['vl_tabs_closable']: |
| 560 |
m.addAction(QIcon.ic('drm-locked.png'), _('Lock Virtual library tabs'), self.lock_tab) |
| 561 |
else: |
| 562 |
m.addAction(QIcon.ic('drm-unlocked.png'), _('Unlock Virtual library tabs'), self.unlock_tab) |
| 563 |
i = self.tabAt(a0.pos()) |
| 564 |
if i > -1: |
| 565 |
vl = str(self.tabData(i) or '') |
| 566 |
if vl: |
| 567 |
vln = vl.replace('&', '&&') |
| 568 |
m.addSeparator() |
| 569 |
m.addAction(QIcon.ic('edit_input.png'), _('Edit "%s"') % vln, partial(self.gui.do_create_edit, name=vl)) |
| 570 |
m.addAction(QIcon.ic('trash.png'), _('Delete "%s"') % vln, partial(self.gui.remove_vl_triggered, name=vl)) |
| 571 |
m.exec(a0.globalPos()) |
| 572 |
|
| 573 |
def sort_alphabetically(self): |
| 574 |
self.current_db.new_api.set_pref('virt_libs_order', ()) |
| 575 |
self.rebuild() |
| 576 |
|
| 577 |
def restore(self, x): |
| 578 |
h = self.current_db.new_api.pref('virt_libs_hidden', ()) |
| 579 |
self.current_db.new_api.set_pref('virt_libs_hidden', list(set(h) - {x})) |
| 580 |
self.rebuild() |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
class StatusBarButton(QToolButton): |
| 587 |
def __init__(self, parent, action_name, pref_name, on_click): |
| 588 |
super().__init__(parent=parent) |
| 589 |
act = parent.iactions[action_name] |
| 590 |
self.action_name = action_name |
| 591 |
self.setCursor(Qt.CursorShape.PointingHandCursor) |
| 592 |
self.setPopupMode(QToolButton.ToolButtonPopupMode.InstantPopup) |
| 593 |
self.setAutoRaise(True) |
| 594 |
self.setIcon(QIcon.ic(act.action_spec[1])) |
| 595 |
self.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon) |
| 596 |
self.setText(act.action_spec[0]) |
| 597 |
self.setToolTip(act.action_spec[2]) |
| 598 |
self.setVisible(gprefs[pref_name]) |
| 599 |
parent.status_bar.addPermanentWidget(self) |
| 600 |
if on_click == 'menu': |
| 601 |
self.setMenu(act.qaction.menu()) |
| 602 |
elif on_click == 'trigger': |
| 603 |
self.clicked.connect(act.qaction.trigger) |
| 604 |
else: |
| 605 |
raise ValueError(f'make_status_line_action_button: invalid on_click ({on_click}') |
| 606 |
|
| 607 |
|
| 608 |
class LayoutMixin: |
| 609 |
def place_layout_buttons(self: Main): |
| 610 |
if getattr(self, 'layout_buttons', None): |
| 611 |
for x in self.layout_buttons: |
| 612 |
self.status_bar.removeWidget(x) |
| 613 |
if self.layout_container.is_wide: |
| 614 |
self.button_order = 'sb', 'tb', 'cb', 'bs', 'gv', 'qv', 'bd' |
| 615 |
else: |
| 616 |
self.button_order = 'sb', 'tb', 'bd', 'gv', 'cb', 'bs', 'qv' |
| 617 |
self.layout_buttons = [] |
| 618 |
s = self.style() |
| 619 |
assert s is not None |
| 620 |
stylename = str(s.objectName()) |
| 621 |
for x in self.button_order: |
| 622 |
if x == 'gv': |
| 623 |
button = self.grid_view_button |
| 624 |
elif x == 'bs': |
| 625 |
button = self.bookshelf_view_button |
| 626 |
elif x == 'sb': |
| 627 |
button = self.search_bar_button |
| 628 |
else: |
| 629 |
button = self.layout_container.button_for( |
| 630 |
{ |
| 631 |
'tb': 'tag_browser', |
| 632 |
'bd': 'book_details', |
| 633 |
'cb': 'cover_browser', |
| 634 |
'qv': 'quick_view', |
| 635 |
}[x] |
| 636 |
) |
| 637 |
self.layout_buttons.append(button) |
| 638 |
button.setVisible(gprefs['show_layout_buttons']) |
| 639 |
if ismacos and stylename != 'Calibre': |
| 640 |
button.setStyleSheet(''' |
| 641 |
QToolButton { background: none; border:none; padding: 0px; } |
| 642 |
QToolButton:checked { background: rgba(0, 0, 0, 25%); } |
| 643 |
''') |
| 644 |
for button in reversed(self.layout_buttons): |
| 645 |
self.status_bar.insertPermanentWidget(2, button) |
| 646 |
self.layout_button.setVisible(not gprefs['show_layout_buttons']) |
| 647 |
|
| 648 |
def init_layout_mixin(self: Main): |
| 649 |
self.vl_tabs = VLTabs(self) |
| 650 |
cl = self.centralwidget.layout() |
| 651 |
assert cl is not None |
| 652 |
cl.addWidget(self.vl_tabs) |
| 653 |
self.layout_container = CentralContainer(self) |
| 654 |
cl.addWidget(self.layout_container) |
| 655 |
self.book_details = BookDetails(self.layout_container.is_wide, self) |
| 656 |
self.stack = QStackedWidget(self) |
| 657 |
self.library_view = BooksView(self) |
| 658 |
self.library_view.setObjectName('library_view') |
| 659 |
stack = QStackedWidget(self) |
| 660 |
self.stack.addWidget(stack) |
| 661 |
av = self.library_view.alternate_views |
| 662 |
self.pin_container = av.set_stack(stack) |
| 663 |
self.grid_view = GridView(self) |
| 664 |
self.grid_view.setObjectName('grid_view') |
| 665 |
av.add_view('grid', self.grid_view) |
| 666 |
self.bookshelf_view = BookshelfView(self) |
| 667 |
self.bookshelf_view.setObjectName('bookshelf_view') |
| 668 |
av.add_view('bookshelf', self.bookshelf_view) |
| 669 |
self.tb_widget = TagBrowserWidget(self) |
| 670 |
self.memory_view = DeviceBooksView(self) |
| 671 |
self.stack.addWidget(self.memory_view) |
| 672 |
self.memory_view.setObjectName('memory_view') |
| 673 |
self.card_a_view = DeviceBooksView(self) |
| 674 |
self.stack.addWidget(self.card_a_view) |
| 675 |
self.card_a_view.setObjectName('card_a_view') |
| 676 |
self.card_b_view = DeviceBooksView(self) |
| 677 |
self.stack.addWidget(self.card_b_view) |
| 678 |
self.card_b_view.setObjectName('card_b_view') |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
self.qv = find_plugin('Quickview') |
| 683 |
if self.qv and self.qv.actual_plugin_: |
| 684 |
self.qv = self.qv.actual_plugin_ |
| 685 |
|
| 686 |
self.layout_container.initialize_with_gui(self, self.stack) |
| 687 |
self.layout_container.tag_browser_button.toggled.connect(self.tb_widget.set_pane_is_visible, Qt.ConnectionType.QueuedConnection) |
| 688 |
self.status_bar = StatusBar(self) |
| 689 |
self.grid_view_button = GridViewButton(self) |
| 690 |
self.bookshelf_view_button = BookshelfViewButton(self) |
| 691 |
self.search_bar_button = SearchBarButton(self) |
| 692 |
self.search_bar_button.toggled.connect(self.toggle_search_bar) |
| 693 |
|
| 694 |
self.layout_button = b = QToolButton(self) |
| 695 |
self.layout_button_menu = m = LayoutMenu(self) |
| 696 |
b.setAutoRaise(True), b.setCursor(Qt.CursorShape.PointingHandCursor) |
| 697 |
b.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon) |
| 698 |
b.setText(_('Layout')), b.setIcon(QIcon.ic('layout.png')) |
| 699 |
b.setToolTip(_('Show and hide various parts of the calibre main window')) |
| 700 |
b.clicked.connect(m.toggle_visibility) |
| 701 |
self.status_bar.addPermanentWidget(b) |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
self.sb_all_gui_actions_button = StatusBarButton(self, 'All GUI actions', 'show_sb_all_actions_button', 'menu') |
| 708 |
self.status_bar_extra_buttons = (self.sb_all_gui_actions_button,) |
| 709 |
|
| 710 |
self.status_bar.addPermanentWidget(self.jobs_button) |
| 711 |
self.setStatusBar(self.status_bar) |
| 712 |
self.status_bar.update_label.linkActivated.connect(self.update_link_clicked) |
| 713 |
|
| 714 |
def finalize_layout(self: Main): |
| 715 |
self.status_bar.initialize(self.system_tray_icon) |
| 716 |
self.book_details.show_book_info.connect(self.iactions['Show Book Details'].show_book_info) |
| 717 |
self.book_details.files_dropped.connect(self.iactions['Add Books'].files_dropped_on_book) |
| 718 |
self.book_details.cover_changed.connect(self.bd_cover_changed, type=Qt.ConnectionType.QueuedConnection) |
| 719 |
self.book_details.open_cover_with.connect(self.bd_open_cover_with, type=Qt.ConnectionType.QueuedConnection) |
| 720 |
self.book_details.open_fmt_with.connect(self.bd_open_fmt_with, type=Qt.ConnectionType.QueuedConnection) |
| 721 |
self.book_details.edit_book.connect(self.bd_edit_book, type=Qt.ConnectionType.QueuedConnection) |
| 722 |
self.book_details.cover_removed.connect(self.bd_cover_removed, type=Qt.ConnectionType.QueuedConnection) |
| 723 |
self.book_details.remote_file_dropped.connect(self.iactions['Add Books'].remote_file_dropped_on_book, type=Qt.ConnectionType.QueuedConnection) |
| 724 |
self.book_details.open_containing_folder.connect(self.iactions['View'].view_folder_for_id) |
| 725 |
self.book_details.open_data_folder.connect(self.iactions['View'].view_data_folder_for_id) |
| 726 |
self.book_details.view_specific_format.connect(self.iactions['View'].view_format_by_id) |
| 727 |
self.book_details.search_requested.connect(self.set_search_string_with_append) |
| 728 |
self.book_details.remove_specific_format.connect(self.iactions['Remove Books'].remove_format_by_id) |
| 729 |
self.book_details.remove_metadata_item.connect(self.iactions['Edit Metadata'].remove_metadata_item) |
| 730 |
self.book_details.save_specific_format.connect(self.iactions['Save To Disk'].save_library_format_by_ids) |
| 731 |
self.book_details.restore_specific_format.connect(self.iactions['Remove Books'].restore_format) |
| 732 |
self.book_details.set_cover_from_format.connect(self.iactions['Edit Metadata'].set_cover_from_format) |
| 733 |
self.book_details.copy_link.connect(self.bd_copy_link, type=Qt.ConnectionType.QueuedConnection) |
| 734 |
self.book_details.view_device_book.connect(self.iactions['View'].view_device_book) |
| 735 |
self.book_details.manage_category.connect(self.manage_category_triggerred) |
| 736 |
self.book_details.find_in_tag_browser.connect(self.find_in_tag_browser_triggered) |
| 737 |
self.book_details.edit_identifiers.connect(self.edit_identifiers_triggerred) |
| 738 |
self.book_details.compare_specific_format.connect(self.compare_format) |
| 739 |
|
| 740 |
m = self.library_view._model |
| 741 |
assert m is not None |
| 742 |
if m.rowCount(QModelIndex()) > 0: |
| 743 |
QTimer.singleShot(0, self.library_view.set_current_row) |
| 744 |
m.current_changed(self.library_view.currentIndex(), self.library_view.currentIndex()) |
| 745 |
self.library_view.setFocus(Qt.FocusReason.OtherFocusReason) |
| 746 |
|
| 747 |
def show_panel(self, name): |
| 748 |
if name == 'search_bar': |
| 749 |
self.search_bar_button.setChecked(True) |
| 750 |
return |
| 751 |
self.layout_container.show_panel(name) |
| 752 |
|
| 753 |
def hide_panel(self, name): |
| 754 |
if name == 'search_bar': |
| 755 |
self.search_bar_button.setChecked(False) |
| 756 |
return |
| 757 |
self.layout_container.hide_panel(name) |
| 758 |
|
| 759 |
def set_search_string_with_append(self: Main, expression, append=''): |
| 760 |
current = self.search.text().strip() |
| 761 |
if append: |
| 762 |
expr = f'{current} {append} {expression}' if current else expression |
| 763 |
else: |
| 764 |
expr = expression |
| 765 |
self.search.set_search_string(expr) |
| 766 |
|
| 767 |
def edit_identifiers_triggerred(self: Main): |
| 768 |
book_id = self.library_view.current_book |
| 769 |
db = self.current_db.new_api |
| 770 |
identifiers = db.field_for('identifiers', book_id, default_value={}) |
| 771 |
from calibre.gui2.metadata.basic_widgets import Identifiers |
| 772 |
|
| 773 |
d = Identifiers(identifiers, self) |
| 774 |
if d.exec() == QDialog.DialogCode.Accepted: |
| 775 |
identifiers = d.get_identifiers() |
| 776 |
db.set_field('identifiers', {book_id: identifiers}) |
| 777 |
self.iactions['Edit Metadata'].refresh_books_after_metadata_edit({book_id}) |
| 778 |
|
| 779 |
def manage_category_triggerred(self: Main, field, value): |
| 780 |
if field and value: |
| 781 |
if field == 'authors': |
| 782 |
self.do_author_sort_edit(self, value, select_sort=False, select_link=False, lookup_author=True) |
| 783 |
elif field: |
| 784 |
self.do_tags_list_edit(value, field) |
| 785 |
|
| 786 |
def find_in_tag_browser_triggered(self: Main, field, value): |
| 787 |
if field and value: |
| 788 |
tb = self.tb_widget |
| 789 |
tb.set_focus_to_find_box() |
| 790 |
le = tb.item_search.lineEdit() |
| 791 |
assert le is not None |
| 792 |
le.setText(field + ':=' + value) |
| 793 |
tb.do_find() |
| 794 |
|
| 795 |
def toggle_search_bar(self: Main, show): |
| 796 |
self.search_bar.setVisible(show) |
| 797 |
if show: |
| 798 |
self.search.setFocus(Qt.FocusReason.OtherFocusReason) |
| 799 |
|
| 800 |
def bd_cover_changed(self: Main, id_, cdata): |
| 801 |
self.current_db.set_cover(id_, cdata) |
| 802 |
self.refresh_cover_browser() |
| 803 |
|
| 804 |
def bd_open_cover_with(self: Main, book_id, entry): |
| 805 |
cpath = self.current_db.new_api.format_abspath(book_id, '__COVER_INTERNAL__') |
| 806 |
if cpath: |
| 807 |
if entry is None: |
| 808 |
pm = QPixmap() |
| 809 |
pm.load(cpath) |
| 810 |
pm.setDevicePixelRatio(self.devicePixelRatioF()) |
| 811 |
if pm.isNull(): |
| 812 |
open_local_file(cpath) |
| 813 |
else: |
| 814 |
from calibre.gui2.image_popup import ImageView |
| 815 |
|
| 816 |
iv = ImageView( |
| 817 |
qapplication_or_fail().focusWindow(), |
| 818 |
pm, |
| 819 |
QUrl.fromLocalFile(cpath), |
| 820 |
geom_name='book_details_image_view', |
| 821 |
) |
| 822 |
iv(use_exec=True) |
| 823 |
return |
| 824 |
from calibre.gui2.open_with import run_program |
| 825 |
|
| 826 |
run_program(entry, cpath, self) |
| 827 |
|
| 828 |
def bd_open_fmt_with(self: Main, book_id, fmt, entry): |
| 829 |
path = self.current_db.new_api.format_abspath(book_id, fmt) |
| 830 |
if path: |
| 831 |
from calibre.gui2.open_with import run_program |
| 832 |
|
| 833 |
run_program(entry, path, self) |
| 834 |
else: |
| 835 |
fmt = fmt.upper() |
| 836 |
error_dialog( |
| 837 |
self, |
| 838 |
_('No %s format') % fmt, |
| 839 |
_('The book {0} does not have the {1} format').format(self.current_db.new_api.field_for('title', book_id, default_value=_('Unknown')), fmt), |
| 840 |
show=True, |
| 841 |
) |
| 842 |
|
| 843 |
def bd_edit_book(self: Main, book_id, fmt): |
| 844 |
from calibre.gui2.widgets import BusyCursor |
| 845 |
|
| 846 |
with BusyCursor(): |
| 847 |
self.iactions['Tweak ePub'].ebook_edit_format(book_id, fmt) |
| 848 |
|
| 849 |
def open_with_action_triggerred(self: Main, fmt, entry, *args): |
| 850 |
book_id = self.library_view.current_book |
| 851 |
if book_id is not None: |
| 852 |
if fmt == 'cover_image': |
| 853 |
self.bd_open_cover_with(book_id, entry) |
| 854 |
else: |
| 855 |
self.bd_open_fmt_with(book_id, fmt, entry) |
| 856 |
|
| 857 |
def bd_cover_removed(self: Main, id_): |
| 858 |
self.current_db.remove_cover(id_, commit=True, notify=False) |
| 859 |
self.refresh_cover_browser() |
| 860 |
|
| 861 |
def bd_copy_link(self, url): |
| 862 |
if url: |
| 863 |
clipboard = QApplication.clipboard() |
| 864 |
assert clipboard is not None |
| 865 |
clipboard.setText(url) |
| 866 |
|
| 867 |
def compare_format(self: Main, book_id, fmt): |
| 868 |
db = self.current_db.new_api |
| 869 |
ofmt = fmt |
| 870 |
if fmt.startswith('ORIGINAL_'): |
| 871 |
fmt = fmt.partition('_')[-1] |
| 872 |
else: |
| 873 |
ofmt = 'ORIGINAL_' + fmt |
| 874 |
path1, path2 = db.format_abspath(book_id, ofmt), db.format_abspath(book_id, fmt) |
| 875 |
from calibre.gui2.tweak_book.diff.main import compare_books |
| 876 |
|
| 877 |
compare_books( |
| 878 |
path1, |
| 879 |
path2, |
| 880 |
parent=self, |
| 881 |
revert_msg=_('Restore %s') % ofmt, |
| 882 |
revert_callback=partial(self.iactions['Remove Books'].restore_format, book_id, ofmt), |
| 883 |
names=(ofmt, fmt), |
| 884 |
) |
| 885 |
|
| 886 |
def save_layout_state(self: Main): |
| 887 |
self.library_view.save_state() |
| 888 |
self.memory_view.save_state() |
| 889 |
self.card_a_view.save_state() |
| 890 |
self.card_b_view.save_state() |
| 891 |
self.layout_container.write_settings() |
| 892 |
self.grid_view_button.save_state() |
| 893 |
self.bookshelf_view_button.save_state() |
| 894 |
self.search_bar_button.save_state() |
| 895 |
|
| 896 |
def read_layout_settings(self: Main): |
| 897 |
|
| 898 |
self.layout_container.read_settings() |
| 899 |
self.book_details.change_layout(self.layout_container.is_wide) |
| 900 |
self.place_layout_buttons() |
| 901 |
self.grid_view_button.restore_state() |
| 902 |
self.bookshelf_view_button.restore_state() |
| 903 |
self.search_bar_button.restore_state() |
| 904 |
|
| 905 |
def update_status_bar(self: Main, *args): |
| 906 |
v = self.current_view() |
| 907 |
selected = len(v.selectionModel().selectedRows()) |
| 908 |
try: |
| 909 |
library_total, total, current = v.model().counts() |
| 910 |
except AttributeError: |
| 911 |
if self.shutting_down: |
| 912 |
return |
| 913 |
raise |
| 914 |
self.status_bar.update_state(library_total, total, current, selected) |
| 915 |
|
| 916 |
|
| 917 |
|