• Skip to content
  • Skip to link menu
KDE 4.5 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 <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 namespace Akonadi {
00034 namespace Internal {
00035 
00039 class AgentTypeWidgetDelegate : public QAbstractItemDelegate
00040 {
00041   public:
00042     AgentTypeWidgetDelegate( QObject *parent = 0 );
00043 
00044     virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
00045     virtual QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
00046 
00047   private:
00048     void drawFocus( QPainter*, const QStyleOptionViewItem&, const QRect& ) const;
00049 };
00050 
00051 }
00052 
00053 using Akonadi::Internal::AgentTypeWidgetDelegate;
00054 
00055 
00059 class AgentTypeWidget::Private
00060 {
00061   public:
00062     Private( AgentTypeWidget *parent )
00063       : mParent( parent )
00064     {
00065     }
00066 
00067     void currentAgentTypeChanged( const QModelIndex&, const QModelIndex& );
00068 
00069     void typeActivated( const QModelIndex &index )
00070     {
00071       if ( index.flags() & (Qt::ItemIsSelectable | Qt::ItemIsEnabled) )
00072         emit mParent->activated();
00073     }
00074 
00075     AgentTypeWidget *mParent;
00076     QListView *mView;
00077     AgentTypeModel *mModel;
00078     AgentFilterProxyModel *proxyModel;
00079 };
00080 
00081 void AgentTypeWidget::Private::currentAgentTypeChanged( const QModelIndex &currentIndex, const QModelIndex &previousIndex )
00082 {
00083   AgentType currentType;
00084   if ( currentIndex.isValid() )
00085     currentType = currentIndex.data( AgentTypeModel::TypeRole ).value<AgentType>();
00086 
00087   AgentType previousType;
00088   if ( previousIndex.isValid() )
00089     previousType = previousIndex.data( AgentTypeModel::TypeRole ).value<AgentType>();
00090 
00091   emit mParent->currentChanged( currentType, previousType );
00092 }
00093 
00094 AgentTypeWidget::AgentTypeWidget( QWidget *parent )
00095   : QWidget( parent ), d( new Private( this ) )
00096 {
00097   QHBoxLayout *layout = new QHBoxLayout( this );
00098   layout->setMargin( 0 );
00099 
00100   d->mView = new QListView( this );
00101   d->mView->setItemDelegate( new AgentTypeWidgetDelegate( d->mView ) );
00102   d->mView->setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
00103   d->mView->setAlternatingRowColors( true );
00104   layout->addWidget( d->mView );
00105 
00106   d->mModel = new AgentTypeModel( d->mView );
00107   d->proxyModel = new AgentFilterProxyModel( this );
00108   d->proxyModel->setSourceModel( d->mModel );
00109   d->proxyModel->sort( 0 );
00110   d->mView->setModel( d->proxyModel );
00111 
00112   d->mView->selectionModel()->setCurrentIndex( d->mView->model()->index( 0, 0 ), QItemSelectionModel::Select );
00113   d->mView->scrollTo( d->mView->model()->index( 0, 0 ) );
00114   connect( d->mView->selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00115            this, SLOT( currentAgentTypeChanged( const QModelIndex&, const QModelIndex& ) ) );
00116   connect( d->mView, SIGNAL( activated( const QModelIndex& ) ),
00117            SLOT( typeActivated(QModelIndex) ) );
00118 }
00119 
00120 AgentTypeWidget::~AgentTypeWidget()
00121 {
00122   delete d;
00123 }
00124 
00125 AgentType AgentTypeWidget::currentAgentType() const
00126 {
00127   QItemSelectionModel *selectionModel = d->mView->selectionModel();
00128   if ( !selectionModel )
00129     return AgentType();
00130 
00131   QModelIndex index = selectionModel->currentIndex();
00132   if ( !index.isValid() )
00133     return AgentType();
00134 
00135   return index.data( AgentTypeModel::TypeRole ).value<AgentType>();
00136 }
00137 
00138 AgentFilterProxyModel* AgentTypeWidget::agentFilterProxyModel() const
00139 {
00140   return d->proxyModel;
00141 }
00142 
00147 AgentTypeWidgetDelegate::AgentTypeWidgetDelegate( QObject *parent )
00148  : QAbstractItemDelegate( parent )
00149 {
00150 }
00151 
00152 void AgentTypeWidgetDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
00153 {
00154   if ( !index.isValid() )
00155     return;
00156 
00157   painter->setRenderHint( QPainter::Antialiasing );
00158 
00159   const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
00160   const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
00161 
00162   const QVariant data = index.model()->data( index, Qt::DecorationRole );
00163 
00164   QPixmap pixmap;
00165   if ( data.isValid() && data.type() == QVariant::Icon )
00166     pixmap = qvariant_cast<QIcon>( data ).pixmap( 64, 64 );
00167 
00168   const QFont oldFont = painter->font();
00169   QFont boldFont( oldFont );
00170   boldFont.setBold( true );
00171   painter->setFont( boldFont );
00172   QFontMetrics fm = painter->fontMetrics();
00173   int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
00174   int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
00175   painter->setFont( oldFont );
00176 
00177   fm = painter->fontMetrics();
00178   int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
00179   int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
00180   int wp = pixmap.width();
00181 
00182   QStyleOptionViewItemV4 opt(option);
00183   opt.showDecorationSelected = true;
00184   QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter );
00185 
00186   QPen pen = painter->pen();
00187   QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
00188                             ? QPalette::Normal : QPalette::Disabled;
00189   if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
00190     cg = QPalette::Inactive;
00191   if (option.state & QStyle::State_Selected) {
00192     painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
00193   } else {
00194     painter->setPen(option.palette.color(cg, QPalette::Text));
00195   }
00196 
00197   QFont font = painter->font();
00198   painter->setFont(option.font);
00199 
00200   painter->drawPixmap( option.rect.x() + 5, option.rect.y() + 5, pixmap );
00201 
00202   painter->setFont(boldFont);
00203   if ( !name.isEmpty() )
00204     painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7, wn, hn, Qt::AlignLeft, name );
00205   painter->setFont(oldFont);
00206 
00207   if ( !comment.isEmpty() )
00208     painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7 + hn, wc, hc, Qt::AlignLeft, comment );
00209 
00210   painter->setPen(pen);
00211 
00212   drawFocus( painter, option, option.rect );
00213 }
00214 
00215 QSize AgentTypeWidgetDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
00216 {
00217   if ( !index.isValid() )
00218     return QSize( 0, 0 );
00219 
00220   const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
00221   const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
00222 
00223   QFontMetrics fm = option.fontMetrics;
00224   int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
00225   int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
00226   int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
00227   int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
00228 
00229   int width = 0;
00230   int height = 0;
00231 
00232   if ( !name.isEmpty() ) {
00233     height += hn;
00234     width = qMax( width, wn );
00235   }
00236 
00237   if ( !comment.isEmpty() ) {
00238     height += hc;
00239     width = qMax( width, wc );
00240   }
00241 
00242   height = qMax( height, 64 ) + 10;
00243   width += 64 + 15;
00244 
00245   return QSize( width, height );
00246 }
00247 
00248 void AgentTypeWidgetDelegate::drawFocus( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const
00249 {
00250   if (option.state & QStyle::State_HasFocus) {
00251     QStyleOptionFocusRect o;
00252     o.QStyleOption::operator=(option);
00253     o.rect = rect;
00254     o.state |= QStyle::State_KeyboardFocusChange;
00255     QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled)
00256                               ? QPalette::Normal : QPalette::Disabled;
00257     o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected)
00258                                              ? QPalette::Highlight : QPalette::Background);
00259     QApplication::style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter);
00260   }
00261 }
00262 
00263 }
00264 
00265 #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
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.1
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