Skip to content

Commit 7ecc306

Browse files
committed
Use QTextBrowser for browsing template function documentation
1 parent 6b91f66 commit 7ecc306

2 files changed

Lines changed: 60 additions & 50 deletions

File tree

src/calibre/gui2/dialogs/template_dialog.py

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424
QFontDatabase,
2525
QFontInfo,
2626
QFontMetrics,
27-
QHBoxLayout,
2827
QIcon,
2928
QLineEdit,
3029
QPalette,
31-
QPushButton,
3230
QSize,
3331
QSyntaxHighlighter,
3432
Qt,
@@ -40,14 +38,14 @@
4038
QVBoxLayout,
4139
pyqtSignal,
4240
)
43-
from qt.webengine import QWebEngineView
4441

4542
from calibre import sanitize_file_name
46-
from calibre.constants import config_dir
43+
from calibre.constants import config_dir, iswindows
4744
from calibre.ebooks.metadata.book.base import Metadata
4845
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
5047
from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog
48+
from calibre.gui2.widgets2 import Dialog, HTMLDisplay
5149
from calibre.library.coloring import color_row_key, displayable_columns
5250
from calibre.utils.config_base import tweaks
5351
from calibre.utils.date import DEFAULT_DATE
@@ -60,6 +58,50 @@
6058
from calibre.utils.resources import get_path as P
6159

6260

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+
63105
class ParenPosition:
64106

65107
def __init__(self, block, pos, paren):
@@ -530,47 +572,15 @@ def __init__(self, parent, text, mi=None, fm=None, color_field=None,
530572

531573
def open_documentation_viewer(self):
532574
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)
553576
dv.finished.connect(self.doc_viewer_finished)
554577
dv.show()
555578
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()
571582

572583
def doc_viewer_finished(self):
573-
self.doc_viewer.save_geometry(gprefs, 'template_editor_doc_viewer')
574584
self.doc_viewer = None
575585

576586
def geometry_string(self, txt):

src/calibre/gui2/dialogs/template_dialog.ui

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ you the value as well as all the local variables&lt;/p&gt;</string>
408408
</widget>
409409
</item>
410410
<item row="11" column="1">
411-
<layout class="BoxLayout" name="user_layout_1" dir="TopToBottom">
411+
<layout class="QVBoxLayout" name="user_layout_1">
412412
</layout>
413413
</item>
414414
<item row="12" column="0">
@@ -425,7 +425,7 @@ you the value as well as all the local variables&lt;/p&gt;</string>
425425
</widget>
426426
</item>
427427
<item row="12" column="1">
428-
<layout class="BoxLayout" name="user_layout_2" dir="TopToBottom">
428+
<layout class="QVBoxLayout" name="user_layout_2">
429429
</layout>
430430
</item>
431431
<item row="13" column="0">
@@ -442,7 +442,7 @@ you the value as well as all the local variables&lt;/p&gt;</string>
442442
</widget>
443443
</item>
444444
<item row="13" column="1">
445-
<layout class="BoxLayout" name="user_layout_3" dir="TopToBottom">
445+
<layout class="QVBoxLayout" name="user_layout_3">
446446
</layout>
447447
</item>
448448
<item row="14" column="0">
@@ -459,7 +459,7 @@ you the value as well as all the local variables&lt;/p&gt;</string>
459459
</widget>
460460
</item>
461461
<item row="14" column="1">
462-
<layout class="BoxLayout" name="user_layout_4" dir="TopToBottom">
462+
<layout class="QVBoxLayout" name="user_layout_4">
463463
</layout>
464464
</item>
465465
<item row="15" column="0">
@@ -476,7 +476,7 @@ you the value as well as all the local variables&lt;/p&gt;</string>
476476
</widget>
477477
</item>
478478
<item row="15" column="1">
479-
<layout class="BoxLayout" name="user_layout_5" dir="TopToBottom">
479+
<layout class="QVBoxLayout" name="user_layout_5">
480480
</layout>
481481
</item>
482482
<item row="16" column="0">
@@ -493,7 +493,7 @@ you the value as well as all the local variables&lt;/p&gt;</string>
493493
</widget>
494494
</item>
495495
<item row="16" column="1">
496-
<layout class="BoxLayout" name="user_layout_6" dir="TopToBottom">
496+
<layout class="QVBoxLayout" name="user_layout_6">
497497
</layout>
498498
</item>
499499
<item row="17" column="0">
@@ -510,7 +510,7 @@ you the value as well as all the local variables&lt;/p&gt;</string>
510510
</widget>
511511
</item>
512512
<item row="17" column="1">
513-
<layout class="BoxLayout" name="user_layout_7" dir="TopToBottom">
513+
<layout class="QVBoxLayout" name="user_layout_7">
514514
</layout>
515515
</item>
516516
<item row="18" column="0">
@@ -527,7 +527,7 @@ you the value as well as all the local variables&lt;/p&gt;</string>
527527
</widget>
528528
</item>
529529
<item row="18" column="1">
530-
<layout class="BoxLayout" name="user_layout_8" dir="TopToBottom">
530+
<layout class="QVBoxLayout" name="user_layout_8">
531531
</layout>
532532
</item>
533533
<item row="19" column="0">
@@ -744,7 +744,7 @@ you the value as well as all the local variables&lt;/p&gt;</string>
744744
<item>
745745
<widget class="QPushButton" name="doc_button">
746746
<property name="text">
747-
<string>&amp;Documentation:</string>
747+
<string>&amp;Documentation</string>
748748
</property>
749749
<property name="toolTip">
750750
<string>Click this button to open the documentation in a separate dialog</string>

0 commit comments

Comments
 (0)