/
gui.py
executable file
·120 lines (92 loc) · 3.54 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
109
110
111
112
113
114
115
116
117
118
119
120
from pathlib import Path
import sys
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5 import QtWebEngineWidgets
from PyQt5 import QtCore
from application import views
PARENT = Path(__file__).parent
TITLE = "Topics Explorer :: DARIAH-DE"
ICON = str(Path(PARENT, "static", "img", "logos", "favicon-template.png"))
PORT = 5000
# This is for high DPI scaling:
if hasattr(QtCore.Qt, "AA_EnableHighDpiScaling"):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, "AA_UseHighDpiPixmaps"):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
def download_request(item):
"""Opens a file dialog to save the ZIP archive.
"""
mimetype = item.mimeType()
if "octet-stream" in mimetype:
ext = ".png"
elif "svg" in item.mimeType():
ext = ".svg"
elif "zip" in mimetype:
ext = ".zip"
else:
ext = ""
path = QtWidgets.QFileDialog.getSaveFileName(None,
"Select destination folder and file name",
"",
"")[0]
item.setPath("{path}{ext}".format(path=path, ext=ext))
item.accept()
class ApplicationThread(QtCore.QThread):
def __init__(self, application, port=PORT):
super(ApplicationThread, self).__init__()
self.application = application
self.port = port
def __del__(self):
self.wait()
def run(self):
self.application.run(port=self.port)
class WebPage(QtWebEngineWidgets.QWebEnginePage):
def __init__(self, root_url):
super(WebPage, self).__init__()
self.root_url = root_url
def home(self):
self.load(QtCore.QUrl(self.root_url))
def acceptNavigationRequest(self, url, kind, is_main_frame):
ready_url = url.toEncoded().data().decode()
is_clicked = kind == self.NavigationTypeLinkClicked
if is_clicked and (self.root_url not in ready_url):
QtGui.QDesktopServices.openUrl(url)
return False
return super(WebPage, self).acceptNavigationRequest(url, kind, is_main_frame)
def init_gui(application, port=PORT, argv=None, title=TITLE, icon=ICON):
"""Initialize Qt web engine, start Flask application.
"""
if argv is None:
argv = sys.argv
# Starting the Flask application:
qtapp = QtWidgets.QApplication(argv)
web = ApplicationThread(application, port)
web.start()
def kill(application=web):
"""Kill the Flask process.
"""
application.terminate()
qtapp.aboutToQuit.connect(kill)
# Setting width and height individually based on the
# screen resolution: 93% of the screen for width,
# 80% for height:
screen = qtapp.primaryScreen()
size = screen.size()
width = size.width() - size.width() / 100 * 7
height = size.height() - size.height() / 100 * 20
# Applying settings and loading the home page:
webview = QtWebEngineWidgets.QWebEngineView()
webview.resize(width, height)
webview.setWindowTitle(title)
webview.setWindowIcon(QtGui.QIcon(icon))
webview.load(QtCore.QUrl("http://localhost:{}".format(port)))
# If the user clicks a download button, a window pops up:
webview.page().profile().downloadRequested.connect(download_request)
# Show the webview:
webview.show()
return qtapp.exec_()
def run():
"""Run Topics Explorer.
"""
sys.exit(init_gui(views.web))