• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi

agenttypewidget.cpp

00001 /*
00002     Copyright (c) 2006-2008 Tobias Koenig <tokoe@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
00018 */
00019 
00020 #include "agenttypewidget.h"
00021 
00022 #include <QtGui/QApplication>
00023 #include <QtGui/QHBoxLayout>
00024 #include <QtGui/QListView>
00025 #include <QtGui/QPainter>
00026 
00027 #include "agentfilterproxymodel.h"
00028 #include "agenttype.h"
00029 #include "agenttypemodel.h"
00030 
00031 using namespace Akonadi;
00032 
00036 class AgentTypeWidgetDelegate : public QAbstractItemDelegate
00037 {
00038   public:
00039     AgentTypeWidgetDelegate( QObject *parent = 0 );
00040 
00041     virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
00042     virtual QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
00043 
00044   private:
00045     void drawFocus( QPainter*, const QStyleOptionViewItem&, const QRect& ) const;
00046 };
00047 
00048 
00052 class AgentTypeWidget::Private
00053 {
00054   public:
00055     Private( AgentTypeWidget *parent )
00056       : mParent( parent )
00057     {
00058     }
00059 
00060     void currentAgentTypeChanged( const QModelIndex&, const QModelIndex& );
00061 
00062     AgentTypeWidget *mParent;
00063     QListView *mView;
00064     AgentTypeModel *mModel;
00065     AgentFilterProxyModel *proxyModel;
00066 };
00067 
00068 void AgentTypeWidget::Private::currentAgentTypeChanged( const QModelIndex &currentIndex, const QModelIndex &previousIndex )
00069 {
00070   AgentType currentType;
00071   if ( currentIndex.isValid() )
00072     currentType = currentIndex.data( AgentTypeModel::TypeRole ).value<AgentType>();
00073 
00074   AgentType previousType;
00075   if ( previousIndex.isValid() )
00076     previousType = previousIndex.data( AgentTypeModel::TypeRole ).value<AgentType>();
00077 
00078   emit mParent->currentChanged( currentType, previousType );
00079 }
00080 
00081 AgentTypeWidget::AgentTypeWidget( QWidget *parent )
00082   : QWidget( parent ), d( new Private( this ) )
00083 {
00084   QHBoxLayout *layout = new QHBoxLayout( this );
00085   layout->setMargin( 0 );
00086   layout->setSpacing( 0 );
00087 
00088   d->mView = new QListView( this );
00089   d->mView->setItemDelegate( new AgentTypeWidgetDelegate( d->mView ) );
00090   layout->addWidget( d->mView );
00091 
00092   d->mModel = new AgentTypeModel( d->mView );
00093   d->proxyModel = new AgentFilterProxyModel( this );
00094   d->proxyModel->setSourceModel( d->mModel );
00095   d->mView->setModel( d->proxyModel );
00096 
00097   d->mView->selectionModel()->setCurrentIndex( d->mView->model()->index( 0, 0 ), QItemSelectionModel::Select );
00098   d->mView->scrollTo( d->mView->model()->index( 0, 0 ) );
00099   connect( d->mView->selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00100            this, SLOT( currentAgentTypeChanged( const QModelIndex&, const QModelIndex& ) ) );
00101 }
00102 
00103 AgentTypeWidget::~AgentTypeWidget()
00104 {
00105   delete d;
00106 }
00107 
00108 AgentType AgentTypeWidget::currentAgentType() const
00109 {
00110   QItemSelectionModel *selectionModel = d->mView->selectionModel();
00111   if ( !selectionModel )
00112     return AgentType();
00113 
00114   QModelIndex index = selectionModel->currentIndex();
00115   if ( !index.isValid() )
00116     return AgentType();
00117 
00118   return index.data( AgentTypeModel::TypeRole ).value<AgentType>();
00119 }
00120 
00121 AgentFilterProxyModel* AgentTypeWidget::agentFilterProxyModel() const
00122 {
00123   return d->proxyModel;
00124 }
00125 
00130 AgentTypeWidgetDelegate::AgentTypeWidgetDelegate( QObject *parent )
00131  : QAbstractItemDelegate( parent )
00132 {
00133 }
00134 
00135 void AgentTypeWidgetDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
00136 {
00137   if ( !index.isValid() )
00138     return;
00139 
00140   painter->setRenderHint( QPainter::Antialiasing );
00141 
00142   const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
00143   const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
00144 
00145   const QVariant data = index.model()->data( index, Qt::DecorationRole );
00146 
00147   QPixmap pixmap;
00148   if ( data.isValid() && data.type() == QVariant::Icon )
00149     pixmap = qvariant_cast<QIcon>( data ).pixmap( 64, 64 );
00150 
00151   const QFont oldFont = painter->font();
00152   QFont boldFont( oldFont );
00153   boldFont.setBold( true );
00154   painter->setFont( boldFont );
00155   QFontMetrics fm = painter->fontMetrics();
00156   int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
00157   int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
00158   painter->setFont( oldFont );
00159 
00160   fm = painter->fontMetrics();
00161   int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
00162   int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
00163   int wp = pixmap.width();
00164 
00165   QPen pen = painter->pen();
00166   QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
00167                             ? QPalette::Normal : QPalette::Disabled;
00168   if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
00169     cg = QPalette::Inactive;
00170   if (option.state & QStyle::State_Selected) {
00171     painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
00172     painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
00173   } else {
00174     painter->setPen(option.palette.color(cg, QPalette::Text));
00175   }
00176 
00177   QFont font = painter->font();
00178   painter->setFont(option.font);
00179 
00180   painter->drawPixmap( option.rect.x() + 5, option.rect.y() + 5, pixmap );
00181 
00182   painter->setFont(boldFont);
00183   if ( !name.isEmpty() )
00184     painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7, wn, hn, Qt::AlignLeft, name );
00185   painter->setFont(oldFont);
00186 
00187   if ( !comment.isEmpty() )
00188     painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7 + hn, wc, hc, Qt::AlignLeft, comment );
00189 
00190   painter->setPen(pen);
00191 
00192   drawFocus( painter, option, option.rect );
00193 }
00194 
00195 QSize AgentTypeWidgetDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
00196 {
00197   if ( !index.isValid() )
00198     return QSize( 0, 0 );
00199 
00200   const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
00201   const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
00202 
00203   QFontMetrics fm = option.fontMetrics;
00204   int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
00205   int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
00206   int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
00207   int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
00208 
00209   int width = 0;
00210   int height = 0;
00211 
00212   if ( !name.isEmpty() ) {
00213     height += hn;
00214     width = qMax( width, wn );
00215   }
00216 
00217   if ( !comment.isEmpty() ) {
00218     height += hc;
00219     width = qMax( width, wc );
00220   }
00221 
00222   height = qMax( height, 64 ) + 10;
00223   width += 64 + 15;
00224 
00225   return QSize( width, height );
00226 }
00227 
00228 void AgentTypeWidgetDelegate::drawFocus( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const
00229 {
00230   if (option.state & QStyle::State_HasFocus) {
00231     QStyleOptionFocusRect o;
00232     o.QStyleOption::operator=(option);
00233     o.rect = rect;
00234     o.state |= QStyle::State_KeyboardFocusChange;
00235     QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled)
00236                               ? QPalette::Normal : QPalette::Disabled;
00237     o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected)
00238                                              ? QPalette::Highlight : QPalette::Background);
00239     QApplication::style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter);
00240   }
00241 }
00242 
00243 #include "agenttypewidget.moc"

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.6
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal