Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PaletteItemDelegate: Fix highlighted item foreground color #1400

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions angrmanagement/ui/dialogs/command_palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TYPE_CHECKING, Any

from PySide6.QtCore import QAbstractItemModel, QMargins, QModelIndex, QRectF, QSize, Qt
from PySide6.QtGui import QBrush, QColor, QTextDocument
from PySide6.QtGui import QBrush, QColor, QPalette, QPen, QTextCharFormat, QTextCursor, QTextDocument
from PySide6.QtWidgets import QDialog, QLineEdit, QListView, QStyle, QStyledItemDelegate, QVBoxLayout
from thefuzz import process

Expand All @@ -17,6 +17,7 @@
from angrmanagement.ui.workspace import Workspace


# pylint:disable=unused-argument,no-self-use
class PaletteModel(QAbstractItemModel):
"""
Data provider for item palette.
Expand All @@ -32,20 +33,20 @@ def __init__(self, workspace: Workspace) -> None:
self._filtered_items: list[Any] = self._available_items
self._filter_text: str = ""

def rowCount(self, _):
def rowCount(self, parent=None):
return len(self._filtered_items)

def columnCount(self, _) -> int: # pylint:disable=no-self-use
def columnCount(self, parent=None):
return 1

def index(self, row, col, _):
return self.createIndex(row, col, None)
def index(self, row, column, parent=None):
return self.createIndex(row, column, None)

def parent(self, _): # pylint:disable=no-self-use
def parent(self, index=None): # type:ignore
return QModelIndex()

def data(self, index, role):
if not index.isValid() or role != Qt.DisplayRole:
def data(self, index, role: int = Qt.ItemDataRole.DisplayRole) -> Any:
if not index.isValid() or role != Qt.ItemDataRole.DisplayRole:
return None
row = index.row()
return self._filtered_items[row] if row < len(self._filtered_items) else None
Expand Down Expand Up @@ -105,6 +106,7 @@ def get_items(self) -> list[Function]:
instance = self.workspace.main_instance
if instance and not instance.project.am_none:
project = instance.project.am_obj
assert project is not None
items.extend([func for _, func in project.kb.functions.items()])

return items
Expand All @@ -119,7 +121,7 @@ def get_icon_color_and_text_for_item(self, item: Function) -> tuple[QColor | Non
elif item.alignment:
color = Conf.function_table_alignment_color
else:
color = Qt.GlobalColor.gray
color = QColor(Qt.GlobalColor.gray)
return (color, "f")

def get_caption_for_item(self, item: Function) -> str:
Expand Down Expand Up @@ -170,6 +172,7 @@ def _get_text_document(index):

def paint(self, painter, option, index) -> None:
if index.column() == 0:
painter.save()
if option.state & QStyle.StateFlag.State_Selected:
b = QBrush(option.palette.highlight())
painter.fillRect(option.rect, b)
Expand All @@ -182,13 +185,14 @@ def paint(self, painter, option, index) -> None:

annotation_text = model.get_annotation_for_item(item)
if annotation_text:
if option.state & QStyle.StateFlag.State_Selected:
painter.setPen(QPen(option.palette.color(QPalette.ColorRole.HighlightedText)))
painter.drawText(
option.rect.marginsRemoved(QMargins(3, 3, 3, 3)),
Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter,
annotation_text,
)

painter.save()
painter.translate(option.rect.topLeft())

if self._display_icons:
Expand All @@ -201,6 +205,13 @@ def paint(self, painter, option, index) -> None:
painter.drawText(icon_rect, Qt.AlignmentFlag.AlignCenter, icon_text)
painter.translate(self.icon_width, 0)

if option.state & QStyle.StateFlag.State_Selected:
cursor = QTextCursor(td)
cursor.select(QTextCursor.SelectionType.Document)
char_format = QTextCharFormat()
char_format.setForeground(option.palette.highlightedText())
cursor.mergeCharFormat(char_format)

td.drawContents(painter)
painter.restore()
else:
Expand All @@ -214,7 +225,7 @@ def sizeHint(self, option, index) -> QSize:
width = s.width()
if self._display_icons:
width += self.icon_width
return QSize(width, s.height())
return QSize(int(width), int(s.height()))
return super().sizeHint(option, index)


Expand Down
Loading