-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathabout_dialog.py
More file actions
279 lines (240 loc) · 8.66 KB
/
about_dialog.py
File metadata and controls
279 lines (240 loc) · 8.66 KB
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
"""
about_dialog.py
About Dialog Box class for PyGPSClient application.
Created on 20 Sep 2020
:author: semuadmin (Steve Smith)
:copyright: 2020 semuadmin
:license: BSD 3-Clause
"""
import logging
from platform import machine, python_version
from tkinter import Button, Checkbutton, Frame, IntVar, Label, Tcl
from webbrowser import open_new_tab
from PIL import Image, ImageTk
from pygpsclient.globals import (
ERRCOL,
ICON_APP128,
ICON_GITHUB,
ICON_SPONSOR,
INFOCOL,
LICENSE_URL,
OKCOL,
SPONSOR_URL,
TRACEMODE_WRITE,
)
from pygpsclient.helpers import LIBVERSIONS, brew_installed, check_for_updates
from pygpsclient.sqlite_handler import SQLSTATUS
from pygpsclient.strings import (
ABOUTTXT,
BREWUPDATE,
BREWWARN,
COPYRIGHT,
DLGTABOUT,
GITHUB_URL,
NA,
UPDATEERR,
UPDATEINPROG,
UPDATERESTART,
)
from pygpsclient.toplevel_dialog import ToplevelDialog
class AboutDialog(ToplevelDialog):
"""
About dialog box class
"""
def __init__(self, app, *args, **kwargs): # pylint: disable=unused-argument
"""
Initialise Toplevel dialog
:param Frame app: reference to main tkinter application
"""
self.__app = app # Reference to main application class
self.__master = self.__app.appmaster # Reference to root class (Tk)
self.logger = logging.getLogger(__name__)
self._img_icon = ImageTk.PhotoImage(Image.open(ICON_APP128).resize((64, 64)))
self._img_github = ImageTk.PhotoImage(Image.open(ICON_GITHUB).resize((32, 32)))
self._img_sponsor = ImageTk.PhotoImage(Image.open(ICON_SPONSOR))
self._checkonstartup = IntVar()
self._checkonstartup.set(self.__app.configuration.get("checkforupdate_b"))
super().__init__(app, DLGTABOUT)
self._body()
self._do_layout()
self._attach_events()
self._finalise()
def _body(self):
"""
Set up widgets.
"""
self._frm_body = Frame(self.container)
self._lbl_icon = Label(self._frm_body, image=self._img_icon, borderwidth=0)
self._lbl_descs = []
for txt in ABOUTTXT:
self._lbl_descs.append(
Label(
self._frm_body,
text=txt,
borderwidth=0,
)
)
tkv = Tcl().call("info", "patchlevel")
self._lbl_python_version = Label(
self._frm_body,
text=(
f"Arch: {machine()} "
f"Python: {python_version()} Tk: {tkv} "
f"Spatial: {SQLSTATUS[self.__app.db_enabled]}"
),
)
self._lbl_lib_versions = []
for nam, ver in LIBVERSIONS.items():
self._lbl_lib_versions.append(
Label(
self._frm_body,
text=f"{nam}: {ver}",
borderwidth=0,
highlightthickness=0,
)
)
self._btn_checkupdate = Button(
self._frm_body,
text="",
width=16,
cursor="hand2",
)
self._chk_checkupdate = Checkbutton(
self._frm_body,
text="Check on startup",
variable=self._checkonstartup,
)
self._lbl_sponsoricon = Label(
self._frm_body,
image=self._img_sponsor,
cursor="hand2",
)
self._lbl_github = Label(
self._frm_body,
text=GITHUB_URL,
fg=INFOCOL,
cursor="hand2",
)
self._lbl_copyright = Label(
self._frm_body,
text=COPYRIGHT,
cursor="hand2",
)
def _do_layout(self):
"""
Arrange widgets in dialog.
"""
i = 0
self._frm_body.grid(column=0, row=0, padx=5, pady=5, ipadx=5, ipady=5)
self._lbl_icon.grid(column=0, row=1, columnspan=2, padx=3, pady=0)
for i, lbl in enumerate(self._lbl_descs):
lbl.grid(column=0, row=2 + i, columnspan=2, padx=3, pady=0)
self._lbl_python_version.grid(column=0, row=3 + i, columnspan=2, padx=3, pady=1)
n = 4 + i
for i, lbl in enumerate(self._lbl_lib_versions):
lbl.grid(column=0, row=n + i, columnspan=2, padx=2)
self._btn_checkupdate.grid(
column=0, row=1 + n + i, ipadx=3, ipady=3, padx=3, pady=3
)
self._chk_checkupdate.grid(
column=1, row=1 + n + i, ipadx=3, ipady=3, padx=3, pady=3
)
self._lbl_sponsoricon.grid(
column=0, row=2 + n + i, columnspan=2, padx=3, pady=3
)
self._lbl_github.grid(column=0, row=3 + n + i, columnspan=2, padx=3, pady=0)
self._lbl_copyright.grid(column=0, row=4 + n + i, columnspan=2, padx=3, pady=0)
def _attach_events(self):
"""
Bind events to dialog.
"""
self._set_update_btn_mode(False)
self._lbl_github.bind("<Button>", self._on_github)
self._lbl_sponsoricon.bind("<Button>", self._on_sponsor)
self._lbl_copyright.bind("<Button>", self._on_license)
self._checkonstartup.trace_add(TRACEMODE_WRITE, self._on_update_startup)
self._btn_exit.focus_set()
def _on_update_startup(self, var, index, mode): # pylint: disable=unused-argument
"""
Action when check on startup flag updated.
"""
self.__app.configuration.set(
"checkforupdate_b", int(self._checkonstartup.get())
)
def _on_github(self, *args, **kwargs): # pylint: disable=unused-argument
"""
Close dialog and go to GitHub.
"""
if brew_installed():
self.status_label = (BREWWARN, INFOCOL)
return
open_new_tab(GITHUB_URL)
self.on_exit()
def _on_sponsor(self, *args, **kwargs): # pylint: disable=unused-argument
"""
Close dialog and go to Sponsor website.
"""
if brew_installed():
self.status_label = (BREWWARN, INFOCOL)
return
open_new_tab(SPONSOR_URL)
self.on_exit()
def _on_license(self, *args, **kwargs): # pylint: disable=unused-argument
"""
Close dialog and go to GitHub LICENSE file.
"""
if brew_installed():
self.status_label = (BREWWARN, INFOCOL)
return
open_new_tab(LICENSE_URL)
self.on_exit()
def _check_for_update(self, *args, **kwargs): # pylint: disable=unused-argument
"""
Check for updates.
"""
self.status_label = ("Checking for updates...", INFOCOL)
versions = check_for_updates()
for i, (nam, current, latest) in enumerate(versions):
txt = f"{nam}: {current}"
if latest == current:
txt += " ✓"
col = OKCOL
elif latest == NA:
txt += " - Info not available!"
col = ERRCOL
else:
txt += f" - Latest version is {latest}"
col = ERRCOL
self._lbl_lib_versions[i].config(text=txt, fg=col)
updates = [nam for (nam, current, latest) in versions if latest != current]
if len(updates) > 0:
self.status_label = ("Updates available", OKCOL)
self._set_update_btn_mode(True)
else:
self.status_label = ("No updates available", INFOCOL)
def _do_update(self, *args, **kwargs): # pylint: disable=unused-argument
"""
Run python update.
"""
if brew_installed():
self.status_label = (BREWUPDATE, INFOCOL)
return
self.status_label = (UPDATEINPROG, INFOCOL)
rc = self.__app.do_app_update()
if rc:
self.status_label = (UPDATERESTART, OKCOL)
else:
self.status_label = (UPDATEERR.format(err=rc), ERRCOL)
self._set_update_btn_mode(False)
def _set_update_btn_mode(self, update: bool):
"""
Set Check for update button label and binding.
:param bool update: False = check, True = update
"""
if update:
self._btn_checkupdate.config(text="UPDATE", fg=OKCOL)
self._btn_checkupdate.bind("<Button>", self._do_update)
else:
self._btn_checkupdate.config(text="CHECK FOR UPDATES", fg=INFOCOL)
self._btn_checkupdate.bind("<Button>", self._check_for_update)
self.update_idletasks()