Skip to content

Commit

Permalink
- New: 1.Using dynamic font size for lables of GUI by the scaling DPI…
Browse files Browse the repository at this point in the history
… of the screen. 2. New option in Theme menue to set the font size of the GUI.
  • Loading branch information
byemaxx committed Dec 16, 2024
1 parent 168bae0 commit 3f9cc45
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 36 deletions.
5 changes: 5 additions & 0 deletions Docs/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Version: 1.120.0
## Date: 2024-12-15
### Changes:
- New: 1.Using dynamic font size for lables of GUI by the scaling DPI of the screen. 2. New option in Theme menue to set the font size of the GUI.

# Version: 1.119.11
## Date: 2024-12-10
### Changes:
Expand Down
135 changes: 102 additions & 33 deletions metax/gui/main_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ def __init__(self, MainWindow):

self.MainWindow.resize(1200, 800)
self.MainWindow.setWindowTitle("MetaX v" + __version__)
self.font_size = 12
self.current_theme = 'light_blue'

self.logger = LoggerManager()

Expand Down Expand Up @@ -261,7 +263,7 @@ def __init__(self, MainWindow):
self.actionCheck_Update.triggered.connect(lambda: self.check_update(show_message=True, manual_check_trigger=True))
self.actionSettings.triggered.connect(self.show_settings_window)

self.screen = self.screen = QApplication.screenAt(QCursor.pos()).geometry()
self.screen = QApplication.screenAt(QCursor.pos()).geometry()

self.screen_width = self.screen.width()
self.screen_height = self.screen.height()
Expand Down Expand Up @@ -922,37 +924,81 @@ def hide_plot_setting_groupbox(self):


############### basic function End ###############




def init_theme_menu(self):
# Create a menu for themes
theme_menu = QMenu("Themes", self.MainWindow)

# Fetch all available themes
themes = list_themes()
# replace the .xml suffix
# Replace the .xml suffix
themes = [theme.replace('.xml', '') for theme in themes]
# reordering the themes , light theme first
# Reorder the themes, light themes first
light_themes = [theme for theme in themes if "light_" in theme]
dark_themes = [theme for theme in themes if "dark_" in theme]
themes = light_themes + dark_themes

# Add themes to the menu
for theme in themes:
theme_action = QAction(theme, self.MainWindow)
theme_action.triggered.connect(lambda checked, theme=theme: self.change_theme(theme))
theme_action.triggered.connect(lambda checked, theme=theme: self.change_theme(theme, silent=False, is_load_font_size_from_settings=False))
theme_menu.addAction(theme_action)

# Add theme menu to menu bar
# Add a font size submenu
font_size_menu = QMenu("Font Size", self.MainWindow)

# Predefined font size options
predefined_sizes = [10, 12, 14, 16, 18, 20]

for size in predefined_sizes:
size_action = QAction(f"{size} pt", self.MainWindow)
size_action.triggered.connect(lambda checked, size=size: self.set_font_size(size))
font_size_menu.addAction(size_action)

# Add an option for custom font size
custom_size_action = QAction("Custom...", self.MainWindow)
custom_size_action.triggered.connect(self.set_custom_font_size)
font_size_menu.addAction(custom_size_action)

# Add the font size menu to the theme menu
theme_menu.addMenu(font_size_menu)

# Add theme menu to the menu bar
self.MainWindow.menuBar().addMenu(theme_menu)

def set_font_size(self, size):
"""
Set the font size and apply the current theme.
"""
self.font_size = size
self.change_theme(self.current_theme, silent=False, is_load_font_size_from_settings=False)

def set_custom_font_size(self):
"""
Open a dialog for the user to input a custom font size.
"""
from PyQt5.QtWidgets import QInputDialog

size, ok = QInputDialog.getInt(
self.MainWindow,
"Custom Font Size",
"Enter font size (pt):",
value=self.font_size,
min=8,
max=72
)
if ok:
self.set_font_size(size)


def init_theme(self):
if self.settings.contains("theme"):
theme = self.settings.value("theme", type=str)
print(f"Loading theme {theme}...")
theme = theme if theme != "" else "light_blue"
else:
theme = "light_blue"
print(f"Loading default theme {theme}...")
self.current_theme = theme
self.change_theme(theme, silent=True)

# restore the window size
Expand All @@ -967,11 +1013,13 @@ def init_theme(self):



def change_theme(self, theme, silent=False):
def change_theme(self, theme, silent=False, is_load_font_size_from_settings=True):
if not silent:
self.show_message(f"Changing theme to {theme}...")
text = f"Changing...\n\nTheme: {theme}\nFont size: {self.font_size}"
self.show_message(text)
# save the theme to settings
self.settings.setValue("theme", theme)
self.current_theme = theme

