From d3918cd9cf2c2f31f96c26665e51e3b5951832ab Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 3 May 2010 20:22:38 +0000 Subject: [PATCH] correct bug with selection with human readable delegate. git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5.0@2843 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- .../src/gui/RSHumanReadableDelegate.h | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/retroshare-gui/src/gui/RSHumanReadableDelegate.h b/retroshare-gui/src/gui/RSHumanReadableDelegate.h index 39b3d4009..944f7980f 100644 --- a/retroshare-gui/src/gui/RSHumanReadableDelegate.h +++ b/retroshare-gui/src/gui/RSHumanReadableDelegate.h @@ -48,6 +48,40 @@ class RSHumanReadableDelegate: public QAbstractItemDelegate return QSize(50,17) ; } virtual void paint(QPainter *painter,const QStyleOptionViewItem & option, const QModelIndex & index) const = 0; + + protected: + virtual void setPainterOptions(QPainter *painter,QStyleOptionViewItem& option,const QModelIndex& index) const + { + // This part of the code is copied from DLListDelegate.cpp + // + QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled; + QVariant value = index.data(Qt::TextColorRole); + + if(value.isValid() && qvariant_cast(value).isValid()) + option.palette.setColor(QPalette::Text, qvariant_cast(value)); + + // select pen color + if(option.state & QStyle::State_Selected) + painter->setPen(option.palette.color(cg, QPalette::HighlightedText)); + else + painter->setPen(option.palette.color(cg, QPalette::Text)); + + // draw the background color + if(option.showDecorationSelected && (option.state & QStyle::State_Selected)) + { + if(cg == QPalette::Normal && !(option.state & QStyle::State_Active)) + cg = QPalette::Inactive; + + painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight)); + } + else + { + value = index.data(Qt::BackgroundColorRole); + + if(value.isValid() && qvariant_cast(value).isValid()) + painter->fillRect(option.rect, qvariant_cast(value)); + } + } }; class RSHumanReadableAgeDelegate: public RSHumanReadableDelegate @@ -55,7 +89,10 @@ class RSHumanReadableAgeDelegate: public RSHumanReadableDelegate public: virtual void paint(QPainter *painter,const QStyleOptionViewItem & option, const QModelIndex & index) const { - painter->drawText(option.rect, Qt::AlignCenter, misc::userFriendlyDuration(index.data().toLongLong())) ; + QStyleOptionViewItem opt(option) ; + setPainterOptions(painter,opt,index) ; + + painter->drawText(opt.rect, Qt::AlignCenter, misc::userFriendlyDuration(index.data().toLongLong())) ; } }; @@ -64,7 +101,10 @@ class RSHumanReadableSizeDelegate: public RSHumanReadableDelegate public: virtual void paint(QPainter *painter,const QStyleOptionViewItem & option, const QModelIndex & index) const { - painter->drawText(option.rect, Qt::AlignRight, misc::friendlyUnit(index.data().toULongLong())); + QStyleOptionViewItem opt(option) ; + setPainterOptions(painter,opt,index) ; + + painter->drawText(opt.rect, Qt::AlignRight, misc::friendlyUnit(index.data().toULongLong())); } };