Mouse-over for all views
This commit is contained in:
committed by
craig.p.drummond
parent
606976a21a
commit
3a169fe24e
@@ -22,9 +22,12 @@
|
||||
*/
|
||||
|
||||
#include "basicitemdelegate.h"
|
||||
#include "gtkstyle.h"
|
||||
#include <QPainter>
|
||||
#include <QStyle>
|
||||
#include <QStyledItemDelegate>
|
||||
#include <QApplication>
|
||||
#include <QAbstractItemView>
|
||||
|
||||
void BasicItemDelegate::drawLine(QPainter *p, const QRect &r, const QColor &color, bool fadeStart, bool fadeEnd)
|
||||
{
|
||||
@@ -55,7 +58,14 @@ void BasicItemDelegate::drawLine(QPainter *p, const QRect &r, const QColor &colo
|
||||
|
||||
BasicItemDelegate::BasicItemDelegate(QObject *p)
|
||||
: QStyledItemDelegate(p)
|
||||
, trackMouse(false)
|
||||
, underMouse(false)
|
||||
{
|
||||
if (GtkStyle::isActive() && qobject_cast<QAbstractItemView *>(p)) {
|
||||
static_cast<QAbstractItemView *>(p)->setAttribute(Qt::WA_MouseTracking);
|
||||
trackMouse=true;
|
||||
p->installEventFilter(this);
|
||||
}
|
||||
}
|
||||
|
||||
BasicItemDelegate::~BasicItemDelegate()
|
||||
@@ -70,30 +80,60 @@ void BasicItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt
|
||||
|
||||
bool selected=option.state&QStyle::State_Selected;
|
||||
bool active=option.state&QStyle::State_Active;
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
if (GtkStyle::isActive()) {
|
||||
bool mouseOver=option.state&QStyle::State_MouseOver;
|
||||
QStyleOptionViewItemV4 opt = option;
|
||||
initStyleOption(&opt, index);
|
||||
|
||||
if (trackMouse && !underMouse) {
|
||||
mouseOver=false;
|
||||
}
|
||||
|
||||
if (mouseOver) {
|
||||
opt.showDecorationSelected=true;
|
||||
|
||||
GtkStyle::drawSelection(option, painter, selected ? 0.75 : 0.25);
|
||||
opt.showDecorationSelected=false;
|
||||
opt.state&=~(QStyle::State_MouseOver|QStyle::State_Selected);
|
||||
opt.backgroundBrush=QBrush(Qt::transparent);
|
||||
if (selected) {
|
||||
opt.palette.setBrush(QPalette::Text, opt.palette.highlightedText());
|
||||
}
|
||||
}
|
||||
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
|
||||
} else {
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
|
||||
QColor col(option.palette.color(active ? QPalette::Active : QPalette::Inactive,
|
||||
selected ? QPalette::HighlightedText : QPalette::Text));
|
||||
|
||||
|
||||
if (4==option.version) {
|
||||
const QStyleOptionViewItemV4 &v4=(QStyleOptionViewItemV4 &)option;
|
||||
|
||||
switch (v4.viewItemPosition) {
|
||||
case QStyleOptionViewItemV4::Beginning:
|
||||
drawLine(painter, option.rect, col, true, false);
|
||||
break;
|
||||
case QStyleOptionViewItemV4::Middle:
|
||||
drawLine(painter, option.rect, col, false, false);
|
||||
break;
|
||||
case QStyleOptionViewItemV4::End:
|
||||
drawLine(painter, option.rect, col, false, true);
|
||||
break;
|
||||
case QStyleOptionViewItemV4::Invalid:
|
||||
case QStyleOptionViewItemV4::OnlyOne:
|
||||
drawLine(painter, option.rect, col, true, true);
|
||||
}
|
||||
} else {
|
||||
switch (((QStyleOptionViewItemV4 &)option).viewItemPosition) {
|
||||
case QStyleOptionViewItemV4::Beginning:
|
||||
drawLine(painter, option.rect, col, true, false);
|
||||
break;
|
||||
case QStyleOptionViewItemV4::Middle:
|
||||
drawLine(painter, option.rect, col, false, false);
|
||||
break;
|
||||
case QStyleOptionViewItemV4::End:
|
||||
drawLine(painter, option.rect, col, false, true);
|
||||
break;
|
||||
case QStyleOptionViewItemV4::Invalid:
|
||||
case QStyleOptionViewItemV4::OnlyOne:
|
||||
drawLine(painter, option.rect, col, true, true);
|
||||
}
|
||||
}
|
||||
|
||||
bool BasicItemDelegate::eventFilter(QObject *object, QEvent *event)
|
||||
{
|
||||
if (object==parent()) {
|
||||
if (QEvent::Enter==event->type()) {
|
||||
underMouse=true;
|
||||
static_cast<QAbstractItemView *>(parent())->viewport()->update();
|
||||
} else if (QEvent::Leave==event->type()) {
|
||||
underMouse=false;
|
||||
static_cast<QAbstractItemView *>(parent())->viewport()->update();
|
||||
}
|
||||
}
|
||||
return QStyledItemDelegate::eventFilter(object, event);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,11 @@ public:
|
||||
BasicItemDelegate(QObject *p);
|
||||
virtual ~BasicItemDelegate();
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
||||
private:
|
||||
bool eventFilter(QObject *object, QEvent *event);
|
||||
private:
|
||||
bool trackMouse;
|
||||
bool underMouse;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -44,11 +44,10 @@
|
||||
// Exported by QtGui
|
||||
void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0);
|
||||
|
||||
|
||||
class PlayQueueTreeViewItemDelegate : public QStyledItemDelegate
|
||||
class PlayQueueTreeViewItemDelegate : public BasicItemDelegate
|
||||
{
|
||||
public:
|
||||
PlayQueueTreeViewItemDelegate(QObject *p) : QStyledItemDelegate(p) { }
|
||||
PlayQueueTreeViewItemDelegate(QObject *p) : BasicItemDelegate(p) { }
|
||||
virtual ~PlayQueueTreeViewItemDelegate() { }
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
@@ -56,53 +55,16 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
bool selected=option.state&QStyle::State_Selected;
|
||||
bool active=option.state&QStyle::State_Active;
|
||||
QColor col(option.palette.color(active ? QPalette::Active : QPalette::Inactive,
|
||||
selected ? QPalette::HighlightedText : QPalette::Text));
|
||||
|
||||
if (4==option.version) {
|
||||
const QStyleOptionViewItemV4 &v4=(QStyleOptionViewItemV4 &)option;
|
||||
QIcon icon;
|
||||
|
||||
if (QStyleOptionViewItemV4::Beginning==v4.viewItemPosition) {
|
||||
icon=index.data(PlayQueueView::Role_Decoration).value<QIcon>();
|
||||
QStyleOptionViewItemV4 v4((QStyleOptionViewItemV4 &)option);
|
||||
if (QStyleOptionViewItemV4::Beginning==v4.viewItemPosition) {
|
||||
v4.icon=index.data(PlayQueueView::Role_Decoration).value<QIcon>();
|
||||
if (!v4.icon.isNull()) {
|
||||
v4.features |= QStyleOptionViewItemV2::HasDecoration;
|
||||
v4.decorationSize=v4.icon.actualSize(option.decorationSize, QIcon::Normal, QIcon::Off);
|
||||
}
|
||||
QSize decorSize;
|
||||
if (!icon.isNull()) {
|
||||
decorSize = icon.actualSize(option.decorationSize);
|
||||
}
|
||||
|
||||
if (decorSize.isEmpty()) {
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
} else {
|
||||
QStyleOptionViewItemV4 opt=v4;
|
||||
int width=decorSize.width()+(Utils::isHighDpi() ? 8 : 4);
|
||||
opt.rect.adjust(width, 0, 0, 0);
|
||||
QStyledItemDelegate::paint(painter, opt, index);
|
||||
|
||||
QPixmap pix=icon.pixmap(decorSize);
|
||||
painter->drawPixmap(option.rect.x()+((width-pix.width())/2), option.rect.y()+((option.rect.height()-pix.height())/2), pix);
|
||||
}
|
||||
|
||||
switch (v4.viewItemPosition) {
|
||||
case QStyleOptionViewItemV4::Beginning:
|
||||
BasicItemDelegate::drawLine(painter, option.rect, col, true, false);
|
||||
break;
|
||||
case QStyleOptionViewItemV4::Middle:
|
||||
BasicItemDelegate::drawLine(painter, option.rect, col, false, false);
|
||||
break;
|
||||
case QStyleOptionViewItemV4::End:
|
||||
BasicItemDelegate::drawLine(painter, option.rect, col, false, true);
|
||||
break;
|
||||
case QStyleOptionViewItemV4::Invalid:
|
||||
case QStyleOptionViewItemV4::OnlyOne:
|
||||
BasicItemDelegate::drawLine(painter, option.rect, col, true, true);
|
||||
}
|
||||
} else {
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
BasicItemDelegate::drawLine(painter, option.rect, col, false, false);
|
||||
}
|
||||
|
||||
BasicItemDelegate::paint(painter, v4, index);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user