#! Deprecated, switch to manual change in Settings
## save the theme mode to GUI attribute (dark or light)
Expand Down Expand Up @@ -1000,8 +1048,22 @@ def change_theme(self, theme, silent=False):
combobox = getattr(self, attribute_name)
combobox.clear()
############### avoid the window size change when change theme ###############


if is_load_font_size_from_settings:
screen = QApplication.screenAt(QCursor.pos())
logical_dpi = screen.logicalDotsPerInch()
scaling_factor = logical_dpi / 96.0
# read if setting has font size and height
if self.settings.contains("font_size"):
self.font_size = self.settings.value("font_size", type=int)
print(f"Reading font size from settings file: {self.font_size}")
else:
self.font_size = int(12 * scaling_factor)
print(f"Setting default font size: {self.font_size}")


font_size = self.font_size
height = self.font_size + 8

custom_css = '''
QGroupBox {{
text-transform: none;
Expand All @@ -1017,36 +1079,36 @@ def change_theme(self, theme, silent=False):
text-transform: none;
}}
QLineEdit {{
font-size: 12px;
font-size: setted_font_size;
}}
QLabel {{
font-size: 12px;
font-size: setted_font_size;
}}
QComboBox {{
font-size: 12px;
height: 20px;
font-size: setted_font_size;
height: setted_height;
}}
QSpinBox {{
font-size: 12px;
height: 20px;
font-size: setted_font_size;
height: setted_height;
}}
QListWidget {{
font-size: 12px;
font-size: setted_font_size;
}}
QDoubleSpinBox {{
font-size: 12px;
height: 20px;
font-size: setted_font_size;
height: setted_height;
}}
QCheckBox {{
font-size: 12px;
height: 20px;
font-size: setted_font_size;
height: setted_height;
}}
QRadioButton {{
font-size: 12px;
height: 20px;
font-size: setted_font_size;
height: setted_height;
}}
QToolBox {{
font-size: 12px;
font-size: setted_font_size;
font-weight: bold;
}}
QPushButton {{
Expand All @@ -1055,13 +1117,13 @@ def change_theme(self, theme, silent=False):
background-color: {QTMATERIAL_SECONDARYCOLOR};
border: 1px solid {QTMATERIAL_PRIMARYCOLOR};
border-radius: 2px;
font-size: 12px;
font-size: setted_font_size;
padding: 5px;
margin: 2px;
height: 20px;
height: setted_height;
}}
'''
'''.replace('setted_font_size', f'{font_size}px').replace('setted_height', f'{height}px')
current_app = QtWidgets.QApplication.instance()

extra = {
Expand Down Expand Up @@ -1288,6 +1350,9 @@ def save_basic_settings(self, line_edit_name: str | None = None):

# save current window size
self.settings.setValue("window_size", self.MainWindow.size())

# save font_size
self.settings.setValue("font_size", self.font_size)


def save_set_multi_table_settings(self):
Expand Down Expand Up @@ -1519,16 +1584,20 @@ def closeEvent(self, event):
msgBox.addButton(do_not_close_button, QMessageBox.RejectRole)
reply = msgBox.exec()

if reply == 0 or reply == 1:
if reply == 0 or reply == 1: # 0 is save and close, 1 is close without saving
try:

if reply == 0:
self.show_message("Saving settings...", "Closing...")
if getattr(self, 'tfa', None) is None:
# save settings.ini only
self.save_basic_settings()
else:
self.save_metax_obj_to_file(save_path=self.metax_home_path, no_message=True)

else: # close without saving
# save settings.ini only
self.save_basic_settings()

# 关闭 self.web_list 中的所有窗口
for web_window in self.web_list:
web_window.close()
Expand Down
2 changes: 1 addition & 1 deletion metax/taxafunc_ploter/basic_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def plot_upset(self, df, title_name='Table', width=12, height=6, font_size=10,
plt.rcParams.update({'font.size': font_size})
upset_plot(upset_data, fig = fig, show_counts=show_label,
show_percentages=show_percentages if show_label else False,
element_size=None, sort_categories_by ='input',
element_size=None, sort_categories_by ='-input',
min_subset_size=min_subset_size if min_subset_size != 0 else None,
max_subset_rank=max_subset_rank if max_subset_rank != 0 else None)

Expand Down
2 changes: 1 addition & 1 deletion metax/utils/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '1.119.11'
__version__ = '1.120.0'
API_version = '4'
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "MetaXTools"
version = "1.119.11"
version = "1.120.0"
description = "MetaXTools is a novel tool for linking peptide sequences with taxonomic and functional information in Metaproteomics."
readme = "README_PyPi.md"
license = { text = "NorthOmics" }
Expand Down

0 comments on commit 3f9cc45

Please sign in to comment.