forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qpreso.py
executable file
·183 lines (144 loc) · 6.44 KB
/
qpreso.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
# Copyright (C) 2018 by Akkana Peck.
# Share and enjoy under the GPL v2 or later.
"""Viewer for HTML presentations."""
import sys
import os
import argparse
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QShortcut, QDesktopWidget, QMainWindow
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
class PresoView(QWebEngineView):
def __init__(self, url=None, curviews=None, fullscreen=False, monitor=-1,
zoom=1.0, show_notes=True):
"""Create a new PresoView. Leave curviews unset unless requesting
a new window, in which case it must be set to a list of
existing PresoViews.
Monitor is the monitor number from xrandr.
"""
# Example showing list of views (the ONLY one I've found):
# https://stackoverflow.com/questions/55472415/python-pyqt5-window-does-not-open-new-window-or-tab-on-external-url-link-click
super().__init__()
if not curviews:
self._windows = []
else:
self._windows = curviews
self._windows.append(self)
# Are we making screenshots? TODO: make this a command-line param.
self.make_screenshots = False
self.imgnum = 0
# Size the audience will see (used for converting to images):
displaysize = QDesktopWidget().screenGeometry(-1)
self.displaywidth = displaysize.width()
self.displayheight = displaysize.height()
# Size of the window we'll actually display.
# XXX Currently assumes a projector at 1024x768
# and should be made more general.
if show_notes:
self.fullwidth = 1366
else:
self.fullwidth = 1024
self.fullheight = 768
if zoom != 1.0 :
self.displaywidth = int(self.displaywidth * zoom)
self.displayheight = int(self.displayheight * zoom)
self.fullwidth = int(self.fullwidth * zoom)
self.fullheight = int(self.fullheight * zoom)
print("Display size: %d x %d" % (self.displaywidth,
self.displayheight))
print("Full size: %d x %d" % (self.fullwidth,
self.fullheight))
if monitor >= 0:
# The number of the monitor where the window should show up
screens = app.screens()
print("screens:", screens)
if monitor > len(screens):
print("There is no monitor", monitor)
sys.exit(1)
geom = screens[monitor].geometry()
self.move(geom.left(), geom.top())
self.showFullScreen()
else:
# Run fullscreen if the display is XGA or smaller,
# or if fullscreen was explicitly set.
# displaysize = QApplication.desktop.screenGeometry()
if fullscreen or self.displayheight <= 768:
self.showFullScreen()
# Key bindings
# For keys like function keys, use QtGui.QKeySequence("F12")
QShortcut("Ctrl+Q", self, activated=self.close)
QShortcut("Ctrl+R", self, activated=self.reload)
QShortcut("Alt+Left", self, activated=self.back)
QShortcut("Alt+Right", self, activated=self.forward)
self.resize(self.fullwidth, self.fullheight)
if url:
self.load(QUrl.fromUserInput(url))
def createWindow(self, wintype):
"""Create an empty window when requested to do so by
Javascript or _target.
JS may pass a size, and either JS or target will likely pass
a URL, but does QWebEngineView pass that info along? Nooooo!
I don't know if there's any way to get those details.
Even qutebrowser just punts on new window creation.
"""
# Possible wintypes (all part of QWebEnginePage):
# WebBrowserWindow: A new window
# WebBrowserTab: A new tab
# WebDialog: A JavaScript-created window
# WebBrowserBackgroundTab: A new tab that isn't immediately active
if wintype == QWebEnginePage.WebBrowserWindow or \
wintype == QWebEnginePage.WebDialog:
v = PresoView('about:blank', curviews=self._windows)
# v.resize(640, 480)
v.show()
return v
# def resizeEvent(self, e):
# """This is called on window create, but not from javascript resizeTo.
# Args:
# e: The QResizeEvent
# """
# print("resizeEvent!", e)
# print("Current geometry", self.geometry())
# print("new size", e.size())
# print("old size", e.oldSize())
# super().resizeEvent(e)
# # self._update_overlay_geometries()
# # self._downloadview.updateGeometry()
# # self.tabbed_browser.widget.tabBar().refresh()
# def event(self, ev):
# print("event!", ev)
# return super().event(ev)
def parse_args():
"""Parse commandline arguments."""
parser = argparse.ArgumentParser()
parser.add_argument('-f', "--fullscreen", dest="fullscreen", default=False,
action="store_true",
help="Run fullscreen regardless of screen size")
parser.add_argument('-m', '--monitor', action="store", default=-1,
dest="monitor", type=int,
help='Run fullscreen on this monitor')
parser.add_argument('url', help='The URL to open')
args = parser.parse_known_args()[0]
# Figure out if the url is a filename or a url
if args.url.find(':') < 0 :
if args.url[0] == '/' :
args.url = 'file://' + args.url
else :
args.url = 'file://' + os.getcwd() + '/' + args.url
return args
if __name__ == '__main__':
# Try to catch exceptions instead of dumping core,
# but it dumps core anyway.
import traceback
def excepthook(excType=None, excValue=None, tracebackobj=None, *,
message=None, version_tag=None, parent=None):
# print("exception! excValue='%s'" % excValue)
# logging.critical(''.join(traceback.format_tb(tracebackobj)))
# logging.critical('{0}: {1}'.format(excType, excValue))
traceback.print_exception(excType, excValue, tracebackobj)
sys.excepthook = excepthook
args = parse_args()
app = QApplication(sys.argv)
pv = PresoView(args.url, fullscreen=args.fullscreen, monitor=args.monitor)
pv.show()
app.exec_()