Files
Adaptive_Resource_Monitor/arm/about.py

238 lines
9.8 KiB
Python

"""The Design Creations About tab and bundled project documents."""
import json
import logging
import platform
import struct
import sys
import winreg
from html import escape
from pathlib import Path
from PySide6.QtCore import QRectF, Qt, QUrl
from PySide6.QtGui import QDesktopServices, QFont, QPainter, QPixmap
from PySide6.QtWidgets import (
QDialog, QDialogButtonBox, QFormLayout, QGroupBox, QHBoxLayout, QLabel,
QPushButton, QTextBrowser, QVBoxLayout, QWidget,
)
from .branding import (
APPLICATION_IMAGE, COMPANY_LOGO, COPYRIGHT, DEVELOPER, DESCRIPTION, INSTALL_ID,
SUPPORT_URL, WEBSITE, asset, document,
)
from .version import NAME, VERSION
from .update_ui import UpdatePanel
from .window_geometry import WindowGeometry
LOG = logging.getLogger(__name__)
DOCS = {
"README.md": "README",
"CHANGELOG.md": "Change Log",
"LICENSE.md": "Software License",
"THIRD_PARTY.md": "Third-party Notices",
}
UNINSTALL_KEY = rf"Software\Microsoft\Windows\CurrentVersion\Uninstall\{INSTALL_ID}_is1"
def release_classification():
return VERSION.partition("_")[2].capitalize() or "Stable"
def installation():
if not getattr(sys, "frozen", False):
return "Source checkout", document("README.md").parent
directory = Path(sys.executable).resolve().parent
for view in (winreg.KEY_WOW64_64KEY, winreg.KEY_WOW64_32KEY):
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, UNINSTALL_KEY, 0, winreg.KEY_READ | view) as key:
path, _ = winreg.QueryValueEx(key, "InstallLocation")
if path and Path(path).resolve() == directory:
return "Windows installed", directory
except OSError:
continue
return "Windows portable", directory
def build_data():
try:
data = json.loads(document("BUILD.json").read_text(encoding="utf-8"))
return data if isinstance(data, dict) else {}
except (OSError, ValueError):
return {}
def link(url, text, color):
return f'<a style="color:{color}" href="{escape(url, quote=True)}">{escape(text)}</a>'
class BrandImage(QWidget):
def __init__(self, picture, description, size=(138, 116)):
super().__init__()
self.setFixedSize(*size)
self.setAccessibleName(description)
self.picture = picture
def paintEvent(self, _event):
if self.picture.isNull():
return
size = self.picture.size().scaled(self.size(), Qt.AspectRatioMode.KeepAspectRatio)
destination = QRectF((self.width() - size.width()) / 2, (self.height() - size.height()) / 2,
size.width(), size.height())
painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform)
painter.drawPixmap(destination, self.picture, QRectF(self.picture.rect()))
class AboutPage(QWidget):
def __init__(self, data_root):
super().__init__()
self.data_root = data_root
self.setObjectName("armAbout")
self.setAccessibleName(f"About {NAME}")
self.documents = {name: label for name, label in DOCS.items() if document(name).is_file()}
layout = QVBoxLayout(self)
layout.setContentsMargins(10, 9, 10, 10)
layout.setSpacing(9)
header = QHBoxLayout()
header.setSpacing(12)
self.app_image = BrandImage(QPixmap(str(asset(APPLICATION_IMAGE))), f"{NAME} application image")
self.company_image = BrandImage(QPixmap(str(asset(COMPANY_LOGO))), "Design Creations logo")
header.addWidget(self.app_image)
center = Qt.AlignmentFlag.AlignCenter
identity = QVBoxLayout()
identity.setSpacing(4)
identity.addStretch()
self.title = QLabel(NAME, alignment=center)
self.title.setWordWrap(True)
self.title.setFont(QFont("Georgia", 26, QFont.Weight.Bold))
identity.addWidget(self.title)
attribution = QLabel(f"{DEVELOPER} · {COPYRIGHT}", alignment=center)
attribution.setWordWrap(True)
identity.addWidget(attribution)
self.website = QLabel(alignment=center)
self.website.setTextInteractionFlags(Qt.TextInteractionFlag.TextBrowserInteraction)
self.website.linkActivated.connect(self.open_link)
identity.addWidget(self.website)
identity.addStretch()
header.addLayout(identity, 1)
header.addWidget(self.company_image)
layout.addLayout(header)
information = QGroupBox("Application Information")
details = QFormLayout(information)
details.setHorizontalSpacing(14)
description = QLabel(DESCRIPTION)
description.setWordWrap(True)
details.addRow(description)
self.version = QLabel(VERSION)
details.addRow("Installed version", self.version)
details.addRow("Release classification", QLabel(release_classification()))
license_info = ("See Software License below." if "LICENSE.md" in self.documents
else "Project license terms unavailable.")
details.addRow("Software license", QLabel(license_info))
view = QPushButton("Application Details")
view.clicked.connect(self.show_details)
buttons = QHBoxLayout()
buttons.addWidget(view)
buttons.addStretch()
details.addRow(buttons)
layout.addWidget(information)
self.updates = UpdatePanel(data_root, installation()[0])
layout.addWidget(self.updates)
self.links = QLabel(alignment=center)
self.links.setWordWrap(True)
self.links.setTextInteractionFlags(Qt.TextInteractionFlag.TextBrowserInteraction)
self.links.linkActivated.connect(self.open_link)
layout.addWidget(self.links)
self.document_group = QGroupBox("Change Log")
document_layout = QVBoxLayout(self.document_group)
self.browser = QTextBrowser()
self.browser.setAccessibleName("Project documentation")
self.browser.setOpenLinks(False)
self.browser.anchorClicked.connect(lambda url: self.open_link(url.toString()))
self.browser.setMinimumHeight(135)
document_layout.addWidget(self.browser)
layout.addWidget(self.document_group, 1)
self.show_document("CHANGELOG.md")
def theme(self, colors):
self.setStyleSheet(f"""
QWidget#armAbout {{ background: {colors.background}; }}
QWidget#armAbout QLabel {{ color: {colors.text}; }}
QWidget#armAbout QGroupBox {{
color: {colors.accent}; border: 1px solid {colors.border};
border-radius: 4px; margin-top: 8px; padding: 7px 6px 5px;
}}
QWidget#armAbout QGroupBox::title {{
subcontrol-origin: margin; left: 8px; padding: 0 4px;
}}
QWidget#armAbout QTextBrowser {{
background: {colors.panel}; color: {colors.text};
border: 1px solid {colors.border}; selection-background-color: {colors.highlight};
}}
QWidget#armAbout QPushButton {{
background: {colors.panel}; color: {colors.text}; padding: 4px 10px;
border: 1px solid {colors.border}; border-radius: 3px;
}}
QWidget#armAbout QPushButton:hover, QWidget#armAbout QPushButton:focus {{
background: {colors.highlight}; border-color: {colors.accent};
}}
""")
self.website.setText(link(WEBSITE, "www.designcreations.us", colors.accent))
items = [link(name, name if name in ("README.md", "CHANGELOG.md") else label, colors.accent)
for name, label in self.documents.items()]
items.append(link(SUPPORT_URL, "Contact & Support", colors.accent))
self.links.setText(" | ".join(items))
def show_document(self, name):
if name not in self.documents:
return
path = document(name)
self.document_group.setTitle(self.documents[name])
try:
self.browser.setMarkdown(path.read_text(encoding="utf-8"))
except (OSError, UnicodeError) as exc:
LOG.warning("Unable to read About document %s: %s", name, exc)
self.browser.setPlainText(f"Unable to open {name}.")
self.browser.verticalScrollBar().setValue(0)
def open_link(self, target):
if target in self.documents:
self.show_document(target)
elif QUrl(target).scheme() == "https":
QDesktopServices.openUrl(QUrl(target))
def show_details(self):
kind, directory = installation()
build = build_data()
revision = build.get("source_revision") or "Unavailable"
if build.get("source_modified"):
revision += " (including local changes)"
rows = (
("Application", NAME), ("Version", VERSION), ("Release classification", release_classification()),
("Build time (UTC)", build.get("built_at") or "Source checkout"),
("Source revision", revision),
("Platform", f"{platform.system()} · {platform.machine()} · {struct.calcsize('P') * 8}-bit"),
("Installation type", kind), ("Installation location", str(directory)),
("Application data", str(self.data_root)),
)
dialog = QDialog(self)
dialog.setWindowTitle("Application Details")
layout = QVBoxLayout(dialog)
form = QFormLayout()
for name, value in rows:
label = QLabel(str(value))
label.setWordWrap(True)
label.setTextInteractionFlags(Qt.TextInteractionFlag.TextSelectableByMouse)
form.addRow(name, label)
layout.addLayout(form)
buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Close)
buttons.rejected.connect(dialog.reject)
layout.addWidget(buttons)
dialog.resize(620, dialog.sizeHint().height())
dialog.window_geometry = WindowGeometry(dialog, self.data_root / "application-details-window.bin")
dialog.exec()