| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from PyQt5 import QtCore, QtGui, QtWidgets |
| 20 |
from breezy.plugins.qbrz.lib.i18n import gettext |
| 21 |
from breezy.plugins.qbrz.lib.util import QBzrWindow |
| 22 |
|
| 23 |
from breezy.help import HelpIndices, NoHelpTopic |
| 24 |
try: |
| 25 |
from docutils.core import publish_string |
| 26 |
from docutils.writers.html4css1 import Writer as BaseHTMLWriter |
| 27 |
have_docutils = True |
| 28 |
except ImportError: |
| 29 |
have_docutils = False |
| 30 |
|
| 31 |
if have_docutils: |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class CustomWriter(BaseHTMLWriter): |
| 36 |
|
| 37 |
template = """%(head_prefix)s |
| 38 |
%(head)s |
| 39 |
%(stylesheet)s |
| 40 |
%(body_prefix)s |
| 41 |
%(body_pre_docinfo)s |
| 42 |
%(docinfo)s |
| 43 |
%(body)s |
| 44 |
%(body_suffix)s |
| 45 |
""" |
| 46 |
|
| 47 |
def apply_template(self): |
| 48 |
|
| 49 |
subs = self.interpolation_dict() |
| 50 |
return self.template % subs |
| 51 |
|
| 52 |
|
| 53 |
def get_help_topic_as_html(topic): |
| 54 |
topics = HelpIndices() |
| 55 |
try: |
| 56 |
results = topics.search(topic) |
| 57 |
except NoHelpTopic: |
| 58 |
tpl = gettext("No help can be found for <i>%s</i>") |
| 59 |
return tpl % topic |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
index, topic = results[0] |
| 68 |
if have_docutils: |
| 69 |
|
| 70 |
text = topic.get_help_text(plain=False) |
| 71 |
html = publish_string(text, writer=CustomWriter(), |
| 72 |
settings_overrides={'stylesheet': None, |
| 73 |
'stylesheet_path': None}) |
| 74 |
else: |
| 75 |
|
| 76 |
|
| 77 |
text = topic.get_help_text(plain=True) |
| 78 |
bits = ['''<p><small>(Please install the Python <i>docutils</i> package for |
| 79 |
improved formatting)</p></small>'''] |
| 80 |
bits.append('<pre>%s</pre>' % text) |
| 81 |
html = ' '.join(bits) |
| 82 |
return html |
| 83 |
|
| 84 |
|
| 85 |
class QBzrHelpWindow(QBzrWindow): |
| 86 |
|
| 87 |
def __init__(self, parent=None): |
| 88 |
"""Create help window. |
| 89 |
@param parent: If None, a normal top-level window will be opened. |
| 90 |
If not None, a 'tool' style window will be opened, be |
| 91 |
'always on top' of the parent and have no taskbar |
| 92 |
entry. |
| 93 |
""" |
| 94 |
if parent is None: |
| 95 |
|
| 96 |
window_id = "help" |
| 97 |
default_size = (780, 580) |
| 98 |
else: |
| 99 |
|
| 100 |
window_id = "help-popup" |
| 101 |
default_size = (500, 400) |
| 102 |
QBzrWindow.__init__(self, [gettext("Help")], parent, |
| 103 |
centralwidget=QtWidgets.QTextBrowser()) |
| 104 |
self.restoreSize(window_id, default_size) |
| 105 |
if parent is not None: |
| 106 |
|
| 107 |
self.setWindowFlags(QtCore.Qt.Tool) |
| 108 |
|
| 109 |
|
| 110 |
self.setAttribute(QtCore.Qt.WA_DeleteOnClose) |
| 111 |
|
| 112 |
def move_next_to(self, parent): |
| 113 |
"""Move next to a parent window so we aren't obscuring any widgets""" |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
desktop = QtWidgets.QApplication.desktop() |
| 119 |
geo = desktop.screenGeometry(parent) |
| 120 |
|
| 121 |
pt = parent.pos() |
| 122 |
parent_size = parent.size() |
| 123 |
my_size = self.size() |
| 124 |
new_pt = QtCore.QPoint(pt.x()+parent_size.width()+15, pt.y()) |
| 125 |
if geo.contains(QtCore.QRect(new_pt, my_size)): |
| 126 |
self.move(new_pt) |
| 127 |
else: |
| 128 |
|
| 129 |
new_pt = QtCore.QPoint(pt.x()-my_size.width()-15, pt.y()) |
| 130 |
if geo.contains(QtCore.QRect(new_pt, my_size)): |
| 131 |
self.move(new_pt) |
| 132 |
else: |
| 133 |
|
| 134 |
pass |
| 135 |
|
| 136 |
def load_help_topic(self, topic): |
| 137 |
html = get_help_topic_as_html(topic) |
| 138 |
self.centralwidget.setHtml(html) |
| 139 |
|
| 140 |
|
| 141 |
def show_help(topic, parent=None): |
| 142 |
"""Create a help window displaying the specified topic. |
| 143 |
|
| 144 |
If a parent is given, the window will be a 'tool' window for the parent, |
| 145 |
otherwise a normal MainWindow. |
| 146 |
""" |
| 147 |
|
| 148 |
for window in QtWidgets.QApplication.topLevelWidgets(): |
| 149 |
if isinstance(window, QBzrHelpWindow) and window.parentWidget()==parent: |
| 150 |
break |
| 151 |
else: |
| 152 |
|
| 153 |
window = QBzrHelpWindow(parent) |
| 154 |
|
| 155 |
window.load_help_topic(topic) |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if parent is not None and not window.isVisible(): |
| 160 |
window.move_next_to(parent) |
| 161 |
window.show() |
| 162 |
return window |