20 #include "agenttypewidget.h"
24 #include <QApplication>
25 #include <QHBoxLayout>
29 #include "agentfilterproxymodel.h"
30 #include "agenttype.h"
31 #include "agenttypemodel.h"
39 class AgentTypeWidgetDelegate :
public QAbstractItemDelegate
42 AgentTypeWidgetDelegate( QObject *parent = 0 );
44 virtual void paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const;
45 virtual QSize sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const;
48 void drawFocus( QPainter*,
const QStyleOptionViewItem&,
const QRect& )
const;
53 using Akonadi::Internal::AgentTypeWidgetDelegate;
58 class AgentTypeWidget::Private
66 void currentAgentTypeChanged(
const QModelIndex&,
const QModelIndex& );
68 void typeActivated(
const QModelIndex &index )
70 if ( index.flags() & ( Qt::ItemIsSelectable | Qt::ItemIsEnabled ) ) {
71 emit mParent->activated();
77 AgentTypeModel *mModel;
78 AgentFilterProxyModel *proxyModel;
81 void AgentTypeWidget::Private::currentAgentTypeChanged(
const QModelIndex ¤tIndex,
const QModelIndex &previousIndex )
83 AgentType currentType;
84 if ( currentIndex.isValid() ) {
88 AgentType previousType;
89 if ( previousIndex.isValid() ) {
93 emit mParent->currentChanged( currentType, previousType );
97 : QWidget( parent ), d( new Private( this ) )
99 QHBoxLayout *layout =
new QHBoxLayout(
this );
100 layout->setMargin( 0 );
102 d->mView =
new QListView(
this );
103 d->mView->setItemDelegate(
new AgentTypeWidgetDelegate( d->mView ) );
104 d->mView->setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
105 d->mView->setAlternatingRowColors(
true );
106 layout->addWidget( d->mView );
110 d->proxyModel->setSourceModel( d->mModel );
111 d->proxyModel->sort( 0 );
112 d->mView->setModel( d->proxyModel );
114 d->mView->selectionModel()->setCurrentIndex( d->mView->model()->index( 0, 0 ), QItemSelectionModel::Select );
115 d->mView->scrollTo( d->mView->model()->index( 0, 0 ) );
116 connect( d->mView->selectionModel(), SIGNAL(
currentChanged(QModelIndex,QModelIndex)),
117 this, SLOT(currentAgentTypeChanged(QModelIndex,QModelIndex)) );
118 connect( d->mView, SIGNAL(
activated(QModelIndex)),
119 SLOT(typeActivated(QModelIndex)) );
129 QItemSelectionModel *selectionModel = d->mView->selectionModel();
130 if ( !selectionModel ) {
134 QModelIndex index = selectionModel->currentIndex();
135 if ( !index.isValid() ) {
144 return d->proxyModel;
151 AgentTypeWidgetDelegate::AgentTypeWidgetDelegate( QObject *parent )
152 : QAbstractItemDelegate( parent )
156 void AgentTypeWidgetDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
158 if ( !index.isValid() ) {
162 painter->setRenderHint( QPainter::Antialiasing );
164 const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
167 const QVariant data = index.model()->data( index, Qt::DecorationRole );
170 if ( data.isValid() && data.type() == QVariant::Icon ) {
171 pixmap = qvariant_cast<QIcon>( data ).pixmap( 64, 64 );
174 const QFont oldFont = painter->font();
175 QFont boldFont( oldFont );
176 boldFont.setBold(
true );
177 painter->setFont( boldFont );
178 QFontMetrics fm = painter->fontMetrics();
179 int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
180 int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
181 painter->setFont( oldFont );
183 fm = painter->fontMetrics();
184 int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
185 int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
186 int wp = pixmap.width();
188 QStyleOptionViewItemV4 opt( option );
189 opt.showDecorationSelected =
true;
190 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter );
192 QPen pen = painter->pen();
193 QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
194 ? QPalette::Normal : QPalette::Disabled;
195 if ( cg == QPalette::Normal && !( option.state & QStyle::State_Active ) ) {
196 cg = QPalette::Inactive;
198 if ( option.state & QStyle::State_Selected ) {
199 painter->setPen( option.palette.color( cg, QPalette::HighlightedText ) );
201 painter->setPen( option.palette.color( cg, QPalette::Text ) );
204 QFont font = painter->font();
205 painter->setFont( option.font );
207 painter->drawPixmap( option.rect.x() + 5, option.rect.y() + 5, pixmap );
209 painter->setFont( boldFont );
210 if ( !name.isEmpty() ) {
211 painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7, wn, hn, Qt::AlignLeft, name );
213 painter->setFont( oldFont );
215 if ( !comment.isEmpty() ) {
216 painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7 + hn, wc, hc, Qt::AlignLeft, comment );
219 painter->setPen( pen );
221 drawFocus( painter, option, option.rect );
224 QSize AgentTypeWidgetDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
226 if ( !index.isValid() ) {
227 return QSize( 0, 0 );
230 const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
233 QFontMetrics fm = option.fontMetrics;
234 int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
235 int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
236 int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
237 int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
242 if ( !name.isEmpty() ) {
244 width = qMax( width, wn );
247 if ( !comment.isEmpty() ) {
249 width = qMax( width, wc );
252 height = qMax( height, 64 ) + 10;
255 return QSize( width, height );
258 void AgentTypeWidgetDelegate::drawFocus( QPainter *painter,
const QStyleOptionViewItem &option,
const QRect &rect )
const
260 if ( option.state & QStyle::State_HasFocus ) {
261 QStyleOptionFocusRect o;
262 o.QStyleOption::operator=( option );
264 o.state |= QStyle::State_KeyboardFocusChange;
265 QPalette::ColorGroup cg = ( option.state & QStyle::State_Enabled )
266 ? QPalette::Normal : QPalette::Disabled;
267 o.backgroundColor = option.palette.color( cg, ( option.state & QStyle::State_Selected )
268 ? QPalette::Highlight : QPalette::Background );
269 QApplication::style()->drawPrimitive( QStyle::PE_FrameFocusRect, &o, painter );
275 #include "moc_agenttypewidget.cpp"
A representation of an agent type.
A description of the agent type.
Provides a data model for agent types.
A proxy model for filtering AgentType or AgentInstance.