• Skip to content
  • Skip to link menu
KDE 4.2 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->proxyModel->sort( 0 );
00096   d->mView->setModel( d->proxyModel );
00097 
00098   d->mView->selectionModel()->setCurrentIndex( d->mView->model()->index( 0, 0 ), QItemSelectionModel::Select );
00099   d->mView->scrollTo( d->mView->model()->index( 0, 0 ) );
00100   connect( d->mView->selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00101            this, SLOT( currentAgentTypeChanged( const QModelIndex&, const QModelIndex& ) ) );
00102   connect( d->mView, SIGNAL( activated( const QModelIndex& ) ),
00103            SIGNAL( activated() ) );
00104 }
00105 
00106 AgentTypeWidget::~AgentTypeWidget()
00107 {
00108   delete d;
00109 }
00110 
00111 AgentType AgentTypeWidget::currentAgentType() const
00112 {
00113   QItemSelectionModel *selectionModel = d->mView->selectionModel();
00114   if ( !selectionModel )
00115     return AgentType();
00116 
00117   QModelIndex index = selectionModel->currentIndex();
00118   if ( !index.isValid() )
00119     return AgentType();
00120 
00121   return index.data( AgentTypeModel::TypeRole ).value<AgentType>();
00122 }
00123 
00124 AgentFilterProxyModel* AgentTypeWidget::agentFilterProxyModel() const
00125 {
00126   return d->proxyModel;
00127 }
00128 
00133 AgentTypeWidgetDelegate::AgentTypeWidgetDelegate( QObject *parent )
00134  : QAbstractItemDelegate( parent )
00135 {
00136 }
00137 
00138 void AgentTypeWidgetDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
00139 {
00140   if ( !index.isValid() )
00141     return;
00142 
00143   painter->setRenderHint( QPainter::Antialiasing );
00144 
00145   const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
00146   const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
00147 
00148   const QVariant data = index.model()->data( index, Qt::DecorationRole );
00149 
00150   QPixmap pixmap;
00151   if ( data.isValid() && data.type() == QVariant::Icon )
00152     pixmap = qvariant_cast<QIcon>( data ).pixmap( 64, 64 );
00153 
00154   const QFont oldFont = painter->font();
00155   QFont boldFont( oldFont );
00156   boldFont.setBold( true );
00157   painter->setFont( boldFont );
00158   QFontMetrics fm = painter->fontMetrics();
00159   int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
00160   int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
00161   painter->setFont( oldFont );
00162 
00163   fm = painter->fontMetrics();
00164   int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
00165   int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
00166   int wp = pixmap.width();
00167 
00168   QPen pen = painter->pen();
00169   QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
00170                             ? QPalette::Normal : QPalette::Disabled;
00171   if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
00172     cg = QPalette::Inactive;
00173   if (option.state & QStyle::State_Selected) {
00174     painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
00175     painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
00176   } else {
00177     painter->setPen(option.palette.color(cg, QPalette::Text));
00178   }
00179 
00180   QFont font = painter->font();
00181   painter->setFont(option.font);
00182 
00183   painter->drawPixmap( option.rect.x() + 5, option.rect.y() + 5, pixmap );
00184 
00185   painter->setFont(boldFont);
00186   if ( !name.isEmpty() )
00187     painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7, wn, hn, Qt::AlignLeft, name );
00188   painter->setFont(oldFont);
00189 
00190   if ( !comment.isEmpty() )
00191     painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7 + hn, wc, hc, Qt::AlignLeft, comment );
00192 
00193   painter->setPen(pen);
00194 
00195   drawFocus( painter, option, option.rect );
00196 }
00197 
00198 QSize AgentTypeWidgetDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
00199 {
00200   if ( !index.isValid() )
00201     return QSize( 0, 0 );
00202 
00203   const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
00204   const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
00205 
00206   QFontMetrics fm = option.fontMetrics;
00207   int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
00208   int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
00209   int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
00210   int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
00211 
00212   int width = 0;
00213   int height = 0;
00214 
00215   if ( !name.isEmpty() ) {
00216     height += hn;
00217     width = qMax( width, wn );
00218   }
00219 
00220   if ( !comment.isEmpty() ) {
00221     height += hc;
00222     width = qMax( width, wc );
00223   }
00224 
00225   height = qMax( height, 64 ) + 10;
00226   width += 64 + 15;
00227 
00228   return QSize( width, height );
00229 }
00230 
00231 void AgentTypeWidgetDelegate::drawFocus( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const
00232 {
00233   if (option.state & QStyle::State_HasFocus) {
00234     QStyleOptionFocusRect o;
00235     o.QStyleOption::operator=(option);
00236     o.rect = rect;
00237     o.state |= QStyle::State_KeyboardFocusChange;
00238     QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled)
00239                               ? QPalette::Normal : QPalette::Disabled;
00240     o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected)
00241                                              ? QPalette::Highlight : QPalette::Background);
00242     QApplication::style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter);
00243   }
00244 }
00245 
00246 #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
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.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