| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import json |
| 5 |
import os |
| 6 |
import re |
| 7 |
import sys |
| 8 |
import time |
| 9 |
import uuid |
| 10 |
from collections import defaultdict, namedtuple |
| 11 |
from hashlib import sha256 |
| 12 |
from threading import Thread |
| 13 |
|
| 14 |
from qt.core import ( |
| 15 |
QCursor, |
| 16 |
QDockWidget, |
| 17 |
QEvent, |
| 18 |
QImage, |
| 19 |
QMainWindow, |
| 20 |
QMenu, |
| 21 |
QMimeData, |
| 22 |
QModelIndex, |
| 23 |
QPainter, |
| 24 |
QPixmap, |
| 25 |
QSizeF, |
| 26 |
QSvgRenderer, |
| 27 |
Qt, |
| 28 |
QTimer, |
| 29 |
QToolBar, |
| 30 |
QUrl, |
| 31 |
QVBoxLayout, |
| 32 |
QWidget, |
| 33 |
pyqtSignal, |
| 34 |
sip, |
| 35 |
) |
| 36 |
|
| 37 |
import calibre.gui2.viewer.web_view as _web_view_module |
| 38 |
from calibre import prints |
| 39 |
from calibre.constants import ismacos, iswindows |
| 40 |
from calibre.customize.ui import available_input_formats |
| 41 |
from calibre.db.annotations import merge_annotations |
| 42 |
from calibre.gui2 import add_to_recent_docs, choose_files, error_dialog, qapplication_or_fail, sanitize_env_vars |
| 43 |
from calibre.gui2.dialogs.drm_error import DRMErrorMessage |
| 44 |
from calibre.gui2.image_popup import ImagePopup |
| 45 |
from calibre.gui2.main_window import MainWindow |
| 46 |
from calibre.gui2.viewer import get_boss, get_current_book_data, performance_monitor |
| 47 |
from calibre.gui2.viewer.annotations import AnnotationsSaveWorker, annotations_dir, parse_annotations |
| 48 |
from calibre.gui2.viewer.bookmarks import BookmarkManager |
| 49 |
from calibre.gui2.viewer.config import delete_all_reading_rates, get_session_pref, load_reading_rates, save_reading_rates, vprefs |
| 50 |
from calibre.gui2.viewer.convert_book import prepare_book |
| 51 |
from calibre.gui2.viewer.highlights import HighlightsPanel, style_definition_for_name |
| 52 |
from calibre.gui2.viewer.integration import get_book_library_details, load_annotations_map_from_library |
| 53 |
from calibre.gui2.viewer.overlay import LoadingOverlay |
| 54 |
from calibre.gui2.viewer.search import SearchPanel |
| 55 |
from calibre.gui2.viewer.toc import TOC, TOCItem, TOCSearch, TOCView |
| 56 |
from calibre.gui2.viewer.toolbars import ActionsToolBar |
| 57 |
from calibre.gui2.viewer.web_view import WebView, get_path_for_name, set_book_path |
| 58 |
from calibre.live import async_stop_worker |
| 59 |
from calibre.startup import connect_lambda |
| 60 |
from calibre.utils.date import utcnow |
| 61 |
from calibre.utils.filenames import make_long_path_useable |
| 62 |
from calibre.utils.img import image_from_path |
| 63 |
from calibre.utils.ipc.simple_worker import WorkerError |
| 64 |
from calibre.utils.localization import _ |
| 65 |
from polyglot.builtins import as_bytes, as_unicode |
| 66 |
|
| 67 |
|
| 68 |
def is_float(x): |
| 69 |
try: |
| 70 |
float(x) |
| 71 |
return True |
| 72 |
except Exception: |
| 73 |
pass |
| 74 |
return False |
| 75 |
|
| 76 |
|
| 77 |
def dock_defs(): |
| 78 |
Dock = namedtuple('Dock', 'name title initial_area allowed_areas') |
| 79 |
ans = {} |
| 80 |
|
| 81 |
def d(title, name, area, allowed=Qt.DockWidgetArea.LeftDockWidgetArea | Qt.DockWidgetArea.RightDockWidgetArea): |
| 82 |
ans[name] = Dock(name + '-dock', title, area, allowed) |
| 83 |
|
| 84 |
d(_('Table of Contents'), 'toc', Qt.DockWidgetArea.LeftDockWidgetArea) |
| 85 |
d(_('Lookup'), 'lookup', Qt.DockWidgetArea.RightDockWidgetArea) |
| 86 |
d(_('Bookmarks'), 'bookmarks', Qt.DockWidgetArea.RightDockWidgetArea) |
| 87 |
d(_('Search'), 'search', Qt.DockWidgetArea.LeftDockWidgetArea) |
| 88 |
d(_('Inspector'), 'inspector', Qt.DockWidgetArea.RightDockWidgetArea, Qt.DockWidgetArea.AllDockWidgetAreas) |
| 89 |
d(_('Highlights'), 'highlights', Qt.DockWidgetArea.RightDockWidgetArea) |
| 90 |
return ans |
| 91 |
|
| 92 |
|
| 93 |
def path_key(path): |
| 94 |
return sha256(as_bytes(path)).hexdigest() |
| 95 |
|
| 96 |
|
| 97 |
class EbookViewer(MainWindow): |
| 98 |
msg_from_anotherinstance = pyqtSignal(object) |
| 99 |
book_preparation_started = pyqtSignal() |
| 100 |
book_prepared = pyqtSignal(object, object) |
| 101 |
MAIN_WINDOW_STATE_VERSION = 1 |
| 102 |
|
| 103 |
toc_dock: QDockWidget |
| 104 |
search_dock: QDockWidget |
| 105 |
inspector_dock: QDockWidget |
| 106 |
bookmarks_dock: QDockWidget |
| 107 |
highlights_dock: QDockWidget |
| 108 |
lookup_dock: QDockWidget |
| 109 |
|
| 110 |
def __init__(self, open_at=None, continue_reading=None, force_reload=False, calibre_book_data=None): |
| 111 |
MainWindow.__init__(self, None) |
| 112 |
get_boss(self) |
| 113 |
|
| 114 |
self.annotations_saver = None |
| 115 |
self.last_read_pos_saver = None |
| 116 |
self.calibre_book_data_for_first_book = calibre_book_data |
| 117 |
self.shutting_down = self.close_forced = self.shutdown_done = False |
| 118 |
self.force_reload = force_reload |
| 119 |
connect_lambda( |
| 120 |
self.book_preparation_started, |
| 121 |
self, |
| 122 |
lambda self: self.loading_overlay(_('Preparing book for first read, please wait')), |
| 123 |
type=Qt.ConnectionType.QueuedConnection, |
| 124 |
) |
| 125 |
self.maximized_at_last_fullscreen = False |
| 126 |
self.save_pos_timer = t = QTimer(self) |
| 127 |
t.setSingleShot(True), t.setInterval(3000), t.setTimerType(Qt.TimerType.VeryCoarseTimer) |
| 128 |
t.timeout.connect(self._on_save_pos_timer) |
| 129 |
self.pending_open_at = open_at |
| 130 |
self.pending_search = None |
| 131 |
self.base_window_title = _('E-book viewer') |
| 132 |
self.setDockOptions(QMainWindow.DockOption.AnimatedDocks | QMainWindow.DockOption.AllowTabbedDocks | QMainWindow.DockOption.AllowNestedDocks) |
| 133 |
self.setWindowTitle(self.base_window_title) |
| 134 |
self.in_full_screen_mode = None |
| 135 |
self.image_popup = ImagePopup(self, prefs=vprefs) |
| 136 |
self.actions_toolbar = at = ActionsToolBar(self) |
| 137 |
at.open_book_at_path.connect(self.ask_for_open) |
| 138 |
self.addToolBar(Qt.ToolBarArea.LeftToolBarArea, at) |
| 139 |
try: |
| 140 |
os.makedirs(annotations_dir()) |
| 141 |
except OSError: |
| 142 |
pass |
| 143 |
self.current_book_data = {} |
| 144 |
get_current_book_data(self.current_book_data) |
| 145 |
self.book_prepared.connect(self.load_finished, type=Qt.ConnectionType.QueuedConnection) |
| 146 |
self.dock_defs = dock_defs() |
| 147 |
|
| 148 |
def create_dock(title, name, area, areas=Qt.DockWidgetArea.LeftDockWidgetArea | Qt.DockWidgetArea.RightDockWidgetArea): |
| 149 |
ans = QDockWidget(title, self) |
| 150 |
ans.setObjectName(name) |
| 151 |
self.addDockWidget(area, ans) |
| 152 |
ans.setVisible(False) |
| 153 |
ans.visibilityChanged.connect(self.dock_visibility_changed) |
| 154 |
return ans |
| 155 |
|
| 156 |
for dock_def in self.dock_defs.values(): |
| 157 |
setattr( |
| 158 |
self, |
| 159 |
'{}_dock'.format(dock_def.name.partition('-')[0]), |
| 160 |
create_dock(dock_def.title, dock_def.name, dock_def.initial_area, dock_def.allowed_areas), |
| 161 |
) |
| 162 |
|
| 163 |
self.toc_container = w = QWidget(self) |
| 164 |
toc_layout = QVBoxLayout(w) |
| 165 |
self.toc = TOCView(w) |
| 166 |
self.toc.clicked[QModelIndex].connect(self.toc_clicked) |
| 167 |
self.toc.searched.connect(self.toc_searched) |
| 168 |
self.toc_search = TOCSearch(self.toc, parent=w) |
| 169 |
toc_layout.addWidget(self.toc), toc_layout.addWidget(self.toc_search), toc_layout.setContentsMargins(0, 0, 0, 0) |
| 170 |
self.toc_dock.setWidget(w) |
| 171 |
|
| 172 |
self.search_widget = w = SearchPanel(self) |
| 173 |
w.search_requested.connect(self.start_search) |
| 174 |
w.hide_search_panel.connect(self.search_dock.close) |
| 175 |
w.count_changed.connect(self.search_results_count_changed) |
| 176 |
w.goto_cfi.connect(self.goto_cfi) |
| 177 |
self.search_dock.setWidget(w) |
| 178 |
self.search_dock.visibilityChanged.connect(self.search_widget.visibility_changed) |
| 179 |
|
| 180 |
from calibre.gui2.viewer.lookup import Lookup |
| 181 |
|
| 182 |
self.lookup_widget = w = Lookup(self, viewer=self) |
| 183 |
self.lookup_dock.visibilityChanged.connect(self.lookup_widget.visibility_changed) |
| 184 |
self.lookup_dock.setWidget(w) |
| 185 |
|
| 186 |
self.bookmarks_widget = w = BookmarkManager(self) |
| 187 |
connect_lambda(w.create_requested, self, lambda self: self.web_view.trigger_shortcut('new_bookmark')) |
| 188 |
w.edited.connect(self.bookmarks_edited) |
| 189 |
w.activated.connect(self.bookmark_activated) |
| 190 |
w.toggle_requested.connect(self.toggle_bookmarks) |
| 191 |
self.bookmarks_dock.setWidget(w) |
| 192 |
|
| 193 |
self.highlights_widget = w = HighlightsPanel(self) |
| 194 |
self.highlights_dock.setWidget(w) |
| 195 |
w.toggle_requested.connect(self.toggle_highlights) |
| 196 |
|
| 197 |
self.web_view = WebView(self) |
| 198 |
self.web_view.cfi_changed.connect(self.cfi_changed) |
| 199 |
self.web_view.update_last_read_position.connect(self._on_last_read_pos_data) |
| 200 |
self.web_view.reload_book.connect(self.reload_book) |
| 201 |
self.web_view.toggle_toc.connect(self.toggle_toc) |
| 202 |
self.web_view.show_search.connect(self.show_search) |
| 203 |
self.web_view.find_next.connect(self.search_widget.find_next_requested) |
| 204 |
self.search_widget.show_search_result.connect(self.show_search_result) |
| 205 |
self.web_view.search_result_not_found.connect(self.search_widget.search_result_not_found) |
| 206 |
self.web_view.search_result_discovered.connect(self.search_widget.search_result_discovered) |
| 207 |
self.web_view.toggle_bookmarks.connect(self.toggle_bookmarks) |
| 208 |
self.web_view.toggle_highlights.connect(self.toggle_highlights) |
| 209 |
self.web_view.new_bookmark.connect(self.bookmarks_widget.create_new_bookmark) |
| 210 |
self.web_view.toggle_inspector.connect(self.toggle_inspector) |
| 211 |
self.web_view.toggle_lookup.connect(self.toggle_lookup) |
| 212 |
self.web_view.quit.connect(self.quit) |
| 213 |
self.web_view.update_current_toc_nodes.connect(self.toc.update_current_toc_nodes) |
| 214 |
self.web_view.toggle_full_screen.connect(self.toggle_full_screen) |
| 215 |
self.web_view.ask_for_open.connect(self.ask_for_open_from_js, type=Qt.ConnectionType.QueuedConnection) |
| 216 |
self.web_view.selection_changed.connect(self.lookup_widget.selected_text_changed, type=Qt.ConnectionType.QueuedConnection) |
| 217 |
self.web_view.selection_changed.connect(self.highlights_widget.selected_text_changed, type=Qt.ConnectionType.QueuedConnection) |
| 218 |
self.web_view.view_image.connect(self.view_image, type=Qt.ConnectionType.QueuedConnection) |
| 219 |
self.web_view.copy_image.connect(self.copy_image, type=Qt.ConnectionType.QueuedConnection) |
| 220 |
self.web_view.show_loading_message.connect(self.show_loading_message) |
| 221 |
self.web_view.show_error.connect(self.show_error) |
| 222 |
self.web_view.print_book.connect(self.print_book, type=Qt.ConnectionType.QueuedConnection) |
| 223 |
self.web_view.reset_interface.connect(self.reset_interface, type=Qt.ConnectionType.QueuedConnection) |
| 224 |
self.web_view.quit.connect(self.quit, type=Qt.ConnectionType.QueuedConnection) |
| 225 |
self.web_view.shortcuts_changed.connect(self.shortcuts_changed) |
| 226 |
self.web_view.scrollbar_context_menu.connect(self.scrollbar_context_menu) |
| 227 |
self.web_view.close_prep_finished.connect(self.close_prep_finished) |
| 228 |
self.web_view.highlights_changed.connect(self.highlights_changed) |
| 229 |
self.web_view.update_reading_rates.connect(self.update_reading_rates) |
| 230 |
self.web_view.reset_reading_rates.connect(self.reset_reading_rates) |
| 231 |
self.web_view.edit_book.connect(self.edit_book) |
| 232 |
self.web_view.content_file_changed.connect(self.content_file_changed) |
| 233 |
|
| 234 |
self.actions_toolbar.initialize(self.web_view, self.search_dock.toggleViewAction()) |
| 235 |
self.setCentralWidget(self.web_view) |
| 236 |
self.loading_overlay = LoadingOverlay(self) |
| 237 |
self.restore_state() |
| 238 |
self.actions_toolbar.update_visibility() |
| 239 |
self.dock_visibility_changed() |
| 240 |
self.highlights_widget.request_highlight_action.connect(self.web_view.highlight_action) |
| 241 |
self.highlights_widget.web_action.connect(self.web_view.generic_action) |
| 242 |
self.highlights_widget.notes_edited_signal.connect(self.notes_edited) |
| 243 |
if continue_reading: |
| 244 |
self.continue_reading() |
| 245 |
|
| 246 |
self.setup_mouse_auto_hide() |
| 247 |
self.lookup_widget.add_note_requested.connect(self.add_notes_or_create_highlight) |
| 248 |
|
| 249 |
def create_uuid(self): |
| 250 |
return uuid.uuid4().hex |
| 251 |
|
| 252 |
def shortcuts_changed(self, smap): |
| 253 |
rmap = defaultdict(list) |
| 254 |
for k, v in smap.items(): |
| 255 |
rmap[v].append(k) |
| 256 |
self.actions_toolbar.set_tooltips(rmap) |
| 257 |
if hasattr(self, 'highlights_widget'): |
| 258 |
self.highlights_widget.set_tooltips(rmap) |
| 259 |
self.search_widget.set_tooltips(rmap) |
| 260 |
|
| 261 |
def resizeEvent(self, a0): |
| 262 |
self.loading_overlay.resize(self.size()) |
| 263 |
return MainWindow.resizeEvent(self, a0) |
| 264 |
|
| 265 |
def scrollbar_context_menu(self, x, y, frac): |
| 266 |
m = QMenu(self) |
| 267 |
amap = {} |
| 268 |
|
| 269 |
def a(text, name): |
| 270 |
m.addAction(text) |
| 271 |
amap[text] = name |
| 272 |
|
| 273 |
a(_('Scroll here'), 'here') |
| 274 |
m.addSeparator() |
| 275 |
a(_('Start of book'), 'start_of_book') |
| 276 |
a(_('End of book'), 'end_of_book') |
| 277 |
m.addSeparator() |
| 278 |
a(_('Previous section'), 'previous_section') |
| 279 |
a(_('Next section'), 'next_section') |
| 280 |
m.addSeparator() |
| 281 |
a(_('Start of current file'), 'start_of_file') |
| 282 |
a(_('End of current file'), 'end_of_file') |
| 283 |
m.addSeparator() |
| 284 |
a(_('Hide this scrollbar'), 'toggle_scrollbar') |
| 285 |
|
| 286 |
q = m.exec(QCursor.pos()) |
| 287 |
if not q: |
| 288 |
return |
| 289 |
q = amap[q.text()] |
| 290 |
if q == 'here': |
| 291 |
self.web_view.goto_frac(frac) |
| 292 |
else: |
| 293 |
self.web_view.trigger_shortcut(q) |
| 294 |
|
| 295 |
|
| 296 |
def handle_commandline_arg(self, arg): |
| 297 |
if arg: |
| 298 |
if os.path.isfile(arg) and os.access(arg, os.R_OK): |
| 299 |
self.load_ebook(arg) |
| 300 |
else: |
| 301 |
prints('Cannot read from:', arg, file=sys.stderr) |
| 302 |
|
| 303 |
def message_from_other_instance(self, msg): |
| 304 |
try: |
| 305 |
msg = json.loads(msg) |
| 306 |
path, open_at = msg |
| 307 |
except Exception as err: |
| 308 |
print('Invalid message from other instance', file=sys.stderr) |
| 309 |
print(err, file=sys.stderr) |
| 310 |
return |
| 311 |
self.load_ebook(path, open_at=open_at) |
| 312 |
self.raise_and_focus() |
| 313 |
self.activateWindow() |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
def set_full_screen(self, on): |
| 319 |
if on: |
| 320 |
self.maximized_at_last_fullscreen = self.isMaximized() |
| 321 |
if not self.actions_toolbar.visible_in_fullscreen: |
| 322 |
self.actions_toolbar.setVisible(False) |
| 323 |
self.showFullScreen() |
| 324 |
else: |
| 325 |
self.actions_toolbar.update_visibility() |
| 326 |
if self.maximized_at_last_fullscreen: |
| 327 |
self.showMaximized() |
| 328 |
else: |
| 329 |
self.showNormal() |
| 330 |
|
| 331 |
def changeEvent(self, a0): |
| 332 |
if a0.type() == QEvent.Type.WindowStateChange: |
| 333 |
in_full_screen_mode = self.isFullScreen() |
| 334 |
if self.in_full_screen_mode is None or self.in_full_screen_mode != in_full_screen_mode: |
| 335 |
self.in_full_screen_mode = in_full_screen_mode |
| 336 |
self.web_view.notify_full_screen_state_change(self.in_full_screen_mode) |
| 337 |
return MainWindow.changeEvent(self, a0) |
| 338 |
|
| 339 |
def toggle_full_screen(self): |
| 340 |
self.set_full_screen(not self.isFullScreen()) |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
def toggle_inspector(self): |
| 347 |
action = self.inspector_dock.toggleViewAction() |
| 348 |
visible = action.isChecked() if action is not None else False |
| 349 |
self.inspector_dock.setVisible(not visible) |
| 350 |
|
| 351 |
def toggle_toc(self): |
| 352 |
is_visible = self.toc_dock.isVisible() |
| 353 |
self.toc_dock.setVisible(not is_visible) |
| 354 |
if not is_visible: |
| 355 |
self.toc.scroll_to_current_toc_node() |
| 356 |
|
| 357 |
def show_search_result(self, sr): |
| 358 |
self.web_view.show_search_result(sr) |
| 359 |
|
| 360 |
def show_search(self, text, trigger=False, search_type=None, case_sensitive=None, no_history=False): |
| 361 |
self.search_dock.setVisible(True) |
| 362 |
self.search_dock.activateWindow() |
| 363 |
self.search_dock.raise_and_focus() |
| 364 |
self.search_widget.focus_input(text, search_type, case_sensitive, no_history=no_history) |
| 365 |
if trigger: |
| 366 |
self.search_widget.trigger() |
| 367 |
|
| 368 |
def search_results_count_changed(self, num=-1): |
| 369 |
if num < 0: |
| 370 |
tt = _('Search') |
| 371 |
elif num == 0: |
| 372 |
tt = _('Search :: no matches') |
| 373 |
elif num == 1: |
| 374 |
tt = _('Search :: one match') |
| 375 |
else: |
| 376 |
tt = _('Search :: {} matches').format(num) |
| 377 |
self.search_dock.setWindowTitle(tt) |
| 378 |
|
| 379 |
def start_search(self, search_query): |
| 380 |
name = self.web_view.current_content_file |
| 381 |
if name: |
| 382 |
if search_query.is_empty and search_query.text: |
| 383 |
return error_dialog( |
| 384 |
self, |
| 385 |
_('Empty search expression'), |
| 386 |
_('Cannot search for {!r} as it contains only punctuation and spaces.').format(search_query.text), |
| 387 |
show=True, |
| 388 |
) |
| 389 |
self.web_view.get_current_cfi(self.search_widget.set_anchor_cfi) |
| 390 |
self.search_widget.start_search(search_query, name) |
| 391 |
self.web_view.setFocus(Qt.FocusReason.OtherFocusReason) |
| 392 |
|
| 393 |
def toggle_bookmarks(self): |
| 394 |
is_visible = self.bookmarks_dock.isVisible() |
| 395 |
self.bookmarks_dock.setVisible(not is_visible) |
| 396 |
if is_visible: |
| 397 |
self.web_view.setFocus(Qt.FocusReason.OtherFocusReason) |
| 398 |
else: |
| 399 |
self.bookmarks_widget.bookmarks_list.setFocus(Qt.FocusReason.OtherFocusReason) |
| 400 |
|
| 401 |
def toggle_highlights(self): |
| 402 |
is_visible = self.highlights_dock.isVisible() |
| 403 |
self.highlights_dock.setVisible(not is_visible) |
| 404 |
if is_visible: |
| 405 |
self.web_view.setFocus(Qt.FocusReason.OtherFocusReason) |
| 406 |
else: |
| 407 |
self.highlights_widget.focus() |
| 408 |
|
| 409 |
def toggle_lookup(self, force_show=False): |
| 410 |
self.lookup_dock.setVisible(force_show or not self.lookup_dock.isVisible()) |
| 411 |
if force_show and self.lookup_dock.isVisible(): |
| 412 |
self.lookup_widget.on_forced_show() |
| 413 |
|
| 414 |
def check_for_read_aloud(self, where: str): |
| 415 |
if self.actions_toolbar.toggle_read_aloud_action.isChecked(): |
| 416 |
error_dialog( |
| 417 |
self, |
| 418 |
_('Cannot jump to location'), |
| 419 |
_('The Read aloud feature is active, cannot jump to {}. Close it first.').format(where), |
| 420 |
show=True, |
| 421 |
) |
| 422 |
return True |
| 423 |
return False |
| 424 |
|
| 425 |
def toc_clicked(self, index): |
| 426 |
if self.check_for_read_aloud(_('Table of Contents locations')): |
| 427 |
return |
| 428 |
item = self.toc_model.itemFromIndex(index) |
| 429 |
assert item is not None |
| 430 |
assert isinstance(item, TOCItem) |
| 431 |
self.web_view.goto_toc_node(item.node_id) |
| 432 |
self.force_focus_on_web_view() |
| 433 |
|
| 434 |
def force_focus_on_web_view(self): |
| 435 |
self.activateWindow() |
| 436 |
self.web_view.setFocus(Qt.FocusReason.OtherFocusReason) |
| 437 |
|
| 438 |
def toc_searched(self, index): |
| 439 |
item = self.toc_model.itemFromIndex(index) |
| 440 |
assert item is not None |
| 441 |
assert isinstance(item, TOCItem) |
| 442 |
self.web_view.goto_toc_node(item.node_id) |
| 443 |
|
| 444 |
def bookmarks_edited(self, bookmarks): |
| 445 |
self.current_book_data['annotations_map']['bookmark'] = bookmarks |
| 446 |
self.save_annotations(in_book_file=False) |
| 447 |
|
| 448 |
def goto_cfi(self, cfi, add_to_history=False): |
| 449 |
self.web_view.goto_cfi(cfi, add_to_history=add_to_history) |
| 450 |
|
| 451 |
def bookmark_activated(self, cfi): |
| 452 |
if self.check_for_read_aloud(_('bookmark')): |
| 453 |
return |
| 454 |
self.goto_cfi(cfi, add_to_history=True) |
| 455 |
|
| 456 |
def view_image(self, name): |
| 457 |
path = get_path_for_name(name) |
| 458 |
if path: |
| 459 |
from calibre.utils.imghdr import what |
| 460 |
|
| 461 |
with open(make_long_path_useable(path), 'rb') as f: |
| 462 |
fmt = what(f) |
| 463 |
if fmt == 'svg': |
| 464 |
r = QSvgRenderer(path) |
| 465 |
if not r.isValid(): |
| 466 |
return error_dialog(self, _('Invalid image'), _('Failed to load the image {}').format(name), show=True) |
| 467 |
dpr = self.devicePixelRatioF() |
| 468 |
sz = (QSizeF(r.defaultSize()) * dpr).toSize() |
| 469 |
img = QImage(sz, QImage.Format.Format_ARGB32_Premultiplied) |
| 470 |
img.fill(Qt.GlobalColor.transparent) |
| 471 |
p = QPainter(img) |
| 472 |
r.render(p) |
| 473 |
p.end() |
| 474 |
img.setDevicePixelRatio(dpr) |
| 475 |
pmap = QPixmap.fromImage(img) |
| 476 |
self.image_popup.current_img = pmap |
| 477 |
self.image_popup.current_url = QUrl.fromLocalFile(path) |
| 478 |
self.image_popup.current_image_is_svg = True |
| 479 |
self.image_popup() |
| 480 |
else: |
| 481 |
pmap = QPixmap() |
| 482 |
if pmap.load(path): |
| 483 |
self.image_popup.current_img = pmap |
| 484 |
self.image_popup.current_url = QUrl.fromLocalFile(path) |
| 485 |
self.image_popup.current_image_is_svg = False |
| 486 |
self.image_popup() |
| 487 |
else: |
| 488 |
error_dialog(self, _('Invalid image'), _('Failed to load the image {}').format(name), show=True) |
| 489 |
else: |
| 490 |
error_dialog(self, _('Image not found'), _('Failed to find the image {}').format(name), show=True) |
| 491 |
|
| 492 |
def copy_image(self, name): |
| 493 |
path = get_path_for_name(name) |
| 494 |
if not path: |
| 495 |
return error_dialog(self, _('Image not found'), _('Failed to find the image {}').format(name), show=True) |
| 496 |
try: |
| 497 |
img = image_from_path(path) |
| 498 |
except Exception: |
| 499 |
return error_dialog(self, _('Invalid image'), _('Failed to load the image {}').format(name), show=True) |
| 500 |
url = QUrl.fromLocalFile(path) |
| 501 |
md = QMimeData() |
| 502 |
md.setImageData(img) |
| 503 |
md.setUrls([url]) |
| 504 |
cb = qapplication_or_fail().clipboard() |
| 505 |
assert cb is not None |
| 506 |
cb.setMimeData(md) |
| 507 |
|
| 508 |
def dock_visibility_changed(self): |
| 509 |
vmap = {dock.objectName().partition('-')[0]: dock.toggleViewAction().isChecked() for dock in self.dock_widgets} |
| 510 |
self.actions_toolbar.update_dock_actions(vmap) |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
def show_loading_message(self, msg): |
| 517 |
if msg: |
| 518 |
self.loading_overlay(msg) |
| 519 |
self.actions_toolbar.update_action_state(False) |
| 520 |
else: |
| 521 |
if not hasattr(self, 'initial_loading_performace_reported'): |
| 522 |
performance_monitor('loading finished') |
| 523 |
self.initial_loading_performace_reported = True |
| 524 |
self.loading_overlay.hide() |
| 525 |
self.actions_toolbar.update_action_state(True) |
| 526 |
|
| 527 |
def content_file_changed(self, fname): |
| 528 |
if self.pending_search: |
| 529 |
search, self.pending_search = self.pending_search, None |
| 530 |
self.show_search( |
| 531 |
text=search['query'], |
| 532 |
trigger=True, |
| 533 |
search_type=search['type'], |
| 534 |
case_sensitive=search['case_sensitive'], |
| 535 |
no_history=True, |
| 536 |
) |
| 537 |
|
| 538 |
def show_error(self, title, msg, details): |
| 539 |
self.loading_overlay.hide() |
| 540 |
error_dialog(self, title, msg, det_msg=details or None, show=True) |
| 541 |
|
| 542 |
def print_book(self): |
| 543 |
if _web_view_module._book_pathtoebook is None: |
| 544 |
error_dialog(self, _('Cannot print book'), _('No book is currently open'), show=True) |
| 545 |
return |
| 546 |
from .printing import print_book |
| 547 |
|
| 548 |
print_book(_web_view_module._book_pathtoebook, book_title=self.current_book_data['metadata']['title'], parent=self) |
| 549 |
|
| 550 |
@property |
| 551 |
def dock_widgets(self): |
| 552 |
return self.findChildren(QDockWidget, options=Qt.FindChildOption.FindDirectChildrenOnly) |
| 553 |
|
| 554 |
def reset_interface(self): |
| 555 |
for dock in self.dock_widgets: |
| 556 |
dock.setFloating(False) |
| 557 |
area = self.dock_defs[dock.objectName().partition('-')[0]].initial_area |
| 558 |
self.removeDockWidget(dock) |
| 559 |
self.addDockWidget(area, dock) |
| 560 |
dock.setVisible(False) |
| 561 |
|
| 562 |
for toolbar in self.findChildren(QToolBar): |
| 563 |
toolbar.setVisible(False) |
| 564 |
self.removeToolBar(toolbar) |
| 565 |
self.addToolBar(Qt.ToolBarArea.LeftToolBarArea, toolbar) |
| 566 |
|
| 567 |
def ask_for_open_from_js(self, path): |
| 568 |
if path and not os.path.exists(path): |
| 569 |
self.web_view.remove_recently_opened(path) |
| 570 |
error_dialog(self, _('Book does not exist'), _('Cannot open {} as it no longer exists').format(path), show=True) |
| 571 |
else: |
| 572 |
self.ask_for_open(path) |
| 573 |
|
| 574 |
def ask_for_open(self, path=None): |
| 575 |
if path is None: |
| 576 |
files = choose_files( |
| 577 |
self, |
| 578 |
'ebook viewer open dialog', |
| 579 |
_('Choose e-book'), |
| 580 |
[(_('E-books'), available_input_formats())], |
| 581 |
all_files=False, |
| 582 |
select_only_single_file=True, |
| 583 |
) |
| 584 |
if not files: |
| 585 |
return |
| 586 |
path = files[0] |
| 587 |
self.load_ebook(path) |
| 588 |
|
| 589 |
def continue_reading(self): |
| 590 |
rl = vprefs['session_data'].get('standalone_recently_opened') |
| 591 |
if rl: |
| 592 |
entry = rl[0] |
| 593 |
self.load_ebook(entry['pathtoebook']) |
| 594 |
|
| 595 |
def load_ebook(self, pathtoebook, open_at=None, reload_book=False): |
| 596 |
if '.' not in os.path.basename(pathtoebook): |
| 597 |
pathtoebook = os.path.abspath(os.path.realpath(pathtoebook)) |
| 598 |
performance_monitor('Load of book started', reset=True) |
| 599 |
self.actions_toolbar.update_action_state(False) |
| 600 |
self.web_view.show_home_page_on_ready = False |
| 601 |
if open_at: |
| 602 |
self.pending_open_at = open_at |
| 603 |
self.setWindowTitle(_('Loading book') + f'… — {self.base_window_title}') |
| 604 |
self.loading_overlay(_('Loading book, please wait')) |
| 605 |
self.save_annotations() |
| 606 |
self.save_reading_rates() |
| 607 |
self.current_book_data = {} |
| 608 |
get_current_book_data(self.current_book_data) |
| 609 |
self.search_widget.clear_searches() |
| 610 |
t = Thread( |
| 611 |
name='LoadBook', |
| 612 |
target=self._load_ebook_worker, |
| 613 |
args=(pathtoebook, open_at, reload_book or self.force_reload), |
| 614 |
) |
| 615 |
t.daemon = True |
| 616 |
t.start() |
| 617 |
|
| 618 |
def reload_book(self): |
| 619 |
if self.current_book_data: |
| 620 |
self.load_ebook(self.current_book_data['pathtoebook'], reload_book=True) |
| 621 |
|
| 622 |
def _load_ebook_worker(self, pathtoebook, open_at, reload_book): |
| 623 |
try: |
| 624 |
ans = prepare_book(pathtoebook, force=reload_book, prepare_notify=self.prepare_notify) |
| 625 |
except WorkerError as e: |
| 626 |
if not sip.isdeleted(self): |
| 627 |
self.book_prepared.emit(False, {'exception': e, 'tb': e.orig_tb, 'pathtoebook': pathtoebook}) |
| 628 |
except Exception as e: |
| 629 |
import traceback |
| 630 |
|
| 631 |
if not sip.isdeleted(self): |
| 632 |
self.book_prepared.emit(False, {'exception': e, 'tb': traceback.format_exc(), 'pathtoebook': pathtoebook}) |
| 633 |
else: |
| 634 |
performance_monitor('prepared emitted') |
| 635 |
if not sip.isdeleted(self): |
| 636 |
self.book_prepared.emit(True, {'base': ans, 'pathtoebook': pathtoebook, 'open_at': open_at, 'reloaded': reload_book}) |
| 637 |
|
| 638 |
def prepare_notify(self): |
| 639 |
performance_monitor('preparation started') |
| 640 |
self.book_preparation_started.emit() |
| 641 |
|
| 642 |
def load_finished(self, ok, data): |
| 643 |
cbd = self.calibre_book_data_for_first_book |
| 644 |
self.calibre_book_data_for_first_book = None |
| 645 |
if self.shutting_down: |
| 646 |
return |
| 647 |
open_at, self.pending_open_at = self.pending_open_at, None |
| 648 |
self.pending_search = None |
| 649 |
self.web_view.clear_caches() |
| 650 |
if not ok: |
| 651 |
self.actions_toolbar.update_action_state(False) |
| 652 |
self.setWindowTitle(self.base_window_title) |
| 653 |
tb = as_unicode(data['tb'].strip(), errors='replace') |
| 654 |
tb = re.split(r'^calibre\.gui2\.viewer\.convert_book\.ConversionFailure:\s*', tb, maxsplit=1, flags=re.M)[-1] |
| 655 |
last_line = tuple(tb.strip().splitlines())[-1] |
| 656 |
if last_line.startswith('calibre.ebooks.DRMError'): |
| 657 |
DRMErrorMessage(self).exec() |
| 658 |
else: |
| 659 |
error_dialog( |
| 660 |
self, |
| 661 |
_('Loading book failed'), |
| 662 |
_('Failed to open the book at {0}. Click "Show details" for more info.').format(data['pathtoebook']), |
| 663 |
det_msg=tb, |
| 664 |
show=True, |
| 665 |
) |
| 666 |
self.loading_overlay.hide() |
| 667 |
self.web_view.show_home_page() |
| 668 |
return |
| 669 |
try: |
| 670 |
set_book_path(data['base'], data['pathtoebook']) |
| 671 |
except Exception: |
| 672 |
if data['reloaded']: |
| 673 |
raise |
| 674 |
self.load_ebook(data['pathtoebook'], open_at=data['open_at'], reload_book=True) |
| 675 |
return |
| 676 |
if iswindows: |
| 677 |
try: |
| 678 |
add_to_recent_docs(data['pathtoebook']) |
| 679 |
except Exception: |
| 680 |
import traceback |
| 681 |
|
| 682 |
traceback.print_exc() |
| 683 |
self.current_book_data = data |
| 684 |
get_current_book_data(self.current_book_data) |
| 685 |
self.current_book_data['annotations_map'] = defaultdict(list) |
| 686 |
self.current_book_data['annotations_path_key'] = path_key(data['pathtoebook']) + '.json' |
| 687 |
self.load_book_data(cbd) |
| 688 |
self.update_window_title() |
| 689 |
initial_cfi = self.initial_cfi_for_current_book() |
| 690 |
initial_position = {'type': 'cfi', 'data': initial_cfi} if initial_cfi else None |
| 691 |
if open_at: |
| 692 |
if open_at.startswith('toc:'): |
| 693 |
initial_toc_node = self.toc_model.node_id_for_text(open_at[len('toc:') :]) |
| 694 |
initial_position = {'type': 'toc', 'data': initial_toc_node} |
| 695 |
elif open_at.startswith('toc-href:'): |
| 696 |
initial_toc_node = self.toc_model.node_id_for_href(open_at[len('toc-href:') :], exact=True) |
| 697 |
initial_position = {'type': 'toc', 'data': initial_toc_node} |
| 698 |
elif open_at.startswith('toc-href-contains:'): |
| 699 |
initial_toc_node = self.toc_model.node_id_for_href(open_at[len('toc-href-contains:') :], exact=False) |
| 700 |
initial_position = {'type': 'toc', 'data': initial_toc_node} |
| 701 |
elif open_at.startswith('epubcfi(/'): |
| 702 |
initial_position = {'type': 'cfi', 'data': open_at} |
| 703 |
elif open_at.startswith('ref:'): |
| 704 |
initial_position = {'type': 'ref', 'data': open_at[len('ref:') :]} |
| 705 |
elif open_at.startswith('search:'): |
| 706 |
self.pending_search = {'type': 'normal', 'query': open_at[len('search:') :], 'case_sensitive': False} |
| 707 |
initial_position = {'type': 'bookpos', 'data': 0} |
| 708 |
elif open_at.startswith('regex:'): |
| 709 |
self.pending_search = {'type': 'regex', 'query': open_at[len('regex:') :], 'case_sensitive': True} |
| 710 |
initial_position = {'type': 'bookpos', 'data': 0} |
| 711 |
elif is_float(open_at): |
| 712 |
initial_position = {'type': 'bookpos', 'data': float(open_at)} |
| 713 |
highlights = self.current_book_data['annotations_map']['highlight'] |
| 714 |
self.highlights_widget.load(highlights) |
| 715 |
rates = load_reading_rates(self.current_book_data['annotations_path_key']) |
| 716 |
self.web_view.start_book_load( |
| 717 |
initial_position=initial_position, |
| 718 |
highlights=highlights, |
| 719 |
current_book_data=self.current_book_data, |
| 720 |
reading_rates=rates, |
| 721 |
) |
| 722 |
performance_monitor('webview loading requested') |
| 723 |
|
| 724 |
self.lookup_widget.book_loaded(self.current_book_data) |
| 725 |
|
| 726 |
def load_book_data(self, calibre_book_data=None): |
| 727 |
self.current_book_data['book_library_details'] = get_book_library_details(self.current_book_data['pathtoebook']) |
| 728 |
if calibre_book_data is not None: |
| 729 |
self.current_book_data['calibre_book_id'] = calibre_book_data['book_id'] |
| 730 |
self.current_book_data['calibre_book_uuid'] = calibre_book_data['uuid'] |
| 731 |
self.current_book_data['calibre_book_fmt'] = calibre_book_data['fmt'] |
| 732 |
self.current_book_data['calibre_library_id'] = calibre_book_data['library_id'] |
| 733 |
self.load_book_annotations(calibre_book_data) |
| 734 |
path = os.path.join(self.current_book_data['base'], 'calibre-book-manifest.json') |
| 735 |
with open(path, 'rb') as f: |
| 736 |
raw = f.read() |
| 737 |
self.current_book_data['manifest'] = manifest = json.loads(raw) |
| 738 |
toc = manifest.get('toc') |
| 739 |
self.toc_model = TOC(toc) |
| 740 |
self.toc.setModel(self.toc_model) |
| 741 |
self.bookmarks_widget.set_bookmarks(self.current_book_data['annotations_map']['bookmark']) |
| 742 |
self.current_book_data['metadata'] = _web_view_module._book_parsed_metadata |
| 743 |
self.current_book_data['manifest'] = _web_view_module._book_parsed_manifest |
| 744 |
|
| 745 |
def load_book_annotations(self, calibre_book_data=None): |
| 746 |
amap = self.current_book_data['annotations_map'] |
| 747 |
path = os.path.join(self.current_book_data['base'], 'calibre-book-annotations.json') |
| 748 |
if os.path.exists(path): |
| 749 |
with open(path, 'rb') as f: |
| 750 |
raw = f.read() |
| 751 |
merge_annotations(parse_annotations(raw), amap) |
| 752 |
path = os.path.join(annotations_dir(), self.current_book_data['annotations_path_key']) |
| 753 |
if os.path.exists(path): |
| 754 |
with open(path, 'rb') as f: |
| 755 |
raw = f.read() |
| 756 |
merge_annotations(parse_annotations(raw), amap) |
| 757 |
if calibre_book_data is None: |
| 758 |
bld = self.current_book_data['book_library_details'] |
| 759 |
if bld is not None: |
| 760 |
lib_amap = load_annotations_map_from_library(bld) |
| 761 |
sau = get_session_pref('sync_annots_user', default='') |
| 762 |
if sau: |
| 763 |
other_amap = load_annotations_map_from_library(bld, user_type='web', user=sau) |
| 764 |
if other_amap: |
| 765 |
merge_annotations(other_amap, lib_amap) |
| 766 |
if lib_amap: |
| 767 |
for annot_type, annots in lib_amap.items(): |
| 768 |
merge_annotations(annots, amap) |
| 769 |
else: |
| 770 |
for annot_type, annots in calibre_book_data['annotations_map'].items(): |
| 771 |
merge_annotations(annots, amap) |
| 772 |
|
| 773 |
def update_window_title(self): |
| 774 |
try: |
| 775 |
title = self.current_book_data['metadata']['title'] |
| 776 |
except Exception: |
| 777 |
title = _('Unknown') |
| 778 |
book_format = self.current_book_data['manifest']['book_format'] |
| 779 |
title = f'{title} [{book_format}] — {self.base_window_title}' |
| 780 |
self.setWindowTitle(title) |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
def initial_cfi_for_current_book(self): |
| 786 |
lrp = self.current_book_data['annotations_map']['last-read'] |
| 787 |
if lrp and get_session_pref('remember_last_read', default=True): |
| 788 |
lrp = lrp[0] |
| 789 |
if lrp['pos_type'] == 'epubcfi': |
| 790 |
return lrp['pos'] |
| 791 |
|
| 792 |
def cfi_changed(self, cfi): |
| 793 |
if not self.current_book_data: |
| 794 |
return |
| 795 |
self.current_book_data['annotations_map']['last-read'] = [{'pos': cfi, 'pos_type': 'epubcfi', 'timestamp': utcnow().isoformat()}] |
| 796 |
self.save_pos_timer.start() |
| 797 |
|
| 798 |
def _on_save_pos_timer(self): |
| 799 |
self.save_annotations(in_book_file=False) |
| 800 |
|
| 801 |
def _on_last_read_pos_data(self, cfi, pos_frac): |
| 802 |
if not self.current_book_data: |
| 803 |
return |
| 804 |
self.current_book_data['pos_frac'] = pos_frac or 0.0 |
| 805 |
self.save_pos_timer.start() |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
def save_annotations(self, in_book_file=True): |
| 811 |
if not self.current_book_data: |
| 812 |
return |
| 813 |
if self.annotations_saver is None: |
| 814 |
self.annotations_saver = AnnotationsSaveWorker() |
| 815 |
self.annotations_saver.start() |
| 816 |
self.annotations_saver.save_annotations( |
| 817 |
self.current_book_data, |
| •818 |
in_book_file and get_session_pref('save_annotations_in_ebook', default=False), |
| 819 |
get_session_pref('sync_annots_user', default=''), |
| 820 |
) |
| 821 |
|
| 822 |
def update_reading_rates(self, rates): |
| 823 |
if not self.current_book_data: |
| 824 |
return |
| 825 |
self.current_book_data['reading_rates'] = rates |
| 826 |
self.save_reading_rates() |
| 827 |
|
| 828 |
def reset_reading_rates(self): |
| 829 |
if self.current_book_data: |
| 830 |
self.current_book_data.pop('reading_rates', None) |
| 831 |
delete_all_reading_rates() |
| 832 |
|
| 833 |
def save_reading_rates(self): |
| 834 |
if not self.current_book_data: |
| 835 |
return |
| 836 |
key = self.current_book_data.get('annotations_path_key') |
| 837 |
rates = self.current_book_data.get('reading_rates') |
| 838 |
if key and rates: |
| 839 |
save_reading_rates(key, rates) |
| 840 |
|
| 841 |
def edit_book(self, file_name, progress_frac, selected_text): |
| 842 |
import subprocess |
| 843 |
|
| 844 |
from calibre.ebooks.oeb.polish.main import SUPPORTED |
| 845 |
from calibre.utils.ipc.launch import exe_path, macos_edit_book_bundle_path |
| 846 |
|
| 847 |
path = _web_view_module._book_pathtoebook |
| 848 |
if path is None: |
| 849 |
return error_dialog(self, _('Cannot edit book'), _('No book is currently open'), show=True) |
| 850 |
fmt = path.rpartition('.')[-1].upper().replace('ORIGINAL_', '') |
| 851 |
if fmt not in SUPPORTED: |
| 852 |
return error_dialog( |
| 853 |
self, |
| 854 |
_('Cannot edit book'), |
| 855 |
_('The book must be in the {} formats to edit.\n\nFirst convert the book to one of these formats.').format(_(' or ').join(SUPPORTED)), |
| 856 |
show=True, |
| 857 |
) |
| 858 |
exe = 'ebook-edit' |
| 859 |
if ismacos: |
| 860 |
exe = os.path.join(macos_edit_book_bundle_path(), exe) |
| 861 |
else: |
| 862 |
exe = exe_path(exe) |
| 863 |
cmd = [exe] if isinstance(exe, str) else list(exe) |
| 864 |
if selected_text: |
| 865 |
cmd += ['--select-text', selected_text] |
| 866 |
from calibre.gui2.widgets import BusyCursor |
| 867 |
|
| 868 |
with sanitize_env_vars(): |
| 869 |
subprocess.Popen(cmd + [path, file_name]) |
| 870 |
with BusyCursor(): |
| 871 |
time.sleep(2) |
| 872 |
|
| 873 |
def save_state(self): |
| 874 |
with vprefs: |
| 875 |
vprefs['main_window_state'] = bytearray(self.saveState(self.MAIN_WINDOW_STATE_VERSION)) |
| 876 |
self.save_geometry(vprefs, 'main_window_geometry') |
| 877 |
|
| 878 |
def restore_state(self): |
| 879 |
state = vprefs['main_window_state'] |
| 880 |
if not get_session_pref('remember_window_geometry', default=False) or not self.restore_geometry(vprefs, 'main_window_geometry'): |
| 881 |
qapplication_or_fail().ensure_window_on_screen(self) |
| 882 |
if state: |
| 883 |
self.restoreState(state, self.MAIN_WINDOW_STATE_VERSION) |
| 884 |
self.inspector_dock.setVisible(False) |
| 885 |
if not get_session_pref('restore_docks', True): |
| 886 |
for dock_def in self.dock_defs.values(): |
| 887 |
d = getattr(self, '{}_dock'.format(dock_def.name.partition('-')[0])) |
| 888 |
d.setVisible(False) |
| 889 |
|
| 890 |
def quit(self): |
| 891 |
self.close() |
| 892 |
|
| 893 |
def force_close(self): |
| 894 |
if not self.close_forced: |
| 895 |
self.close_forced = True |
| 896 |
self.quit() |
| 897 |
|
| 898 |
def close_prep_finished(self, cfi): |
| 899 |
if cfi: |
| 900 |
self.cfi_changed(cfi) |
| 901 |
self.force_close() |
| 902 |
|
| 903 |
def request_close(self): |
| 904 |
self.close() |
| 905 |
|
| 906 |
def closeEvent(self, a0): |
| 907 |
if self.shutdown_done: |
| 908 |
return |
| 909 |
if self.current_book_data and self.web_view.view_is_ready and not self.close_forced: |
| 910 |
a0.ignore() |
| 911 |
if not self.shutting_down: |
| 912 |
self.shutting_down = True |
| 913 |
QTimer.singleShot(2000, self.force_close) |
| 914 |
self.web_view.prepare_for_close() |
| 915 |
return |
| 916 |
self.shutting_down = True |
| 917 |
wait_for_worker = async_stop_worker() |
| 918 |
self.search_widget.shutdown() |
| 919 |
self.web_view.shutdown() |
| 920 |
try: |
| 921 |
self.save_state() |
| 922 |
self.save_annotations() |
| 923 |
self.save_reading_rates() |
| 924 |
if self.annotations_saver is not None: |
| 925 |
self.annotations_saver.shutdown() |
| 926 |
self.annotations_saver = None |
| 927 |
if self.last_read_pos_saver is not None: |
| 928 |
self.last_read_pos_saver.shutdown() |
| 929 |
self.last_read_pos_saver = None |
| 930 |
except Exception: |
| 931 |
import traceback |
| 932 |
|
| 933 |
traceback.print_exc() |
| 934 |
wait_for_worker() |
| 935 |
self.shutdown_done = True |
| 936 |
return MainWindow.closeEvent(self, a0) |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
def setup_mouse_auto_hide(self): |
| 942 |
qapplication_or_fail().installEventFilter(self) |
| 943 |
self.cursor_hidden = False |
| 944 |
self.hide_cursor_timer = t = QTimer(self) |
| 945 |
t.setSingleShot(True), t.setInterval(3000) |
| 946 |
t.timeout.connect(self.hide_cursor) |
| 947 |
t.start() |
| 948 |
|
| 949 |
def eventFilter(self, a0, a1): |
| 950 |
et = a1.type() |
| 951 |
if et == QEvent.Type.MouseMove: |
| 952 |
if self.cursor_hidden: |
| 953 |
self.cursor_hidden = False |
| 954 |
qapplication_or_fail().restoreOverrideCursor() |
| 955 |
self.hide_cursor_timer.start() |
| 956 |
elif et == QEvent.Type.FocusIn: |
| 957 |
if iswindows and a0 and a0.objectName() == 'EbookViewerClassWindow' and self.isFullScreen(): |
| 958 |
self.web_view.repair_after_fullscreen_switch() |
| 959 |
return False |
| 960 |
|
| 961 |
def hide_cursor(self): |
| 962 |
if get_session_pref('auto_hide_mouse', True): |
| 963 |
self.cursor_hidden = True |
| 964 |
qapplication_or_fail().setOverrideCursor(Qt.CursorShape.BlankCursor) |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
def highlights_changed(self, changed_annotations: list): |
| 969 |
try: |
| 970 |
master_map = self.current_book_data.setdefault('annotations_map', {}) |
| 971 |
master_map['highlight'] = changed_annotations |
| 972 |
self.highlights_widget.load(changed_annotations) |
| 973 |
self.save_annotations() |
| 974 |
except Exception: |
| 975 |
import traceback |
| 976 |
|
| 977 |
traceback.print_exc() |
| 978 |
|
| 979 |
def notes_edited(self, uuid, notes): |
| 980 |
for h in self.current_book_data['annotations_map']['highlight']: |
| 981 |
if h.get('uuid') == uuid: |
| 982 |
h['notes'] = notes |
| 983 |
h['timestamp'] = utcnow().isoformat() |
| 984 |
break |
| 985 |
else: |
| 986 |
return |
| 987 |
self.save_annotations() |
| 988 |
self.web_view.generic_action('set-notes-in-highlight', {'uuid': uuid, 'notes': notes}) |
| 989 |
|
| 990 |
def add_notes_or_create_highlight(self, notes: str, style_name_for_new_highlight: str = ''): |
| 991 |
if not notes: |
| 992 |
return |
| 993 |
data = { |
| 994 |
'style': style_definition_for_name(style_name_for_new_highlight), |
| 995 |
'notes': notes, |
| 996 |
} |
| 997 |
self.web_view.generic_action('add-notes-or-create-highlight', data) |