This repository was archived by the owner on Sep 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathWidgets.py
334 lines (287 loc) · 11.9 KB
/
Widgets.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#### Jade Application Kit
# * https://codesardine.github.io/Jade-Application-Kit
# * Vitor Lopes Copyright (c) 2016 - 2020
# * https://vitorlopes.me
import os
from JAK.Utils import Instance, bindings, getScreenGeometry
from JAK.KeyBindings import KeyPress
if bindings() == "PyQt5":
from PyQt5.QtCore import Qt, QSize, QUrl
from PyQt5.QtGui import QIcon, QPixmap, QImage
from PyQt5.QtWidgets import QMainWindow, QWidget, QMessageBox, QSystemTrayIcon,\
QAction, QToolBar, QMenu, QMenuBar, QFileDialog, QLabel
else:
from PySide2.QtCore import Qt, QSize, QUrl
from PySide2.QtGui import QIcon, QPixmap, QImage
from PySide2.QtWidgets import QMainWindow, QWidget, QMessageBox, QSystemTrayIcon,\
QAction, QToolBar, QMenu, QMenuBar, QFileDialog, QLabel
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, icon, app, config):
self.config = config
self.icon = icon
super(SystemTrayIcon, self).__init__(icon, parent=app)
self.setContextMenu(self.tray_menu())
self.show()
def tray_menu(self):
"""
Create menu for the tray icon
"""
self.menu = QMenu()
for item in self.config['window']["SystemTrayIcon"]:
try:
self.action = QAction(f"{item['title']}", self)
self.action.triggered.connect(item['action'])
if item['icon']:
self.action.setIcon(QIcon(QPixmap(item['icon'])))
self.menu.addAction(self.action)
except KeyError:
pass
return self.menu
class JWindow(QMainWindow):
""" #### Imports: from JAK.Widgets import JWindow """
def __init__(self, config):
super().__init__()
self.config = config
if config["window"]["backgroundImage"]:
# Transparency must be set to True
self.label = QLabel(self)
self.setObjectName("JAKWindow")
self.setBackgroundImage(config["window"]["backgroundImage"])
self.video_corner = False
self.center = getScreenGeometry().center()
self.setWindowTitle(config['window']["title"])
self.setWindowFlags(config['window']["setFlags"])
self.setWAttribute(Qt.WA_DeleteOnClose)
for attr in config['window']["setAttribute"]:
self.setWAttribute(attr)
if config['window']["state"]:
self.setWindowState(config['window']["state"])
if config['window']["icon"] and os.path.isfile(config['window']["icon"]):
self.icon = QIcon(config['window']["icon"])
else:
print(f"icon not found: {config['window']['icon']}")
print("loading default icon:")
self.icon = QIcon.fromTheme("applications-internet")
view = Instance.retrieve("view")
if view:
self.view = view
self.setCentralWidget(self.view)
self.view.iconChanged.connect(self._icon_changed)
if config['webview']["online"]:
self.view.page().titleChanged.connect(self.status_message)
if config['window']["transparent"]:
# Set Background Transparency
self.setWAttribute(Qt.WA_TranslucentBackground)
self.setAutoFillBackground(True)
if config['webview']["online"]:
# Do not display toolbar or system tray offline
if config['window']["toolbar"]:
self.toolbar = JToolbar(self, config['window']["toolbar"], self.icon, config['window']["title"])
self.addToolBar(self.toolbar)
self.setMenuBar(Menu(self, config['window']["menus"]))
else:
if config['window']["showHelpMenu"]:
self.setMenuBar(Menu(self, config['window']["menus"]))
self.view.page().titleChanged.connect(self.status_message)
if config['window']["SystemTrayIcon"]:
self.system_tray = SystemTrayIcon(self.icon, self, config)
if config["debug"]:
self.showInspector()
self._set_icons()
def setBackgroundImage(self, image):
screen = getScreenGeometry()
pixmap = QPixmap(QImage(image)).scaled(screen.width(), screen.height(), Qt.KeepAspectRatioByExpanding)
self.label.setPixmap(pixmap)
self.label.setGeometry(0, 0, screen.width(), self.label.sizeHint().height())
def showInspector(self):
from JAK.DevTools import WebView, InspectorDock
self.inspector_dock = InspectorDock(self)
self.inspector_view = WebView(parent=self)
self.inspector_view.set_inspected_view(self.view)
self.inspector_dock.setWidget(self.inspector_view)
self.addDockWidget(Qt.TopDockWidgetArea, self.inspector_dock)
def hideInspector(self):
self.inspector_dock.hide()
def setWAttribute(self, attr):
self.setAttribute(attr, True)
def keyPressEvent(self, event):
KeyPress(event, self.config)
def _set_icons(self):
self.setWindowIcon(self.icon)
if self.config['window']["SystemTrayIcon"]:
self.system_tray.setIcon(self.icon)
def _icon_changed(self):
if not self.view.icon().isNull():
self.icon = self.view.icon()
self._set_icons()
def status_message(self):
# Show status message
self.statusbar = self.statusBar()
self.statusbar.showMessage(self.view.page().title(), 10000)
def hide_show_bar(self):
if self.isFullScreen() or self.video_corner:
self.statusbar.hide()
if self.config['window']["toolbar"]:
self.toolbar.hide()
else:
self.statusbar.show()
if self.config['window']["toolbar"]:
self.toolbar.show()
def default_size(self, size: str):
# Set to 70% screen size
screen = getScreenGeometry()
if size == "width":
return screen.width() * 2 / 3
elif size == "height":
return screen.height() * 2 / 3
def set_window_to_defaults(self):
self.window_original_position.moveCenter(self.center)
self.move(self.window_original_position.topLeft())
self.resize(self.default_size("width"), self.default_size("height"))
self.hide_show_bar()
self.setWindowFlags(Qt.Window)
self.show()
def set_window_to_corner(self):
self.move(self.window_original_position.bottomRight())
# Set to 30% screen size
screen = getScreenGeometry()
self.resize(screen.width() * 0.7 / 2, screen.height() * 0.7 / 2)
self.hide_show_bar()
self.setWindowFlags(Qt.SplashScreen | Qt.WindowStaysOnTopHint)
self.show()
def corner_window(self):
if self.video_corner:
self.video_corner = False
self.set_window_to_defaults()
else:
self.video_corner = True
if self.isFullScreen():
self.showNormal()
self.set_window_to_corner()
class JToolbar(QToolBar):
""" #### Imports: from JAK.Widgets import JToolbar """
def __init__(self, parent, toolbar, icon, title):
"""
* :param parent: Parent window
* :param toolbar:dict
* :param icon:str
* :param title:str
"""
super(JToolbar, self).__init__(parent)
self.icon = icon
self.setMovable(False)
self.setContextMenuPolicy(Qt.PreventContextMenu)
self.setIconSize(QSize(32, 32))
self.about_title = "About"
if toolbar:
# If a dict is passed generate buttons from dict
for btn in toolbar:
try:
if btn["icon"]:
item = QAction(QIcon(btn["icon"]), btn["name"], self)
except KeyError:
item = QAction(btn["name"], self)
item.triggered.connect(self._on_click(btn["url"]))
self.addAction(item)
def _on_click(self, url: str, title=""):
view = Instance.retrieve("view")
if url.startswith("https"):
return lambda: view.setUrl(QUrl(url))
else:
msg = url
return lambda: Dialog.information(self, title, msg)
class Menu(QMenuBar):
def __init__(self, parent, menus):
super(Menu, self).__init__(parent)
if menus:
for menu in menus:
if type(menu) is dict:
title = self.addMenu(menu["title"])
for entry in menu["entries"]:
submenu = QAction(entry[0], self)
title.addAction(submenu)
print(entry[1])
submenu.triggered.connect(self._on_click(entry[1]))
help_menu = {"title": "Keyboard Shortcuts", "text": """
<body style='margin-right:46px;'><b>
F11 Toggle Full Screen
<br>
F10 Toggle Corner Window
<br>
CTRL + Zoom In
<br>
CTRL - Zoom Out
</body>
"""},\
{"title": "About JAK", "text": """
<body style='margin-right:46px;'>
<small>
This online application is copyright and ownership of their respective authors.
<br><br>
This wrapper offers the ability to run web applications, as a self contained native desktop application.
Enjoy.
</small>
<br>
<center>
<b><small>
Powered by:
</b></small>
<a href='https://github.com/codesardine/Jade-Application-Kit'>Jade Application Kit</a><center>
<small>
Native Hybrid Apps on Linux.
<br>
<b>
Using QT WebEngine
<b>
</small>
<br>
<small>
<br>
This application comes with no warranty. License: <b>GPL</b>
<br>
Author:<b>Vitor Lopes<b>
</small>
</center>
</body>
"""}
_help = self.addMenu("Help")
for entry in help_menu:
submenu = QAction(entry["title"], self)
submenu.triggered.connect(self._on_click(entry["text"], entry["title"]))
_help.addAction(submenu)
def _on_click(self, url: str, title=""):
if url.startswith("https"):
view = Instance.retrieve("view")
return lambda: view.setUrl(QUrl(url))
elif url.endswith("()"):
from JAK import IPC
return lambda: IPC.Communication.send(url)
else:
msg = url
return lambda: Dialog.information(self, title, msg)
class Dialog:
@staticmethod
def question(parent, title, msg, on_confirm=""):
reply = QMessageBox.question(parent, title, msg, QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
on_confirm()
@staticmethod
def information(parent, title, msg):
QMessageBox.information(parent, title, msg)
class FileChooserDialog(QWidget):
def __init__(self, parent=None, file_type="", title="Choose a File"):
super().__init__(parent)
self.file_type = file_type
self.title = title
self.show()
def choose_file(self):
dialog = QFileDialog()
options = dialog.Options()
file_name = dialog.getOpenFileName(
self, self.title, os.environ['HOME'],
f"{self.file_type.upper()} Files ({self.file_type})",
options=options)
if file_name[0]:
return file_name[0]
self.hide()
self.destroy()