eric6/UI/Previewer.py

Wed, 30 Dec 2020 11:00:44 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 30 Dec 2020 11:00:44 +0100
branch
maintenance
changeset 7924
8a96736d465e
parent 7781
607a6098cb44
child 8218
7c09585bd960
permissions
-rw-r--r--

Updated copyright for 2021.

2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7924
8a96736d465e Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
3 # Copyright (c) 2013 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a previewer widget for HTML, Markdown and ReST files.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import os
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
7318
5d39cd2d7960 Editor, Previewer:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
12 from PyQt5.QtCore import pyqtSlot, QTimer
3656
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3591
diff changeset
13 from PyQt5.QtWidgets import QStackedWidget
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 import Preferences
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
18 class Previewer(QStackedWidget):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
20 Class implementing a previewer widget containing a stack of
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
21 specialized previewers.
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 def __init__(self, viewmanager, splitter, parent=None):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 Constructor
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 @param viewmanager reference to the viewmanager object (ViewManager)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 @param splitter reference to the embedding splitter (QSplitter)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 @param parent reference to the parent widget (QWidget)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2500
diff changeset
31 super(Previewer, self).__init__(parent)
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 self.__vm = viewmanager
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 self.__splitter = splitter
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 self.__firstShow = True
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
38 self.__htmlPreviewer = None
3459
275cb00c83e2 Continued adding QSS preview support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3458
diff changeset
39 self.__qssPreviewer = None
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 # Don't update too often because the UI might become sluggish
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 self.__typingTimer = QTimer()
7318
5d39cd2d7960 Editor, Previewer:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
43 self.__typingTimer.setInterval(
5d39cd2d7960 Editor, Previewer:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
44 Preferences.getEditor("PreviewRefreshWaitTimer")) # default 500ms
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
45 self.__typingTimer.timeout.connect(self.__processEditor)
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 self.__vm.editorChangedEd.connect(self.__editorChanged)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 self.__vm.editorLanguageChanged.connect(self.__editorLanguageChanged)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 self.__vm.editorTextChanged.connect(self.__editorTextChanged)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 self.__vm.previewStateChanged.connect(self.__previewStateChanged)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.__splitter.splitterMoved.connect(self.__splitterMoved)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 self.hide()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56
7318
5d39cd2d7960 Editor, Previewer:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
57 @pyqtSlot()
5d39cd2d7960 Editor, Previewer:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
58 def preferencesChanged(self):
5d39cd2d7960 Editor, Previewer:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
59 """
5d39cd2d7960 Editor, Previewer:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
60 Public slot handling a change of preferences.
5d39cd2d7960 Editor, Previewer:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
61 """
5d39cd2d7960 Editor, Previewer:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
62 self.__typingTimer.setInterval(
5d39cd2d7960 Editor, Previewer:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
63 Preferences.getEditor("PreviewRefreshWaitTimer"))
5d39cd2d7960 Editor, Previewer:
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7264
diff changeset
64
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 def show(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 Public method to show the preview widget.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2500
diff changeset
69 super(Previewer, self).show()
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 if self.__firstShow:
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
71 self.__splitter.restoreState(
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
72 Preferences.getUI("PreviewSplitterState"))
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 self.__firstShow = False
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 self.__typingTimer.start()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 def hide(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 Public method to hide the preview widget.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2500
diff changeset
80 super(Previewer, self).hide()
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 self.__typingTimer.stop()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 def shutdown(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 Public method to perform shutdown actions.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 self.__typingTimer.stop()
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
88 self.__htmlPreviewer and self.__htmlPreviewer.shutdown()
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 def __splitterMoved(self):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 Private slot to handle the movement of the embedding splitter's handle.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 state = self.__splitter.saveState()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 Preferences.setUI("PreviewSplitterState", state)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 def __editorChanged(self, editor):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 Private slot to handle a change of the current editor.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 @param editor reference to the editor (Editor)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 if editor is None:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 self.hide()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 return
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
107 if (
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
108 Preferences.getUI("ShowFilePreview") and
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
109 self.__isPreviewable(editor)
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
110 ):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 self.show()
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
112 self.__processEditor()
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 self.hide()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 def __editorLanguageChanged(self, editor):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 Private slot to handle a change of the current editor's language.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 @param editor reference to the editor (Editor)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 self.__editorChanged(editor)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 def __editorTextChanged(self, editor):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 Private slot to handle changes of an editor's text.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 @param editor reference to the editor (Editor)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 if self.isVisible():
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 self.__typingTimer.stop()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 self.__typingTimer.start()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 def __previewStateChanged(self, on):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 """
3591
2f2a4a76dd22 Corrected a bunch of source docu issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3515
diff changeset
136 Private slot to toggle the display of the preview.
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 @param on flag indicating to show a preview (boolean)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 editor = self.__vm.activeWindow()
2720
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
141 if on and editor and self.__isPreviewable(editor):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 self.show()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 self.hide()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 def __isPreviewable(self, editor):
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 """
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
148 Private method to check, if a preview can be shown for the given
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
149 editor.
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 @param editor reference to an editor (Editor)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 @return flag indicating if a preview can be shown (boolean)
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 """
2720
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
154 if editor:
5396
8d8940307e44 Little enhancement to the previewers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
155 if bool(editor.getFileName()):
2720
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
156 extension = os.path.normcase(
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
157 os.path.splitext(editor.getFileName())[1][1:])
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
158 return extension in (
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
159 Preferences.getEditor("PreviewHtmlFileNameExtensions") +
3012
d177226027e2 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2965
diff changeset
160 Preferences.getEditor(
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
161 "PreviewMarkdownFileNameExtensions") +
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
162 Preferences.getEditor("PreviewRestFileNameExtensions") +
3459
275cb00c83e2 Continued adding QSS preview support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3458
diff changeset
163 Preferences.getEditor("PreviewQssFileNameExtensions")
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
164 )
5396
8d8940307e44 Little enhancement to the previewers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
165 elif editor.getLanguage().lower() in [
8d8940307e44 Little enhancement to the previewers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
166 "html", "markdown", "restructuredtext", "qss"]:
2720
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
167 return True
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
168
a41f3093a03b Fixed an issue in the preview window that could cause a traceback in certain situations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2537
diff changeset
169 return False
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
171 def __processEditor(self):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 Private slot to schedule the processing of the current editor's text.
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 """
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 self.__typingTimer.stop()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 editor = self.__vm.activeWindow()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 if editor is not None:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 fn = editor.getFileName()
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 if fn:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 extension = os.path.normcase(os.path.splitext(fn)[1][1:])
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 else:
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 extension = ""
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
185 if (
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
186 extension in Preferences.getEditor(
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
187 "PreviewHtmlFileNameExtensions") or
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
188 editor.getLanguage().lower() == "html"
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
189 ):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 language = "HTML"
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
191 elif (
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
192 extension in Preferences.getEditor(
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
193 "PreviewMarkdownFileNameExtensions") or
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
194 editor.getLanguage().lower() == "markdown"
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
195 ):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 language = "Markdown"
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
197 elif (
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
198 extension in Preferences.getEditor(
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
199 "PreviewRestFileNameExtensions") or
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
200 editor.getLanguage().lower() == "restructuredtext"
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
201 ):
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 language = "ReST"
7264
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
203 elif (
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
204 extension in Preferences.getEditor(
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
205 "PreviewQssFileNameExtensions") or
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
206 editor.getLanguage().lower() == "qss"
bedbe458d792 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
207 ):
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
208 language = "QSS"
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 else:
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
210 language = ""
2424
8fe1fdc174ab Added the capability to preview HTML, Mardown and ReST files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211
3458
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
212 if language in ["HTML", "Markdown", "ReST"]:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
213 if self.__htmlPreviewer is None:
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
214 from .Previewers.PreviewerHTML import PreviewerHTML
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
215 self.__htmlPreviewer = PreviewerHTML()
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
216 self.addWidget(self.__htmlPreviewer)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
217 self.setCurrentWidget(self.__htmlPreviewer)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
218 self.__htmlPreviewer.processEditor(editor)
64bbac483843 Refactored the previewer code to allow to add more previewers and started adding support for a QSS previewer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3190
diff changeset
219 elif language == "QSS":
3459
275cb00c83e2 Continued adding QSS preview support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3458
diff changeset
220 if self.__qssPreviewer is None:
275cb00c83e2 Continued adding QSS preview support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3458
diff changeset
221 from .Previewers.PreviewerQSS import PreviewerQSS
275cb00c83e2 Continued adding QSS preview support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3458
diff changeset
222 self.__qssPreviewer = PreviewerQSS()
275cb00c83e2 Continued adding QSS preview support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3458
diff changeset
223 self.addWidget(self.__qssPreviewer)
275cb00c83e2 Continued adding QSS preview support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3458
diff changeset
224 self.setCurrentWidget(self.__qssPreviewer)
275cb00c83e2 Continued adding QSS preview support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3458
diff changeset
225 self.__qssPreviewer.processEditor(editor)

eric ide

mercurial