mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use QTextBrowser for browsing template function documentation
This commit is contained in:
parent
6b91f666e4
commit
7ecc30663f
@ -24,11 +24,9 @@ from qt.core import (
|
|||||||
QFontDatabase,
|
QFontDatabase,
|
||||||
QFontInfo,
|
QFontInfo,
|
||||||
QFontMetrics,
|
QFontMetrics,
|
||||||
QHBoxLayout,
|
|
||||||
QIcon,
|
QIcon,
|
||||||
QLineEdit,
|
QLineEdit,
|
||||||
QPalette,
|
QPalette,
|
||||||
QPushButton,
|
|
||||||
QSize,
|
QSize,
|
||||||
QSyntaxHighlighter,
|
QSyntaxHighlighter,
|
||||||
Qt,
|
Qt,
|
||||||
@ -40,14 +38,14 @@ from qt.core import (
|
|||||||
QVBoxLayout,
|
QVBoxLayout,
|
||||||
pyqtSignal,
|
pyqtSignal,
|
||||||
)
|
)
|
||||||
from qt.webengine import QWebEngineView
|
|
||||||
|
|
||||||
from calibre import sanitize_file_name
|
from calibre import sanitize_file_name
|
||||||
from calibre.constants import config_dir
|
from calibre.constants import config_dir, iswindows
|
||||||
from calibre.ebooks.metadata.book.base import Metadata
|
from calibre.ebooks.metadata.book.base import Metadata
|
||||||
from calibre.ebooks.metadata.book.formatter import SafeFormat
|
from calibre.ebooks.metadata.book.formatter import SafeFormat
|
||||||
from calibre.gui2 import choose_files, choose_save_file, error_dialog, gprefs, pixmap_to_data, question_dialog
|
from calibre.gui2 import choose_files, choose_save_file, error_dialog, gprefs, pixmap_to_data, question_dialog, safe_open_url
|
||||||
from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog
|
from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog
|
||||||
|
from calibre.gui2.widgets2 import Dialog, HTMLDisplay
|
||||||
from calibre.library.coloring import color_row_key, displayable_columns
|
from calibre.library.coloring import color_row_key, displayable_columns
|
||||||
from calibre.utils.config_base import tweaks
|
from calibre.utils.config_base import tweaks
|
||||||
from calibre.utils.date import DEFAULT_DATE
|
from calibre.utils.date import DEFAULT_DATE
|
||||||
@ -60,6 +58,50 @@ from calibre.utils.localization import localize_user_manual_link, ngettext
|
|||||||
from calibre.utils.resources import get_path as P
|
from calibre.utils.resources import get_path as P
|
||||||
|
|
||||||
|
|
||||||
|
class DocViewer(Dialog):
|
||||||
|
|
||||||
|
def __init__(self, docs_dsl, parent=None):
|
||||||
|
self.docs_dsl = docs_dsl
|
||||||
|
super().__init__(title=_('Template function documentation'), name='template_editor_doc_viewer_dialog',
|
||||||
|
default_buttons=QDialogButtonBox.StandardButton.Close, parent=parent)
|
||||||
|
|
||||||
|
def sizeHint(self):
|
||||||
|
return QSize(800, 600)
|
||||||
|
|
||||||
|
def set_html(self, html):
|
||||||
|
print(html)
|
||||||
|
self.doc_viewer_widget.setHtml(html)
|
||||||
|
|
||||||
|
def setup_ui(self):
|
||||||
|
l = QVBoxLayout(self)
|
||||||
|
e = self.doc_viewer_widget = HTMLDisplay(self)
|
||||||
|
if iswindows:
|
||||||
|
e.setDefaultStyleSheet('pre { font-family: "Segoe UI Mono", "Consolas", monospace; }')
|
||||||
|
e.anchor_clicked.connect(safe_open_url)
|
||||||
|
l.addWidget(e)
|
||||||
|
l.addWidget(self.bb)
|
||||||
|
b = self.bb.addButton(_('Show &all functions'), QDialogButtonBox.ButtonRole.ActionRole)
|
||||||
|
b.clicked.connect(self.show_all_functions)
|
||||||
|
b.setToolTip((_('Shows a list of all built-in functions in alphabetic order')))
|
||||||
|
|
||||||
|
def show_function(self):
|
||||||
|
self.set_html(
|
||||||
|
self.docs_dsl.document_to_html(self.all_functions[self.current_function_name].doc,
|
||||||
|
self.current_function_name))
|
||||||
|
def show_all_functions(self):
|
||||||
|
funcs = formatter_functions().get_builtins()
|
||||||
|
result = []
|
||||||
|
a = result.append
|
||||||
|
for name in sorted(funcs):
|
||||||
|
a(f'\n<h2>{name}</h2>\n')
|
||||||
|
try:
|
||||||
|
a(self.docs_dsl.document_to_html(funcs[name].doc.strip(), name))
|
||||||
|
except Exception:
|
||||||
|
print('Exception in', name)
|
||||||
|
raise
|
||||||
|
self.doc_viewer_widget.setHtml(''.join(result))
|
||||||
|
|
||||||
|
|
||||||
class ParenPosition:
|
class ParenPosition:
|
||||||
|
|
||||||
def __init__(self, block, pos, paren):
|
def __init__(self, block, pos, paren):
|
||||||
@ -530,47 +572,15 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
|
|||||||
|
|
||||||
def open_documentation_viewer(self):
|
def open_documentation_viewer(self):
|
||||||
if self.doc_viewer is None:
|
if self.doc_viewer is None:
|
||||||
dv = self.doc_viewer = QDialog(self)
|
dv = self.doc_viewer = DocViewer(self.docs_dsl, self)
|
||||||
l = QVBoxLayout()
|
|
||||||
dv.setLayout(l)
|
|
||||||
e = self.doc_viewer_widget = QWebEngineView() #QTextBrowser()
|
|
||||||
# e.setOpenExternalLinks(True)
|
|
||||||
# e.setReadOnly(True)
|
|
||||||
l.addWidget(e)
|
|
||||||
b = QHBoxLayout()
|
|
||||||
b.addStretch(10)
|
|
||||||
pb = QPushButton(_('Show all functions'))
|
|
||||||
pb.setToolTip((_('Shows a list of all built-in functions in alphabetic order')))
|
|
||||||
pb.clicked.connect(self.doc_viewer_show_all)
|
|
||||||
b.addWidget(pb)
|
|
||||||
|
|
||||||
pb = QPushButton(_('Close'))
|
|
||||||
pb.clicked.connect(dv.close)
|
|
||||||
b.addWidget(pb)
|
|
||||||
l.addLayout(b)
|
|
||||||
e.setHtml('')
|
|
||||||
dv.restore_geometry(gprefs, 'template_editor_doc_viewer')
|
|
||||||
dv.finished.connect(self.doc_viewer_finished)
|
dv.finished.connect(self.doc_viewer_finished)
|
||||||
dv.show()
|
dv.show()
|
||||||
if self.current_function_name is not None:
|
if self.current_function_name is not None:
|
||||||
self.doc_viewer_widget.setHtml(
|
self.doc_viewer.show_function(self.current_function_name)
|
||||||
self.docs_dsl.document_to_html(self.all_functions[self.current_function_name].doc,
|
else:
|
||||||
self.current_function_name))
|
self.doc_viewer.show_all_functions()
|
||||||
|
|
||||||
def doc_viewer_show_all(self):
|
|
||||||
funcs = formatter_functions().get_builtins()
|
|
||||||
result = ''
|
|
||||||
for name in sorted(funcs):
|
|
||||||
result += f'\n<h2>{name}</h2>\n'
|
|
||||||
try:
|
|
||||||
result += self.docs_dsl.document_to_html(funcs[name].doc.strip(), name)
|
|
||||||
except Exception:
|
|
||||||
print('Exception in', name)
|
|
||||||
raise
|
|
||||||
self.doc_viewer_widget.setHtml(result)
|
|
||||||
|
|
||||||
def doc_viewer_finished(self):
|
def doc_viewer_finished(self):
|
||||||
self.doc_viewer.save_geometry(gprefs, 'template_editor_doc_viewer')
|
|
||||||
self.doc_viewer = None
|
self.doc_viewer = None
|
||||||
|
|
||||||
def geometry_string(self, txt):
|
def geometry_string(self, txt):
|
||||||
|
@ -408,7 +408,7 @@ you the value as well as all the local variables</p></string>
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="11" column="1">
|
<item row="11" column="1">
|
||||||
<layout class="BoxLayout" name="user_layout_1" dir="TopToBottom">
|
<layout class="QVBoxLayout" name="user_layout_1">
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item row="12" column="0">
|
<item row="12" column="0">
|
||||||
@ -425,7 +425,7 @@ you the value as well as all the local variables</p></string>
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="12" column="1">
|
<item row="12" column="1">
|
||||||
<layout class="BoxLayout" name="user_layout_2" dir="TopToBottom">
|
<layout class="QVBoxLayout" name="user_layout_2">
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item row="13" column="0">
|
<item row="13" column="0">
|
||||||
@ -442,7 +442,7 @@ you the value as well as all the local variables</p></string>
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="13" column="1">
|
<item row="13" column="1">
|
||||||
<layout class="BoxLayout" name="user_layout_3" dir="TopToBottom">
|
<layout class="QVBoxLayout" name="user_layout_3">
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item row="14" column="0">
|
<item row="14" column="0">
|
||||||
@ -459,7 +459,7 @@ you the value as well as all the local variables</p></string>
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="14" column="1">
|
<item row="14" column="1">
|
||||||
<layout class="BoxLayout" name="user_layout_4" dir="TopToBottom">
|
<layout class="QVBoxLayout" name="user_layout_4">
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item row="15" column="0">
|
<item row="15" column="0">
|
||||||
@ -476,7 +476,7 @@ you the value as well as all the local variables</p></string>
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="15" column="1">
|
<item row="15" column="1">
|
||||||
<layout class="BoxLayout" name="user_layout_5" dir="TopToBottom">
|
<layout class="QVBoxLayout" name="user_layout_5">
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item row="16" column="0">
|
<item row="16" column="0">
|
||||||
@ -493,7 +493,7 @@ you the value as well as all the local variables</p></string>
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="16" column="1">
|
<item row="16" column="1">
|
||||||
<layout class="BoxLayout" name="user_layout_6" dir="TopToBottom">
|
<layout class="QVBoxLayout" name="user_layout_6">
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item row="17" column="0">
|
<item row="17" column="0">
|
||||||
@ -510,7 +510,7 @@ you the value as well as all the local variables</p></string>
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="17" column="1">
|
<item row="17" column="1">
|
||||||
<layout class="BoxLayout" name="user_layout_7" dir="TopToBottom">
|
<layout class="QVBoxLayout" name="user_layout_7">
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item row="18" column="0">
|
<item row="18" column="0">
|
||||||
@ -527,7 +527,7 @@ you the value as well as all the local variables</p></string>
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="18" column="1">
|
<item row="18" column="1">
|
||||||
<layout class="BoxLayout" name="user_layout_8" dir="TopToBottom">
|
<layout class="QVBoxLayout" name="user_layout_8">
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item row="19" column="0">
|
<item row="19" column="0">
|
||||||
@ -744,7 +744,7 @@ you the value as well as all the local variables</p></string>
|
|||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="doc_button">
|
<widget class="QPushButton" name="doc_button">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>&Documentation:</string>
|
<string>&Documentation</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="toolTip">
|
<property name="toolTip">
|
||||||
<string>Click this button to open the documentation in a separate dialog</string>
|
<string>Click this button to open the documentation in a separate dialog</string>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user