00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "agenttypewidget.h"
00021
00022 #include <KDebug>
00023
00024 #include <QtGui/QApplication>
00025 #include <QtGui/QHBoxLayout>
00026 #include <QtGui/QListView>
00027 #include <QtGui/QPainter>
00028
00029 #include "agentfilterproxymodel.h"
00030 #include "agenttype.h"
00031 #include "agenttypemodel.h"
00032
00033 using namespace Akonadi;
00034
00038 class AgentTypeWidgetDelegate : public QAbstractItemDelegate
00039 {
00040 public:
00041 AgentTypeWidgetDelegate( QObject *parent = 0 );
00042
00043 virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
00044 virtual QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
00045
00046 private:
00047 void drawFocus( QPainter*, const QStyleOptionViewItem&, const QRect& ) const;
00048 };
00049
00050
00054 class AgentTypeWidget::Private
00055 {
00056 public:
00057 Private( AgentTypeWidget *parent )
00058 : mParent( parent )
00059 {
00060 }
00061
00062 void currentAgentTypeChanged( const QModelIndex&, const QModelIndex& );
00063
00064 void typeActivated( const QModelIndex &index )
00065 {
00066 if ( index.flags() & (Qt::ItemIsSelectable | Qt::ItemIsEnabled) )
00067 emit mParent->activated();
00068 }
00069
00070 AgentTypeWidget *mParent;
00071 QListView *mView;
00072 AgentTypeModel *mModel;
00073 AgentFilterProxyModel *proxyModel;
00074 };
00075
00076 void AgentTypeWidget::Private::currentAgentTypeChanged( const QModelIndex ¤tIndex, const QModelIndex &previousIndex )
00077 {
00078 AgentType currentType;
00079 if ( currentIndex.isValid() )
00080 currentType = currentIndex.data( AgentTypeModel::TypeRole ).value<AgentType>();
00081
00082 AgentType previousType;
00083 if ( previousIndex.isValid() )
00084 previousType = previousIndex.data( AgentTypeModel::TypeRole ).value<AgentType>();
00085
00086 emit mParent->currentChanged( currentType, previousType );
00087 }
00088
00089 AgentTypeWidget::AgentTypeWidget( QWidget *parent )
00090 : QWidget( parent ), d( new Private( this ) )
00091 {
00092 QHBoxLayout *layout = new QHBoxLayout( this );
00093 layout->setMargin( 0 );
00094
00095 d->mView = new QListView( this );
00096 d->mView->setItemDelegate( new AgentTypeWidgetDelegate( d->mView ) );
00097 d->mView->setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
00098 d->mView->setAlternatingRowColors( true );
00099 layout->addWidget( d->mView );
00100
00101 d->mModel = new AgentTypeModel( d->mView );
00102 d->proxyModel = new AgentFilterProxyModel( this );
00103 d->proxyModel->setSourceModel( d->mModel );
00104 d->proxyModel->sort( 0 );
00105 d->mView->setModel( d->proxyModel );
00106
00107 d->mView->selectionModel()->setCurrentIndex( d->mView->model()->index( 0, 0 ), QItemSelectionModel::Select );
00108 d->mView->scrollTo( d->mView->model()->index( 0, 0 ) );
00109 connect( d->mView->selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00110 this, SLOT( currentAgentTypeChanged( const QModelIndex&, const QModelIndex& ) ) );
00111 connect( d->mView, SIGNAL( activated( const QModelIndex& ) ),
00112 SLOT( typeActivated(QModelIndex) ) );
00113 }
00114
00115 AgentTypeWidget::~AgentTypeWidget()
00116 {
00117 delete d;
00118 }
00119
00120 AgentType AgentTypeWidget::currentAgentType() const
00121 {
00122 QItemSelectionModel *selectionModel = d->mView->selectionModel();
00123 if ( !selectionModel )
00124 return AgentType();
00125
00126 QModelIndex index = selectionModel->currentIndex();
00127 if ( !index.isValid() )
00128 return AgentType();
00129
00130 return index.data( AgentTypeModel::TypeRole ).value<AgentType>();
00131 }
00132
00133 AgentFilterProxyModel* AgentTypeWidget::agentFilterProxyModel() const
00134 {
00135 return d->proxyModel;
00136 }
00137
00142 AgentTypeWidgetDelegate::AgentTypeWidgetDelegate( QObject *parent )
00143 : QAbstractItemDelegate( parent )
00144 {
00145 }
00146
00147 void AgentTypeWidgetDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
00148 {
00149 if ( !index.isValid() )
00150 return;
00151
00152 painter->setRenderHint( QPainter::Antialiasing );
00153
00154 const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
00155 const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
00156
00157 const QVariant data = index.model()->data( index, Qt::DecorationRole );
00158
00159 QPixmap pixmap;
00160 if ( data.isValid() && data.type() == QVariant::Icon )
00161 pixmap = qvariant_cast<QIcon>( data ).pixmap( 64, 64 );
00162
00163 const QFont oldFont = painter->font();
00164 QFont boldFont( oldFont );
00165 boldFont.setBold( true );
00166 painter->setFont( boldFont );
00167 QFontMetrics fm = painter->fontMetrics();
00168 int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
00169 int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
00170 painter->setFont( oldFont );
00171
00172 fm = painter->fontMetrics();
00173 int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
00174 int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
00175 int wp = pixmap.width();
00176
00177 QStyleOptionViewItemV4 opt(option);
00178 opt.showDecorationSelected = true;
00179 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter );
00180
00181 QPen pen = painter->pen();
00182 QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
00183 ? QPalette::Normal : QPalette::Disabled;
00184 if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
00185 cg = QPalette::Inactive;
00186 if (option.state & QStyle::State_Selected) {
00187 painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
00188 } else {
00189 painter->setPen(option.palette.color(cg, QPalette::Text));
00190 }
00191
00192 QFont font = painter->font();
00193 painter->setFont(option.font);
00194
00195 painter->drawPixmap( option.rect.x() + 5, option.rect.y() + 5, pixmap );
00196
00197 painter->setFont(boldFont);
00198 if ( !name.isEmpty() )
00199 painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7, wn, hn, Qt::AlignLeft, name );
00200 painter->setFont(oldFont);
00201
00202 if ( !comment.isEmpty() )
00203 painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7 + hn, wc, hc, Qt::AlignLeft, comment );
00204
00205 painter->setPen(pen);
00206
00207 drawFocus( painter, option, option.rect );
00208 }
00209
00210 QSize AgentTypeWidgetDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
00211 {
00212 if ( !index.isValid() )
00213 return QSize( 0, 0 );
00214
00215 const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
00216 const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
00217
00218 QFontMetrics fm = option.fontMetrics;
00219 int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
00220 int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
00221 int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
00222 int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
00223
00224 int width = 0;
00225 int height = 0;
00226
00227 if ( !name.isEmpty() ) {
00228 height += hn;
00229 width = qMax( width, wn );
00230 }
00231
00232 if ( !comment.isEmpty() ) {
00233 height += hc;
00234 width = qMax( width, wc );
00235 }
00236
00237 height = qMax( height, 64 ) + 10;
00238 width += 64 + 15;
00239
00240 return QSize( width, height );
00241 }
00242
00243 void AgentTypeWidgetDelegate::drawFocus( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const
00244 {
00245 if (option.state & QStyle::State_HasFocus) {
00246 QStyleOptionFocusRect o;
00247 o.QStyleOption::operator=(option);
00248 o.rect = rect;
00249 o.state |= QStyle::State_KeyboardFocusChange;
00250 QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled)
00251 ? QPalette::Normal : QPalette::Disabled;
00252 o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected)
00253 ? QPalette::Highlight : QPalette::Background);
00254 QApplication::style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter);
00255 }
00256 }
00257
00258 #include "agenttypewidget.moc"