24#include "moc_qgsattributesformtreeviewitemdelegate.cpp"
30 , mAttributesFormTreeView( treeView )
34QRect QgsAttributesFormTreeViewProxyStyle::subElementRect( QStyle::SubElement element,
const QStyleOption *option,
const QWidget *widget )
const
36 if ( element == SE_AttributesFormTreeItemIndicator )
38 if (
const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>( option ) )
40 if ( vopt->index.isValid() )
42 const int count = mAttributesFormTreeView->indicators( vopt->index ).count();
45 const QRect vpr = mAttributesFormTreeView->viewport()->rect();
46 const QRect r = QProxyStyle::subElementRect( SE_ItemViewItemText, option, widget );
47 const int indiWidth = r.height() * count;
48 const int spacing = r.height() / 10;
49 const int vpIndiWidth = vpr.width() - indiWidth - spacing;
50 return QRect( vpIndiWidth, r.top(), indiWidth, r.height() );
55 return QProxyStyle::subElementRect( element, option, widget );
60 : QStyledItemDelegate( parent )
61 , mAttributesFormTreeView( parent )
65void QgsAttributesFormTreeViewItemDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
67 QStyledItemDelegate::paint( painter, option, index );
69 if ( !index.isValid() )
72 QStyleOptionViewItem opt = option;
73 initStyleOption( &opt, index );
75 const QColor baseColor = opt.palette.base().color();
76 const QList<QgsAttributesFormTreeViewIndicator *> indicators = mAttributesFormTreeView->indicators( index );
77 if ( indicators.isEmpty() )
80 const QRect indRect = mAttributesFormTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsAttributesFormTreeViewProxyStyle::SE_AttributesFormTreeItemIndicator ), &opt, mAttributesFormTreeView );
81 const int spacing = indRect.height() / 10;
82 const int h = indRect.height();
83 int x = indRect.left();
87 const QRect rect( x + spacing, indRect.top() + spacing, h - spacing * 2, h - spacing * 2 );
89 const QRect iconRect( x + spacing * 2, indRect.top() + spacing * 2, h - spacing * 4, h - spacing * 4 );
92 QIcon::Mode mode = QIcon::Normal;
93 if ( !( opt.state & QStyle::State_Enabled ) )
94 mode = QIcon::Disabled;
95 else if ( opt.state & QStyle::State_Selected )
96 mode = QIcon::Selected;
99 const qreal bradius = spacing;
100 const QBrush pb = painter->brush();
101 const QPen pp = painter->pen();
102 QBrush b = QBrush( opt.palette.midlight() );
103 QColor bc = b.color();
104 bc.setRed(
static_cast<int>( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
105 bc.setGreen(
static_cast<int>( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
106 bc.setBlue(
static_cast<int>( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
108 painter->setBrush( b );
109 painter->setPen( QPen( QBrush( opt.palette.mid() ), 0.25 ) );
110 painter->drawRoundedRect( rect, bradius, bradius );
111 painter->setBrush( pb );
112 painter->setPen( pp );
114 indicator->icon().paint( painter, iconRect, Qt::AlignCenter, mode );
118bool QgsAttributesFormTreeViewItemDelegate::helpEvent( QHelpEvent *event, QAbstractItemView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index )
120 if ( event && event->type() == QEvent::ToolTip )
122 if ( index.isValid() )
124 const QList<QgsAttributesFormTreeViewIndicator *> indicators = mAttributesFormTreeView->indicators( index );
125 if ( !indicators.isEmpty() )
127 QStyleOptionViewItem opt = option;
128 initStyleOption( &opt, index );
129 opt.showDecorationSelected =
true;
131 const QRect indRect = mAttributesFormTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsAttributesFormTreeViewProxyStyle::SE_AttributesFormTreeItemIndicator ), &opt, mAttributesFormTreeView );
133 if ( indRect.contains( event->pos() ) )
135 const int indicatorIndex = (
event->pos().x() - indRect.left() ) / indRect.height();
136 if ( indicatorIndex >= 0 && indicatorIndex < indicators.count() )
138 const QString tooltip = indicators[indicatorIndex]->toolTip();
139 if ( !tooltip.isEmpty() )
141 QToolTip::showText( event->globalPos(), tooltip, view );
149 return QStyledItemDelegate::helpEvent( event, view, option, index );
A QProxyStyle subclass which correctly sets the base style to match the QGIS application style,...