Skip to content

Commit a7a21e7

Browse files
authored
Merge pull request #1365 from coffeedogs/final_code_quality_7
Changes based on style and lint checks. (final_code_quality_7)
2 parents ea38e91 + 0a30eb0 commit a7a21e7

File tree

3 files changed

+271
-184
lines changed

3 files changed

+271
-184
lines changed

src/bitmessageqt/messageview.py

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
"""
2+
src/bitmessageqt/messageview.py
3+
===============================
4+
5+
"""
6+
17
from PyQt4 import QtCore, QtGui
28

3-
import multiprocessing
4-
import Queue
5-
from urlparse import urlparse
6-
from safehtmlparser import *
9+
from safehtmlparser import SafeHTMLParser
10+
711

812
class MessageView(QtGui.QTextBrowser):
13+
"""Message content viewer class, can switch between plaintext and HTML"""
914
MODE_PLAIN = 0
1015
MODE_HTML = 1
11-
12-
def __init__(self, parent = 0):
16+
17+
def __init__(self, parent=0):
1318
super(MessageView, self).__init__(parent)
14-
self.mode = MessageView.MODE_PLAIN
19+
self.mode = MessageView.MODE_PLAIN
1520
self.html = None
1621
self.setOpenExternalLinks(False)
1722
self.setOpenLinks(False)
@@ -25,12 +30,14 @@ def __init__(self, parent = 0):
2530
self.setWrappingWidth()
2631

2732
def resizeEvent(self, event):
33+
"""View resize event handler"""
2834
super(MessageView, self).resizeEvent(event)
2935
self.setWrappingWidth(event.size().width())
30-
36+
3137
def mousePressEvent(self, event):
32-
#text = textCursor.block().text()
33-
if event.button() == QtCore.Qt.LeftButton and self.html and self.html.has_html and self.cursorForPosition(event.pos()).block().blockNumber() == 0:
38+
"""Mouse press button event handler"""
39+
if event.button() == QtCore.Qt.LeftButton and self.html and self.html.has_html and self.cursorForPosition(
40+
event.pos()).block().blockNumber() == 0:
3441
if self.mode == MessageView.MODE_PLAIN:
3542
self.showHTML()
3643
else:
@@ -39,19 +46,24 @@ def mousePressEvent(self, event):
3946
super(MessageView, self).mousePressEvent(event)
4047

4148
def wheelEvent(self, event):
49+
"""Mouse wheel scroll event handler"""
4250
# super will actually automatically take care of zooming
4351
super(MessageView, self).wheelEvent(event)
44-
if (QtGui.QApplication.queryKeyboardModifiers() & QtCore.Qt.ControlModifier) == QtCore.Qt.ControlModifier and event.orientation() == QtCore.Qt.Vertical:
52+
if (QtGui.QApplication.queryKeyboardModifiers() &
53+
QtCore.Qt.ControlModifier) == QtCore.Qt.ControlModifier and event.orientation() == QtCore.Qt.Vertical:
4554
zoom = self.currentFont().pointSize() * 100 / self.defaultFontPointSize
46-
QtGui.QApplication.activeWindow().statusBar().showMessage(QtGui.QApplication.translate("MainWindow", "Zoom level %1%").arg(str(zoom)))
55+
QtGui.QApplication.activeWindow().statusBar().showMessage(
56+
QtGui.QApplication.translate("MainWindow", "Zoom level %1%").arg(str(zoom)))
4757

4858
def setWrappingWidth(self, width=None):
59+
"""Set word-wrapping width"""
4960
self.setLineWrapMode(QtGui.QTextEdit.FixedPixelWidth)
5061
if width is None:
5162
width = self.width()
5263
self.setLineWrapColumnOrWidth(width)
5364

