1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-13 17:43:59 +02:00

fix checkrect hit area in delegate too

This commit is contained in:
Leo Franchi
2011-05-04 11:42:53 -04:00
parent 59b64bfdd9
commit 3bc05bc73b
6 changed files with 48 additions and 15 deletions

View File

@@ -58,7 +58,6 @@ ConfigDelegateBase::sizeHint( const QStyleOptionViewItem& option, const QModelIn
void
ConfigDelegateBase::drawCheckBox( QStyleOptionViewItemV4& opt, QPainter* p, const QWidget* w ) const
{
m_checkRect = opt.rect;
QStyle* style = w ? w->style() : QApplication::style();
opt.checkState == Qt::Checked ? opt.state |= QStyle::State_On : opt.state |= QStyle::State_Off;
style->drawPrimitive( QStyle::PE_IndicatorViewItemCheck, &opt, p, w );
@@ -94,7 +93,7 @@ ConfigDelegateBase::editorEvent ( QEvent* event, QAbstractItemModel* model, cons
m_configPressed = false;
QMouseEvent* me = static_cast< QMouseEvent* >( event );
if( me->button() != Qt::LeftButton || !m_checkRect.contains( me->pos() ) )
if( me->button() != Qt::LeftButton || !checkRectForIndex( option, index ).contains( me->pos() ) )
return false;
// eat the double click events inside the check rect

View File

@@ -35,6 +35,8 @@ public:
virtual bool editorEvent ( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index );
// if you want to use a checkbox, you need to have this say where to paint it
virtual QRect checkRectForIndex( const QStyleOptionViewItem& option, const QModelIndex& idx ) const = 0;
// if you want to use a config wrench, you need to have this say where to paint it
virtual QRect configRectForIndex( const QStyleOptionViewItem& option, const QModelIndex& idx ) const = 0;
@@ -46,8 +48,6 @@ protected:
void drawConfigWrench( QPainter* painter, QStyleOptionViewItemV4& option, QStyleOptionToolButton& topt ) const;
private:
mutable QRect m_checkRect;
mutable QRect m_configRect;
bool m_configPressed;
};

View File

@@ -112,6 +112,17 @@ ResolverConfigDelegate::configRectForIndex( const QStyleOptionViewItem& option,
return confRect;
}
QRect
ResolverConfigDelegate::checkRectForIndex( const QStyleOptionViewItem &option, const QModelIndex &idx ) const
{
QStyleOptionViewItemV4 opt = option;
initStyleOption( &opt, idx );
QRect itemRect = opt.rect;
int top = itemRect.top();
QRect checkRect = QRect(2 * PADDING, 2 * PADDING + top, ICONSIZE, ICONSIZE );
return checkRect;
}
void
ResolverConfigDelegate::onConfigPressed( const QModelIndex& idx )

View File

@@ -29,7 +29,9 @@ class ResolverConfigDelegate : public ConfigDelegateBase
public:
explicit ResolverConfigDelegate(QObject* parent = 0);
virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
virtual QRect configRectForIndex ( const QStyleOptionViewItem& option, const QModelIndex& idx ) const;
virtual QRect checkRectForIndex( const QStyleOptionViewItem &option, const QModelIndex &idx ) const;
virtual QRect configRectForIndex( const QStyleOptionViewItem& option, const QModelIndex& idx ) const;
private slots:
void onConfigPressed ( const QModelIndex& );

View File

@@ -25,6 +25,7 @@
#include <QPainter>
#define ICONSIZE 24
#define CHECK_LEFT_EDGE 8
SipConfigDelegate::SipConfigDelegate( QObject* parent )
: ConfigDelegateBase ( parent )
{
@@ -62,8 +63,7 @@ SipConfigDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option
QStyle* style = w ? w->style() : QApplication::style();
style->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter, w );
int checkLeftEdge = 8;
int iconLeftEdge = checkLeftEdge + ICONSIZE + PADDING;
int iconLeftEdge = CHECK_LEFT_EDGE + ICONSIZE + PADDING;
int textLeftEdge = iconLeftEdge + ICONSIZE + PADDING;
if( index.data( SipModel::FactoryRole ).toBool() ) { // this is the "add new account" row
@@ -82,7 +82,7 @@ SipConfigDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option
int rectW = 18;
int diff = ( ICONSIZE/ 2 ) - ( rectW / 2) ;
int pos = ( mid ) - ( rectW / 2 );
QRect plusRect = QRect( checkLeftEdge + diff, pos + top, rectW, rectW );
QRect plusRect = QRect( CHECK_LEFT_EDGE + diff, pos + top, rectW, rectW );
QPixmap p( RESPATH "images/list-add.png" );
painter->drawPixmap( plusRect, p );
@@ -118,7 +118,7 @@ SipConfigDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option
int rectW = 18;
int diff = ( ICONSIZE/ 2 ) - ( rectW / 2) ;
int pos = ( mid ) - ( rectW / 2 );
QRect rect = QRect( checkLeftEdge + diff, pos + top, rectW, rectW );
QRect rect = QRect( CHECK_LEFT_EDGE + diff, pos + top, rectW, rectW );
QPixmap p( icon.pixmap( rect.size() ) );
painter->drawPixmap( rect, p );
}
@@ -138,7 +138,7 @@ SipConfigDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option
} else { // this is an existing account to show
// draw checkbox first
int pos = ( mid ) - ( ICONSIZE / 2 );
QRect checkRect = QRect( checkLeftEdge, pos + top, ICONSIZE, ICONSIZE );
QRect checkRect = QRect( CHECK_LEFT_EDGE, pos + top, ICONSIZE, ICONSIZE );
opt.rect = checkRect;
drawCheckBox( opt, painter, w );
@@ -214,14 +214,34 @@ SipConfigDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option
}
}
QRect
SipConfigDelegate::checkRectForIndex( const QStyleOptionViewItem &option, const QModelIndex &idx ) const
{
if( !idx.data( SipModel::FactoryItemRole ).toBool() && !idx.data( SipModel::FactoryRole ).toBool() )
{
QStyleOptionViewItemV4 opt = option;
initStyleOption( &opt, idx );
int mid = opt.rect.height() / 2;
int pos = ( mid ) - ( ICONSIZE / 2 );
QRect checkRect = QRect( CHECK_LEFT_EDGE, pos + opt.rect.top(), ICONSIZE, ICONSIZE );
return checkRect;
}
return QRect();
}
QRect
SipConfigDelegate::configRectForIndex( const QStyleOptionViewItem& option, const QModelIndex& idx ) const
{
QStyleOptionViewItemV4 opt = option;
initStyleOption( &opt, idx );
QRect itemRect = opt.rect;
QRect confRect = QRect( itemRect.width() - ICONSIZE - 2 * PADDING, (opt.rect.height() / 2) - ICONSIZE / 2 + opt.rect.top(), ICONSIZE, ICONSIZE );
return confRect;
if( !idx.data( SipModel::FactoryItemRole ).toBool() && !idx.data( SipModel::FactoryRole ).toBool() )
{
QStyleOptionViewItemV4 opt = option;
initStyleOption( &opt, idx );
QRect itemRect = opt.rect;
QRect confRect = QRect( itemRect.width() - ICONSIZE - 2 * PADDING, (opt.rect.height() / 2) - ICONSIZE / 2 + opt.rect.top(), ICONSIZE, ICONSIZE );
return confRect;
}
return QRect();
}

View File

@@ -33,6 +33,7 @@ public:
virtual bool editorEvent ( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index );
virtual QSize sizeHint ( const QStyleOptionViewItem& option, const QModelIndex& index ) const;
virtual QRect checkRectForIndex( const QStyleOptionViewItem &option, const QModelIndex &idx ) const;
virtual QRect configRectForIndex( const QStyleOptionViewItem& option, const QModelIndex& idx ) const;
private slots:
void askedForEdit( const QModelIndex& idx );