20 #include "agenttypewidget.h"
24 #include <QtGui/QApplication>
25 #include <QtGui/QHBoxLayout>
26 #include <QtGui/QListView>
27 #include <QtGui/QPainter>
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;
59 class AgentTypeWidget::Private
67 void currentAgentTypeChanged(
const QModelIndex&,
const QModelIndex& );
69 void typeActivated(
const QModelIndex &index )
71 if ( index.flags() & (Qt::ItemIsSelectable | Qt::ItemIsEnabled) )
72 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() )
87 AgentType previousType;
88 if ( previousIndex.isValid() )
91 emit mParent->currentChanged( currentType, previousType );
95 : QWidget( parent ), d( new Private( this ) )
97 QHBoxLayout *layout =
new QHBoxLayout(
this );
98 layout->setMargin( 0 );
100 d->mView =
new QListView(
this );
101 d->mView->setItemDelegate(
new AgentTypeWidgetDelegate( d->mView ) );
102 d->mView->setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
103 d->mView->setAlternatingRowColors(
true );
104 layout->addWidget( d->mView );
108 d->proxyModel->setSourceModel( d->mModel );
109 d->proxyModel->sort( 0 );
110 d->mView->setModel( d->proxyModel );
112 d->mView->selectionModel()->setCurrentIndex( d->mView->model()->index( 0, 0 ), QItemSelectionModel::Select );
113 d->mView->scrollTo( d->mView->model()->index( 0, 0 ) );
114 connect( d->mView->selectionModel(), SIGNAL(
currentChanged(QModelIndex,QModelIndex)),
115 this, SLOT(currentAgentTypeChanged(QModelIndex,QModelIndex)) );
116 connect( d->mView, SIGNAL(
activated(QModelIndex)),
117 SLOT(typeActivated(QModelIndex)) );
127 QItemSelectionModel *selectionModel = d->mView->selectionModel();
128 if ( !selectionModel )
131 QModelIndex index = selectionModel->currentIndex();
132 if ( !index.isValid() )
140 return d->proxyModel;
147 AgentTypeWidgetDelegate::AgentTypeWidgetDelegate( QObject *parent )
148 : QAbstractItemDelegate( parent )
152 void AgentTypeWidgetDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
154 if ( !index.isValid() )
157 painter->setRenderHint( QPainter::Antialiasing );
159 const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
162 const QVariant data = index.model()->data( index, Qt::DecorationRole );
165 if ( data.isValid() && data.type() == QVariant::Icon )
166 pixmap = qvariant_cast<QIcon>( data ).pixmap( 64, 64 );
168 const QFont oldFont = painter->font();
169 QFont boldFont( oldFont );
170 boldFont.setBold(
true );
171 painter->setFont( boldFont );
172 QFontMetrics fm = painter->fontMetrics();
173 int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
174 int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
175 painter->setFont( oldFont );
177 fm = painter->fontMetrics();
178 int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
179 int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
180 int wp = pixmap.width();
182 QStyleOptionViewItemV4 opt(option);
183 opt.showDecorationSelected =
true;
184 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter );
186 QPen pen = painter->pen();
187 QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
188 ? QPalette::Normal : QPalette::Disabled;
189 if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
190 cg = QPalette::Inactive;
191 if (option.state & QStyle::State_Selected) {
192 painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
194 painter->setPen(option.palette.color(cg, QPalette::Text));
197 QFont font = painter->font();
198 painter->setFont(option.font);
200 painter->drawPixmap( option.rect.x() + 5, option.rect.y() + 5, pixmap );
202 painter->setFont(boldFont);
203 if ( !name.isEmpty() )
204 painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7, wn, hn, Qt::AlignLeft, name );
205 painter->setFont(oldFont);
207 if ( !comment.isEmpty() )
208 painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7 + hn, wc, hc, Qt::AlignLeft, comment );
210 painter->setPen(pen);
212 drawFocus( painter, option, option.rect );
215 QSize AgentTypeWidgetDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
217 if ( !index.isValid() )
218 return QSize( 0, 0 );
220 const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
223 QFontMetrics fm = option.fontMetrics;
224 int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
225 int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
226 int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
227 int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
232 if ( !name.isEmpty() ) {
234 width = qMax( width, wn );
237 if ( !comment.isEmpty() ) {
239 width = qMax( width, wc );
242 height = qMax( height, 64 ) + 10;
245 return QSize( width, height );
248 void AgentTypeWidgetDelegate::drawFocus( QPainter *painter,
const QStyleOptionViewItem &option,
const QRect &rect )
const
250 if (option.state & QStyle::State_HasFocus) {
251 QStyleOptionFocusRect o;
252 o.QStyleOption::operator=(option);
254 o.state |= QStyle::State_KeyboardFocusChange;
255 QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled)
256 ? QPalette::Normal : QPalette::Disabled;
257 o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected)
258 ? QPalette::Highlight : QPalette::Background);
259 QApplication::style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter);
265 #include "agenttypewidget.moc"