5465
def confirmURL(self, link):
66+
"""Show a dialog requesting URL opening confirmation"""
5567
if link.scheme() == "mailto":
5668
window = QtGui.QApplication.activeWindow()
5769
window.ui.lineEditTo.setText(link.path())
@@ -68,35 +80,39 @@ def confirmURL(self, link):
6880
)
6981
window.ui.textEditMessage.setFocus()
7082
return
71-
reply = QtGui.QMessageBox.warning(self,
72-
QtGui.QApplication.translate("MessageView", "Follow external link"),
73-
QtGui.QApplication.translate("MessageView", "The link \"%1\" will open in a browser. It may be a security risk, it could de-anonymise you or download malicious data. Are you sure?").arg(unicode(link.toString())),
74-
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
83+
reply = QtGui.QMessageBox.warning(
84+
self,
85+
QtGui.QApplication.translate(
86+
"MessageView",
87+
"Follow external link"),
88+
QtGui.QApplication.translate(
89+
"MessageView",
90+
"The link \"%1\" will open in a browser. It may be a security risk, it could de-anonymise you"
91+
" or download malicious data. Are you sure?").arg(unicode(link.toString())),
92+
QtGui.QMessageBox.Yes,
93+
QtGui.QMessageBox.No)
7594
if reply == QtGui.QMessageBox.Yes:
7695
QtGui.QDesktopServices.openUrl(link)
7796

78-
def loadResource (self, restype, name):
79-
if restype == QtGui.QTextDocument.ImageResource and name.scheme() == "bmmsg":
80-
pass
81-
# QImage correctImage;
82-
# lookup the correct QImage from a cache
83-
# return QVariant::fromValue(correctImage);
84-
# elif restype == QtGui.QTextDocument.HtmlResource:
85-
# elif restype == QtGui.QTextDocument.ImageResource:
86-
# elif restype == QtGui.QTextDocument.StyleSheetResource:
87-
# elif restype == QtGui.QTextDocument.UserResource:
88-
else:
89-
pass
90-
# by default, this will interpret it as a local file
91-
# QtGui.QTextBrowser.loadResource(restype, name)
97+
def loadResource(self, restype, name):
98+
"""
99+
Callback for loading referenced objects, such as an image. For security reasons at the moment doesn't do
100+
anything)
101+
"""
102+
pass
92103

93104
def lazyRender(self):
105+
"""
106+
Partially render a message. This is to avoid UI freezing when loading huge messages. It continues loading as
107+
you scroll down.
108+
"""
94109
if self.rendering:
95110
return
96111
self.rendering = True
97112
position = self.verticalScrollBar().value()
98113
cursor = QtGui.QTextCursor(self.document())
99-
while self.outpos < len(self.out) and self.verticalScrollBar().value() >= self.document().size().height() - 2 * self.size().height():
114+
while self.outpos < len(self.out) and self.verticalScrollBar().value(
115+
) >= self.document().size().height() - 2 * self.size().height():
100116
startpos = self.outpos
101117
self.outpos += 10240
102118
# find next end of tag
@@ -108,27 +124,33 @@ def lazyRender(self):
108124
cursor.insertHtml(QtCore.QString(self.out[startpos:self.outpos]))
109125
self.verticalScrollBar().setValue(position)
110126
self.rendering = False
111-
127+
112128
def showPlain(self):
129+
"""Render message as plain text."""
113130
self.mode = MessageView.MODE_PLAIN
114131
out = self.html.raw
115132
if self.html.has_html:
116-
out = "<div align=\"center\" style=\"text-decoration: underline;\"><b>" + unicode(QtGui.QApplication.translate("MessageView", "HTML detected, click here to display")) + "</b></div><br/>" + out
133+
out = "<div align=\"center\" style=\"text-decoration: underline;\"><b>" + unicode(
134+
QtGui.QApplication.translate(
135+
"MessageView", "HTML detected, click here to display")) + "</b></div><br/>" + out
117136
self.out = out
118137
self.outpos = 0
119138
self.setHtml("")
120139
self.lazyRender()
121140

122141
def showHTML(self):
142+
"""Render message as HTML"""
123143
self.mode = MessageView.MODE_HTML
124144
out = self.html.sanitised
125-
out = "<div align=\"center\" style=\"text-decoration: underline;\"><b>" + unicode(QtGui.QApplication.translate("MessageView", "Click here to disable HTML")) + "</b></div><br/>" + out
145+
out = "<div align=\"center\" style=\"text-decoration: underline;\"><b>" + unicode(
146+
QtGui.QApplication.translate("MessageView", "Click here to disable HTML")) + "</b></div><br/>" + out
126147
self.out = out
127148
self.outpos = 0
128149
self.setHtml("")
129150
self.lazyRender()
130151

131152
def setContent(self, data):
153+
"""Set message content from argument"""
132154
self.html = SafeHTMLParser()
133155
self.html.reset()
134156
self.html.reset_safe()

0 commit comments

Comments
 (0)