diff --git a/src/calibre/gui2/chat_widget.py b/src/calibre/gui2/chat_widget.py index c1c11b768d..67279ab82f 100644 --- a/src/calibre/gui2/chat_widget.py +++ b/src/calibre/gui2/chat_widget.py @@ -8,6 +8,7 @@ from typing import NamedTuple from qt.core import QFrame, QHBoxLayout, QIcon, QPalette, QSize, QSizePolicy, Qt, QTextBrowser, QTextEdit, QToolButton, QUrl, QVBoxLayout, QWidget, pyqtSignal from calibre.utils.logging import INFO, WARN +from calibre.utils.resources import get_image_path class Browser(QTextBrowser): @@ -33,22 +34,24 @@ class Browser(QTextBrowser): class Button(NamedTuple): - text: str + icon: str link: str tooltip: str = '' - @property - def as_html(self) -> str: - return f'''{escape(self.text)}''' + def as_html(self, line_spacing: int) -> str: + path = get_image_path(self.icon) + url = QUrl.fromLocalFile(path).toString(QUrl.ComponentFormattingOption.FullyEncoded) + sz = line_spacing - 2 + return f'''''' class Header(NamedTuple): title: str = '' buttons: tuple[Button, ...] = () - @property - def as_html(self) -> str: - links = '\xa0\xa0'.join(b.as_html for b in self.buttons) + def as_html(self, line_spacing: int) -> str: + links = '\xa0\xa0'.join(b.as_html(line_spacing) for b in self.buttons) title = '' + escape(self.title) if links: return f''' @@ -152,6 +155,7 @@ class ChatWidget(QWidget): self.response_color = pal.color(QPalette.ColorRole.Window).name() self.base_color = pal.color(QPalette.ColorRole.Base).name() self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding) + self.line_spacing = self.browser.fontMetrics().lineSpacing() def wrap_content_in_padding_table(self, html: str, background_color: str = '') -> str: style = f'style="background-color: {background_color}"' if background_color else '' @@ -162,7 +166,7 @@ class ChatWidget(QWidget): self.current_message = '' html = '' if header.title or header.buttons: - html += f'
{header.as_html}
' + html += f'
{header.as_html(self.line_spacing)}
' html += f'
{body_html}
' bg = self.response_color if is_response else self.base_color self.blocks.append(self.wrap_content_in_padding_table(html, bg)) diff --git a/src/calibre/gui2/viewer/llm.py b/src/calibre/gui2/viewer/llm.py index ce438dfcc2..d282c8bd16 100644 --- a/src/calibre/gui2/viewer/llm.py +++ b/src/calibre/gui2/viewer/llm.py @@ -369,9 +369,9 @@ class LLMPanel(QWidget): if message.from_assistant: is_response = True header = Header(assistant, ( - Button(_('💾'), f'http://{self.save_note_hostname}/{i}', _( + Button('save.png', f'http://{self.save_note_hostname}/{i}', _( 'Save this specific response as the note')), - Button('📋', f'http://{self.copy_hostname}/{i}', _( + Button('edit-copy.png', f'http://{self.copy_hostname}/{i}', _( 'Copy this specific response to the clipboard')), )) self.result_display.add_block(content_for_display, header, is_response)
{title}\xa0