-
Notifications
You must be signed in to change notification settings - Fork 10
/
gui.py
executable file
·108 lines (83 loc) · 3.22 KB
/
gui.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
import application
import pathlib
import sys
import PyQt5.QtGui
import PyQt5.QtWidgets
import PyQt5.QtWebEngineWidgets
import PyQt5.QtCore
if hasattr(PyQt5.QtCore.Qt, 'AA_EnableHighDpiScaling'):
PyQt5.QtWidgets.QApplication.setAttribute(PyQt5.QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(PyQt5.QtCore.Qt, 'AA_UseHighDpiPixmaps'):
PyQt5.QtWidgets.QApplication.setAttribute(PyQt5.QtCore.Qt.AA_UseHighDpiPixmaps, True)
class ApplicationThread(PyQt5.QtCore.QThread):
def __init__(self, application, port=5000):
super(ApplicationThread, self).__init__()
self.application = application
self.port = port
def __del__(self):
self.wait()
def run(self):
self.application.run(port=self.port, threaded=True)
class WebPage(PyQt5.QtWebEngineWidgets.QWebEnginePage):
def __init__(self, root_url):
super(WebPage, self).__init__()
self.root_url = root_url
def home(self):
"""
Loads the root URL.
"""
self.load(PyQt5.QtCore.QUrl(self.root_url))
def acceptNavigationRequest(self, url, kind, is_main_frame):
"""
Open external links in browser and internal links in the webview.
"""
ready_url = url.toEncoded().data().decode()
is_clicked = kind == self.NavigationTypeLinkClicked
if is_clicked and (self.root_url not in ready_url):
PyQt5.QtGui.QDesktopServices.openUrl(url)
return False
return super(WebPage, self).acceptNavigationRequest(url, kind, is_main_frame)
def init_gui(application, port=5000, argv=None):
"""
Initializes the Qt web engine, starts the web application, and loads the
main page.
"""
if argv is None:
argv = sys.argv
title = "Topics Explorer"
icon = str(pathlib.Path("application", "static", "img", "app_icon.png"))
qtapp = PyQt5.QtWidgets.QApplication(argv)
webapp = ApplicationThread(application, port)
webapp.start()
qtapp.aboutToQuit.connect(webapp.terminate)
window = PyQt5.QtWidgets.QMainWindow()
screen = qtapp.primaryScreen()
size = screen.size()
width = size.width() - (size.width() / 100 * 7)
height = size.height() - (size.height() / 100 * 20)
window.resize(width, height)
window.setWindowTitle(title)
window.setWindowIcon(PyQt5.QtGui.QIcon(icon))
webview = PyQt5.QtWebEngineWidgets.QWebEngineView(window)
window.setCentralWidget(webview)
page = WebPage("http://localhost:{}".format(port))
page.home()
webview.setPage(page)
def download_requested(item):
"""
Opens a file dialog to save the ZIP archive.
"""
path = PyQt5.QtWidgets.QFileDialog.getSaveFileName(None,
"Select destination folder and file name",
"",
"Zip files (*.zip)")[0]
item.setPath("{path}.{ext}".format(path=path, ext="zip"))
item.accept()
webview.page().profile().downloadRequested.connect(download_requested)
window.show()
return qtapp.exec_()
def run():
"""
Calls the main function.
"""
sys.exit(init_gui(application.web.app))