Fix color of highlighted text item

This commit is contained in:
Kovid Goyal 2014-12-24 13:13:25 +05:30
parent eec398a8c5
commit 4fa1dcff15
2 changed files with 12 additions and 7 deletions

View File

@ -10,9 +10,9 @@ import textwrap
from math import ceil from math import ceil
from PyQt5.Qt import ( from PyQt5.Qt import (
QWidget, Qt, QStaticText, QTextOption, QSize, QPainter, QTimer, QPen) QWidget, Qt, QStaticText, QTextOption, QSize, QPainter, QTimer, QPalette)
from calibre import prints from calibre import prints, prepare_string_for_xml
from calibre.gui2 import error_dialog from calibre.gui2 import error_dialog
from calibre.gui2.tweak_book.widgets import make_highlighted_text from calibre.gui2.tweak_book.widgets import make_highlighted_text
from calibre.utils.icu import string_length from calibre.utils.icu import string_length
@ -67,7 +67,9 @@ class CompletionPopup(QWidget):
text = make_highlighted_text('color: magenta', text, positions) text = make_highlighted_text('color: magenta', text, positions)
desc = self.descriptions.get(otext) desc = self.descriptions.get(otext)
if desc: if desc:
text += ' - ' + desc text += ' - <i>%s</i>' % prepare_string_for_xml(desc)
color = self.palette().color(QPalette.Text).name()
text = '<span style="color: %s">%s</span>' % (color, text)
st = self.rendered_text_cache[otext] = QStaticText(text) st = self.rendered_text_cache[otext] = QStaticText(text)
st.setTextOption(self.text_option) st.setTextOption(self.text_option)
st.setTextFormat(Qt.RichText) st.setTextFormat(Qt.RichText)
@ -82,7 +84,7 @@ class CompletionPopup(QWidget):
sz = self.get_static_text(text, positions).size() sz = self.get_static_text(text, positions).size()
height += int(ceil(sz.height())) + self.TOP_MARGIN + self.BOTTOM_MARGIN height += int(ceil(sz.height())) + self.TOP_MARGIN + self.BOTTOM_MARGIN
max_width = max(max_width, int(ceil(sz.width()))) max_width = max(max_width, int(ceil(sz.width())))
self.current_size_hint = QSize(max_width, height + 2) self.current_size_hint = QSize(max_width, height + self.BOTTOM_MARGIN)
return self.current_size_hint return self.current_size_hint
def iter_visible_items(self): def iter_visible_items(self):
@ -102,13 +104,15 @@ class CompletionPopup(QWidget):
pal = self.palette() pal = self.palette()
painter.fillRect(self.rect(), pal.color(pal.Base)) painter.fillRect(self.rect(), pal.color(pal.Base))
painter.setFont(self.parent().font()) painter.setFont(self.parent().font())
painter.setPen(QPen(pal.color(pal.Text)))
width = self.rect().width() width = self.rect().width()
for i, st, y, height in self.iter_visible_items(): for i, st, y, height in self.iter_visible_items():
painter.save() painter.save()
if i == self.current_index: if i == self.current_index:
painter.fillRect(0, y, width, height, pal.color(pal.Highlight)) painter.fillRect(0, y, width, height, pal.color(pal.Highlight))
painter.setPen(QPen(pal.color(pal.HighlightedText))) color = pal.color(QPalette.HighlightedText).name()
st = QStaticText(st)
text = st.text().partition('>')[2]
st.setText('<span style="color: %s">%s' % (color, text))
painter.drawStaticText(0, y, st) painter.drawStaticText(0, y, st)
painter.restore() painter.restore()
painter.end() painter.end()
@ -228,7 +232,7 @@ if __name__ == '__main__':
items = 'a ab abc abcd abcde abcdef abcdefg abcdefgh'.split() items = 'a ab abc abcd abcde abcdef abcdefg abcdefgh'.split()
m = Matcher(items) m = Matcher(items)
c.set_items(m('a'), descriptions={x:x for x in items}) c.set_items(m('a'), descriptions={x:x for x in items})
QTimer.singleShot(10, c.show) QTimer.singleShot(100, c.show)
from calibre.gui2.tweak_book.editor.widget import launch_editor from calibre.gui2.tweak_book.editor.widget import launch_editor
raw = textwrap.dedent('''\ raw = textwrap.dedent('''\
Is the same as saying through shrinking from toil and pain. These Is the same as saying through shrinking from toil and pain. These

View File

@ -217,6 +217,7 @@ class TextEdit(PlainTextEdit):
self.size_hint = QSize(self.expected_geometry[0] * w.averageCharWidth(), self.expected_geometry[1] * w.height()) self.size_hint = QSize(self.expected_geometry[0] * w.averageCharWidth(), self.expected_geometry[1] * w.height())
self.highlight_color = theme_color(theme, 'HighlightRegion', 'bg') self.highlight_color = theme_color(theme, 'HighlightRegion', 'bg')
self.highlight_cursor_line() self.highlight_cursor_line()
self.completion_popup.clear_caches(), self.completion_popup.update()
# }}} # }}}
def load_text(self, text, syntax='html', process_template=False, doc_name=None): def load_text(self, text, syntax='html', process_template=False, doc_name=None):