-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqt_utils.py
81 lines (59 loc) · 2.52 KB
/
qt_utils.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
# Copyright(C) 2024 Advanced Micro Devices, Inc. All rights reserved.
import os
from typing import List, Union, Optional
import psutil
# pylint: disable=no-name-in-module
from PySide6.QtWidgets import QWidget, QApplication
from PySide6.QtCore import QFile, QTextStream
from digest.dialog import StatusDialog
ROOT_FOLDER = os.path.dirname(__file__)
BASE_STYLE_FILE = os.path.join(ROOT_FOLDER, "styles", "darkstyle.qss")
def get_ram_utilization() -> float:
mem = psutil.virtual_memory()
ram_util_perc = mem.percent
return ram_util_perc
def prompt_user_ram_limit(
sys_ram_percent_limit: float = 90.0,
message: Optional[str] = None,
parent: Optional[QWidget] = None,
) -> bool:
current_ram_util = get_ram_utilization()
if current_ram_util >= sys_ram_percent_limit:
if not message:
message = f"System RAM utilization is at {current_ram_util}."
dialog = StatusDialog(message, parent=parent)
dialog.show()
return True
return False
def apply_style_sheet(widget: Union[QWidget, QApplication], style_path: str) -> None:
if not os.path.exists(style_path):
raise FileExistsError(f"File {style_path} not found.")
style_qfile = QFile(style_path)
style_qfile.open(QFile.ReadOnly | QFile.Text) # type: ignore
style_stream = QTextStream(style_qfile).readAll()
widget.setStyleSheet(style_stream)
def apply_dark_style_sheet(widget: Union[QWidget, QApplication]) -> None:
apply_style_sheet(widget, BASE_STYLE_FILE)
def apply_multiple_style_sheets(
widget: Union[QWidget, QApplication], style_file_paths: List[str]
) -> None:
style_stream = ""
for style_path in style_file_paths:
if not os.path.exists(style_path):
raise FileExistsError(f"File {style_path} not found.")
style_qfile = QFile(style_path)
style_qfile.open(QFile.ReadOnly | QFile.Text) # type: ignore
style_stream += QTextStream(style_qfile).readAll()
widget.setStyleSheet(style_stream)
def find_available_save_path(save_path: str) -> str:
"""Increments a counter until it finds a suitable save location
For example, if my/dir already exists this function will return the first
available location out of my/dir(1) or my/dir(2) etc..."""
counter = 1
new_path = save_path
while os.path.exists(new_path):
base_dir, base_name = os.path.split(save_path)
name, ext = os.path.splitext(base_name)
new_path = os.path.join(base_dir, f"{name}({counter}){ext}")
counter += 1
return new_path