Skip to content

Commit

Permalink
Preferences: Simplify font settings display
Browse files Browse the repository at this point in the history
  • Loading branch information
mborgerson committed Sep 22, 2023
1 parent 258520d commit d1441ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
29 changes: 17 additions & 12 deletions angrmanagement/ui/dialogs/preferences.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import enum
from datetime import datetime
from itertools import chain

from bidict import bidict
from PySide6.QtGui import QColor
Expand All @@ -10,6 +11,7 @@
QDialog,
QDialogButtonBox,
QFrame,
QGridLayout,
QGroupBox,
QHBoxLayout,
QLabel,
Expand All @@ -32,6 +34,7 @@
from angrmanagement.ui.css import refresh_theme
from angrmanagement.ui.widgets.qcolor_option import QColorOption
from angrmanagement.ui.widgets.qfont_option import QFontOption
from angrmanagement.utils.layout import add_to_grid


class Page(QWidget):
Expand Down Expand Up @@ -223,26 +226,28 @@ def _init_widgets(self):
page_layout.addLayout(log_format_layout)

# Font options
self._font_options = [
QFontOption("Application Font", "ui_default_font", self),
QFontOption("Tab View Font", "tabular_view_font", self),
QFontOption("Disassembly Font", "disasm_font", self),
QFontOption("SymExc Font", "symexec_font", self),
QFontOption("Code Font", "code_font", self),
fonts_group_box = QGroupBox("Fonts")
fonts_layout = QGridLayout()
fonts_group_box.setLayout(fonts_layout)
page_layout.addWidget(fonts_group_box)
entries = [
("Application Font", "ui_default_font"),
("Tabular View Font", "tabular_view_font"),
("Disassembly Font", "disasm_font"),
("SymExc Font", "symexec_font"),
("Code Font", "code_font"),
]
font_layout = QVBoxLayout()
for i in self._font_options:
font_layout.addWidget(i)
page_layout.addLayout(font_layout)
self._fonts_widgets = [(QLabel(f"{name}:"), QFontOption(key, self)) for name, key in entries]
add_to_grid(fonts_layout, 2, chain(*self._fonts_widgets))

page_layout.addStretch()

def save_config(self):
fmt = self.log_format_entry.currentText()
if fmt:
Conf.log_timestamp_format = self._fmt_map.get(fmt, fmt)
for i in self._font_options:
i.update()
for _, font_picker in self._fonts_widgets:
font_picker.update()


class Preferences(QDialog):
Expand Down
33 changes: 11 additions & 22 deletions angrmanagement/ui/widgets/qfont_option.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
from typing import TYPE_CHECKING

from PySide6.QtWidgets import QFontDialog, QHBoxLayout, QLabel, QPushButton, QWidget
from PySide6.QtWidgets import QFontDialog, QPushButton

from angrmanagement.config import Conf

if TYPE_CHECKING:
from PySide6.QtGui import QFont


class QFontOption(QWidget):
class QFontOption(QPushButton):
"""
A widget used to allow users to change a font stored in Conf
"""

def __init__(self, name: str, key: str, parent=None):
def __init__(self, config_key: str, parent=None):
"""
:param name: The name of the font
:param key: The key of the font in Conf
:parent: The optional parent of this QWidget
:param key: Key of the font in Conf
:parent: Optional parent of this QWidget
"""
super().__init__(parent)
self.name: str = name
self._key = key
self.font: QFont = getattr(Conf, key)
layout = QHBoxLayout(self)
# Label
self.label = QLabel(parent=self)
layout.addWidget(self.label)
# Button
self.button = QPushButton("Change", parent=self)
self.button.released.connect(self._prompt)
layout.addWidget(self.button)
# Finish
self.setLayout(layout)
self._config_key = config_key
self.font: QFont = getattr(Conf, config_key)
self.released.connect(self._prompt)
self._format()

def update(self):
"""
Update Conf with the selected font
"""
setattr(Conf, self._key, self.font)
setattr(Conf, self._config_key, self.font)

def _format(self):
"""
Format the widget's UI elements
"""
self.label.setText(f"{self.name}: {self.font.family()}")
self.label.setFont(self.font) # We do not ._sync.keep_synced this; it should update always
self.setText(f"{self.font.family()}, {self.font.pointSize()} pt")
self.setFont(self.font) # We do not ._sync.keep_synced this; it should update always

def _prompt(self):
"""
Expand Down

0 comments on commit d1441ce

Please sign in to comment.