UI/Previewers/PreviewerHTML.py

changeset 4615
7c090c9d389d
parent 4021
195a471c327b
child 4625
ac72a3d8f89e
equal deleted inserted replaced
4614:e8996f522895 4615:7c090c9d389d
20 import shutil 20 import shutil
21 import tempfile 21 import tempfile
22 import sys 22 import sys
23 23
24 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QUrl, QSize, QThread 24 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QUrl, QSize, QThread
25 from PyQt5.QtWidgets import QWidget 25 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QCheckBox, \
26 from PyQt5.QtWebKitWidgets import QWebPage 26 QSizePolicy
27 27
28 from E5Gui.E5Application import e5App 28 from E5Gui.E5Application import e5App
29
30 from .Ui_PreviewerHTML import Ui_PreviewerHTML
31 29
32 import Utilities 30 import Utilities
33 import Preferences 31 import Preferences
34 32
35 33
36 class PreviewerHTML(QWidget, Ui_PreviewerHTML): 34 class PreviewerHTML(QWidget):
37 """ 35 """
38 Class implementing a previewer widget for HTML, Markdown and ReST files. 36 Class implementing a previewer widget for HTML, Markdown and ReST files.
39 """ 37 """
40 def __init__(self, parent=None): 38 def __init__(self, parent=None):
41 """ 39 """
42 Constructor 40 Constructor
43 41
44 @param parent reference to the parent widget (QWidget) 42 @param parent reference to the parent widget (QWidget)
45 """ 43 """
46 super(PreviewerHTML, self).__init__(parent) 44 super(PreviewerHTML, self).__init__(parent)
47 self.setupUi(self) 45
46 self.__layout = QVBoxLayout(self)
47
48 self.titleLabel = QLabel(self)
49 self.titleLabel.setWordWrap(True)
50 self.titleLabel.setTextInteractionFlags(Qt.NoTextInteraction)
51 self.__layout.addWidget(self.titleLabel)
52
53 try:
54 from PyQt5.QtWebKitWidgets import QWebPage, QWebView
55 self.previewView = QWebView(self)
56 self.previewView.page().setLinkDelegationPolicy(
57 QWebPage.DelegateAllLinks)
58 self.__usesWebKit = True
59 except ImportError:
60 from PyQt5.QtWebEngineWidgets import QWebEngineView
61 self.previewView = QWebEngineView(self)
62 self.__usesWebKit = False
63
64 sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
65 sizePolicy.setHorizontalStretch(0)
66 sizePolicy.setVerticalStretch(0)
67 sizePolicy.setHeightForWidth(
68 self.previewView.sizePolicy().hasHeightForWidth())
69 self.previewView.setSizePolicy(sizePolicy)
70 self.previewView.setContextMenuPolicy(Qt.NoContextMenu)
71 self.previewView.setUrl(QUrl("about:blank"))
72 self.__layout.addWidget(self.previewView)
73
74 self.jsCheckBox = QCheckBox(self.tr("Enable JavaScript"), self)
75 self.jsCheckBox.setToolTip(self.tr(
76 "Select to enable JavaScript for HTML previews"))
77 self.__layout.addWidget(self.jsCheckBox)
78
79 self.ssiCheckBox = QCheckBox(self.tr("Enable Server Side Includes"),
80 self)
81 self.ssiCheckBox.setToolTip(self.tr(
82 "Select to enable support for Server Side Includes"))
83 self.__layout.addWidget(self.ssiCheckBox)
84
85 self.jsCheckBox.clicked[bool].connect(self.on_jsCheckBox_clicked)
86 self.ssiCheckBox.clicked[bool].connect(self.on_ssiCheckBox_clicked)
87 self.previewView.titleChanged.connect(self.on_previewView_titleChanged)
88 if self.__usesWebKit:
89 self.previewView.linkClicked.connect(
90 self.on_previewView_linkClicked)
48 91
49 self.jsCheckBox.setChecked( 92 self.jsCheckBox.setChecked(
50 Preferences.getUI("ShowFilePreviewJS")) 93 Preferences.getUI("ShowFilePreviewJS"))
51 self.ssiCheckBox.setChecked( 94 self.ssiCheckBox.setChecked(
52 Preferences.getUI("ShowFilePreviewSSI")) 95 Preferences.getUI("ShowFilePreviewSSI"))
53
54 self.previewView.page().setLinkDelegationPolicy(
55 QWebPage.DelegateAllLinks)
56 96
57 self.__scrollBarPositions = {} 97 self.__scrollBarPositions = {}
58 self.__vScrollBarAtEnd = {} 98 self.__vScrollBarAtEnd = {}
59 self.__hScrollBarAtEnd = {} 99 self.__hScrollBarAtEnd = {}
60 100
156 positions. 196 positions.
157 197
158 @param filePath file path of the previewed editor (string) 198 @param filePath file path of the previewed editor (string)
159 @param html processed HTML text ready to be shown (string) 199 @param html processed HTML text ready to be shown (string)
160 """ 200 """
161 self.__saveScrollBarPositions()
162 self.__previewedPath = Utilities.normcasepath( 201 self.__previewedPath = Utilities.normcasepath(
163 Utilities.fromNativeSeparators(filePath)) 202 Utilities.fromNativeSeparators(filePath))
164 self.previewView.page().mainFrame().contentsSizeChanged.connect( 203 if self.__usesWebKit:
165 self.__restoreScrollBarPositions) 204 self.__saveScrollBarPositions()
205 self.previewView.page().mainFrame().contentsSizeChanged.connect(
206 self.__restoreScrollBarPositions)
166 self.previewView.setHtml(html, baseUrl=QUrl.fromLocalFile(filePath)) 207 self.previewView.setHtml(html, baseUrl=QUrl.fromLocalFile(filePath))
167 208
168 @pyqtSlot(str) 209 @pyqtSlot(str)
169 def on_previewView_titleChanged(self, title): 210 def on_previewView_titleChanged(self, title):
170 """ 211 """
179 220
180 def __saveScrollBarPositions(self): 221 def __saveScrollBarPositions(self):
181 """ 222 """
182 Private method to save scroll bar positions for a previewed editor. 223 Private method to save scroll bar positions for a previewed editor.
183 """ 224 """
184 frame = self.previewView.page().mainFrame() 225 if self.__usesWebKit:
185 if frame.contentsSize() == QSize(0, 0): 226 frame = self.previewView.page().mainFrame()
186 return # no valid data, nothing to save 227 if frame.contentsSize() == QSize(0, 0):
187 228 return # no valid data, nothing to save
188 pos = frame.scrollPosition() 229
189 self.__scrollBarPositions[self.__previewedPath] = pos 230 pos = frame.scrollPosition()
190 self.__hScrollBarAtEnd[self.__previewedPath] = \ 231 self.__scrollBarPositions[self.__previewedPath] = pos
191 frame.scrollBarMaximum(Qt.Horizontal) == pos.x() 232 self.__hScrollBarAtEnd[self.__previewedPath] = \
192 self.__vScrollBarAtEnd[self.__previewedPath] = \ 233 frame.scrollBarMaximum(Qt.Horizontal) == pos.x()
193 frame.scrollBarMaximum(Qt.Vertical) == pos.y() 234 self.__vScrollBarAtEnd[self.__previewedPath] = \
235 frame.scrollBarMaximum(Qt.Vertical) == pos.y()
194 236
195 def __restoreScrollBarPositions(self): 237 def __restoreScrollBarPositions(self):
196 """ 238 """
197 Private method to restore scroll bar positions for a previewed editor. 239 Private method to restore scroll bar positions for a previewed editor.
198 """ 240 """
199 try: 241 if self.__usesWebKit:
200 self.previewView.page().mainFrame().contentsSizeChanged.disconnect( 242 try:
201 self.__restoreScrollBarPositions) 243 self.previewView.page().mainFrame().contentsSizeChanged.\
202 except TypeError: 244 disconnect(self.__restoreScrollBarPositions)
203 # not connected, simply ignore it 245 except TypeError:
204 pass 246 # not connected, simply ignore it
205 247 pass
206 if self.__previewedPath not in self.__scrollBarPositions: 248
207 return 249 if self.__previewedPath not in self.__scrollBarPositions:
208 250 return
209 frame = self.previewView.page().mainFrame() 251
210 frame.setScrollPosition( 252 frame = self.previewView.page().mainFrame()
211 self.__scrollBarPositions[self.__previewedPath]) 253 frame.setScrollPosition(
212 254 self.__scrollBarPositions[self.__previewedPath])
213 if self.__hScrollBarAtEnd[self.__previewedPath]: 255
214 frame.setScrollBarValue( 256 if self.__hScrollBarAtEnd[self.__previewedPath]:
215 Qt.Horizontal, frame.scrollBarMaximum(Qt.Horizontal)) 257 frame.setScrollBarValue(
216 258 Qt.Horizontal, frame.scrollBarMaximum(Qt.Horizontal))
217 if self.__vScrollBarAtEnd[self.__previewedPath]: 259
218 frame.setScrollBarValue( 260 if self.__vScrollBarAtEnd[self.__previewedPath]:
219 Qt.Vertical, frame.scrollBarMaximum(Qt.Vertical)) 261 frame.setScrollBarValue(
262 Qt.Vertical, frame.scrollBarMaximum(Qt.Vertical))
220 263
221 @pyqtSlot(QUrl) 264 @pyqtSlot(QUrl)
222 def on_previewView_linkClicked(self, url): 265 def on_previewView_linkClicked(self, url):
223 """ 266 """
224 Private slot handling the clicking of a link. 267 Private slot handling the clicking of a link.

eric ide

mercurial