Debian Codegraph @

lib/help.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 # We run into issues in Windows binaries where docutils may be in a .zip
33 # file, so template and .css files can't be located. This is OK for now,
34 # as the HTML we generate is so basic we don't need stylesheets.
35 class CustomWriter(BaseHTMLWriter):
36 # This is the context of template.txt from distutils.
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 # Docutils expects to find this on disk, but its not there
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 # assert len(results)==1, "what does more than one result mean?"
62 # An example of when one might get more than one result, is if you have
63 # bzrtools plugin install, you will get a second result for it command,
64 # if you search for "Branches". We (like bzr help) only want to show the
65 # first result.
66
67 index, topic = results[0]
68 if have_docutils:
69 # we can make pretty HTML on the fly
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 # No docutils - we don't try too hard here - installing docutils is
76 # easy! But we do try and make the line-breaks correct at least.
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 # a normal window.
96 window_id = "help"
97 default_size = (780, 580)
98 else:
99 # a smallish tooltop window.
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 # make it a tool window for the parent.
107 self.setWindowFlags(QtCore.Qt.Tool)
108 # Without this, the window object remains alive even after its closed.
109 # There's no good reason for that...
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 # We move to the right of the parent if room for the window to be
116 # full visible on the same screen as the parent.
117 # Get the geometry for the screen holding the parent.
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 # see if we can make it fit on the left.
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 # coulnd't fit it to the left *or* right - give up.
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 # find an existing one with the same parent.
148 for window in QtWidgets.QApplication.topLevelWidgets():
149 if isinstance(window, QBzrHelpWindow) and window.parentWidget()==parent:
150 break
151 else:
152 # create a new one.
153 window = QBzrHelpWindow(parent)
154
155 window.load_help_topic(topic)
156 # If a parent is specified and the window isn't visible, move it next to
157 # the parent before displaying it. If the window is visible, its probably
158 # exactly where the user wants it already...
159 if parent is not None and not window.isVisible():
160 window.move_next_to(parent)
161 window.show()
162 return window