From 030415e850cf139b6249150458f164a0f1826e56 Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Sat, 29 Jan 2011 16:22:49 +0100 Subject: [PATCH] restable highlighting: fix background and foreground colours for selected rows --- src/qtgui/restable.cpp | 51 ++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/qtgui/restable.cpp b/src/qtgui/restable.cpp index 90f32bf9..7e6379e1 100644 --- a/src/qtgui/restable.cpp +++ b/src/qtgui/restable.cpp @@ -330,25 +330,48 @@ void RecollModel::sort(int column, Qt::SortOrder order) class ResTableDelegate: public QStyledItemDelegate { public: ResTableDelegate(QObject *parent) : QStyledItemDelegate(parent) {} + + // We might want to optimize by passing the data to the base + // method if the text does not contain any term matches. Would + // need a modif to plaintorich to return the match count (easy), + // and a way to pass an indicator from data(), a bit more + // difficult. Anyway, the display seems fast enough as is. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { + QStyleOptionViewItemV4 opt = option; + initStyleOption(&opt, index); QVariant value = index.data(Qt::DisplayRole); if (value.isValid() && !value.isNull()) { - // We might possibly want to optimize by passing the data - // to the base method if the text does not contain any - // term matches. Would need a modif to plaintorich to - // return the match count (easy), and a way to pass an - // indicator from data(), a bit more difficult. Anyway, - // the display seems fast enough as is. - QTextDocument document; - document.setHtml(value.toString()); - painter->save(); - painter->setClipRect(option.rect); - painter->translate(option.rect.topLeft()); - document.drawContents(painter); - painter->restore(); - } + QString text = value.toString(); + if (!text.isEmpty()) { + QTextDocument document; + painter->save(); + if (opt.state & QStyle::State_Selected) { + painter->fillRect(opt.rect, opt.palette.highlight()); + // Set the foreground color. The pen approach does + // not seem to work, probably it's reset by the + // textdocument. Couldn't use + // setdefaultstylesheet() either. the div thing is + // an ugly hack. Works for now +#if 0 + QPen pen = painter->pen(); + pen.setBrush(opt.palette.brush(QPalette::HighlightedText)); + painter->setPen(pen); +#else + text = QString::fromAscii("
") + + text + QString::fromAscii("
"); +#endif + } + painter->setClipRect(option.rect); + painter->translate(option.rect.topLeft()); + document.setHtml(text); + document.drawContents(painter); + painter->restore(); + return; + } + } + QStyledItemDelegate::paint(painter, option, index); } };