|
24 | 24 | QFontDatabase, |
25 | 25 | QFontInfo, |
26 | 26 | QFontMetrics, |
27 | | - QHBoxLayout, |
28 | 27 | QIcon, |
29 | 28 | QLineEdit, |
30 | 29 | QPalette, |
31 | | - QPushButton, |
32 | 30 | QSize, |
33 | 31 | QSyntaxHighlighter, |
34 | 32 | Qt, |
|
40 | 38 | QVBoxLayout, |
41 | 39 | pyqtSignal, |
42 | 40 | ) |
43 | | -from qt.webengine import QWebEngineView |
44 | 41 |
|
45 | 42 | from calibre import sanitize_file_name |
46 | | -from calibre.constants import config_dir |
| 43 | +from calibre.constants import config_dir, iswindows |
47 | 44 | from calibre.ebooks.metadata.book.base import Metadata |
48 | 45 | from calibre.ebooks.metadata.book.formatter import SafeFormat |
49 | | -from calibre.gui2 import choose_files, choose_save_file, error_dialog, gprefs, pixmap_to_data, question_dialog |
| 46 | +from calibre.gui2 import choose_files, choose_save_file, error_dialog, gprefs, pixmap_to_data, question_dialog, safe_open_url |
50 | 47 | from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog |
| 48 | +from calibre.gui2.widgets2 import Dialog, HTMLDisplay |
51 | 49 | from calibre.library.coloring import color_row_key, displayable_columns |
52 | 50 | from calibre.utils.config_base import tweaks |
53 | 51 | from calibre.utils.date import DEFAULT_DATE |
|
60 | 58 | from calibre.utils.resources import get_path as P |
61 | 59 |
|
62 | 60 |
|
| 61 | +class DocViewer(Dialog): |
| 62 | + |
| 63 | + def __init__(self, docs_dsl, parent=None): |
| 64 | + self.docs_dsl = docs_dsl |
| 65 | + super().__init__(title=_('Template function documentation'), name='template_editor_doc_viewer_dialog', |
| 66 | + default_buttons=QDialogButtonBox.StandardButton.Close, parent=parent) |
| 67 | + |
| 68 | + def sizeHint(self): |
| 69 | + return QSize(800, 600) |
| 70 | + |
| 71 | + def set_html(self, html): |
| 72 | + print(html) |
| 73 | + self.doc_viewer_widget.setHtml(html) |
| 74 | + |
| 75 | + def setup_ui(self): |
| 76 | + l = QVBoxLayout(self) |
| 77 | + e = self.doc_viewer_widget = HTMLDisplay(self) |
| 78 | + if iswindows: |
| 79 | + e.setDefaultStyleSheet('pre { font-family: "Segoe UI Mono", "Consolas", monospace; }') |
| 80 | + e.anchor_clicked.connect(safe_open_url) |
| 81 | + l.addWidget(e) |
| 82 | + l.addWidget(self.bb) |
| 83 | + b = self.bb.addButton(_('Show &all functions'), QDialogButtonBox.ButtonRole.ActionRole) |
| 84 | + b.clicked.connect(self.show_all_functions) |
| 85 | + b.setToolTip((_('Shows a list of all built-in functions in alphabetic order'))) |
| 86 | + |
| 87 | + def show_function(self): |
| 88 | + self.set_html( |
| 89 | + self.docs_dsl.document_to_html(self.all_functions[self.current_function_name].doc, |
| 90 | + self.current_function_name)) |
| 91 | + def show_all_functions(self): |
| 92 | + funcs = formatter_functions().get_builtins() |
| 93 | + result = [] |
| 94 | + a = result.append |
| 95 | + for name in sorted(funcs): |
| 96 | + a(f'\n<h2>{name}</h2>\n') |
| 97 | + try: |
| 98 | + a(self.docs_dsl.document_to_html(funcs[name].doc.strip(), name)) |
| 99 | + except Exception: |
| 100 | + print('Exception in', name) |
| 101 | + raise |
| 102 | + self.doc_viewer_widget.setHtml(''.join(result)) |
| 103 | + |
| 104 | + |
63 | 105 | class ParenPosition: |
64 | 106 |
|
65 | 107 | def __init__(self, block, pos, paren): |
@@ -530,47 +572,15 @@ def __init__(self, parent, text, mi=None, fm=None, color_field=None, |
530 | 572 |
|
531 | 573 | def open_documentation_viewer(self): |
532 | 574 | if self.doc_viewer is None: |
533 | | - dv = self.doc_viewer = QDialog(self) |
534 | | - l = QVBoxLayout() |
535 | | - dv.setLayout(l) |
536 | | - e = self.doc_viewer_widget = QWebEngineView() #QTextBrowser() |
537 | | - # e.setOpenExternalLinks(True) |
538 | | - # e.setReadOnly(True) |
539 | | - l.addWidget(e) |
540 | | - b = QHBoxLayout() |
541 | | - b.addStretch(10) |
542 | | - pb = QPushButton(_('Show all functions')) |
543 | | - pb.setToolTip((_('Shows a list of all built-in functions in alphabetic order'))) |
544 | | - pb.clicked.connect(self.doc_viewer_show_all) |
545 | | - b.addWidget(pb) |
546 | | - |
547 | | - pb = QPushButton(_('Close')) |
548 | | - pb.clicked.connect(dv.close) |
549 | | - b.addWidget(pb) |
550 | | - l.addLayout(b) |
551 | | - e.setHtml('') |
552 | | - dv.restore_geometry(gprefs, 'template_editor_doc_viewer') |
| 575 | + dv = self.doc_viewer = DocViewer(self.docs_dsl, self) |
553 | 576 | dv.finished.connect(self.doc_viewer_finished) |
554 | 577 | dv.show() |
555 | 578 | if self.current_function_name is not None: |
556 | | - self.doc_viewer_widget.setHtml( |
557 | | - self.docs_dsl.document_to_html(self.all_functions[self.current_function_name].doc, |
558 | | - self.current_function_name)) |
559 | | - |
560 | | - def doc_viewer_show_all(self): |
561 | | - funcs = formatter_functions().get_builtins() |
562 | | - result = '' |
563 | | - for name in sorted(funcs): |
564 | | - result += f'\n<h2>{name}</h2>\n' |
565 | | - try: |
566 | | - result += self.docs_dsl.document_to_html(funcs[name].doc.strip(), name) |
567 | | - except Exception: |
568 | | - print('Exception in', name) |
569 | | - raise |
570 | | - self.doc_viewer_widget.setHtml(result) |
| 579 | + self.doc_viewer.show_function(self.current_function_name) |
| 580 | + else: |
| 581 | + self.doc_viewer.show_all_functions() |
571 | 582 |
|
572 | 583 | def doc_viewer_finished(self): |
573 | | - self.doc_viewer.save_geometry(gprefs, 'template_editor_doc_viewer') |
574 | 584 | self.doc_viewer = None |
575 | 585 |
|
576 | 586 | def geometry_string(self, txt): |
|
0 commit comments