24#include "moc_qgsattributesformtreeviewitemdelegate.cpp"
30 , mAttributesFormTreeView( treeView )
33QRect QgsAttributesFormTreeViewProxyStyle::subElementRect( QStyle::SubElement element,
const QStyleOption *option,
const QWidget *widget )
const
35 if ( element == SE_AttributesFormTreeItemIndicator )
37 if (
const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>( option ) )
39 if ( vopt->index.isValid() )
41 const int count = mAttributesFormTreeView->indicators( vopt->index ).count();
44 const QRect vpr = mAttributesFormTreeView->viewport()->rect();
45 const QRect r = QProxyStyle::subElementRect( SE_ItemViewItemText, option, widget );
46 const int indiWidth = r.height() * count;
47 const int spacing = r.height() / 10;
48 const int vpIndiWidth = vpr.width() - indiWidth - spacing;
49 return QRect( vpIndiWidth, r.top(), indiWidth, r.height() );
54 return QProxyStyle::subElementRect( element, option, widget );
59 : QStyledItemDelegate( parent )
60 , mAttributesFormTreeView( parent )
63void QgsAttributesFormTreeViewItemDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
65 QStyledItemDelegate::paint( painter, option, index );
67 if ( !index.isValid() )
70 QStyleOptionViewItem opt = option;
71 initStyleOption( &opt, index );
73 const QColor baseColor = opt.palette.base().color();
74 const QList<QgsAttributesFormTreeViewIndicator *> indicators = mAttributesFormTreeView->indicators( index );
75 if ( indicators.isEmpty() )
79 = mAttributesFormTreeView->style()->subElementRect(
static_cast<QStyle::SubElement
>( QgsAttributesFormTreeViewProxyStyle::SE_AttributesFormTreeItemIndicator ), &opt, mAttributesFormTreeView );
80 const int spacing = indRect.height() / 10;
81 const int h = indRect.height();
82 int x = indRect.left();
86 const QRect rect( x + spacing, indRect.top() + spacing, h - spacing * 2, h - spacing * 2 );
88 const QRect iconRect( x + spacing * 2, indRect.top() + spacing * 2, h - spacing * 4, h - spacing * 4 );
91 QIcon::Mode mode = QIcon::Normal;
92 if ( !( opt.state & QStyle::State_Enabled ) )
93 mode = QIcon::Disabled;
94 else if ( opt.state & QStyle::State_Selected )
95 mode = QIcon::Selected;
98 const qreal bradius = spacing;
99 const QBrush pb = painter->brush();
100 const QPen pp = painter->pen();
101 QBrush b = QBrush( opt.palette.midlight() );
102 QColor bc = b.color();
103 bc.setRed(
static_cast<int>( bc.red() * 0.3 + baseColor.red() * 0.7 ) );
104 bc.setGreen(
static_cast<int>( bc.green() * 0.3 + baseColor.green() * 0.7 ) );
105 bc.setBlue(
static_cast<int>( bc.blue() * 0.3 + baseColor.blue() * 0.7 ) );
107 painter->setBrush( b );
108 painter->setPen( QPen( QBrush( opt.palette.mid() ), 0.25 ) );
109 painter->drawRoundedRect( rect, bradius, bradius );
110 painter->setBrush( pb );
111 painter->setPen( pp );
113 indicator->icon().paint( painter, iconRect, Qt::AlignCenter, mode );
117bool QgsAttributesFormTreeViewItemDelegate::helpEvent( QHelpEvent *event, QAbstractItemView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index )
119 if ( event && event->type() == QEvent::ToolTip )
121 if ( index.isValid() )
123 const QList<QgsAttributesFormTreeViewIndicator *> indicators = mAttributesFormTreeView->indicators( index );
124 if ( !indicators.isEmpty() )
126 QStyleOptionViewItem opt = option;
127 initStyleOption( &opt, index );
128 opt.showDecorationSelected =
true;
131 = 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,...