restable highlighting: fix background and foreground colours for selected rows

This commit is contained in:
Jean-Francois Dockes 2011-01-29 16:22:49 +01:00
parent 44528876e7
commit 030415e850

View File

@ -330,26 +330,49 @@ void RecollModel::sort(int column, Qt::SortOrder order)
class ResTableDelegate: public QStyledItemDelegate { class ResTableDelegate: public QStyledItemDelegate {
public: public:
ResTableDelegate(QObject *parent) : QStyledItemDelegate(parent) {} 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, void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const const QModelIndex &index) const
{ {
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
QVariant value = index.data(Qt::DisplayRole); QVariant value = index.data(Qt::DisplayRole);
if (value.isValid() && !value.isNull()) { if (value.isValid() && !value.isNull()) {
// We might possibly want to optimize by passing the data QString text = value.toString();
// to the base method if the text does not contain any if (!text.isEmpty()) {
// 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; QTextDocument document;
document.setHtml(value.toString());
painter->save(); 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("<div style='color: white'> ") +
text + QString::fromAscii("</div>");
#endif
}
painter->setClipRect(option.rect); painter->setClipRect(option.rect);
painter->translate(option.rect.topLeft()); painter->translate(option.rect.topLeft());
document.setHtml(text);
document.drawContents(painter); document.drawContents(painter);
painter->restore(); painter->restore();
return;
} }
} }
QStyledItemDelegate::paint(painter, option, index);
}
}; };
void ResTable::init() void ResTable::init()